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
Describe the bug
Adding or removing entries from a directory requires that the user has write permissions to that directory. Trying to modify entries in a directory to which the user cannot write should raise PermissionError.
How To Reproduce
The following test cases should both succeed:
importosimportpytestimportpyfakefs.fake_filesystemdeftest_fail_write_dir(fs):
# Prereq: Create a directory at '/readonly-dir' that is owned by root and has# permissions 0o755 (rwxr-xr-x)rodir="/readonly-dir"os.makedirs(rodir, exist_ok=True)
st=os.stat(rodir)
ifst.st_uid!=0:
os.chown(rodir, 0, 0)
os.chmod(rodir, 0b111_101_101)
st=os.stat(rodir)
# Only modifyable by the root, but readable/traversable by allassertst.st_uid==0assertst.st_mode&0o777==0b111_101_101# == 0o755# Try to add a new entry to the readonly subdirectorywithpytest.raises(PermissionError):
withopen(f"{rodir}/file.txt", "w"):
passdeftest_fail_fakefs_root_owned_dir():
fs=pyfakefs.fake_filesystem.FakeFilesystem()
fake_os=pyfakefs.fake_filesystem.FakeOsModule(fs)
# Create a regular filefs.create_file("file.txt")
# Create a directory that can only be written-to by rootfake_os.mkdir("/readonly-dir", mode=0b111_101_101)
fake_os.chown("/readonly-dir", 0, 0)
# Creating a new entry should failwithpytest.raises(PermissionError):
fake_os.link("/file.txt", "/readonly-dir/file.txt")
Your environment
Please run the following in the environment where the problem happened and
paste the output.
Thanks! pyfakefs currently has only a rudimentary concept of user rights - it is currently mostly concerned with root vs. non-root users, so this (and your other issue) are not surprising.
Thanks for digging into this and providing the tests - I will have a go at this, but that may take a bit.
Describe the bug
Adding or removing entries from a directory requires that the user has write permissions to that directory. Trying to modify entries in a directory to which the user cannot write should raise
PermissionError
.How To Reproduce
The following test cases should both succeed:
Your environment
Please run the following in the environment where the problem happened and
paste the output.
If I understand correctly, the issue appears to be in
FakeDirectory.add_entry
, which starts with this guard:The check on
st_mode
checks that theS_IWUSR
bit is set, but does not consider the case thatst_uid != get_uid()
.I'm trying to write code that is robust against "adverse" filesystem conditions, with non-writable directories being a common example.
The text was updated successfully, but these errors were encountered: