PyFilesystem / pyfilesystem2

Python's Filesystem abstraction layer

Home Page:https://www.pyfilesystem.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ResourceNotFound - moving files on the same filesystem with preserve_time=True

mj0nez opened this issue · comments

Hi,
while experimenting with the FS lib, I’ve encountered an unexpected ResourceNotFound error. The error is raised if the param preserve_time=True and we try to move a file on the same OS or in-memory filesystem.

This block represents a reproduceable example for a MemoryFS:

    mem_fs = memoryfs.MemoryFS()

    mem_fs.makedir("foo")
    mem_fs.writetext("foo/README.md", "Tetris clone")

    move_file(
        src_fs=mem_fs,
        src_path="foo/README.md",
        dst_fs=mem_fs,
        dst_path="foo/README_2.md",
        preserve_time=True,
    )

The raised error message:

./tests/test_resource_error.py::test_move_between_same_fs_mem Failed: [undefined]fs.errors.ResourceNotFound: resource 'foo/README.md' not found
def test_move_between_same_fs_mem():
        mem_fs = memoryfs.MemoryFS()
    
        mem_fs.makedir("foo")
        mem_fs.writetext("foo/README.md", "Tetris clone")
    
>       move_file(
            src_fs=mem_fs,
            src_path="foo/README.md",
            dst_fs=mem_fs,
            dst_path="foo/README_2.md",
            preserve_time=True,
        )

tests\test_resource_error.py:31: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
fs\move.py:68: in move_file
    _src_fs.move(
fs\memoryfs.py:478: in move
    copy_modified_time(self, src_path, self, dst_path)
fs\copy.py:532: in copy_modified_time
    src_meta = _src_fs.getinfo(src_path, namespaces)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = MemoryFS(), path = 'foo/README.md', namespaces = ('details',)

    def getinfo(self, path, namespaces=None):
        # type: (Text, Optional[Collection[Text]]) -> Info
        _path = self.validatepath(path)
        dir_entry = self._get_dir_entry(_path)
        if dir_entry is None:
>           raise errors.ResourceNotFound(path)
E           fs.errors.ResourceNotFound: resource 'foo/README.md' not found

fs\memoryfs.py:394: ResourceNotFound

I have modified the test case test_move_file_same_fs and found similar errors for OS- and Temp-filesystems. For my understanding, the current implementation wants to copy the file’s metadata, after moving it or renaming its path. Therefore, the copy_modified_time function encounters a missing resource under these conditions:

  • moving a file on the same filesystem
  • preserve_time=True

AND either

  1. copy_modified_time is called after moving the file, see memoryfs.py

  2. or FS.move of base.py is used and the filesystem supports renaming, see base.py

Is this a limitation of my system (Win11 x64)?
I could not find similar issues or information in the docs.

If this is a not yet supported edge case, I would suggest that we read the file’s meta data first, then move it and update the meta info afterwards.

Any review and comments are welcome!
Regards

Yup, it sounds like you've found a bug, and it sounds like you've already identified the fix. Good work.
Do you want to submit a suitable PR with the fix and updated tests?

Gladly, I'm going to submit a draft tomorrow at the latest. Thanks for the feedback!