Skip to content

Commit 7617616

Browse files
authored
Test more edge cases in test_unlink (#16396)
Test that open files can still be read or written after they are unlinked and that open directories can still be read after they are unlinked but that they cannot gain new children. Somewhat surprisingly, the old filesystem implementation passes this test with no modifications. WasmFS cannot run it because it does not have an implementation of `unlinkat`, and even if it did, it still would not pass.
1 parent 0f78b87 commit 7617616

File tree

1 file changed

+37
-5
lines changed

1 file changed

+37
-5
lines changed

tests/other/test_unlink.cpp

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
// University of Illinois/NCSA Open Source License. Both these licenses can be
44
// found in the LICENSE file.
55

6+
#include <dirent.h>
7+
#include <errno.h>
68
#include <fcntl.h>
79
#include <stdio.h>
10+
#include <string.h>
811
#include <sys/stat.h>
912
#include <unistd.h>
1013

@@ -13,13 +16,10 @@ int main() {
1316
const char *dirname = "test";
1417

1518
// Create a file
16-
FILE *f = fopen(filename, "wb");
19+
FILE* f = fopen(filename, "w+");
1720
if (f == NULL) {
1821
return 1;
1922
}
20-
if (fclose(f)) {
21-
return 1;
22-
}
2323
// Check it exists
2424
if (access(filename, F_OK) != 0) {
2525
return 1;
@@ -32,9 +32,30 @@ int main() {
3232
if (access(filename, F_OK) != -1) {
3333
return 1;
3434
}
35+
// Check that we can still write to it
36+
if (fwrite("hello", 1, 5, f) != 5) {
37+
return 1;
38+
}
39+
// And seek in it.
40+
if (fseek(f, 0, SEEK_SET) != 0) {
41+
return 1;
42+
}
43+
// And read from it.
44+
char buf[6] = {0};
45+
if (fread(buf, 1, 5, f) != 5 || strcmp("hello", buf) != 0) {
46+
return 1;
47+
}
48+
if (fclose(f)) {
49+
return 1;
50+
}
3551

3652
// Create a directory
37-
if (mkdir(dirname, 0700)) {
53+
if (mkdir(dirname, 0700) != 0) {
54+
return 1;
55+
}
56+
// Open the directory
57+
DIR* d = opendir(dirname);
58+
if (d == NULL) {
3859
return 1;
3960
}
4061
// Delete the directory
@@ -45,6 +66,17 @@ int main() {
4566
if (access(dirname, F_OK) != -1) {
4667
return 1;
4768
}
69+
// Check that we can still read the directory, but that it is empty.
70+
errno = 0;
71+
if (readdir(d) != NULL || errno != 0) {
72+
return 1;
73+
}
74+
// Check that we *cannot* create a child
75+
if (openat(dirfd(d), filename, O_CREAT | O_WRONLY, S_IRWXU) != -1) {
76+
return 1;
77+
}
78+
79+
closedir(d);
4880

4981
printf("ok\n");
5082

0 commit comments

Comments
 (0)