pyfilesystem2
pyfilesystem2 copied to clipboard
OSFS Bug: incorrect handling of symbolic links with non-existing target
OSFS won't acknowledge the existence of symlinks if the symlink points to a non-existing destination:
>>> from fs import tempfs
>>> import os
>>> fs = tempfs.TempFS() # TempFS is a OSFS subclass
>>> src, dst = "foo", "bar"
>>> os.symlink(src, fs.getsyspath(dst)) # dst is the link, pointing to src
>>> fs.touch(src) # ensure that src exists
>>> fs.exists(dst) # fs acknowledges that dst exists
True
>>> fs.islink(dst) # and that dst is a link
True
>>> fs.remove(src) # but if we remove the src file
>>> os.path.islink(fs.getsyspath(dst)) # it is still a link
True
>>> fs.exists(dst) # but fs does not want to acknowledge it
False
>>> fs.islink(dst) # this raises, rather than return False
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/.../site-packages/fs/osfs.py", line 612, in islink
raise errors.ResourceNotFound(path)
fs.errors.ResourceNotFound: resource 'bar' not found
Note the exception thrown by fs.islink(); this goes counter to the behaviour of fs.isfile() and fs.isdir(), which both return False for this case.
First of all, I don't think fs.islink() should throw exceptions, it should return False if the path truly doesn't exist.
Now, I know that symlinks are a complex subject with miriad edge cases. Symlinks can point to other symlinks, or a parent directory of a path could be a symlink (e.g. realpath has -P / --physical, -L / -- logical modes that can lead to vastly different end results), etc. It could be that the intent is that only links that point to paths within the root of the filesystem are supported.
But the OSFS islink() documentation makes no mention of such restrictions, it simply claims to return True if the given path is a symlink on disk.
So the behavior of the standard library's os.path.islink() is to return False if the symlink is broken (note the cross-reference to this). I think we do need to fix this so that it returns False, like the standard library does:
>>> import os.path as p
>>> import os
>>> os.symlink("X", "Y")
>>> os.remove("X")
>>> os.path.exists("X")
False
>>> os.path.exists("Y")
False
>>> os.path.lexists("Y")
True
@dargueta : are you sure about this? I think os.path.islink returns True even if the symlink is broken. os.exists however returns False on a broken symlink.
Hm... I tested it manually using the above code in my post, with touch X on the command line just before that. What OS and version of Python are you using? I'm on OSX Catalina 10.15.6 with Python 3.8.5.
I'm on ArchLinux 5.8.9, with Python 3.8.5 as well. Maybe it's a OSX/Linux discrepancy, but the documentation states the following:
os.path.islink(path)Return
Trueif path refers to an existing directory entry that is a symbolic link. AlwaysFalseif symbolic links are not supported by the Python runtime.
Albeit dangling, a symlink is an existing directory entry; it's its target that does not exist. So I would assume os.path.islink('dangling') to return True.
So I would assume os.path.islink('dangling') to return True.
As would I. I'll test this on Ubuntu 20.04 when I get the chance and report back. This is weird.
Gaaah I can't read -- I got these backwards. Sorry about that.
Moving this to v2.5.0 milestone because this is going to introduce a change in the behaviour of FS.islink.