/
fileio.c
1381 lines (1132 loc) · 37.7 KB
1
2
3
4
5
6
7
8
/**
* 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.
*/
9
#include "fileio.h"
10
#include "platform.h"
11
12
13
14
typedef MojoArchive* (*MojoArchiveCreateEntryPoint)(MojoInput *io);
MojoArchive *MojoArchive_createZIP(MojoInput *io);
15
MojoArchive *MojoArchive_createTAR(MojoInput *io);
16
MojoArchive *MojoArchive_createUZ2(MojoInput *io);
17
MojoArchive *MojoArchive_createPCK(MojoInput *io);
18
19
20
21
22
typedef struct
{
const char *ext;
MojoArchiveCreateEntryPoint create;
23
boolean hasMagic; // can determine file type from contents?
24
25
} MojoArchiveType;
26
27
// !!! FIXME: order by archiver, not extension, since most of this is
// !!! FIXME: duplicates for .tar
28
29
static const MojoArchiveType archives[] =
{
30
31
32
{ "zip", MojoArchive_createZIP, true },
{ "tar", MojoArchive_createTAR, true },
{ "tar.gz", MojoArchive_createTAR, true },
33
{ "tar.xz", MojoArchive_createTAR, true },
34
35
36
37
38
{ "tar.bz2", MojoArchive_createTAR, true },
{ "tgz", MojoArchive_createTAR, true },
{ "tbz2", MojoArchive_createTAR, true },
{ "tb2", MojoArchive_createTAR, true },
{ "tbz", MojoArchive_createTAR, true },
39
{ "txz", MojoArchive_createTAR, true },
40
{ "uz2", MojoArchive_createUZ2, false },
41
{ "pck", MojoArchive_createPCK, true },
42
43
};
44
45
46
#if SUPPORT_GZIP
47
#include "zlib/zlib.h"
48
49
50
51
52
53
54
55
56
#define GZIP_READBUFSIZE (128 * 1024)
static MojoInput *make_gzip_input(MojoInput *origio);
typedef struct GZIPinfo
{
MojoInput *origio;
uint64 uncompressed_position;
57
uint8 buffer[GZIP_READBUFSIZE];
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
z_stream stream;
} GZIPinfo;
static voidpf mojoZlibAlloc(voidpf opaque, uInt items, uInt size)
{
return xmalloc(items * size);
} // mojoZlibAlloc
static void mojoZlibFree(voidpf opaque, voidpf address)
{
free(address);
} // mojoZlibFree
static void initializeZStream(z_stream *pstr)
{
memset(pstr, '\0', sizeof (z_stream));
pstr->zalloc = mojoZlibAlloc;
pstr->zfree = mojoZlibFree;
} // initializeZStream
static boolean MojoInput_gzip_ready(MojoInput *io)
{
return true; // !!! FIXME: ready if there are bytes uncompressed.
} // MojoInput_gzip_ready
static boolean MojoInput_gzip_seek(MojoInput *io, uint64 offset)
{
// This is all really expensive.
GZIPinfo *info = (GZIPinfo *) io->opaque;
/*
* If seeking backwards, we need to redecode the file
* from the start and throw away the compressed bits until we hit
* the offset we need. If seeking forward, we still need to
* decode, but we don't rewind first.
*/
if (offset < info->uncompressed_position)
{
if (!info->origio->seek(info->origio, 0))
return false;
inflateEnd(&info->stream);
initializeZStream(&info->stream);
if (inflateInit2(&info->stream, 31) != Z_OK)
return false;
info->uncompressed_position = 0;
} // if
while (info->uncompressed_position != offset)
{
uint8 buf[512];
uint32 maxread;
int64 br;
maxread = (uint32) (offset - info->uncompressed_position);
if (maxread > sizeof (buf))
maxread = sizeof (buf);
br = io->read(io, buf, maxread);
if (br != maxread)
return false;
} /* while */
return true;
} // MojoInput_gzip_seek
static int64 MojoInput_gzip_tell(MojoInput *io)
{
return (((GZIPinfo *) io->opaque)->uncompressed_position);
} // MojoInput_gzip_tell
static int64 MojoInput_gzip_length(MojoInput *io)
{
return -1;
} // MojoInput_gzip_length
static int64 MojoInput_gzip_read(MojoInput *io, void *buf, uint32 bufsize)
{
GZIPinfo *info = (GZIPinfo *) io->opaque;
MojoInput *origio = info->origio;
int64 retval = 0;
if (bufsize == 0)
return 0; // quick rejection.
141
142
143
144
145
info->stream.next_out = buf;
info->stream.avail_out = bufsize;
while (retval < ((int64) bufsize))
146
{
147
148
const uint32 before = info->stream.total_out;
int rc;
149
150
if (info->stream.avail_in == 0)
151
{
152
153
int64 br = origio->length(origio) - origio->tell(origio);
if (br > 0)
154
{
155
156
if (br > GZIP_READBUFSIZE)
br = GZIP_READBUFSIZE;
157
158
159
160
br = origio->read(origio, info->buffer, (uint32) br);
if (br <= 0)
return -1;
161
162
163
info->stream.next_in = info->buffer;
info->stream.avail_in = (uint32) br;
164
} // if
165
} // if
166
167
168
rc = inflate(&info->stream, Z_SYNC_FLUSH);
retval += (info->stream.total_out - before);
169
170
171
172
if (rc != Z_OK) // !!! FIXME: Z_STREAM_END?
return -1;
} // while
173
174
175
assert(retval >= 0);
info->uncompressed_position += (uint32) retval;
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
return retval;
} // MojoInput_gzip_read
static MojoInput* MojoInput_gzip_duplicate(MojoInput *io)
{
GZIPinfo *info = (GZIPinfo *) io->opaque;
MojoInput *retval = NULL;
MojoInput *newio = info->origio->duplicate(info->origio);
if (newio != NULL)
{
retval = make_gzip_input(newio);
if (retval != NULL)
retval->seek(retval, io->tell(io)); // slow, slow, slow...
} // if
return retval;
} // MojoInput_gzip_duplicate
static void MojoInput_gzip_close(MojoInput *io)
{
GZIPinfo *info = (GZIPinfo *) io->opaque;
if (info->origio != NULL)
info->origio->close(info->origio);
inflateEnd(&info->stream);
free(info);
free(io);
} // MojoInput_gzip_close
static MojoInput *make_gzip_input(MojoInput *origio)
{
MojoInput *io = NULL;
GZIPinfo *info = (GZIPinfo *) xmalloc(sizeof (GZIPinfo));
initializeZStream(&info->stream);
if (inflateInit2(&info->stream, 31) != Z_OK)
{
free(info);
return NULL;
} // if
info->origio = origio;
io = (MojoInput *) xmalloc(sizeof (MojoInput));
io->ready = MojoInput_gzip_ready;
io->read = MojoInput_gzip_read;
io->seek = MojoInput_gzip_seek;
io->tell = MojoInput_gzip_tell;
io->length = MojoInput_gzip_length;
io->duplicate = MojoInput_gzip_duplicate;
io->close = MojoInput_gzip_close;
io->opaque = info;
return io;
} // make_gzip_input
#endif // SUPPORT_GZIP
#if SUPPORT_BZIP2
235
#include "bzip2/bzlib.h"
236
237
238
239
240
241
242
243
244
#define BZIP2_READBUFSIZE (128 * 1024)
static MojoInput *make_bzip2_input(MojoInput *origio);
typedef struct BZIP2info
{
MojoInput *origio;
uint64 uncompressed_position;
245
uint8 buffer[BZIP2_READBUFSIZE];
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
bz_stream stream;
} BZIP2info;
static void *mojoBzlib2Alloc(void *opaque, int items, int size)
{
return xmalloc(items * size);
} // mojoBzlib2Alloc
static void mojoBzlib2Free(void *opaque, void *address)
{
free(address);
} // mojoBzlib2Free
static void initializeBZ2Stream(bz_stream *pstr)
{
memset(pstr, '\0', sizeof (bz_stream));
pstr->bzalloc = mojoBzlib2Alloc;
pstr->bzfree = mojoBzlib2Free;
} // initializeBZ2Stream
static boolean MojoInput_bzip2_ready(MojoInput *io)
{
return true; // !!! FIXME: ready if there are bytes uncompressed.
} // MojoInput_bzip2_ready
static boolean MojoInput_bzip2_seek(MojoInput *io, uint64 offset)
{
// This is all really expensive.
BZIP2info *info = (BZIP2info *) io->opaque;
/*
* If seeking backwards, we need to redecode the file
* from the start and throw away the compressed bits until we hit
* the offset we need. If seeking forward, we still need to
* decode, but we don't rewind first.
*/
if (offset < info->uncompressed_position)
{
#if 0
/* we do a copy so state is sane if inflateInit2() fails. */
bz_stream str;
initializeBZ2Stream(&str);
if (BZ2_bzDecompressInit(&str, 0, 0) != BZ_OK)
return false;
if (!info->origio->seek(info->origio, 0))
return false; // !!! FIXME: leaking (str)?
BZ2_bzDecompressEnd(&info->stream);
memcpy(&info->stream, &str, sizeof (bz_stream));
#endif
if (!info->origio->seek(info->origio, 0))
return false;
BZ2_bzDecompressEnd(&info->stream);
initializeBZ2Stream(&info->stream);
if (BZ2_bzDecompressInit(&info->stream, 0, 0) != BZ_OK)
return false;
info->uncompressed_position = 0;
} // if
while (info->uncompressed_position != offset)
{
uint8 buf[512];
uint32 maxread;
int64 br;
maxread = (uint32) (offset - info->uncompressed_position);
if (maxread > sizeof (buf))
maxread = sizeof (buf);
br = io->read(io, buf, maxread);
if (br != maxread)
return false;
} /* while */
return true;
} // MojoInput_bzip2_seek
static int64 MojoInput_bzip2_tell(MojoInput *io)
{
return (((BZIP2info *) io->opaque)->uncompressed_position);
} // MojoInput_bzip2_tell
static int64 MojoInput_bzip2_length(MojoInput *io)
{
return -1;
} // MojoInput_bzip2_length
static int64 MojoInput_bzip2_read(MojoInput *io, void *buf, uint32 bufsize)
{
BZIP2info *info = (BZIP2info *) io->opaque;
MojoInput *origio = info->origio;
int64 retval = 0;
if (bufsize == 0)
return 0; // quick rejection.
343
344
345
346
347
info->stream.next_out = buf;
info->stream.avail_out = bufsize;
while (retval < ((int64) bufsize))
348
{
349
350
const uint32 before = info->stream.total_out_lo32;
int rc;
351
352
if (info->stream.avail_in == 0)
353
{
354
355
int64 br = origio->length(origio) - origio->tell(origio);
if (br > 0)
356
{
357
358
if (br > BZIP2_READBUFSIZE)
br = BZIP2_READBUFSIZE;
359
360
361
362
br = origio->read(origio, info->buffer, (uint32) br);
if (br <= 0)
return -1;
363
364
365
info->stream.next_in = (char *) info->buffer;
info->stream.avail_in = (uint32) br;
366
} // if
367
} // if
368
369
370
371
372
373
rc = BZ2_bzDecompress(&info->stream);
retval += (info->stream.total_out_lo32 - before);
if (rc != BZ_OK)
return -1;
} // while
374
375
376
assert(retval >= 0);
info->uncompressed_position += (uint32) retval;
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
return retval;
} // MojoInput_bzip2_read
static MojoInput* MojoInput_bzip2_duplicate(MojoInput *io)
{
BZIP2info *info = (BZIP2info *) io->opaque;
MojoInput *retval = NULL;
MojoInput *newio = info->origio->duplicate(info->origio);
if (newio != NULL)
{
retval = make_bzip2_input(newio);
if (retval != NULL)
retval->seek(retval, io->tell(io)); // slow, slow, slow...
} // if
return retval;
} // MojoInput_bzip2_duplicate
static void MojoInput_bzip2_close(MojoInput *io)
{
BZIP2info *info = (BZIP2info *) io->opaque;
if (info->origio != NULL)
info->origio->close(info->origio);
BZ2_bzDecompressEnd(&info->stream);
free(info);
free(io);
} // MojoInput_bzip2_close
static MojoInput *make_bzip2_input(MojoInput *origio)
{
MojoInput *io = NULL;
BZIP2info *info = (BZIP2info *) xmalloc(sizeof (BZIP2info));
initializeBZ2Stream(&info->stream);
if (BZ2_bzDecompressInit(&info->stream, 0, 0) != BZ_OK)
{
free(info);
return NULL;
} // if
info->origio = origio;
io = (MojoInput *) xmalloc(sizeof (MojoInput));
io->ready = MojoInput_bzip2_ready;
io->read = MojoInput_bzip2_read;
io->seek = MojoInput_bzip2_seek;
io->tell = MojoInput_bzip2_tell;
io->length = MojoInput_bzip2_length;
io->duplicate = MojoInput_bzip2_duplicate;
io->close = MojoInput_bzip2_close;
io->opaque = info;
return io;
} // make_bzip2_input
#endif // SUPPORT_BZIP2
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
#if SUPPORT_XZ
#include "lzma.h"
#define XZ_READBUFSIZE (128 * 1024)
static MojoInput *make_xz_input(MojoInput *origio);
typedef struct XZinfo
{
MojoInput *origio;
uint64 uncompressed_position;
uint8 buffer[XZ_READBUFSIZE];
lzma_stream stream;
} XZinfo;
static void *mojoLzmaAlloc(void *opaque, size_t items, size_t size)
{
return xmalloc(items * size);
} // mojoLzmaAlloc
static void mojoLzmaFree(void *opaque, void *address)
{
free(address);
} // mojoZlibFree
static void initializeXZStream(lzma_stream *pstr)
{
static lzma_allocator lzmaAlloc = { mojoLzmaAlloc, mojoLzmaFree };
memset(pstr, '\0', sizeof (lzma_stream));
pstr->allocator = &lzmaAlloc;
} // initializeXZStream
static boolean MojoInput_xz_ready(MojoInput *io)
{
return true; // !!! FIXME: ready if there are bytes uncompressed.
} // MojoInput_xz_ready
static boolean MojoInput_xz_seek(MojoInput *io, uint64 offset)
{
// This is all really expensive.
XZinfo *info = (XZinfo *) io->opaque;
/*
* If seeking backwards, we need to redecode the file
* from the start and throw away the compressed bits until we hit
* the offset we need. If seeking forward, we still need to
* decode, but we don't rewind first.
*/
if (offset < info->uncompressed_position)
{
lzma_stream *strm = &info->stream;
if (!info->origio->seek(info->origio, 0))
return false;
lzma_end(strm);
initializeXZStream(strm);
if (lzma_stream_decoder(strm, UINT64_MAX, LZMA_CONCATENATED) != LZMA_OK)
return false;
info->uncompressed_position = 0;
} // if
while (info->uncompressed_position != offset)
{
uint8 buf[512];
uint32 maxread;
int64 br;
maxread = (uint32) (offset - info->uncompressed_position);
if (maxread > sizeof (buf))
maxread = sizeof (buf);
br = io->read(io, buf, maxread);
if (br != maxread)
return false;
} /* while */
return true;
} // MojoInput_xz_seek
static int64 MojoInput_xz_tell(MojoInput *io)
{
return (((XZinfo *) io->opaque)->uncompressed_position);
} // MojoInput_xz_tell
static int64 MojoInput_xz_length(MojoInput *io)
{
return -1;
} // MojoInput_xz_length
static int64 MojoInput_xz_read(MojoInput *io, void *buf, uint32 bufsize)
{
XZinfo *info = (XZinfo *) io->opaque;
MojoInput *origio = info->origio;
int64 retval = 0;
if (bufsize == 0)
return 0; // quick rejection.
info->stream.next_out = buf;
info->stream.avail_out = bufsize;
while (retval < ((int64) bufsize))
{
const uint32 before = info->stream.total_out;
lzma_ret rc;
lzma_action action = LZMA_FINISH;
if (info->stream.avail_in == 0)
{
int64 br = origio->length(origio) - origio->tell(origio);
if (br > 0)
{
action = LZMA_RUN;
if (br > XZ_READBUFSIZE)
br = XZ_READBUFSIZE;
br = origio->read(origio, info->buffer, (uint32) br);
if (br <= 0)
return -1;
info->stream.next_in = info->buffer;
info->stream.avail_in = (uint32) br;
} // if
} // if
rc = lzma_code(&info->stream, LZMA_RUN);
retval += (info->stream.total_out - before);
if (rc != LZMA_OK)
return -1;
} // while
assert(retval >= 0);
info->uncompressed_position += (uint32) retval;
return retval;
} // MojoInput_xz_read
static MojoInput* MojoInput_xz_duplicate(MojoInput *io)
{
XZinfo *info = (XZinfo *) io->opaque;
MojoInput *retval = NULL;
MojoInput *newio = info->origio->duplicate(info->origio);
if (newio != NULL)
{
retval = make_xz_input(newio);
if (retval != NULL)
retval->seek(retval, io->tell(io)); // slow, slow, slow...
} // if
return retval;
} // MojoInput_xz_duplicate
static void MojoInput_xz_close(MojoInput *io)
{
XZinfo *info = (XZinfo *) io->opaque;
if (info->origio != NULL)
info->origio->close(info->origio);
lzma_end(&info->stream);
free(info);
free(io);
} // MojoInput_xz_close
static MojoInput *make_xz_input(MojoInput *origio)
{
MojoInput *io = NULL;
XZinfo *info = (XZinfo *) xmalloc(sizeof (XZinfo));
lzma_stream *strm = &info->stream;
// UINT64_MAX is the memory usage limit (so basically, no artificial
// limit here). Internally, this holds its entire dictionary in
// RAM (8 megabytes by default), plus a few other structures, so we
// shouldn't eat tons of memory in the normal case. Note that the
// dictionary can max out at _four gigabytes_, but obviously no one does
// that.
initializeXZStream(strm);
if (lzma_stream_decoder(strm, UINT64_MAX, LZMA_CONCATENATED) != LZMA_OK)
{
free(info);
return NULL;
} // if
info->origio = origio;
io = (MojoInput *) xmalloc(sizeof (MojoInput));
io->ready = MojoInput_xz_ready;
io->read = MojoInput_xz_read;
io->seek = MojoInput_xz_seek;
io->tell = MojoInput_xz_tell;
io->length = MojoInput_xz_length;
io->duplicate = MojoInput_xz_duplicate;
io->close = MojoInput_xz_close;
io->opaque = info;
return io;
} // make_xz_input
#endif // SUPPORT_XZ
632
633
MojoInput *MojoInput_newCompressedStream(MojoInput *origio)
{
634
#if SUPPORT_GZIP || SUPPORT_BZIP2 || SUPPORT_XZ
635
636
637
// Look at the first piece of the file to decide if it is compressed
// by a general compression algorithm, and if so, wrap the MojoInput
// in a decompressor.
638
uint8 magic[6];
639
const int64 br = origio->read(origio, magic, sizeof (magic));
640
if ((origio->seek(origio, 0)) && (br == sizeof (magic)))
641
642
{
#if SUPPORT_GZIP
643
644
645
646
647
{
static const uint8 gzip_sig[] = { 0x1F, 0x8B, 0x08 };
if (memcmp(magic, gzip_sig, sizeof (gzip_sig)) == 0)
return make_gzip_input(origio);
}
648
649
650
#endif
#if SUPPORT_BZIP2
651
652
653
654
655
656
657
658
659
660
661
662
663
{
static const uint8 bzip2_sig[] = { 0x42, 0x5A };
if (memcmp(magic, bzip2_sig, sizeof (bzip2_sig)) == 0)
return make_bzip2_input(origio);
}
#endif
#if SUPPORT_XZ
{
static const uint8 xz_sig[] = { 0xFD, '7', 'z', 'X', 'Z', 0x00 };
if (memcmp(magic, xz_sig, sizeof (xz_sig)) == 0)
return make_xz_input(origio);
}
664
665
666
667
668
669
670
671
672
#endif
} // if
#endif
return NULL;
} // MojoInput_newCompressedStream
MojoArchive *MojoArchive_newFromInput(MojoInput *_io, const char *origfname)
673
674
675
{
int i;
MojoArchive *retval = NULL;
676
const char *ext = NULL;
677
678
679
MojoInput *io = MojoInput_newCompressedStream(_io);
if (io == NULL)
io = _io;
680
681
682
if (origfname != NULL)
{
683
ext = strrchr(origfname, '/');
684
685
686
687
688
689
if (ext == NULL)
ext = strchr(origfname, '.');
else
ext = strchr(ext+1, '.');
} // if
690
while (ext != NULL)
691
{
692
// Try for an exact match by filename extension.
693
ext++; // skip that '.'
694
695
for (i = 0; i < STATICARRAYLEN(archives); i++)
{
696
697
698
const MojoArchiveType *arc = &archives[i];
if (strcasecmp(ext, arc->ext) == 0)
return arc->create(io);
699
} // for
700
701
ext = strchr(ext, '.');
} // while
702
703
// Try any that could be determined without the file extension...
704
705
for (i = 0; i < STATICARRAYLEN(archives); i++)
{
706
707
const MojoArchiveType *arc = &archives[i];
if ((arc->hasMagic) && ((retval = arc->create(io)) != NULL))
708
709
710
711
712
713
714
715
return retval;
} // for
io->close(io);
return NULL; // nothing can handle this data.
} // MojoArchive_newFromInput
716
void MojoArchive_resetEntry(MojoArchiveEntry *info)
717
718
{
free(info->filename);
719
free(info->linkdest);
720
721
memset(info, '\0', sizeof (MojoArchiveEntry));
} // MojoArchive_resetEntry
722
723
724
725
// !!! FIXME: I'd rather not use a callback here, but I can't see a cleaner
// !!! FIXME: way right now...
726
boolean MojoInput_toPhysicalFile(MojoInput *in, const char *fname, uint16 perms,
727
MojoChecksums *checksums, int64 maxbytes,
728
MojoInput_FileCopyCallback cb, void *data)
729
{
730
731
boolean retval = false;
uint32 start = MojoPlatform_ticks();
732
void *out = NULL;
733
boolean iofailure = false;
734
735
int64 flen = 0;
int64 bw = 0;
736
MojoChecksumContext sumctx;
737
738
739
740
if (in == NULL)
return false;
741
742
743
744
745
746
if (checksums != NULL)
{
memset(checksums, '\0', sizeof (MojoChecksums));
MojoChecksum_init(&sumctx);
} // if
747
748
749
750
751
752
// Wait for a ready(), so length() can be meaningful on network streams.
while ((!in->ready(in)) && (!iofailure))
{
MojoPlatform_sleep(100);
if (cb != NULL)
{
753
if (!cb(MojoPlatform_ticks() - start, 0, 0, -1, data))
754
755
756
757
iofailure = true;
} // if
} // while
758
flen = in->length(in);
759
760
if ((maxbytes >= 0) && (flen > maxbytes))
flen = maxbytes;
761
762
MojoPlatform_unlink(fname);
763
if (!iofailure)
764
765
766
767
768
{
const uint32 flags = MOJOFILE_WRITE|MOJOFILE_CREATE|MOJOFILE_TRUNCATE;
const uint16 mode = MojoPlatform_defaultFilePerms();
out = MojoPlatform_open(fname, flags, mode);
} // if
769
770
if (out != NULL)
771
{
772
while (!iofailure)
773
{
774
int64 br = 0;
775
776
777
778
779
780
781
782
783
784
785
786
787
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
788
789
790
791
// If there's a callback, then poll. Otherwise, just block on
// the reads from the MojoInput.
if ((cb != NULL) && (!in->ready(in)))
792
793
MojoPlatform_sleep(100);
else
794
{
795
br = in->read(in, scratchbuf_128k, (uint32) maxread);
796
797
798
799
800
if (br == 0) // we're done!
break;
else if (br < 0)
iofailure = true;
else
801
{
802
if (MojoPlatform_write(out, scratchbuf_128k, (uint32) br) != br)
803
iofailure = true;
804
else
805
806
{
if (checksums != NULL)
807
MojoChecksum_append(&sumctx, scratchbuf_128k, (uint32) br);
808
bw += br;
809
} // else
810
} // else
811
} // else
812
813
814
if (cb != NULL)
{
815
if (!cb(MojoPlatform_ticks() - start, br, bw, flen, data))
816
817
818
819
iofailure = true;
} // if
} // while
820
if (MojoPlatform_close(out) != 0)
821
iofailure = true;
822
823
else if (bw != flen)
iofailure = true;
824
825
826
827
828
829
if (iofailure)
MojoPlatform_unlink(fname);
else
{
MojoPlatform_chmod(fname, perms);
830
831
if (checksums != NULL)
MojoChecksum_finish(&sumctx, checksums);
832
833
retval = true;
} // else
834
835
} // if
836
837
in->close(in);
return retval;
838
} // MojoInput_toPhysicalFile
839
840
841
842
843
MojoInput *MojoInput_newFromArchivePath(MojoArchive *ar, const char *fname)
{
MojoInput *retval = NULL;
844
if (ar->enumerate(ar))
845
{
846
const MojoArchiveEntry *entinfo;
847
848
while ((entinfo = ar->enumNext(ar)) != NULL)
{
849
if (strcmp(entinfo->filename, fname) == 0)
850
851
852
853
854
855
856
857
858
859
860
861
{
if (entinfo->type == MOJOARCHIVE_ENTRY_FILE)
retval = ar->openCurrentEntry(ar);
break;
} // if
} // while
} // if
return retval;
} // MojoInput_newFromArchivePath
862
863
864
// MojoInputs from files on the OS filesystem.
865
866
typedef struct
{
867
void *handle;
868
869
870
char *path;
} MojoInputFileInstance;
871
872
873
874
875
876
static boolean MojoInput_file_ready(MojoInput *io)
{
// !!! FIXME: select()? Does that help with network filesystems?
return true;
} // MojoInput_file_ready
877
static int64 MojoInput_file_read(MojoInput *io, void *buf, uint32 bufsize)
878
{
879
MojoInputFileInstance *inst = (MojoInputFileInstance *) io->opaque;
880
return MojoPlatform_read(inst->handle, buf, bufsize);
881
882
883
884
} // MojoInput_file_read
static boolean MojoInput_file_seek(MojoInput *io, uint64 pos)
{
885
MojoInputFileInstance *inst = (MojoInputFileInstance *) io->opaque;
886
return (MojoPlatform_seek(inst->handle, pos, MOJOSEEK_SET) == pos);
887
888
} // MojoInput_file_seek
889
static int64 MojoInput_file_tell(MojoInput *io)
890
{
891
MojoInputFileInstance *inst = (MojoInputFileInstance *) io->opaque;
892
return MojoPlatform_tell(inst->handle);
893
894
} // MojoInput_file_tell
895
896
897
static int64 MojoInput_file_length(MojoInput *io)
{
MojoInputFileInstance *inst = (MojoInputFileInstance *) io->opaque;
898
return MojoPlatform_flen(inst->handle);
899
900
901
902
903
904
905
906
} // MojoInput_file_length
static MojoInput *MojoInput_file_duplicate(MojoInput *io)
{
MojoInputFileInstance *inst = (MojoInputFileInstance *) io->opaque;
return MojoInput_newFromFile(inst->path);
} // MojoInput_file_duplicate
907
908
static void MojoInput_file_close(MojoInput *io)
{
909
MojoInputFileInstance *inst = (MojoInputFileInstance *) io->opaque;
910
MojoPlatform_close(inst->handle);
911
912
free(inst->path);
free(inst);
913
914
915
free(io);
} // MojoInput_file_close
916
MojoInput *MojoInput_newFromFile(const char *path)
917
{
918
MojoInput *io = NULL;
919
void *f = NULL;
920
921
f = MojoPlatform_open(path, MOJOFILE_READ, 0);
922
if (f != NULL)
923
{
924
925
926
927
928
929
MojoInputFileInstance *inst;
inst = (MojoInputFileInstance *) xmalloc(sizeof (MojoInputFileInstance));
inst->path = xstrdup(path);
inst->handle = f;
io = (MojoInput *) xmalloc(sizeof (MojoInput));
930
io->ready = MojoInput_file_ready;
931
932
933
934
935
936
937
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;
938
} // if
939
940
941
942
943
return io;
} // MojoInput_newFromFile
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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)
{