-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileops.c
416 lines (370 loc) · 6.42 KB
/
fileops.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
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/*\
|*| Parity Archive - A way to restore missing files in a set.
|*|
|*| Copyright (C) 2001 Willem Monsuwe ([email protected])
|*|
|*| File operations, in a separate file because these are probably
|*| going to cause the most portability problems.
\*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <errno.h>
#include "md5.h"
#include "fileops.h"
#include "util.h"
#include "par.h"
/*\
|*| Translate an ASCII string into unicode, write it onto the buffer
\*/
void
unistr(const char *str, u16 *buf)
{
do *buf++ = *str; while (*str++);
}
/*\
|*| Translate an ASCII string into unicode
|*| Returns an allocated string
\*/
u16 *
make_uni_str(const char *str)
{
u16 *uni;
NEW(uni, strlen(str) +1);
unistr(str, uni);
return uni;
}
/*\
|*| Translate a unicode string into ASCII
|*| Returns static string, which is overwritten at next call
\*/
char *
stuni(const u16 *str)
{
i64 i;
static char *buf = 0;
static i64 bufsize = 0;
/*\ Count the length \*/
for (i = 0; str[i]; i++)
;
if ((i + 1) > bufsize) {
bufsize = i + 1;
RENEW(buf, bufsize);
}
/*\ For now, just copy the low byte \*/
for (i = 0; str[i]; i++)
buf[i] = str[i];
buf[i] = 0;
return buf;
}
/*\
|*| Translate an ASCII string into unicode
|*| Returns static string, which is overwritten at next call
\*/
u16 *
unist(const char *str)
{
i64 i;
static u16 *buf = 0;
static i64 bufsize = 0;
/*\ Count the length \*/
for (i = 0; str[i]; i++)
;
if ((i + 1) > bufsize) {
bufsize = i + 1;
RENEW(buf, bufsize);
}
/*\ For now, just copy the low byte \*/
for (i = 0; str[i]; i++)
buf[i] = str[i];
buf[i] = 0;
return buf;
}
/*\
|*| Translate an md5 hash into ASCII
|*| Returns static string, which is overwritten at next call
\*/
char *
stmd5(const md5 hash)
{
int i;
static char buf[33];
for (i = 0; i < 16; i++) {
buf[i * 2] = HEXDIGIT((hash[i] >> 4) & 0xF);
buf[i * 2 + 1] = HEXDIGIT(hash[i] & 0xF);
}
buf[i * 2] = 0;
return buf;
}
i64
uni_copy(u16 *dst, u16 *src, i64 n)
{
i64 i;
for (i = 0; src[i] && (i < (n - 1)); i++)
dst[i] = src[i];
dst[i] = 0;
return i;
}
u16 *
unicode_copy(u16 *str)
{
u16 *ret, *p;
if (!str) {
NEW(ret, 1);
ret[0] = 0;
return ret;
}
for (p = str; *p; p++)
;
NEW(ret, (p - str) + 1);
COPY(ret, str, (p - str) + 1);
return ret;
}
int
file_rename(u16 *src, u16 *dst)
{
int i;
char *s, *d;
s = stuni(dst);
NEW(d, strlen(s) + 1);
strcpy(d, s);
s = stuni(src);
i = rename(s, d);
free(d);
return i;
}
static int
do_seek(file_t f)
{
if (f->off == f->s_off) return 0;
if (!f->f) return 0;
if (fseek(f->f, f->s_off, SEEK_SET) < 0)
return -1;
f->off = f->s_off;
return 0;
}
static int
do_close(file_t f)
{
int i;
if (!f->f) return 0;
i = fclose(f->f);
f->f = 0;
return i;
}
static int
do_open(file_t f)
{
static file_t openfiles = 0;
int i;
while (!f->f) {
/*\ This is so complicated to make sure we don't overwrite \*/
i = open(f->name, f->wr ? O_RDWR|O_CREAT|O_EXCL : O_RDONLY,
0666);
if (i >= 0) f->f = fdopen(i, f->wr ? "w+b" : "rb");
if (!f->f) {
if ((errno != EMFILE) && (errno != ENFILE))
return -1;
if (!openfiles)
return -1;
while (!openfiles->f)
openfiles = openfiles->next;
do_close(openfiles);
openfiles = openfiles->next;
} else {
f->off = 0;
if (!f->wr) {
f->next = openfiles;
openfiles = f;
}
}
}
return do_seek(f);
}
file_t
file_open_ascii(const char *path, int wr)
{
file_t f;
CNEW(f, 1);
NEW(f->name, strlen(path) + 1);
strcpy(f->name, path);
f->wr = wr;
return f;
}
file_t
file_open(const u16 *path, int wr)
{
return file_open_ascii(stuni(path), wr);
}
int
file_close(file_t f)
{
int i;
if (!f) return 0;
i = do_close(f);
free(f->name);
free(f);
return i;
}
int
file_exists(u16 *file)
{
FILE *f;
f = fopen(stuni(file), "rb");
if (!f) return (errno != ENOENT);
fclose(f);
return 1;
}
int
file_delete(u16 *file)
{
return remove(stuni(file));
}
int
file_seek(file_t f, i64 off)
{
f->s_off = off;
return do_seek(f);
}
char *
complete_path(char *path)
{
#if 0 /* def PATH_MAX */
static u8 buf[PATH_MAX];
if (realpath(path, buf))
return buf;
#endif
return path;
}
/*\ Read a directory.
|*| Returns a linked list of file entries.
\*/
hfile_t *
read_dir(char *dir)
{
DIR *d;
struct dirent *de;
hfile_t *rd = 0, **rdptr = &rd;
u16 *p;
int l, i;
char *dr, *dp, *ds;
dir = complete_path(dir);
l = 0;
for (i = 0; dir[i]; i++)
if (dir[i] == DIR_SEP)
l = i + 1;
NEW(dr, l + 1);
memcpy(dr, dir, l);
dr[l] = 0;
d = opendir(l ? dr : ".");
if (d) {
while ((de = readdir(d))) {
CNEW(*rdptr, 1);
NEW(p, l + strlen(de->d_name) + 1);
unistr(dr, p);
unistr(de->d_name, p + l);
(*rdptr)->filename = p;
rdptr = &((*rdptr)->next);
}
closedir(d);
}
free(dr);
return rd;
}
i64
file_read(file_t f, void *buf, i64 n)
{
i64 i;
if (!f) return 0;
if (do_open(f) < 0)
return 0;
i = fread(buf, 1, n, f->f);
if (i > 0) {
f->off += i;
f->s_off = f->off;
}
return i;
}
i64
file_write(file_t f, void *buf, i64 n)
{
i64 i;
if (!f) return 0;
if (do_open(f) < 0)
return 0;
i = fwrite(buf, 1, n, f->f);
if (i > 0) {
f->off += i;
f->s_off = f->off;
}
return i;
}
/*\ Calculate md5 sums on a file \*/
i64
file_md5(u16 *file, md5 block)
{
FILE *f;
i64 i;
f = fopen(stuni(file), "rb");
if (!f) return 0;
i = md5_stream(f, block);
fclose(f);
return i;
}
int
file_md5_buffer(u16 *file, md5 block, u8 *buf, i64 size)
{
file_t f;
i64 s;
f = file_open(file, 0);
if (!f) return 0;
s = file_read(f, buf, size);
file_close(f);
if (s < 0) return 0;
return (md5_buffer(buf, s, block) != 0);
}
/*\ Calculate the md5sum of a file from offset 'off',
|*| put it at offset 'md5off'
|*| Also, check the file size. Return 0 on failure.
\*/
int
file_add_md5(file_t f, i64 md5off, i64 off, i64 len)
{
md5 hash;
i64 i;
if (!f) return 0;
f->s_off = off;
if (do_open(f) < 0)
return 0;
i = md5_stream(f->f, hash);
if (i < 0)
return 0;
f->off += i;
f->s_off = f->off;
/*\ Filepointer should be at EOF now \*/
if (f->off != len)
return 0;
if (file_seek(f, md5off) < 0)
return 0;
if (file_write(f, hash, sizeof(hash)) < 0)
return 0;
return 1;
}
/*\ Get the md5sum over part of a file. \*/
int
file_get_md5(file_t f, i64 off, md5 block)
{
i64 i, tmp = f->off;
f->s_off = off;
if (do_open(f) < 0)
return 0;
i = md5_stream(f->f, block);
f->s_off = tmp;
return (i != 0);
}