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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
#include <sys/stat.h>
#include <sys/time.h>
#include <assert.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <sched.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <utime.h>
int main(void)
{
struct timespec times[2];
struct dirent * entry;
char buf[4];
DIR * dir;
FILE * fp;
struct stat st;
off_t offset;
int fd;
int res;
fd = open("/sandbox/testfile", O_CREAT | O_RDWR, 0644);
assert(fd != -1);
res = posix_fallocate(fd, 0, 10000);
assert(res == 0);
res = fstat(fd, &st);
assert(res == 0);
assert(st.st_size == 10000);
res = ftruncate(fd, 1000);
res = fstat(fd, &st);
assert(res == 0);
assert(st.st_size == 1000);
assert(st.st_nlink == 1);
res = posix_fadvise(fd, 0, 1000, POSIX_FADV_RANDOM);
assert(res == 0);
res = (int) write(fd, "test", 4);
assert(res == 4);
offset = lseek(fd, 0, SEEK_CUR);
assert(offset == 4);
offset = lseek(fd, 0, SEEK_END);
assert(offset == 1000);
offset = lseek(fd, 0, SEEK_SET);
assert(offset == 0);
res = fdatasync(fd);
assert(res == 0);
res = fsync(fd);
assert(res == 0);
times[0] = (struct timespec){ .tv_sec = 1557403800, .tv_nsec = 0 };
times[1] = (struct timespec){ .tv_sec = 1557403800, .tv_nsec = 0 };
res = futimens(fd, times);
assert(res == 0);
res = pread(fd, buf, sizeof buf, 2);
assert(res == 4);
assert(buf[1] == 't');
res = pwrite(fd, "T", 1, 3);
assert(res == 1);
res = pread(fd, buf, sizeof buf, 2);
assert(res == 4);
assert(buf[1] == 'T');
res = close(fd);
assert(res == 0);
dir = opendir("/nonexistent");
assert(dir == NULL);
res = mkdir("/sandbox/test", 0755);
assert(res == 0);
res = mkdir("/sandbox/test", 0755);
assert(res == -1);
assert(errno == EEXIST);
res = rmdir("/sandbox/test");
assert(res == 0);
res = rmdir("/sandbox/test");
assert(res == -1);
res = rename("/sandbox/testfile", "/sandbox/testfile2");
assert(res == 0);
res = unlink("/sandbox/testfile");
assert(res == -1);
res = access("/sandbox/testfile2", R_OK);
assert(res == 0);
res = link("/sandbox/testfile2", "/sandbox/testfile-link");
assert(res == 0);
res = access("/sandbox/testfile-link", R_OK);
assert(res == 0);
res = symlink("/sandbox/testfile-link", "/sandbox/testfile-symlink");
assert(res == 0);
res = symlink("/sandbox/testfile2", "/sandbox/testfile-symlink");
assert(res == -1);
res = sched_yield();
assert(res == 0);
fd = open("/sandbox/testfile2", O_RDONLY);
assert(fd != -1);
fp = fdopen(fd, "r");
assert(fp != NULL);
res = fgetc(fp);
assert(res == 't');
res = fclose(fp);
assert(res == 0);
dir = opendir("/sandbox");
assert(dir != NULL);
res = 0;
while ((entry = readdir(dir)) != NULL) {
if (entry->d_name[0] == '.') {
res += 1000;
} else {
res++;
}
}
assert(res == 2003);
res = closedir(dir);
assert(res == 0);
res = mkdir("/sandbox/a", 0755);
assert(res == 0);
res = mkdir("/sandbox/a/b", 0755);
assert(res == 0);
res = mkdir("/sandbox/a/b/c", 0755);
assert(res == 0);
res = access("/sandbox/a/b/c", R_OK);
assert(res == 0);
return 0;
}
|