/
archive_tar.c
382 lines (315 loc) · 11 KB
1
2
3
4
5
6
7
/**
* 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.
*/
8
9
10
11
// Specs for the tar format can be found here...
// http://www.gnu.org/software/tar/manual/html_section/Standard.html
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include "fileio.h"
#if !SUPPORT_TAR
MojoArchive *MojoArchive_createTAR(MojoInput *io) { return NULL; }
#else
// MojoInput implementation...
// Decompression is handled in the parent MojoInput, so this just needs to
// make sure we stay within the bounds of the tarfile entry.
typedef struct TARinput
{
int64 fsize;
int64 offset;
MojoArchive *ar;
} TARinput;
typedef struct TARinfo
{
MojoInput *input;
33
uint64 curFileStart;
34
35
36
uint64 nextEnumPos;
} TARinfo;
37
38
static boolean MojoInput_tar_ready(MojoInput *io)
{
39
return true; // !!! FIXME: ready if there are bytes uncompressed.
40
41
} // MojoInput_tar_ready
42
43
44
45
46
static int64 MojoInput_tar_read(MojoInput *io, void *buf, uint32 bufsize)
{
TARinput *input = (TARinput *) io->opaque;
int64 pos = io->tell(io);
if ((pos + bufsize) > input->fsize)
47
bufsize = (uint32) (input->fsize - pos);
48
49
50
51
52
53
54
return input->ar->io->read(input->ar->io, buf, bufsize);
} // MojoInput_tar_read
static boolean MojoInput_tar_seek(MojoInput *io, uint64 pos)
{
TARinput *input = (TARinput *) io->opaque;
boolean retval = false;
55
if (pos < ((uint64) input->fsize))
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
retval = input->ar->io->seek(input->ar->io, input->offset + pos);
return retval;
} // MojoInput_tar_seek
static int64 MojoInput_tar_tell(MojoInput *io)
{
TARinput *input = (TARinput *) io->opaque;
return input->ar->io->tell(input->ar->io) - input->offset;
} // MojoInput_tar_tell
static int64 MojoInput_tar_length(MojoInput *io)
{
return ((TARinput *) io->opaque)->fsize;
} // MojoInput_tar_length
static MojoInput *MojoInput_tar_duplicate(MojoInput *io)
{
MojoInput *retval = NULL;
74
fatal(_("BUG: Can't duplicate tar inputs")); // !!! FIXME: why not?
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
#if 0
TARinput *input = (TARinput *) io->opaque;
MojoInput *origio = (MojoInput *) io->opaque;
MojoInput *newio = origio->duplicate(origio);
if (newio != NULL)
{
TARinput *newopaque = (TARinput *) xmalloc(sizeof (TARinput));
newopaque->origio = newio;
newopaque->fsize = input->fsize;
newopaque->offset = input->offset;
retval = (MojoInput *) xmalloc(sizeof (MojoInput));
memcpy(retval, io, sizeof (MojoInput));
retval->opaque = newopaque;
} // if
#endif
return retval;
} // MojoInput_tar_duplicate
static void MojoInput_tar_close(MojoInput *io)
{
TARinput *input = (TARinput *) io->opaque;
TARinfo *info = (TARinfo *) input->ar->opaque;
//input->ar->io->close(input->ar->io);
info->input = NULL;
free(input);
free(io);
} // MojoInput_tar_close
// MojoArchive implementation...
static boolean MojoArchive_tar_enumerate(MojoArchive *ar)
{
TARinfo *info = (TARinfo *) ar->opaque;
MojoArchive_resetEntry(&ar->prevEnum);
if (info->input != NULL)
fatal("BUG: tar entry still open on new enumeration");
113
info->curFileStart = info->nextEnumPos = 0;
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
return true;
} // MojoArchive_tar_enumerate
// These are byte offsets where fields start in the tar header blocks.
#define TAR_FNAME 0
#define TAR_FNAMELEN 100
#define TAR_MODE 100
#define TAR_MODELEN 8
#define TAR_UID 108
#define TAR_UIDLEN 8
#define TAR_GID 116
#define TAR_GIDLEN 8
#define TAR_SIZE 124
#define TAR_SIZELEN 12
#define TAR_MTIME 136
#define TAR_MTIMELEN 12
#define TAR_CHKSUM 148
#define TAR_CHKSUMLEN 8
#define TAR_TYPE 156
#define TAR_TYPELEN 1
#define TAR_LINKNAME 157
#define TAR_LINKNAMELEN 100
#define TAR_MAGIC 257
#define TAR_MAGICLEN 6
#define TAR_VERSION 263
#define TAR_VERSIONLEN 2
#define TAR_UNAME 265
#define TAR_UNAMELEN 32
#define TAR_GNAME 297
#define TAR_GNAMELEN 32
#define TAR_DEVMAJOR 329
#define TAR_DEVMAJORLEN 8
#define TAR_DEVMINOR 337
#define TAR_DEVMINORLEN 8
#define TAR_FNAMEPRE 345
#define TAR_FNAMEPRELEN 155
// tar entry types...
#define TAR_TYPE_FILE '0'
#define TAR_TYPE_HARDLINK '1'
#define TAR_TYPE_SYMLINK '2'
#define TAR_TYPE_CHARDEV '3'
#define TAR_TYPE_BLOCKDEV '4'
#define TAR_TYPE_DIRECTORY '5'
#define TAR_TYPE_FIFO '6'
160
161
#define TAR_TYPE_LONGLINK 'K'
#define TAR_TYPE_LONGNAME 'L'
162
163
164
165
166
167
168
static boolean is_ustar(const uint8 *block)
{
return ( (memcmp(&block[TAR_MAGIC], "ustar ", TAR_MAGICLEN) == 0) ||
(memcmp(&block[TAR_MAGIC], "ustar\0", TAR_MAGICLEN) == 0) );
} // is_ustar
169
static int64 octal_convert(const uint8 *str, const size_t len)
170
171
172
{
int64 retval = 0;
int64 multiplier = 1;
173
174
175
const uint8 *end = str + len;
const uint8 *ptr;
176
177
178
while ((*str == ' ') && (str != end))
str++;
179
180
ptr = str;
while ((ptr != end) && (*ptr >= '0') && (*ptr <= '7'))
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
ptr++;
while (--ptr >= str)
{
uint64 val = *ptr - '0';
retval += val * multiplier;
multiplier *= 8;
} // while
return retval;
} // octal_convert
static const MojoArchiveEntry *MojoArchive_tar_enumNext(MojoArchive *ar)
{
TARinfo *info = (TARinfo *) ar->opaque;
boolean zeroes = true;
boolean ustar = false;
uint8 scratch[512];
uint8 block[512];
size_t fnamelen = 0;
int type = 0;
memset(scratch, '\0', sizeof (scratch));
MojoArchive_resetEntry(&ar->prevEnum);
if (info->input != NULL)
fatal("BUG: tar entry still open on new enumeration");
210
211
get_next_block:
212
213
214
215
216
217
if (!ar->io->seek(ar->io, info->nextEnumPos))
return NULL;
// Find a non-zero block of data. Tarballs have two 512 blocks filled with
// null bytes at the end of the archive, but you can cat tarballs
// together, so you can't treat them as EOF indicators. Just skip them.
218
zeroes = true;
219
220
221
while (zeroes)
{
if (ar->io->read(ar->io, block, sizeof (block)) != sizeof (block))
222
return NULL; // !!! FIXME: fatal() ?
223
224
225
226
227
228
229
zeroes = (memcmp(block, scratch, sizeof (block)) == 0);
} // while
// !!! FIXME We should probably check the checksum.
ustar = is_ustar(block);
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
type = block[TAR_TYPE];
if ((type == TAR_TYPE_LONGNAME) || (type == TAR_TYPE_LONGLINK))
{
int64 filenameLength = octal_convert(&block[TAR_SIZE], TAR_SIZELEN);
char* filename = (char *) xmalloc(filenameLength + 1);
if (!ar->io->read(ar->io, filename, filenameLength))
return NULL;
filename[filenameLength] = '\0';
if (type == TAR_TYPE_LONGLINK)
ar->prevEnum.linkdest = filename;
else
ar->prevEnum.filename = filename;
info->nextEnumPos += 512 + filenameLength;
if (filenameLength % 512)
info->nextEnumPos += 512 - (filenameLength % 512);
goto get_next_block;
}
if (!ar->prevEnum.filename)
{
// We count on (scratch) being zeroed out here!
// prefix of filename is at the end for legacy compat.
if (ustar)
memcpy(scratch, &block[TAR_FNAMEPRE], TAR_FNAMEPRELEN);
fnamelen = strlen((const char *) scratch);
memcpy(&scratch[fnamelen], &block[TAR_FNAME], TAR_FNAMELEN);
fnamelen += strlen((const char *) &scratch[fnamelen]);
if (fnamelen == 0)
return NULL; // corrupt file. !!! FIXME: fatal() ?
ar->prevEnum.filename = xstrdup((const char *) scratch);
}
268
269
ar->prevEnum.perms = (uint16) octal_convert(&block[TAR_MODE], TAR_MODELEN);
ar->prevEnum.filesize = octal_convert(&block[TAR_SIZE], TAR_SIZELEN);
270
info->curFileStart = info->nextEnumPos + 512;
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
info->nextEnumPos += 512 + ar->prevEnum.filesize;
if (ar->prevEnum.filesize % 512)
info->nextEnumPos += 512 - (ar->prevEnum.filesize % 512);
type = block[TAR_TYPE];
if (type == 0) // some archivers do the file type as 0 instead of '0'.
type = TAR_TYPE_FILE;
if (ar->prevEnum.filename[fnamelen-1] == '/')
{
while (ar->prevEnum.filename[fnamelen-1] == '/')
ar->prevEnum.filename[--fnamelen] = '\0';
// legacy tar entries don't have a dir type, they just append a '/' to
// the filename...
if ((!ustar) && (type == TAR_TYPE_FILE))
type = TAR_TYPE_DIRECTORY;
} // if
ar->prevEnum.type = MOJOARCHIVE_ENTRY_UNKNOWN;
if (type == TAR_TYPE_FILE)
ar->prevEnum.type = MOJOARCHIVE_ENTRY_FILE;
else if (type == TAR_TYPE_DIRECTORY)
ar->prevEnum.type = MOJOARCHIVE_ENTRY_DIR;
else if (type == TAR_TYPE_SYMLINK)
{
ar->prevEnum.type = MOJOARCHIVE_ENTRY_SYMLINK;
298
299
300
301
302
303
if(!ar->prevEnum.linkdest)
{
memcpy(scratch, &block[TAR_LINKNAME], TAR_LINKNAMELEN);
scratch[TAR_LINKNAMELEN] = '\0'; // just in case.
ar->prevEnum.linkdest = xstrdup((const char *) scratch);
}
304
305
306
307
308
309
310
311
312
313
314
315
} // else if
return &ar->prevEnum;
} // MojoArchive_tar_enumNext
static MojoInput *MojoArchive_tar_openCurrentEntry(MojoArchive *ar)
{
TARinfo *info = (TARinfo *) ar->opaque;
MojoInput *io = NULL;
TARinput *opaque = NULL;
316
317
318
if (info->curFileStart == 0)
return NULL;
319
320
321
322
323
// Can't open multiple, since we would end up decompressing twice
// to enumerate the next file, so I imposed this limitation for now.
if (info->input != NULL)
fatal("BUG: tar entry double open");
324
325
// !!! FIXME: replace this with MojoInput_newFromSubset()?
326
327
328
opaque = (TARinput *) xmalloc(sizeof (TARinput));
opaque->ar = ar;
opaque->fsize = ar->prevEnum.filesize;
329
opaque->offset = info->curFileStart;
330
331
io = (MojoInput *) xmalloc(sizeof (MojoInput));
332
io->ready = MojoInput_tar_ready;
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
io->read = MojoInput_tar_read;
io->seek = MojoInput_tar_seek;
io->tell = MojoInput_tar_tell;
io->length = MojoInput_tar_length;
io->duplicate = MojoInput_tar_duplicate;
io->close = MojoInput_tar_close;
io->opaque = opaque;
info->input = io;
return io;
} // MojoArchive_tar_openCurrentEntry
static void MojoArchive_tar_close(MojoArchive *ar)
{
TARinfo *info = (TARinfo *) ar->opaque;
MojoArchive_resetEntry(&ar->prevEnum);
ar->io->close(ar->io);
free(info);
free(ar);
} // MojoArchive_tar_close
MojoArchive *MojoArchive_createTAR(MojoInput *io)
{
MojoArchive *ar = NULL;
uint8 sig[512];
359
const int64 br = io->read(io, sig, sizeof (sig));
360
361
// See if this is a tar archive. We only support "USTAR" format,
362
363
// since it has a detectable header. GNU and BSD tar has been creating
// these for years, so it's okay to ignore other ones, I guess.
364
if ((!io->seek(io, 0)) || (br != sizeof (sig)) || (!is_ustar(sig)) )
365
366
return NULL;
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
// okay, it's a tarball, we're good to go.
ar = (MojoArchive *) xmalloc(sizeof (MojoArchive));
ar->opaque = (TARinfo *) xmalloc(sizeof (TARinfo));
ar->enumerate = MojoArchive_tar_enumerate;
ar->enumNext = MojoArchive_tar_enumNext;
ar->openCurrentEntry = MojoArchive_tar_openCurrentEntry;
ar->close = MojoArchive_tar_close;
ar->io = io;
return ar;
} // MojoArchive_createTAR
#endif // SUPPORT_TAR
// end of archive_tar.c ...