summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeremy Rand <jeremy@rand-family.com>2023-03-16 21:27:05 -0400
committerDamien George <damien@micropython.org>2023-03-21 16:13:10 +1100
commitd677023b3decec2f10d6b00b84b34032db4a0fd4 (patch)
tree5765ffe91dba0b8325293a88a5cfa351cbac5a64
parent051e2900d97e6727034b4b59f18ec7abc517c9a8 (diff)
extmod/vfs_posix: Do not filter '..*' in ilistdir when filtering '..'.
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 #11032. Signed-off-by: Jeremy Rand <jeremy@rand-family.com>
-rw-r--r--extmod/vfs_posix.c2
-rw-r--r--tests/extmod/vfs_posix_ilistdir_filter.py47
-rw-r--r--tests/extmod/vfs_posix_ilistdir_filter.py.exp1
3 files changed, 49 insertions, 1 deletions
diff --git a/extmod/vfs_posix.c b/extmod/vfs_posix.c
index 0a8274373..9b856e1f0 100644
--- a/extmod/vfs_posix.c
+++ b/extmod/vfs_posix.c
@@ -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;
}
diff --git a/tests/extmod/vfs_posix_ilistdir_filter.py b/tests/extmod/vfs_posix_ilistdir_filter.py
new file mode 100644
index 000000000..c32d12497
--- /dev/null
+++ b/tests/extmod/vfs_posix_ilistdir_filter.py
@@ -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)
diff --git a/tests/extmod/vfs_posix_ilistdir_filter.py.exp b/tests/extmod/vfs_posix_ilistdir_filter.py.exp
new file mode 100644
index 000000000..2f5107383
--- /dev/null
+++ b/tests/extmod/vfs_posix_ilistdir_filter.py.exp
@@ -0,0 +1 @@
+['...a', '..a', '.a', 'a..b', 'a.b']