Skip to content

Commit

Permalink
extmod/vfs_posix: Do not filter '..*' in ilistdir when filtering '..'.
Browse files Browse the repository at this point in the history
When iterating over os.ilistdir(), the special directories '.' and '..'
are filtered from the results.  But the code inadvertently also filtered
any file/directory which happened to match '..*'.  This change fixes the
filter.

Fixes issue micropython#11032.

Signed-off-by: Jeremy Rand <[email protected]>
  • Loading branch information
jeremysrand authored and dpgeorge committed Mar 21, 2023
1 parent 051e290 commit d677023
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
2 changes: 1 addition & 1 deletion extmod/vfs_posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ STATIC mp_obj_t vfs_posix_ilistdir_it_iternext(mp_obj_t self_in) {
MP_THREAD_GIL_ENTER();
const char *fn = dirent->d_name;

if (fn[0] == '.' && (fn[1] == 0 || fn[1] == '.')) {
if (fn[0] == '.' && (fn[1] == 0 || (fn[1] == '.' && fn[2] == 0))) {
// skip . and ..
continue;
}
Expand Down
47 changes: 47 additions & 0 deletions tests/extmod/vfs_posix_ilistdir_filter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Test ilistdir filter of . and .. for VfsPosix.

try:
import os

os.VfsPosix
except (ImportError, AttributeError):
print("SKIP")
raise SystemExit


def test(testdir):
vfs = os.VfsPosix(testdir)

dirs = [".a", "..a", "...a", "a.b", "a..b"]

for dir in dirs:
vfs.mkdir(dir)

dirs = []
for entry in vfs.ilistdir("/"):
dirs.append(entry[0])
dirs.sort()

print(dirs)


# We need an empty directory for testing.
# Skip the test if it already exists.
temp_dir = "vfs_posix_ilistdir_filter_test_dir"
try:
os.stat(temp_dir)
print("SKIP")
raise SystemExit
except OSError:
pass

os.mkdir(temp_dir)

try:
test(temp_dir)
finally:
# Remove tempdir.
for td in os.listdir(temp_dir):
os.rmdir("/".join((temp_dir, td)))

os.rmdir(temp_dir)
1 change: 1 addition & 0 deletions tests/extmod/vfs_posix_ilistdir_filter.py.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
['...a', '..a', '.a', 'a..b', 'a.b']

0 comments on commit d677023

Please sign in to comment.