-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtest-readdir.c
118 lines (91 loc) · 2.86 KB
/
test-readdir.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <unistd.h>
#include <dirent.h>
#include <fcntl.h>
#include <sys/stat.h>
#include "hfs-tests.h"
#include "test-utils.h"
#include "disk-image.h"
#include "systemx.h"
TEST(readdir)
static disk_image_t *di;
int run_readdir(__unused test_ctx_t *ctx)
{
di = disk_image_get();
char *path;
asprintf(&path, "%s/readdir.data", di->mount_point);
systemx("/bin/rm", "-rf", path, NULL);
assert_no_err(mkdir(path, 0777));
char *file;
asprintf(&file, "%s/file1", path);
int fd;
int numeof;
assert_with_errno((fd = open(file, O_CREAT | O_TRUNC | O_RDWR, 0666)) >= 0);
assert_no_err(close(fd));
file[strlen(file) - 1] = '2';
assert_with_errno((fd = open(file, O_CREAT | O_TRUNC | O_RDWR, 0666)) >= 0);
assert_no_err(close(fd));
DIR *dir;
assert_with_errno((dir = opendir(path)) != NULL);
struct dirent entry, *dp;
assert_no_err(readdir_r(dir, &entry, &dp));
assert(!strcmp(dp->d_name, "."));
assert_no_err(readdir_r(dir, &entry, &dp));
assert(!strcmp(dp->d_name, ".."));
assert_no_err(readdir_r(dir, &entry, &dp));
assert(!strcmp(dp->d_name, "file1"));
assert_no_err(readdir_r(dir, &entry, &dp));
assert(!strcmp(dp->d_name, "file2"));
assert_no_err(readdir_r(dir, &entry, &dp));
assert(dp == NULL);
closedir(dir);
assert_with_errno((dir = opendir(path)) != NULL);
assert_no_err(readdir_r(dir, &entry, &dp));
assert(!strcmp(dp->d_name, "."));
assert_no_err(readdir_r(dir, &entry, &dp));
assert(!strcmp(dp->d_name, ".."));
assert_no_err(readdir_r(dir, &entry, &dp));
assert(!strcmp(dp->d_name, "file1"));
assert_no_err(readdir_r(dir, &entry, &dp));
assert(!strcmp(dp->d_name, "file2"));
assert_no_err(readdir_r(dir, &entry, &dp));
assert(dp == NULL);
assert_no_err(readdir_r(dir, &entry, &dp));
assert(dp == NULL);
unlink(file);
assert_no_err(readdir_r(dir, &entry, &dp));
assert(dp == NULL);
assert_no_err(closedir(dir));
assert_with_errno((dir = opendir(path)) != NULL);
assert_no_err(readdir_r(dir, &entry, &dp));
assert(!strcmp(dp->d_name, "."));
assert_no_err(readdir_r(dir, &entry, &dp));
assert(!strcmp(dp->d_name, ".."));
assert_no_err(readdir_r(dir, &entry, &dp));
assert(!strcmp(dp->d_name, "file1"));
assert_no_err(readdir_r(dir, &entry, &dp));
assert_no_err(readdir_r(dir, &entry, &dp));
assert(dp == NULL);
assert_no_err(readdir_r(dir, &entry, &dp));
assert(dp == NULL);
file[strlen(file) - 1] = '1';
unlink(file);
assert_no_err(readdir_r(dir, &entry, &dp));
assert(dp == NULL);
assert_no_err(closedir(dir));
assert_with_errno((dir = opendir(path)) != NULL);
assert_with_errno((fd = open(file, O_CREAT | O_TRUNC | O_RDWR, 0666)) >= 0);
assert_no_err(close(fd));
numeof = 0;
for (;;) {
assert_no_err(readdir_r(dir, &entry, &dp));
if (dp !=0)
continue;
assert_no_err(readdir_r(dir, &entry, &dp));
if (++numeof == 3)
break;
if (numeof == 1)
assert_no_err(unlink(file));
}
assert_no_err(closedir(dir));
return 0;
}