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>
This commit is contained in:
Jeremy Rand 2023-03-16 21:27:05 -04:00 committed by Damien George
parent 051e2900d9
commit d677023b3d
3 changed files with 49 additions and 1 deletions

View File

@ -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;
}

View File

@ -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)

View File

@ -0,0 +1 @@
['...a', '..a', '.a', 'a..b', 'a.b']