Skip to content

Preserve file permissions in fakefs after add_real_file #1119

Closed

Description

@daviewales

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

Activity

  1. mrbean-bremen commented on Feb 18, 2025

    @mrbean-bremen
    Member

    Thanks, I will have a look!

  2. mrbean-bremen commented on Feb 18, 2025

    @mrbean-bremen
    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.

  3. mrbean-bremen commented on Feb 28, 2025

    @mrbean-bremen
    Member

    Converting this to a discussion issue.
    Or trying to... seems not to work at the moment.

  4. locked and limited conversation to collaborators on Feb 28, 2025
  5. converted this issue into a discussionon Feb 28, 2025
  6. converted this issue into a discussionon Feb 28, 2025
  7. converted this issue into a discussionon Feb 28, 2025
  8. converted this issue into a discussionon Mar 2, 2025
  9. converted this issue into a discussion #1138 on Mar 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions