You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
julia> open("/Users/jakobnissen/Documents/") do file
read(file)
end
0-element Array{UInt8,1}
That doesn't make much sense to me - presumably, attempting to read a directory is always a mistake, so we may as well error there. In fact, I discovered this because I accidentally read a directory and couldn't figure out why my program didn't give any output!
As a sanity check, Python refuses to do this:
>>> f = open("/Users/jakobnissen/Documents")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IsADirectoryError: [Errno 21] Is a directory: '/Users/jakobnissen/Documents'
Reacted by Johnny Chen, Phil Fuchs, Gabriele Bellomia, Gamal Adel, Carlos Parada, Páll Haraldsson, Jerry Ling and Mateus Araújo
Surprisingly I hit the same issue yesterday. In my case, the open was hidden behind a file parser, which also did not error on empty input, but returned an empty object instead. So that was even harder to debug.
Reacted by Kristoffer Carlsson, Phil Fuchs and Gabriele Bellomia
That is unfortunate. Perhaps we could check if it's a directory iff the file is size zero? I think a zero-size file can be determined when filling the initial buffer, so it should be very fast. It will, of course, slow down opening zero-size files that are actually files.
This whole situation still seems pretty unfortunate to me. @vtjnash, how big a performance hit would it be to reinstate the explicit check for whether the thing we're opening is a directory and erroring when it is?
I think it's different notions of file size, Mose: e.g. filesize(open("/")) == 122 for me, but eof(open("/")) == true. Assuming eof or a lower level version of it are fast enough, that could be a trigger to do an extra conditional check to see if the thing is a directory.
And on file systems where directories are readable, Julia will read them, which is probably(?) the correct behaviour?
It's what cat does anyway: error when reading a directory on normal filesystems, print the directory on filesystems where directories are readable.
if (_os_read(s->fd, s->buf, (size_t)s->maxsize, &got)) {
s->_eof=1;
returntot;
}
I suggest handling the error codes properly, then you won't need an explicit check before opening the file, and it will work properly with directories that are also readable as files.
edit: I looked at this a bit more and there is an errcode value in the ios struct, unused AFAICT, but maybe you could store the error code there in the C code and then read it from Julia with a new ios_geterrcode function if the read returns zero bytes.
Thinking on this more, I wonder if we could call faccessat on the new fd, and if that doesn't instantly fail (unlike fstat which slowly succeeded), then we know we accidentally opened a directory
That would mean that julia couldn't read directories on filesystems where directories are readable (CephFS example above), which is probably better than the status quo, but (arguably) worse than perfect.
Isn't it best to disallow reading as files (as done before?) and have a keyword-arg allowing it, the current default behavior? Since it might sometimes be useful:
Activity
aviks commentedon Jul 10, 2020
Surprisingly I hit the same issue yesterday. In my case, the
openwas hidden behind a file parser, which also did not error on empty input, but returned an empty object instead. So that was even harder to debug.StefanKarpinski commentedon Jul 10, 2020
This should definitely error.
vtjnash commentedon Jul 13, 2020
We used to have an
fstattest here, but it was slow (#35907)jakobnissen commentedon Jul 14, 2020
That is unfortunate. Perhaps we could check if it's a directory iff the file is size zero? I think a zero-size file can be determined when filling the initial buffer, so it should be very fast. It will, of course, slow down opening zero-size files that are actually files.
PallHaraldsson commentedon Jul 14, 2020
You can do e.g.:
KristofferC commentedon Oct 17, 2020
Consequence:
staticfloat commentedon Oct 21, 2020
I'm pretty sure this is the branch that needs a special case: https://github.com/JuliaLang/julia/pull/35925/files#r509678679
fredrikekre commentedon Jun 15, 2021
Just a note that some filesystems (this is CephFS) allows reading directories:
StefanKarpinski commentedon Jun 15, 2021
This whole situation still seems pretty unfortunate to me. @vtjnash, how big a performance hit would it be to reinstate the explicit check for whether the thing we're opening is a directory and erroring when it is?
giordano commentedon Jul 26, 2021
But directories don't have zero size according to
fstatcmcaine commentedon Jul 26, 2021
I think it's different notions of file size, Mose: e.g.
filesize(open("/")) == 122for me, buteof(open("/")) == true. Assumingeofor a lower level version of it are fast enough, that could be a trigger to do an extra conditional check to see if the thing is a directory.And on file systems where directories are readable, Julia will read them, which is probably(?) the correct behaviour?
It's what
catdoes anyway: error when reading a directory on normal filesystems, print the directory on filesystems where directories are readable.Mathnerd314 commentedon Jul 26, 2021
read(2)
C and Go use very thin wrappers around read(2) and you get this EISDIR "is a directory" error.
Julia's handling of all error codes is to set EOF:
julia/src/support/ios.c
Lines 312 to 315 in bb5b98e
I suggest handling the error codes properly, then you won't need an explicit check before opening the file, and it will work properly with directories that are also readable as files.
edit: I looked at this a bit more and there is an
errcodevalue in the ios struct, unused AFAICT, but maybe you could store the error code there in the C code and then read it from Julia with a newios_geterrcodefunction if the read returns zero bytes.StefanKarpinski commentedon Aug 4, 2021
Assigning @vtjnash since he made the change that broke this 😁
vtjnash commentedon Nov 3, 2022
Thinking on this more, I wonder if we could call faccessat on the new fd, and if that doesn't instantly fail (unlike fstat which slowly succeeded), then we know we accidentally opened a directory
cmcaine commentedon Nov 16, 2022
That would mean that julia couldn't read directories on filesystems where directories are readable (CephFS example above), which is probably better than the status quo, but (arguably) worse than perfect.
ParadaCarleton commentedon Sep 17, 2023
Ran into this bug yesterday.
PallHaraldsson commentedon Oct 20, 2023
Isn't it best to disallow reading as files (as done before?) and have a keyword-arg allowing it, the current default behavior? Since it might sometimes be useful:
#36605 (comment)
#35907 (comment)