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

Fix directory handling issues under lazy loading #33

Merged
merged 2 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ Changed
* Lazy load directory entries for performance and `regex2fat <https://github.com/8051Enthusiast/regex2fat>`_ compatibility
- Introduce ``lazy_load`` parameter to allow restoring previous behavior
- `PR #32 <https://github.com/nathanhi/pyfatfs/pull/32>`_: Fix tree iteration on non-lazy load by `@zurcher <https://github.com/zurcher>`_ / `@Microsoft <https://github.com/Microsoft>`_
- `PR #33 <https://github.com/nathanhi/pyfatfs/pull/33>`_: Fix missing parent directory entry link on lazy-load by `@zurcher <https://github.com/zurcher>`_ / `@Microsoft <https://github.com/Microsoft>`_
- `PR #33 <https://github.com/nathanhi/pyfatfs/pull/33>`_: Do not re-populate directory structure from disk on pending entry change by `@zurcher <https://github.com/zurcher>`_ / `@Microsoft <https://github.com/Microsoft>`_

Removed
~~~~~~~
Expand Down
5 changes: 3 additions & 2 deletions pyfatfs/FATDirectoryEntry.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,9 @@ def __populate_dirs(self):

clus = self.get_cluster()
self.__dirs = self.__fs.parse_dir_entries_in_cluster_chain(clus)
for dir_entry in self.__dirs:
dir_entry._add_parent(self)
self.__lazy_load = False

def _get_entries_raw(self):
"""Get a full list of entries in current directory."""
Expand Down Expand Up @@ -495,8 +498,6 @@ def remove_dir_entry(self, name):
**NOTE:** This will also remove special entries such
as ».«, »..« and volume labels!
"""
self._verify_is_directory()
self.__populate_dirs()
# Iterate all entries
for dir_entry in self._get_entries_raw():
sn = dir_entry.get_short_name()
Expand Down
42 changes: 42 additions & 0 deletions tests/test_PyFatFS.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,48 @@ def make_fs(self): # pylint: disable=R0201
"""Create filesystem for PyFilesystem2 integration tests."""
return _make_fs(self.FAT_TYPE)[0]

def test_lazy_load_dentry_parent_update(self):
"""#33: Verify parent dentry is properly set on lazy-load."""
fs, in_memory_fs = _make_fs(self.FAT_TYPE, lazy_load=True)
fs.makedirs("/foo")
fs.touch("/foo/bar")
foo_dentry = fs.fs.root_dir.get_entry("foo")
foobar_dentry = fs.fs.root_dir.get_entry("foo/bar")
assert foo_dentry._parent == fs.fs.root_dir
assert foobar_dentry._parent == foo_dentry
assert foo_dentry.get_full_path() == "foo"
assert foobar_dentry.get_full_path() == "foo/bar"

in_memory_fs.seek(0)
fs = PyFatBytesIOFS(BytesIO(in_memory_fs.read()),
encoding='UTF-8', lazy_load=True)
foo_dentry = fs.fs.root_dir.get_entry("foo")
foobar_dentry = fs.fs.root_dir.get_entry("foo/bar")
assert foo_dentry._parent == fs.fs.root_dir
assert foobar_dentry._parent == foo_dentry
assert foo_dentry.get_full_path() == "foo"
assert foobar_dentry.get_full_path() == "foo/bar"

def test_update_dentry_no_repopulate(self):
"""#33: Verify that update_dentry doesn't re-read entries from disk.

This is only problematic in case of lazy-loading, where
directory entries can be dynamically loaded, even when there
is a pending directory entry change; ultimately overwriting
the pending change.
"""
fs, in_memory_fs = _make_fs(self.FAT_TYPE, lazy_load=True)
fs.makedirs("/foo")
fs.touch("/foo/bar")
assert fs.listdir("/foo") == ['bar']

in_memory_fs.seek(0)
fs = PyFatBytesIOFS(BytesIO(in_memory_fs.read()),
encoding='UTF-8', lazy_load=True)
fs.touch("/foo/baz")
fs.remove("/foo/bar")
assert fs.listdir("/foo") == ['baz']

def test_lazy_vs_nonlazy_tree(self):
"""Compare directory tree between lazy and non-lazy loading."""
fs1, in_memory_fs = _make_fs(self.FAT_TYPE, lazy_load=False)
Expand Down
Loading