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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
|
#include "otap.h"
//#define NDEBUG
#include "error.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
#include <inttypes.h>
#include <time.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#include <utime.h>
static int _otap_apply_identify(FILE* stream) {
uint8_t cmd;
if(fread(&cmd, 1, 1, stream) != 1)
otap_error(otap_error_unable_to_read_stream);
if(cmd != otap_cmd_identify)
otap_error(otap_error_invalid_parameter);
uint16_t nlen;
if(fread(&nlen, 2, 1, stream) != 1)
otap_error(otap_error_unable_to_read_stream);
if(strlen(otap_ident) != nlen)
otap_error(otap_error_invalid_parameter);
char nstr[nlen];
if(fread(nstr, 1, nlen, stream) != nlen)
otap_error(otap_error_unable_to_read_stream);
if(strncmp(nstr, otap_ident, nlen) != 0)
otap_error(otap_error_invalid_parameter);
return 0;
}
static int _otap_apply_cmd_dir_create(FILE* stream) {
uint16_t dlen;
if(fread(&dlen, 2, 1, stream) != 1)
otap_error(otap_error_unable_to_read_stream);
char dname[dlen + 1];
if(fread(dname, 1, dlen, stream) != dlen)
otap_error(otap_error_unable_to_read_stream);
dname[dlen] = '\0';
printf("cmd_dir_create %s\n", dname);
if(strchr(dname, '/') != NULL)
otap_error(otap_error_invalid_parameter);
uint32_t mtime;
if(fread(&mtime, 4, 1, stream) != 1)
otap_error(otap_error_unable_to_read_stream);
if(mkdir(dname, (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) != 0)
otap_error(otap_error_unable_to_create_dir);
// Apply metadata.
struct utimbuf timebuff = { time(NULL), mtime };
utime(dname, &timebuff); // Don't care if it succeeds right now.
return 0;
}
static int _otap_apply_cmd_dir_enter(FILE* stream, uintptr_t* depth) {
uint16_t dlen;
if(fread(&dlen, 2, 1, stream) != 1)
otap_error(otap_error_unable_to_read_stream);
char dname[dlen + 1];
if(fread(dname, 1, dlen, stream) != dlen)
otap_error(otap_error_unable_to_read_stream);
dname[dlen] = '\0';
printf("cmd_dir_enter %s\n", dname);
if((strchr(dname, '/') != NULL) || (strcmp(dname, "..") == 0))
otap_error(otap_error_unable_to_change_dir);
if(depth != NULL)
(*depth)++;
if(chdir(dname) != 0)
otap_error(otap_error_unable_to_change_dir);
return 0;
}
static int _otap_apply_cmd_dir_leave(FILE* stream, uintptr_t* depth) {
uint8_t count;
if(fread(&count, 1, 1, stream) != 1)
otap_error(otap_error_unable_to_read_stream);
uintptr_t rcount = count + 1;
printf("cmd_dir_leave %"PRIuPTR"\n", rcount);
if((depth != NULL) && (*depth < rcount))
otap_error(otap_error_invalid_parameter);
uintptr_t i;
for(i = 0; i < rcount; i++) {
if(chdir("..") != 0)
otap_error(otap_error_unable_to_change_dir);
}
if(depth != NULL)
*depth -= rcount;
return 0;
}
static int _otap_apply_cmd_file_create(FILE* stream) {
uint16_t flen;
if(fread(&flen, 2, 1, stream) != 1)
otap_error(otap_error_unable_to_read_stream);
char fname[flen + 1];
if(fread(fname, 1, flen, stream) != flen)
otap_error(otap_error_unable_to_read_stream);
fname[flen] = '\0';
if((strchr(fname, '/') != NULL) || (strcmp(fname, "..") == 0))
otap_error(otap_error_invalid_parameter);
uint32_t mtime;
if(fread(&mtime, 4, 1, stream) != 1)
otap_error(otap_error_unable_to_read_stream);
uint32_t fsize;
if(fread(&fsize, 4, 1, stream) != 1)
otap_error(otap_error_unable_to_read_stream);
printf("cmd_file_create %s:%"PRId32"\n", fname, fsize);
FILE* fp = fopen(fname, "rb");
if(fp != NULL) {
fclose(fp);
otap_error(otap_error_file_already_exists);
}
fp = fopen(fname, "wb");
if(fp == NULL)
otap_error(otap_error_unable_to_open_file_for_writing);
uintptr_t block = 256;
uint8_t fbuff[block];
for(; fsize != 0; fsize -= block) {
if(fsize < block)
block = fsize;
if(fread(fbuff, 1, block, stream) != block)
otap_error(otap_error_unable_to_read_stream);
if(fwrite(fbuff, 1, block, fp) != block)
otap_error(otap_error_unable_to_write_stream);
}
fclose(fp);
// Apply metadata.
struct utimbuf timebuff = { time(NULL), mtime };
utime(fname, &timebuff); // Don't care if it succeeds right now.
return 0;
}
static int _otap_apply_cmd_file_delta(FILE* stream) {
uint16_t flen;
if(fread(&flen, 2, 1, stream) != 1)
otap_error(otap_error_unable_to_read_stream);
char fname[flen + 1];
if(fread(fname, 1, flen, stream) != flen)
otap_error(otap_error_unable_to_read_stream);
fname[flen] = '\0';
printf("cmd_file_delta %s\n", fname);
if((strchr(fname, '/') != NULL) || (strcmp(fname, "..") == 0))
otap_error(otap_error_invalid_parameter);
uint32_t mtime;
if(fread(&mtime, 4, 1, stream) != 1)
otap_error(otap_error_unable_to_read_stream);
FILE* op = fopen(fname, "rb");
if(op == NULL)
otap_error(otap_error_unable_to_open_file_for_reading);
if(remove(fname) != 0) {
fclose(op);
otap_error(otap_error_unable_to_remove_file);
}
FILE* np = fopen(fname, "wb");
if(np == NULL) {
fclose(op);
otap_error(otap_error_unable_to_open_file_for_writing);
}
uint32_t dstart, dend;
if(fread(&dstart, 4, 1, stream) != 1)
otap_error(otap_error_unable_to_read_stream);
if(fread(&dend, 4, 1, stream) != 1)
otap_error(otap_error_unable_to_read_stream);
uintptr_t block;
uint8_t fbuff[256];
for(block = 256; dstart != 0; dstart -= block) {
if(dstart < block)
block = dstart;
if(fread(fbuff, 1, block, op) != block)
otap_error(otap_error_unable_to_read_stream);
if(fwrite(fbuff, 1, block, np) != block)
otap_error(otap_error_unable_to_write_stream);
}
uint32_t fsize;
if(fread(&fsize, 4, 1, stream) != 1)
otap_error(otap_error_unable_to_read_stream);
for(block = 256; fsize != 0; fsize -= block) {
if(fsize < block)
block = fsize;
if(fread(fbuff, 1, block, stream) != block)
otap_error(otap_error_unable_to_read_stream);
if(fwrite(fbuff, 1, block, np) != block)
otap_error(otap_error_unable_to_write_stream);
}
if(fseek(op, dend, SEEK_SET) != 0) {
fclose(np);
fclose(op);
otap_error(otap_error_unable_to_seek_through_stream);
}
for(block = 256; block != 0;) {
block = fread(fbuff, 1, block, op);
if(fwrite(fbuff, 1, block, np) != block)
otap_error(otap_error_unable_to_write_stream);
}
fclose(np);
fclose(op);
// Apply metadata.
struct utimbuf timebuff = { time(NULL), mtime };
utime(fname, &timebuff); // Don't care if it succeeds right now.
return 0;
}
static int __otap_apply_cmd_entity_delete(const char* name) {
DIR* dp = opendir(name);
if(dp == NULL) {
FILE* fp = fopen(name, "rb");
if(fp != NULL) {
fclose(fp);
if(remove(name) != 0)
otap_error(otap_error_unable_to_remove_file);
return 0;
}
otap_error(otap_error_file_does_not_exist);
}
if(chdir(name) != 0) {
closedir(dp);
otap_error(otap_error_unable_to_change_dir);
}
struct dirent* entry;
while((entry = readdir(dp)) != NULL) {
if((strcmp(entry->d_name, ".") == 0) || (strcmp(entry->d_name, "..") == 0))
continue;
int err;
if((err = __otap_apply_cmd_entity_delete(entry->d_name)) != 0) {
closedir(dp);
if (chdir("..") != 0)
otap_error(otap_error_unable_to_change_dir);
return err;
}
}
closedir(dp);
if (chdir("..") != 0)
otap_error(otap_error_unable_to_change_dir);
if(remove(name) != 0)
otap_error(otap_error_unable_to_remove_file);
return 0;
}
static int _otap_apply_cmd_entity_delete(FILE* stream) {
uint16_t elen;
if(fread(&elen, 2, 1, stream) != 1)
otap_error(otap_error_unable_to_read_stream);
char ename[elen + 1];
if(fread(ename, 1, elen, stream) != elen)
otap_error(otap_error_unable_to_read_stream);
ename[elen] = '\0';
printf("cmd_entity_delete %s\n", ename);
if((strchr(ename, '/') != NULL) || (strcmp(ename, "..") == 0))
otap_error(otap_error_invalid_parameter);
return __otap_apply_cmd_entity_delete(ename);
}
int otap_apply(FILE* stream) {
if(stream == NULL)
otap_error(otap_error_null_pointer);
int err;
if((err = _otap_apply_identify(stream)) != 0)
return err;
uintptr_t depth = 0;
bool flush = false;
while(!flush) {
uint8_t cmd;
if(fread(&cmd, 1, 1, stream) != 1)
otap_error(otap_error_unable_to_read_stream);
switch(cmd) {
case otap_cmd_dir_create:
if((err = _otap_apply_cmd_dir_create(stream)) != 0)
return err;
break;
case otap_cmd_dir_enter:
if((err = _otap_apply_cmd_dir_enter(stream, &depth)) != 0)
return err;
break;
case otap_cmd_dir_leave:
if((err = _otap_apply_cmd_dir_leave(stream, &depth)) != 0)
return err;
break;
case otap_cmd_file_create:
if((err = _otap_apply_cmd_file_create(stream)) != 0)
return err;
break;
case otap_cmd_file_delta:
if((err = _otap_apply_cmd_file_delta(stream)) != 0)
return err;
break;
case otap_cmd_entity_move:
case otap_cmd_entity_copy:
otap_error(otap_error_feature_not_implemented); // TODO - Implement.
case otap_cmd_entity_delete:
if((err = _otap_apply_cmd_entity_delete(stream)) != 0)
return err;
break;
case otap_cmd_update:
flush = true;
break;
default:
fprintf(stderr, "Error: Invalid command 0x%02"PRIx8".\n", cmd);
otap_error(otap_error_invalid_parameter);
}
}
return 0;
}
|