Skip to content

Commit

Permalink
Fix missing parent directory entry link on lazy-load
Browse files Browse the repository at this point in the history
When lazy-loading an existing directory structure, the
parent directory entry reference is not properly set. This
leads to issues traversing a directory structure bottom up.
  • Loading branch information
zurcher authored and nathanhi committed Oct 16, 2023
1 parent 57f09cd commit 4e56534
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ 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>`_

Removed
~~~~~~~
Expand Down
2 changes: 2 additions & 0 deletions pyfatfs/FATDirectoryEntry.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,8 @@ 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)

def _get_entries_raw(self):
"""Get a full list of entries in current directory."""
Expand Down
22 changes: 22 additions & 0 deletions tests/test_PyFatFS.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,28 @@ 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_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

0 comments on commit 4e56534

Please sign in to comment.