-
Notifications
You must be signed in to change notification settings - Fork 58
/
p-testwritefs4.cc
165 lines (107 loc) · 3.67 KB
/
p-testwritefs4.cc
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
159
160
161
162
163
164
165
#include "u-lib.hh"
void process_main() {
printf("Starting testwritefs4 (assuming clean file system)...\n");
// read file
printf("%s:%d: read...\n", __FILE__, __LINE__);
int f = sys_open("emerson.txt", OF_READ);
assert_gt(f, 2);
char buf[200];
memset(buf, 0, sizeof(buf));
ssize_t n = sys_read(f, buf, 200);
assert_eq(n, 130);
assert_memeq(buf, "When piped a tiny voice hard by,\n"
"Gay and polite, a cheerful cry,\n"
"Chic-chicadeedee", 81);
sys_close(f);
// create file
printf("%s:%d: create...\n", __FILE__, __LINE__);
f = sys_open("geisel.txt", OF_WRITE | OF_CREATE);
assert_gt(f, 2);
n = sys_write(f, "Why, girl, you're insane!\n"
"Elephants don't hatch chickadee eggs!\n", 64);
assert_eq(n, 64);
sys_close(f);
// read back
printf("%s:%d: read created...\n", __FILE__, __LINE__);
f = sys_open("geisel.txt", OF_READ);
assert_gt(f, 2);
memset(buf, 0, sizeof(buf));
n = sys_read(f, buf, 200);
assert_eq(n, 64);
assert_memeq(buf, "Why, girl, you're insane!\n"
"Elephants don't hatch chickadee eggs!\n", 64);
sys_close(f);
// remove file
printf("%s:%d: unlink...\n", __FILE__, __LINE__);
int r = sys_unlink("geisel.txt");
assert_eq(r, 0);
f = sys_open("geisel.txt", OF_READ);
assert_eq(f, E_NOENT);
// synchronize disk
printf("%s:%d: sync...\n", __FILE__, __LINE__);
r = sys_sync(1);
assert_ge(r, 0);
// read back
printf("%s:%d: reread...\n", __FILE__, __LINE__);
f = sys_open("geisel.txt", OF_READ);
assert_eq(f, E_NOENT);
// unlink while open
printf("%s:%d: unlink while open...\n", __FILE__, __LINE__);
f = sys_open("dickinson.txt", OF_READ);
assert_gt(f, 2);
memset(buf, 0, sizeof(buf));
n = sys_read(f, buf, 34);
assert_eq(n, 34);
assert_memeq(buf, "The Birds begun at Four o'clock -\n", 34);
r = sys_unlink("dickinson.txt");
assert_eq(r, 0);
n = sys_read(f, buf, 24);
assert_eq(n, 24);
assert_memeq(buf, "Their period for Dawn -\n", 24);
r = sys_unlink("dickinson.txt");
assert_eq(r, E_NOENT);
// recreate
printf("%s:%d: recreate...\n", __FILE__, __LINE__);
int wf = sys_open("dickinson.txt", OF_READ | OF_WRITE | OF_CREATE);
assert_gt(wf, 2);
assert_ne(f, wf);
n = sys_read(wf, buf, sizeof(buf));
assert_eq(n, 0);
n = sys_write(wf, "A Bird, came down the Walk -\n"
"He did not know I saw -\n", 53);
assert_eq(n, 53);
size_t sz = sys_lseek(wf, 0, LSEEK_SET);
assert_eq(sz, 0U);
// reread
printf("%s:%d: read both...\n", __FILE__, __LINE__);
n = sys_read(f, buf, 28);
assert_eq(n, 28);
assert_memeq(buf, "A Music numerous as space -\n", 28);
n = sys_read(wf, buf, 29);
assert_eq(n, 29);
assert_memeq(buf, "A Bird, came down the Walk -\n", 29);
// synchronize
printf("%s:%d: sync...\n", __FILE__, __LINE__);
r = sys_sync(1);
assert_ge(r, 0);
// keep reading
printf("%s:%d: continue reading...\n", __FILE__, __LINE__);
n = sys_read(f, buf, 26);
assert_eq(n, 26);
assert_memeq(buf, "But neighboring as Noon -\n", 26);
n = sys_read(wf, buf, 24);
assert_eq(n, 24);
assert_memeq(buf, "He did not know I saw -\n", 24);
// close (should free file data)
printf("%s:%d: close...\n", __FILE__, __LINE__);
r = sys_close(f);
assert_ge(r, 0);
r = sys_close(wf);
assert_ge(r, 0);
// synchronize
printf("%s:%d: sync...\n", __FILE__, __LINE__);
r = sys_sync(1);
assert_ge(r, 0);
printf("testwritefs4 succeeded.\n");
sys_exit(0);
}