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

Preserve file permissions in fakefs after add_real_file #1119

Open
daviewales opened this issue Feb 18, 2025 · 2 comments
Open

Preserve file permissions in fakefs after add_real_file #1119

daviewales opened this issue Feb 18, 2025 · 2 comments

Comments

@daviewales
Copy link

Describe the bug
Suppose I have a real file /usr/bin/env, owned by root:root, with permissions 0755.

If I copy this file into a fake filesystem the user and group are preserved, but the permissions are changed to 0444.

How To Reproduce
Note: This assumes you are on a *nix system with an executable file /usr/bin/env on your real filesystem.
If not, substitute any other file owned by root with permissions 0755.

import os
from pyfakefs.fake_filesystem import FakeFilesystem
from pyfakefs.fake_os import FakeOsModule

file = '/usr/bin/env'

fs = FakeFilesystem()
fs.add_real_file(file)

real_permissions = oct(os.stat(file).st_mode)
print(f"{real_permissions=}")

fake_os = FakeOsModule(fs)

fake_permissions = oct(fake_os.stat(file).st_mode)
print(f"{fake_permissions=}")

# output
# real_permissions='0o100755'
# fake_permissions='0o100444'

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

Linux-6.8.0-53-generic-x86_64-with-glibc2.39
Python 3.12.3 (main, Jan 17 2025, 18:03:48) [GCC 13.3.0]
pyfakefs 5.7.4
pytest 8.3.4
@mrbean-bremen
Copy link
Member

Thanks, I will have a look!

@mrbean-bremen mrbean-bremen added bug and removed bug labels Feb 18, 2025
@mrbean-bremen
Copy link
Member

Ok, I completly forgot that this is expected behavior. By default, files from the real filesystem are mapped as read-only. From the documentation:

The access to the files is by default read-only, but even if you add them using read_only=False, the files are written only in the fake system (e.g. in memory).

Replacing fs.add_real_file(file) with fs.add_real_file(file, read_only=False) should give you the wanted behavior.
Please check if this is sufficient for you.

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

No branches or pull requests

2 participants