8000 extmod/vfs_posix: Do not filter '..*' in ilistdir when filtering '..'. · alphaFred/micropython@d677023 · GitHub
[go: up one dir, main page]

Skip to content

Commit d677023

Browse files
jeremysranddpgeorge
authored andcommitted
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 micropython#11032. Signed-off-by: Jeremy Rand <jeremy@rand-family.com>
1 parent 051e290 commit d677023

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

extmod/vfs_posix.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ STATIC mp_obj_t vfs_posix_ilistdir_it_iternext(mp_obj_t self_in) {
192192
MP_THREAD_GIL_ENTER();
193193
const char *fn = dirent->d_name;
194194

195-
if (fn[0] == '.' && (fn[1] == 0 || fn[1] == '.')) {
195+
if (fn[0] == '.' && (fn[1] == 0 || (fn[1] == '.' && fn[2] == 0))) {
196196
// skip . and ..
197197
continue;
198198
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Test ilistdir filter of . and .. for VfsPosix.
2+
3+
try:
4+
import os
5+
6+
os.VfsPosix
7+
except (ImportError, AttributeError):
8+
print("SKIP")
9+
raise SystemExit
10+
11+
12+
def test(testdir):
13+
vfs = os.VfsPosix(testdir)
14+
15+
dirs = [".a", "..a", "...a", "a.b", "a..b"]
16+
17+
for dir in dirs:
18+
vfs.mkdir(dir)
19+
20+
dirs = []
21+
for entry in vfs.ilistdir("/"):
22+
dirs.append(entry[0])
23+
dirs.sort()
24+
25+
print(dirs)
26+
27+
28+
# We need an empty directory for testing.
29+
# Skip the test if it already exists.
30+
temp_dir = "vfs_posix_ilistdir_filter_test_dir"
31+
try:
32+
os.stat(temp_dir)
33+
print("SKIP")
34+
raise SystemExit
35+
except OSError:
36+
pass
37+
38+
os.mkdir(temp_dir)
39+
40+
try:
41+
test(temp_dir)
42+
finally:
43+
# Remove tempdir.
44+
for td in os.listdir(temp_dir):
45+
os.rmdir("/".join((temp_dir, td)))
46+
47+
os.rmdir(temp_dir)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
['...a', '..a', '.a', 'a..b', 'a.b']

0 commit comments

Comments
 (0)
0