-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-cmd.c
197 lines (178 loc) · 4.26 KB
/
test-cmd.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include "plus.h"
static const char *self; // argv[0]
static void usage(int x)
{
printf("Usage: %s CMDFILE\n", basename(self));
printf("CMDFILE is a file consisting of commands\n");
printf("The following commands are defined:\n");
printf("add DELTA -- add a delta file\n");
printf("open MODE -- open a set of added deltas\n");
printf(" MODE is one of r, rw, w\n");
printf("read OFFSET SIZE FILE -- read a block of data\n");
printf("write OFFSET SIZE FILE -- write a block of data\n");
printf("close -- close the set\n");
printf("# .... -- a comment (ignored)\n");
exit(x);
}
int main(int argc, char **argv)
{
struct plus_image *img = NULL;
self = argv[0];
argv++; argc--;
char *deltas[128];
int num_deltas = 0;
if (argc != 1) {
fprintf(stderr, "Error: invalid number of arguments\n");
usage(2);
}
const char *cmdfile = argv[0];
FILE *f = fopen(cmdfile, "r");
if (!f) {
fprintf(stderr, "Can't open %s: %m\n", cmdfile);
exit(2);
}
char cmd[1024];
size_t size = 0;
off_t offset = 0;
char *file = NULL;
int lineno = 0;
int ret = 0;
while (fgets(cmd, sizeof(cmd), f) != NULL) {
lineno++;
int len = strlen(cmd);
if (cmd[len-1] != '\n') {
fprintf(stderr, "Bad command on line %d "
"(no newline or truncated)\n", lineno);
ret = 2;
goto out;
}
// remove the newline
cmd[len-1] = '\0';
if (cmd[0] == '#') {
// comment
printf("%s\n", cmd);
continue;
}
if (cmd[0] == '\0') {
// empty line
continue;
}
printf("CMD %s\n", cmd);
if (strncmp(cmd, "add ", 4) == 0) {
deltas[num_deltas++] = strdup(cmd + 4);
} else if (strncmp(cmd, "open ", 5) == 0) {
// FIXME: ignore the mode for now
if (num_deltas == 0) {
fprintf(stderr, "No deltas added before open\n");
ret = 2;
goto out;
}
if (img) {
fprintf(stderr, "Ploop already opened\n");
ret = 2;
goto out;
}
img = plus_open(num_deltas, deltas, O_RDWR);
if (!img) {
fprintf(stderr, "Can't open ploop\n");
ret = 1;
goto out;
}
} else if (strncmp(cmd, "read ", 5) == 0) {
if (!img) {
fprintf(stderr, "Ploop not opened\n");
ret = 2;
goto out;
}
if (sscanf(cmd + 5, "%zd %zu %ms", &offset, &size, &file) != 3) {
fprintf(stderr, "Can't parse command %s\n", cmd);
ret = 2;
goto out;
}
int fd = open(file, O_RDWR|O_CREAT|O_EXCL, 0600);
if (fd < 0) {
fprintf(stderr, "Can't open %s for writing: %m\n", file);
ret = 1;
goto out;
}
if (ftruncate(fd, size)) {
fprintf(stderr, "Can't truncate %s to %zd: %m\n", file, size);
ret = 1;
goto out;
}
void *map = mmap(0, size, PROT_WRITE, MAP_SHARED, fd, 0);
if (map == MAP_FAILED) {
fprintf(stderr, "Can't mmap %s: %m\n", file);
ret = 1;
goto out;
}
free(file); file = NULL;
ssize_t ret = plus_read(img, size, offset, map);
if (ret != size) {
fprintf(stderr, "READ failed: %zd\n", ret);
ret = 1;
goto out;
}
munmap(map, size);
close(fd);
} else if (strncmp(cmd, "write ", 6) == 0) {
if (!img) {
fprintf(stderr, "Ploop not opened\n");
ret = 2;
goto out;
}
if (sscanf(cmd + 6, "%zd %zu %ms", &offset, &size, &file) != 3) {
fprintf(stderr, "Can't parse command %s\n", cmd);
ret = 2;
goto out;
}
int fd = open(file, O_RDONLY);
if (fd < 0) {
fprintf(stderr, "Can't open %s for reading: %m\n", file);
ret = 1;
goto out;
}
void *map = mmap(0, size, PROT_READ, MAP_SHARED, fd, 0);
if (map == MAP_FAILED) {
fprintf(stderr, "Can't mmap %s: %m\n", file);
ret = 1;
goto out;
}
free(file); file = NULL;
ssize_t ret = plus_write(img, size, offset, map);
if (ret != size) {
fprintf(stderr, "WRITE failed: %zd\n", ret);
ret = 1;
goto out;
}
munmap(map, size);
close(fd);
} else if (strncmp(cmd, "close", 5) == 0) {
if (!img) {
fprintf(stderr, "Ploop not opened\n");
ret = 2;
goto out;
}
plus_close(img);
img = NULL;
} else {
fprintf(stderr, "Unknown cmd: %s\n", cmd);
ret = 2;
goto out;
}
}
printf("%s %s: PASS\n", self, cmdfile);
out:
if (img)
plus_close(img);
fclose(f);
return ret;
}