Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patched os.symlink mishandles directory descriptors #968

Closed
vector-of-bool opened this issue Mar 10, 2024 · 3 comments · Fixed by #969
Closed

Patched os.symlink mishandles directory descriptors #968

vector-of-bool opened this issue Mar 10, 2024 · 3 comments · Fixed by #969
Labels

Comments

@vector-of-bool
Copy link

Describe the bug
The os.symlink function accepts a dir_fd argument to specify the destination of the created symlink. The fakefs os.symlink is behaves incorrectly and uses the dir_fd to rewrite the src argument:

src = self._path_with_dir_fd(src, self.symlink, dir_fd)  # (wrong)
self.filesystem.create_symlink(dst, src, create_missing_dirs=False)

The src argument should be left verbatim as given. I am guessing that the code should have been written to resolve the dst argument instead:

dst = self._path_with_dir_fd(dst, self.symlink, dir_fd)
self.filesystem.create_symlink(dst, src, create_missing_dirs=False)

The doc comment is written to reflect the incorrect behavior.

How To Reproduce

def _test_symlink(base: Path):
    dir_fd = os.open(base, os.O_DIRECTORY)
    os.symlink("file.txt", "link.txt", dir_fd=dir_fd)
    assert os.readlink(base / "link.txt") == "file.txt"

def test_symlink_realfs(tmp_path: Path):
    _test_symlink(tmp_path)

def test_symlink_fakefs(fs):
    _test_symlink(Path("/tmp/"))

The os.symlink call above will succeed, but creates link.txt in the wrong location, leading to os.readlink generating a FileNotFoundError.

Your environment
Please run the following in the environment where the problem happened and
paste the output.

$ python -c "import platform; print(platform.platform())"
Linux-6.7.6-arch1-1-x86_64-with-glibc2.39
$ python -c "import sys; print('Python', sys.version)"
Python 3.11.7 (main, Jan 29 2024, 16:03:57) [GCC 13.2.1 20230801]
$ python -c "from pyfakefs import __version__; print('pyfakefs', __version__)"
pyfakefs 5.4.dev0
$ python -c "import pytest; print('pytest', pytest.__version__)"
pytest 7.2.0
@mrbean-bremen
Copy link
Member

Thanks, that looks like an obvious bug, together with an incorrect or missing test.

mrbean-bremen added a commit to mrbean-bremen/pyfakefs that referenced this issue Mar 10, 2024
- also removed check for supported dirfd where not used
- changed dirfd tests to run both in the fake and real OS
- fixed pytest-dev#968
mrbean-bremen added a commit that referenced this issue Mar 10, 2024
- also removed check for supported dirfd where not used
- changed dirfd tests to run both in the fake and real OS
- fixed #968
@mrbean-bremen
Copy link
Member

As usually: please test in main.
I will handle the other issue another day...

@vector-of-bool
Copy link
Author

This appears to be fixed. Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants