-
Notifications
You must be signed in to change notification settings - Fork 3
/
file-unit-test.c
200 lines (161 loc) · 4 KB
/
file-unit-test.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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#define _GNU_SOURCE
#include <assert.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <limits.h>
#include <pthread.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <libgen.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include "zunkfs.h"
#include "zunkfs-tests.h"
#include "utils.h"
#include "file.h"
#include "dir.h"
#include "chunk-db.h"
static const char spaces[] = " ";
#define indent_start (spaces + sizeof(spaces) - 1)
void test_import(char *path)
{
off_t offset;
int fd, err;
struct open_file *ofile;
struct timeval start, end, delta;
struct stat stbuf;
gettimeofday(&start, NULL);
fd = open(path, O_RDONLY);
if (fd < 0) {
if (errno == EPERM)
return;
if (errno == EACCES)
return;
panic("open %s: (%d) %s\n", path, errno, strerror(errno));
}
if (fstat(fd, &stbuf)) {
close(fd);
return;
}
if (!S_ISREG(stbuf.st_mode)) {
close(fd);
return;
}
fprintf(stderr, "importing %s\n", path);
ofile = create_file(basename(path), 0700 | S_IFREG);
if (IS_ERR(ofile))
panic("create_file: %s\n", strerror(PTR_ERR(ofile)));
for (offset = 0;;) {
char buf[4096];
int n, m;
read_again:
n = read(fd, buf, sizeof(buf));
if (n < 0) {
if (errno != EINTR)
panic("read %s: %s\n", path, strerror(errno));
goto read_again;
}
if (!n)
break;
write_again:
m = write_file(ofile, buf, n, offset);
if (m < 0) {
if (errno != EINTR)
panic("write: %s\n", strerror(-m));
goto write_again;
}
assert(m == n);
offset += m;
}
err = flush_file(ofile);
if (err < 0)
panic("flush_file: %s\n", strerror(-err));
err = close_file(ofile);
if (err < 0)
panic("close_file: %s\n", strerror(-err));
ofile = open_file(basename(path));
if (IS_ERR(ofile))
panic("open_file: %s\n", strerror(-err));
gettimeofday(&end, NULL);
timersub(&end, &start, &delta);
start = end;
printf("time to import: %lu.%06lu\n",
(unsigned long)delta.tv_sec,
(unsigned long)delta.tv_usec);
fprintf(stderr, "verifying...\n");
err = lseek(fd, 0, SEEK_SET);
assert(err == 0);
for (offset = 0;;) {
char buf[4096];
char buf2[4096];
int n, m;
memset(buf, 0, sizeof(buf));
memset(buf2, 0, sizeof(buf2));
read_again2:
n = read(fd, buf, sizeof(buf));
if (n < 0) {
if (errno != EINTR)
panic("read %s: %s\n", path, strerror(errno));
goto read_again2;
}
if (!n)
break;
read_again3:
m = read_file(ofile, buf2, n, offset);
if (m < 0) {
if (errno != EINTR)
panic("write: %s\n", strerror(-m));
goto read_again3;
}
assert(m == n);
assert(!memcmp(buf, buf2, n));
offset += m;
}
printf("size=%"PRIu64" nr_leafs=%u height=%u\n",
file_dentry(ofile)->size,
file_dentry(ofile)->chunk_tree.nr_leafs,
file_dentry(ofile)->chunk_tree.height);
err = close_file(ofile);
if (err < 0)
panic("close_file: %s\n", strerror(-err));
close(fd);
gettimeofday(&end, NULL);
timersub(&end, &start, &delta);
printf("time to verify: %lu.%06lu\n",
(unsigned long)delta.tv_sec,
(unsigned long)delta.tv_usec);
}
int main(int argc, char **argv)
{
struct disk_dentry root_ddent;
DECLARE_MUTEX(root_mutex);
char *errstr;
int i, err;
err = set_logging("T,stdout");
if (err)
panic("set_logging: %s\n", strerror(-err));
errstr = add_chunkdb("rw,mem:");
if (errstr)
panic("add_chunkdb: %s\n", STR_OR_ERROR(errstr));
err = init_disk_dentry(&root_ddent);
if (err < 0)
panic("init_disk_dentry: %s\n", strerror(-err));
namcpy(root_ddent.name, "/");
root_ddent.mode = htole16(S_IFDIR | S_IRWXU);
root_ddent.size = htole64(0);
root_ddent.ctime = htole32(time(NULL));
root_ddent.mtime = htole32(time(NULL));
err = set_root(&root_ddent, &root_mutex);
if (err)
panic("set_root: %s\n", strerror(-err));
for (i = 1; i < argc; i ++)
test_import(argv[i]);
return 0;
}