/
fileio.c
724 lines (595 loc) · 20.2 KB
/
fileio.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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
/**
* MojoSetup; a portable, flexible installation application.
*
* Please see the file LICENSE.txt in the source's root directory.
*
* This file written by Ryan C. Gordon.
*/
#include "fileio.h"
#include "platform.h"
typedef MojoArchive* (*MojoArchiveCreateEntryPoint)(MojoInput *io);
MojoArchive *MojoArchive_createZIP(MojoInput *io);
MojoArchive *MojoArchive_createTAR(MojoInput *io);
MojoArchive *MojoArchive_createUZ2(MojoInput *io);
typedef struct
{
const char *ext;
MojoArchiveCreateEntryPoint create;
boolean hasMagic; // can determine file type from contents?
} MojoArchiveType;
static const MojoArchiveType archives[] =
{
{ "zip", MojoArchive_createZIP, true },
{ "tar", MojoArchive_createTAR, true },
{ "tar.gz", MojoArchive_createTAR, true },
{ "tar.bz2", MojoArchive_createTAR, true },
{ "tgz", MojoArchive_createTAR, true },
{ "tbz2", MojoArchive_createTAR, true },
{ "tb2", MojoArchive_createTAR, true },
{ "tbz", MojoArchive_createTAR, true },
{ "uz2", MojoArchive_createUZ2, false },
};
MojoArchive *MojoArchive_newFromInput(MojoInput *io, const char *origfname)
{
int i;
MojoArchive *retval = NULL;
const char *ext = NULL;
if (origfname != NULL)
{
ext = strrchr(origfname, '/');
if (ext == NULL)
ext = strchr(origfname, '.');
else
ext = strchr(ext+1, '.');
} // if
while (ext != NULL)
{
// Try for an exact match by filename extension.
ext++; // skip that '.'
for (i = 0; i < STATICARRAYLEN(archives); i++)
{
const MojoArchiveType *arc = &archives[i];
if (strcasecmp(ext, arc->ext) == 0)
return arc->create(io);
} // for
ext = strchr(ext, '.');
} // while
// Try any that could be determined without the file extension...
for (i = 0; i < STATICARRAYLEN(archives); i++)
{
const MojoArchiveType *arc = &archives[i];
if ((arc->hasMagic) && ((retval = arc->create(io)) != NULL))
return retval;
} // for
io->close(io);
return NULL; // nothing can handle this data.
} // MojoArchive_newFromInput
void MojoArchive_resetEntry(MojoArchiveEntry *info)
{
free(info->filename);
free(info->linkdest);
memset(info, '\0', sizeof (MojoArchiveEntry));
} // MojoArchive_resetEntry
// !!! FIXME: I'd rather not use a callback here, but I can't see a cleaner
// !!! FIXME: way right now...
boolean MojoInput_toPhysicalFile(MojoInput *in, const char *fname, uint16 perms,
MojoChecksums *checksums, int64 maxbytes,
MojoInput_FileCopyCallback cb, void *data)
{
boolean retval = false;
uint32 start = MojoPlatform_ticks();
void *out = NULL;
boolean iofailure = false;
int64 flen = 0;
int64 bw = 0;
MojoChecksumContext sumctx;
if (in == NULL)
return false;
if (checksums != NULL)
{
memset(checksums, '\0', sizeof (MojoChecksums));
MojoChecksum_init(&sumctx);
} // if
// Wait for a ready(), so length() can be meaningful on network streams.
while ((!in->ready(in)) && (!iofailure))
{
MojoPlatform_sleep(100);
if (cb != NULL)
{
if (!cb(MojoPlatform_ticks() - start, 0, 0, -1, data))
iofailure = true;
} // if
} // while
flen = in->length(in);
if ((maxbytes >= 0) && (flen > maxbytes))
flen = maxbytes;
MojoPlatform_unlink(fname);
if (!iofailure)
{
const uint32 flags = MOJOFILE_WRITE|MOJOFILE_CREATE|MOJOFILE_TRUNCATE;
const uint16 mode = MojoPlatform_defaultFilePerms();
out = MojoPlatform_open(fname, flags, mode);
} // if
if (out != NULL)
{
while (!iofailure)
{
int64 br = 0;
int64 maxread = sizeof (scratchbuf_128k);
// see if we need to clamp to eof or maxbytes...
if (flen >= 0)
{
const int64 avail = flen - bw;
if (avail < maxread)
{
maxread = avail;
if (maxread == 0)
break; // nothing left to do, break out.
} // if
} // if
// If there's a callback, then poll. Otherwise, just block on
// the reads from the MojoInput.
if ((cb != NULL) && (!in->ready(in)))
MojoPlatform_sleep(100);
else
{
br = in->read(in, scratchbuf_128k, (uint32) maxread);
if (br == 0) // we're done!
break;
else if (br < 0)
iofailure = true;
else
{
if (MojoPlatform_write(out, scratchbuf_128k, (uint32) br) != br)
iofailure = true;
else
{
if (checksums != NULL)
MojoChecksum_append(&sumctx, scratchbuf_128k, (uint32) br);
bw += br;
} // else
} // else
} // else
if (cb != NULL)
{
if (!cb(MojoPlatform_ticks() - start, br, bw, flen, data))
iofailure = true;
} // if
} // while
if (MojoPlatform_close(out) != 0)
iofailure = true;
else if (bw != flen)
iofailure = true;
if (iofailure)
MojoPlatform_unlink(fname);
else
{
MojoPlatform_chmod(fname, perms);
if (checksums != NULL)
MojoChecksum_finish(&sumctx, checksums);
retval = true;
} // else
} // if
in->close(in);
return retval;
} // MojoInput_toPhysicalFile
MojoInput *MojoInput_newFromArchivePath(MojoArchive *ar, const char *fname)
{
MojoInput *retval = NULL;
if (ar->enumerate(ar))
{
const MojoArchiveEntry *entinfo;
while ((entinfo = ar->enumNext(ar)) != NULL)
{
if (strcmp(entinfo->filename, fname) == 0)
{
if (entinfo->type == MOJOARCHIVE_ENTRY_FILE)
retval = ar->openCurrentEntry(ar);
break;
} // if
} // while
} // if
return retval;
} // MojoInput_newFromArchivePath
// MojoInputs from files on the OS filesystem.
typedef struct
{
void *handle;
char *path;
} MojoInputFileInstance;
static boolean MojoInput_file_ready(MojoInput *io)
{
// !!! FIXME: select()? Does that help with network filesystems?
return true;
} // MojoInput_file_ready
static int64 MojoInput_file_read(MojoInput *io, void *buf, uint32 bufsize)
{
MojoInputFileInstance *inst = (MojoInputFileInstance *) io->opaque;
return MojoPlatform_read(inst->handle, buf, bufsize);
} // MojoInput_file_read
static boolean MojoInput_file_seek(MojoInput *io, uint64 pos)
{
MojoInputFileInstance *inst = (MojoInputFileInstance *) io->opaque;
return (MojoPlatform_seek(inst->handle, pos, MOJOSEEK_SET) == pos);
} // MojoInput_file_seek
static int64 MojoInput_file_tell(MojoInput *io)
{
MojoInputFileInstance *inst = (MojoInputFileInstance *) io->opaque;
return MojoPlatform_tell(inst->handle);
} // MojoInput_file_tell
static int64 MojoInput_file_length(MojoInput *io)
{
MojoInputFileInstance *inst = (MojoInputFileInstance *) io->opaque;
return MojoPlatform_flen(inst->handle);
} // MojoInput_file_length
static MojoInput *MojoInput_file_duplicate(MojoInput *io)
{
MojoInputFileInstance *inst = (MojoInputFileInstance *) io->opaque;
return MojoInput_newFromFile(inst->path);
} // MojoInput_file_duplicate
static void MojoInput_file_close(MojoInput *io)
{
MojoInputFileInstance *inst = (MojoInputFileInstance *) io->opaque;
MojoPlatform_close(inst->handle);
free(inst->path);
free(inst);
free(io);
} // MojoInput_file_close
MojoInput *MojoInput_newFromFile(const char *path)
{
MojoInput *io = NULL;
void *f = NULL;
f = MojoPlatform_open(path, MOJOFILE_READ, 0);
if (f != NULL)
{
MojoInputFileInstance *inst;
inst = (MojoInputFileInstance *) xmalloc(sizeof (MojoInputFileInstance));
inst->path = xstrdup(path);
inst->handle = f;
io = (MojoInput *) xmalloc(sizeof (MojoInput));
io->ready = MojoInput_file_ready;
io->read = MojoInput_file_read;
io->seek = MojoInput_file_seek;
io->tell = MojoInput_file_tell;
io->length = MojoInput_file_length;
io->duplicate = MojoInput_file_duplicate;
io->close = MojoInput_file_close;
io->opaque = inst;
} // if
return io;
} // MojoInput_newFromFile
// MojoInputs from blocks of memory.
typedef struct
{
void *ptr; // original pointer from xmalloc()
uint32 *refcount; // address in xmalloc()'d block for reference count.
const uint8 *data; // base of actual "file" data in xmalloc()'d block.
uint32 len; // size, in bytes, of "file" data.
uint32 pos; // current read position.
} MojoInputMemInstance;
static boolean MojoInput_memory_ready(MojoInput *io)
{
return true; // always ready!
} // MojoInput_memory_ready
static int64 MojoInput_memory_read(MojoInput *io, void *buf, uint32 bufsize)
{
MojoInputMemInstance *inst = (MojoInputMemInstance *) io->opaque;
const uint32 avail = inst->len - inst->pos;
if (bufsize > avail)
bufsize = avail;
memcpy(buf, inst->data + inst->pos, bufsize);
inst->pos += bufsize;
return bufsize;
} // MojoInput_memory_read
static boolean MojoInput_memory_seek(MojoInput *io, uint64 pos)
{
MojoInputMemInstance *inst = (MojoInputMemInstance *) io->opaque;
if (pos > (uint64) inst->len)
return false;
inst->pos = (uint32) pos;
return true;
} // MojoInput_memory_seek
static int64 MojoInput_memory_tell(MojoInput *io)
{
MojoInputMemInstance *inst = (MojoInputMemInstance *) io->opaque;
return (int64) inst->pos;
} // MojoInput_memory_tell
static int64 MojoInput_memory_length(MojoInput *io)
{
MojoInputMemInstance *inst = (MojoInputMemInstance *) io->opaque;
return (int64) inst->len;
} // MojoInput_memory_length
static MojoInput *MojoInput_memory_duplicate(MojoInput *io)
{
MojoInputMemInstance *srcinst = (MojoInputMemInstance *) io->opaque;
MojoInput *retval = NULL;
MojoInputMemInstance *inst = NULL;
if (srcinst->refcount != NULL)
{
// we don't copy the data for each duplicate; we just bump a reference
// counter. We free the data when all referencers are closed.
(*srcinst->refcount)++; // !!! FIXME: not thread safe!
} // if
inst = (MojoInputMemInstance*) xmalloc(sizeof (MojoInputMemInstance));
memcpy(inst, srcinst, sizeof (MojoInputMemInstance));
inst->pos = 0;
retval = (MojoInput *) xmalloc(sizeof (MojoInput));
memcpy(retval, io, sizeof (MojoInput));
retval->opaque = inst;
return retval;
} // MojoInput_memory_duplicate
static void MojoInput_memory_close(MojoInput *io)
{
MojoInputMemInstance *inst = (MojoInputMemInstance *) io->opaque;
if (inst->refcount != NULL) // memory we have to free?
{
assert(*inst->refcount > 0);
if (--(*inst->refcount) == 0) // !!! FIXME: not thread safe!
free(inst->ptr);
} // if
free(inst);
free(io);
} // MojoInput_memory_close
MojoInput *MojoInput_newFromMemory(const uint8 *ptr, uint32 len, int constant)
{
MojoInput *io = (MojoInput *) xmalloc(sizeof (MojoInput));
MojoInputMemInstance *inst = (MojoInputMemInstance*)
xmalloc(sizeof (MojoInputMemInstance));
if (constant)
inst->data = ptr;
else
{
inst->ptr = xmalloc(len + sizeof (uint32));
inst->refcount = (uint32 *) inst->ptr;
inst->data = ((const uint8 *) inst->ptr) + sizeof (uint32);
*inst->refcount = 1;
memcpy((void *) inst->data, ptr, len);
} // else
inst->len = len;
io->ready = MojoInput_memory_ready;
io->read = MojoInput_memory_read;
io->seek = MojoInput_memory_seek;
io->tell = MojoInput_memory_tell;
io->length = MojoInput_memory_length;
io->duplicate = MojoInput_memory_duplicate;
io->close = MojoInput_memory_close;
io->opaque = inst;
return io;
} // MojoInput_newFromMemory
// MojoArchives from directories on the OS filesystem.
typedef struct DirStack
{
void *dir;
char *basepath;
struct DirStack *next;
} DirStack;
static void pushDirStack(DirStack **_stack, const char *basepath, void *dir)
{
DirStack *stack = (DirStack *) xmalloc(sizeof (DirStack));
stack->dir = dir;
stack->basepath = xstrdup(basepath);
stack->next = *_stack;
*_stack = stack;
} // pushDirStack
static void popDirStack(DirStack **_stack)
{
DirStack *stack = *_stack;
if (stack != NULL)
{
DirStack *next = stack->next;
if (stack->dir)
MojoPlatform_closedir(stack->dir);
free(stack->basepath);
free(stack);
*_stack = next;
} // if
} // popDirStack
static void freeDirStack(DirStack **_stack)
{
while (*_stack)
popDirStack(_stack);
} // freeDirStack
typedef struct
{
DirStack *dirs;
char *base;
} MojoArchiveDirInstance;
static boolean MojoArchive_dir_enumerate(MojoArchive *ar)
{
MojoArchiveDirInstance *inst = (MojoArchiveDirInstance *) ar->opaque;
void *dir = NULL;
freeDirStack(&inst->dirs);
MojoArchive_resetEntry(&ar->prevEnum);
dir = MojoPlatform_opendir(inst->base);
if (dir != NULL)
pushDirStack(&inst->dirs, inst->base, dir);
return (dir != NULL);
} // MojoArchive_dir_enumerate
static const MojoArchiveEntry *MojoArchive_dir_enumNext(MojoArchive *ar)
{
uint16 perms = 0644; //(S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
char *fullpath = NULL;
char *dent = NULL; // "dent" == "directory entry"
MojoArchiveDirInstance *inst = (MojoArchiveDirInstance *) ar->opaque;
const char *basepath;
MojoArchive_resetEntry(&ar->prevEnum);
if (inst->dirs == NULL)
return NULL;
basepath = inst->dirs->basepath;
// if readdir fails, it's end of dir (!!! FIXME: what about i/o failures?)
dent = MojoPlatform_readdir(inst->dirs->dir);
if (dent == NULL) // end of dir?
{
popDirStack(&inst->dirs);
return MojoArchive_dir_enumNext(ar); // try higher level in tree.
} // if
// MojoPlatform layer shouldn't return "." or ".." paths.
assert((strcmp(dent, ".") != 0) && (strcmp(dent, "..") != 0));
fullpath = (char *) xmalloc(strlen(basepath) + strlen(dent) + 2);
sprintf(fullpath, "%s/%s", basepath, dent);
free(dent);
ar->prevEnum.filename = xstrdup(fullpath + strlen(inst->base) + 1);
ar->prevEnum.filesize = 0;
ar->prevEnum.type = MOJOARCHIVE_ENTRY_UNKNOWN;
// We currently force the perms from physical files, since CDs on
// Linux tend to mark every files as executable and read-only. If you
// want to install something with specific permissions, wrap it in a
// tarball, or use Setup.File.permissions, or return a permissions
// string from Setup.File.filter.
//MojoPlatform_perms(fullpath, &perms);
ar->prevEnum.perms = perms;
if (MojoPlatform_isfile(fullpath))
{
ar->prevEnum.type = MOJOARCHIVE_ENTRY_FILE;
ar->prevEnum.filesize = MojoPlatform_filesize(fullpath);
} // if
else if (MojoPlatform_issymlink(fullpath))
{
ar->prevEnum.type = MOJOARCHIVE_ENTRY_SYMLINK;
ar->prevEnum.linkdest = MojoPlatform_readlink(fullpath);
if (ar->prevEnum.linkdest == NULL)
{
free(fullpath);
return MojoArchive_dir_enumNext(ar);
} // if
} // else if
else if (MojoPlatform_isdir(fullpath))
{
void *dir = MojoPlatform_opendir(fullpath);
ar->prevEnum.type = MOJOARCHIVE_ENTRY_DIR;
if (dir == NULL)
{
free(fullpath);
return MojoArchive_dir_enumNext(ar);
} // if
// push this dir on the stack. Next enum will start there.
pushDirStack(&inst->dirs, fullpath, dir);
} // else if
else
{
assert(false && "possible file i/o error?");
} // else
free(fullpath);
return &ar->prevEnum;
} // MojoArchive_dir_enumNext
static MojoInput *MojoArchive_dir_openCurrentEntry(MojoArchive *ar)
{
MojoInput *retval = NULL;
MojoArchiveDirInstance *inst = (MojoArchiveDirInstance *) ar->opaque;
if ((inst->dirs != NULL) && (ar->prevEnum.type == MOJOARCHIVE_ENTRY_FILE))
{
char *fullpath = (char *) xmalloc(strlen(inst->base) +
strlen(ar->prevEnum.filename) + 2);
sprintf(fullpath, "%s/%s", inst->base, ar->prevEnum.filename);
retval = MojoInput_newFromFile(fullpath);
free(fullpath);
} // if
return retval;
} // MojoArchive_dir_openCurrentEntry
static void MojoArchive_dir_close(MojoArchive *ar)
{
MojoArchiveDirInstance *inst = (MojoArchiveDirInstance *) ar->opaque;
freeDirStack(&inst->dirs);
free(inst->base);
free(inst);
MojoArchive_resetEntry(&ar->prevEnum);
free(ar);
} // MojoArchive_dir_close
MojoArchive *MojoArchive_newFromDirectory(const char *dirname)
{
MojoArchive *ar = NULL;
MojoArchiveDirInstance *inst;
char *real = MojoPlatform_realpath(dirname);
if (real == NULL)
return NULL;
if (!MojoPlatform_exists(real, NULL))
return NULL;
if (!MojoPlatform_isdir(real))
return NULL;
inst = (MojoArchiveDirInstance *) xmalloc(sizeof (MojoArchiveDirInstance));
inst->base = real;
ar = (MojoArchive *) xmalloc(sizeof (MojoArchive));
ar->enumerate = MojoArchive_dir_enumerate;
ar->enumNext = MojoArchive_dir_enumNext;
ar->openCurrentEntry = MojoArchive_dir_openCurrentEntry;
ar->close = MojoArchive_dir_close;
ar->offsetOfStart = -1; // doesn't mean anything here.
ar->opaque = inst;
return ar;
} // MojoArchive_newFromDirectory
MojoArchive *GBaseArchive = NULL;
const char *GBaseArchivePath = NULL;
MojoArchive *MojoArchive_initBaseArchive(void)
{
char *basepath = NULL;
const char *cmd = NULL;
MojoInput *io = NULL;
if (GBaseArchive != NULL)
return GBaseArchive; // already initialized.
if ((cmd = cmdlinestr("base", "MOJOSETUP_BASE", NULL)) != NULL)
{
char *real = MojoPlatform_realpath(cmd);
if (real != NULL)
{
if (MojoPlatform_isdir(real))
GBaseArchive = MojoArchive_newFromDirectory(real);
else
{
io = MojoInput_newFromFile(real);
if (io != NULL)
GBaseArchive = MojoArchive_newFromInput(io, real);
} // else
if (GBaseArchive != NULL)
basepath = real;
else
free(real);
} // if
} // else if
else
{
basepath = MojoPlatform_appBinaryPath();
io = MojoInput_newFromFile(basepath);
if (io != NULL)
GBaseArchive = MojoArchive_newFromInput(io, basepath);
if (GBaseArchive == NULL)
{
// Just use the same directory as the binary instead.
char *ptr = strrchr(basepath, '/');
if (ptr != NULL)
*ptr = '\0';
else
{
free(basepath); // oh well, try cwd.
basepath = MojoPlatform_currentWorkingDir();
} // else
GBaseArchive = MojoArchive_newFromDirectory(basepath);
// !!! FIXME: failing this, maybe default.mojosetup?
} // if
} // else
if (GBaseArchive == NULL)
{
free(basepath);
basepath = NULL;
} // if
GBaseArchivePath = basepath;
return GBaseArchive;
} // MojoArchive_initBaseArchive
void MojoArchive_deinitBaseArchive(void)
{
if (GBaseArchive != NULL)
{
GBaseArchive->close(GBaseArchive);
GBaseArchive = NULL;
} // if
free((void *) GBaseArchivePath);
GBaseArchivePath = NULL;
} // MojoArchive_deinitBaseArchive
// This stub is here if we didn't compile in libfetch...
#if !SUPPORT_URL_HTTP && !SUPPORT_URL_FTP
MojoInput *MojoInput_newFromURL(const char *url)
{
logError("No networking support in this build.");
return NULL;
} // MojoInput_newFromURL
#endif
// end of fileio.c ...