Skip to content

Commit 7cef1a5

Browse files
authored
Update test_wasmfs_getdents. NFC (emscripten-core#23839)
Handle more file entry types. Don't attempt to seek to position 1. Instead seek back to a previously seen position. I'm honestly not even sure this is valid but both old and new filesystems support this. These changes are in preparation for running this test on all filesystem types.
1 parent 5ef962d commit 7cef1a5

File tree

2 files changed

+53
-34
lines changed

2 files changed

+53
-34
lines changed

test/wasmfs/wasmfs_getdents.c

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@
2020
#include <sys/syscall.h>
2121
#include <unistd.h>
2222

23+
const char* type_to_string(char d_type) {
24+
switch (d_type) {
25+
case DT_REG: return "DT_REG";
26+
case DT_DIR: return "DT_DIR";
27+
case DT_CHR: return "DT_CHR";
28+
case DT_BLK: return "DT_BLK";
29+
case DT_LNK: return "DT_LNK";
30+
default: abort();
31+
}
32+
}
33+
2334
void print_one(int fd) {
2435
struct dirent d;
2536
int nread = getdents(fd, &d, sizeof(d));
@@ -29,23 +40,17 @@ void print_one(int fd) {
2940
}
3041
printf("d.d_name = %s\n", d.d_name);
3142
printf("d.d_reclen = %hu\n", d.d_reclen);
32-
printf("d.d_type = %s\n\n",
33-
(d.d_type == DT_REG) ? "regular"
34-
: (d.d_type == DT_DIR) ? "directory"
35-
: "???");
43+
printf("d.d_type = %s\n\n", type_to_string(d.d_type));
3644
}
3745

38-
void print(const char* dir) {
46+
void print_dir(const char* dir) {
3947
struct dirent** entries;
4048
int nentries = scandir(dir, &entries, NULL, alphasort);
4149
assert(nentries != -1);
4250
for (int i = 0; i < nentries; i++) {
4351
printf("d.d_name = %s\n", entries[i]->d_name);
4452
printf("d.d_reclen = %hu\n", entries[i]->d_reclen);
45-
printf("d.d_type = %s\n\n",
46-
(entries[i]->d_type == DT_REG) ? "regular"
47-
: (entries[i]->d_type == DT_DIR) ? "directory"
48-
: "???");
53+
printf("d.d_type = %s\n\n", type_to_string(entries[i]->d_type));
4954
free(entries[i]);
5055
}
5156
free(entries);
@@ -66,7 +71,7 @@ int main() {
6671

6772
// Try opening the directory that was just created.
6873
printf("------------- Reading from root/working Directory -------------\n");
69-
print("root/working");
74+
print_dir("root/working");
7075

7176
int fd = open("root/working", O_RDONLY | O_DIRECTORY);
7277

@@ -95,7 +100,7 @@ int main() {
95100

96101
// Try opening the dev directory and read its contents.
97102
printf("------------- Reading from /dev Directory -------------\n");
98-
print("/dev");
103+
print_dir("/dev");
99104

100105
// The same, but via the JS API.
101106
printf("------------- Reading from /dev Directory via JS -------------\n");
@@ -107,12 +112,17 @@ int main() {
107112
console.log();
108113
});
109114

110-
// Try to advance the offset of the directory.
111-
// Expect that '.' will be skipped.
115+
// Try to advance reset of the offset of the directory.
116+
// Expect that '..' be printed a second time.
112117
fd = open("root/working", O_RDONLY | O_DIRECTORY);
113-
printf("root/working file position is: %lli\n", lseek(fd, 1, SEEK_SET));
114-
printf(
115-
"------------- Reading one from root/working Directory -------------\n");
118+
print_one(fd);
119+
off_t pos = lseek(fd, 0, SEEK_CUR);
120+
print_one(fd);
121+
// Reset back to the previous position
122+
printf("rewinding from position %llu to %lli\n", lseek(fd, 0, SEEK_CUR), pos);
123+
lseek(fd, pos, SEEK_SET);
124+
125+
printf("------------- Reading one from root/working Directory -------------\n");
116126
print_one(fd);
117127
close(fd);
118128

@@ -121,5 +131,5 @@ int main() {
121131
assert(fd != -1);
122132
close(fd);
123133
printf("------------- Reading from root/working Directory -------------\n");
124-
print("root/working");
134+
print_dir("root/working");
125135
}

test/wasmfs/wasmfs_getdents.out

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
------------- Reading from root/working Directory -------------
22
d.d_name = .
33
d.d_reclen = 280
4-
d.d_type = directory
4+
d.d_type = DT_DIR
55

66
d.d_name = ..
77
d.d_reclen = 280
8-
d.d_type = directory
8+
d.d_type = DT_DIR
99

1010
d.d_name = test
1111
d.d_reclen = 280
12-
d.d_type = directory
12+
d.d_type = DT_DIR
1313

1414
Errno: Bad file descriptor
1515
Errno: Invalid argument
@@ -18,35 +18,35 @@ Errno: Not a directory
1818
------------- Reading from /dev Directory -------------
1919
d.d_name = .
2020
d.d_reclen = 280
21-
d.d_type = directory
21+
d.d_type = DT_DIR
2222

2323
d.d_name = ..
2424
d.d_reclen = 280
25-
d.d_type = directory
25+
d.d_type = DT_DIR
2626

2727
d.d_name = null
2828
d.d_reclen = 280
29-
d.d_type = regular
29+
d.d_type = DT_REG
3030

3131
d.d_name = random
3232
d.d_reclen = 280
33-
d.d_type = regular
33+
d.d_type = DT_REG
3434

3535
d.d_name = stderr
3636
d.d_reclen = 280
37-
d.d_type = regular
37+
d.d_type = DT_REG
3838

3939
d.d_name = stdin
4040
d.d_reclen = 280
41-
d.d_type = regular
41+
d.d_type = DT_REG
4242

4343
d.d_name = stdout
4444
d.d_reclen = 280
45-
d.d_type = regular
45+
d.d_type = DT_REG
4646

4747
d.d_name = urandom
4848
d.d_reclen = 280
49-
d.d_type = regular
49+
d.d_type = DT_REG
5050

5151
------------- Reading from /dev Directory via JS -------------
5252
.
@@ -58,25 +58,34 @@ stdin
5858
stdout
5959
urandom
6060

61-
root/working file position is: 1
61+
d.d_name = .
62+
d.d_reclen = 280
63+
d.d_type = DT_DIR
64+
65+
d.d_name = ..
66+
d.d_reclen = 280
67+
d.d_type = DT_DIR
68+
69+
rewinding from position 2 to 1
6270
------------- Reading one from root/working Directory -------------
6371
d.d_name = ..
6472
d.d_reclen = 280
65-
d.d_type = directory
73+
d.d_type = DT_DIR
6674

6775
------------- Reading from root/working Directory -------------
6876
d.d_name = .
6977
d.d_reclen = 280
70-
d.d_type = directory
78+
d.d_type = DT_DIR
7179

7280
d.d_name = ..
7381
d.d_reclen = 280
74-
d.d_type = directory
82+
d.d_type = DT_DIR
7583

7684
d.d_name = foobar
7785
d.d_reclen = 280
78-
d.d_type = regular
86+
d.d_type = DT_REG
7987

8088
d.d_name = test
8189
d.d_reclen = 280
82-
d.d_type = directory
90+
d.d_type = DT_DIR
91+

0 commit comments

Comments
 (0)