-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathefsextract.c
593 lines (539 loc) · 11.8 KB
/
efsextract.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
#define _GNU_SOURCE
#include <errno.h>
#include <inttypes.h>
#include <iso646.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <cdio/iso9660.h>
#include <cdio/logging.h>
#ifndef __MINGW32__
#include <sys/sysmacros.h>
#endif
#include "efs.h"
#include "endian.h"
#include "err.h"
#include "hexdump.h"
#include "pdscan.h"
#include "progname.h"
#include "tar.h"
#include "version.h"
int qflag = 0;
int lflag = 0;
int Lflag = 0;
int Wflag = 0;
int Xflag = 0;
char *outfile = NULL;
efs_t *efs;
static void tryhelp(void);
static void usage(void);
void mode2str(char *str, uint16_t mode)
{
strcpy(str, "----------");
switch (mode & IFMT) {
case IFIFO:
str[0] = 'p';
break;
case IFCHR:
str[0] = 'c';
break;
case IFDIR:
str[0] = 'd';
break;
case IFBLK:
str[0] = 'b';
break;
case IFREG:
str[0] = '-';
break;
case IFLNK:
str[0] = 'l';
break;
case IFSOCK:
str[0] = 's';
break;
default:
str[0] = '?';
break;
}
if (mode & 0400) str[1] = 'r';
if (mode & 0200) str[2] = 'w';
if (mode & 0100) str[3] = 'x';
if (mode & 0040) str[4] = 'r';
if (mode & 0020) str[5] = 'w';
if (mode & 0010) str[6] = 'x';
if (mode & 0004) str[7] = 'r';
if (mode & 0002) str[8] = 'w';
if (mode & 0001) str[9] = 'x';
str[10] = '\0';
}
int mode2color(uint16_t mode)
{
switch (mode & IFMT) {
case IFIFO:
return 33;
case IFCHR:
return 33;
case IFDIR:
return 34;
case IFBLK:
return 33;
case IFREG:
if (mode & 0111)
return 32;
else
return -1;
case IFLNK:
return 36;
case IFSOCK:
return 35;
default:
return -1;
}
}
void emit_regfile(efs_t *efs, const char *path)
{
struct efs_stat sb;
int rc;
FILE *dst;
efs_file_t *src;
char blk[BLKSIZ];
size_t sz;
size_t bytesLeft;
size_t blockNum;
rc = efs_stat(efs, path, &sb);
if (rc == -1)
err(1, "couldn't get stat for '%s'", path);
src = efs_fopen(efs, path);
if (!src)
errx(1, "couldn't open efs file '%s'", path);
dst = fopen(path, "wb");
if (!dst)
err(1, "couldn't open destination file '%s'", path);
for (blockNum = 0; blockNum < (sb.st_size / 512); blockNum++) {
sz = efs_fread(blk, BLKSIZ, 1, src);
if (sz != 1)
err(1, "couldn't read from source file '%s'", path);
sz = fwrite(blk, BLKSIZ, 1, dst);
if (sz != 1)
err(1, "couldn't write to destination file '%s'", path);
}
bytesLeft = sb.st_size & (BLKSIZ - 1);
if (bytesLeft) {
sz = efs_fread(blk, bytesLeft, 1, src);
if (sz != 1)
err(1, "couldn't read from source file '%s'", path);
sz = fwrite(blk, bytesLeft, 1, dst);
if (sz != 1)
err(1, "couldn't write to destination file '%s'", path);
}
efs_fclose(src);
fclose(dst);
rc = chmod(path, sb.st_mode & 0777);
if (rc == -1)
err(1, "couldn't set permissions on '%s'", path);
}
void emit_file(efs_t *efs, const char *path)
{
struct efs_stat sb;
int rc;
rc = efs_stat(efs, path, &sb);
if (rc == -1)
err(1, "couldn't get stat for '%s'", path);
switch (sb.st_mode & IFMT) {
case IFDIR:
#ifdef __MINGW32__
rc = mkdir(path);
#else
rc = mkdir(path, sb.st_mode & 0777);
#endif
if ((rc == -1) && (errno != EEXIST))
err(1, "couldn't make directory '%s'", path);
break;
case IFREG:
emit_regfile(efs, path);
break;
case IFIFO:
#ifndef __MINGW32__
rc = mkfifo(path, sb.st_mode & 0777);
if (rc == -1)
warn("couldn't create fifo '%s'", path);
#else
warnx("extracting fifos not supported");
#endif
break;
case IFCHR:
#ifndef __MINGW32__
rc = mknod(path, S_IFCHR | (sb.st_mode & 0777), makedev(sb.st_major, sb.st_minor));
if (rc == -1)
warn("couldn't create character special '%s'", path);
#else
warnx("extracting character specials not supported");
#endif
break;
case IFBLK:
#ifndef __MINGW32__
rc = mknod(path, S_IFBLK | (sb.st_mode & 0777), makedev(sb.st_major, sb.st_minor));
if (rc == -1)
warn("couldn't create block special '%s'", path);
#else
warnx("extracting block specials not supported");
#endif
break;
case IFLNK:
#ifndef __MINGW32__
{
__label__ done;
char *buf = NULL;
efs_file_t *f = NULL;
size_t sz;
buf = malloc(sb.st_size + 1);
if (!buf)
err(1, "in malloc");
f = efs_fopen(efs, path);
if (!f) {
warnx("couldn't open efs symlink '%s'", path);
goto done;
}
sz = efs_fread(buf, sb.st_size, 1, f);
if (sz != 1) {
warnx("couldn't read efs symlink '%s'", path);
goto done;
}
buf[sb.st_size] = '\0';
rc = symlink(buf, path);
if (rc == -1)
warn("couldn't create symlink '%s'", path);
done:
if (f)
efs_fclose(f);
free(buf);
}
#else
warnx("extracting symlinks not supported");
#endif
break;
case IFSOCK:
warnx("extracting sockets not supported");
break;
default:
break;
}
}
/*
* This function is used as a callback for a later
* invocation of efs_nftw().
*/
int efs_nftw_callback(const char *fpath, const struct efs_stat *sb) {
/* extern: efs, outfile */
int rc;
if (Wflag) {
pdprint(efs, fpath);
return 0;
}
if (!qflag) {
printf("%s\n", fpath);
}
if (!lflag) {
if (outfile) {
rc = tar_emit(efs, fpath);
if (rc == -1)
errx(1, "while writing to tar (emit failure): %d", rc);
} else {
emit_file(efs, fpath);
}
}
return 0;
}
int main(int argc, char *argv[])
{
char *filename = NULL;
int rc;
int parnum = -1;
efs_err_t erc;
dvh_t *dvh;
fileslice_t *par;
progname_init(argc, argv);
while ((rc = getopt(argc, argv, "hLlo:p:qVWX")) != -1)
switch (rc) {
case 'h':
usage();
break;
case 'L':
if (Lflag != 0) {
warnx("multiple use of `-L'");
tryhelp();
}
Lflag = 1;
break;
case 'l':
if (lflag != 0) {
warnx("multiple use of `-l'");
tryhelp();
}
lflag = 1;
break;
case 'o':
if (outfile) {
warnx("multiple use of `-o'");
tryhelp();
}
outfile = optarg;
break;
case 'p':
if (parnum != -1) {
warnx("multiple use of `-p'");
tryhelp();
}
{
char *ptr = NULL;
parnum = strtol(optarg, &ptr, 10);
if (*ptr)
errx(1, "bad partition number `%s'", optarg);
}
break;
case 'q':
if (qflag != 0) {
warnx("multiple use of `-q'");
tryhelp();
}
qflag = 1;
break;
case 'V':
fprintf(stderr, "%s\n", PROG_EMBLEM);
exit(EXIT_SUCCESS);
break;
case 'W':
if (Wflag != 0) {
warnx("multiple use of `-W'");
tryhelp();
}
Wflag = 1;
break;
case 'X':
if (Xflag != 0) {
warnx("multiple use of `-X'");
tryhelp();
}
Xflag = 1;
break;
default:
tryhelp();
}
argc -= optind;
argv += optind;
/* -L flag: cannot be combined with other flags */
if (Lflag && (lflag || qflag || Wflag))
errx(1, "cannot combine -L flag with other flags");
/* -X flag: can be combined with -q flag, but nothing else */
if (Xflag && (lflag || Lflag || Wflag))
errx(1, "cannot combine -X flag with other flags");
if (Xflag && outfile)
errx(1, "cannot combine -X flag with -o");
/* grab filename as first un-flagged argument */
if (*argv != NULL) {
filename = *argv;
} else {
warnx("must specify a file");
tryhelp();
}
if (parnum == -1)
parnum = 7;
if (Lflag) {
efs_err_t erc;
dvh_t *ctx = NULL;
unsigned i;
erc = dvh_open(&ctx, filename);
if (erc != EFS_ERR_OK)
errefs(1, erc, "couldn't open dvh in '%s'", filename);
printf("idx start size type\n");
printf("--- -------- -------- --------\n");
for (i = 0; i < NPARTAB; i++) {
struct dvh_pt_s pt;
const char *typeName;
pt = dvh_getParInfo(ctx, i);
typeName = dvh_getNameForType(pt.pt_type);
if (pt.pt_nblks) {
printf("%3u %8d %8d ",
i,
pt.pt_firstlbn,
pt.pt_nblks
);
if (typeName) {
printf("%s\n", typeName);
} else {
printf("(%d)\n", pt.pt_type);
}
} else {
#if 0
printf("%3u - -\n", i);
#endif
}
}
printf("\n");
printf("start size name\n");
printf("-------- -------- --------\n");
for (i = 0; i < NVDIR; i++) {
struct dvh_vd_s vd;
char name[VDNAMESIZE + 1];
name[VDNAMESIZE] = '\0';
vd = dvh_getFileInfo(ctx, i);
memcpy(name, vd.vd_name, VDNAMESIZE);
if (strlen(name) || vd.vd_lbn || vd.vd_nbytes)
printf("%8d %8d %s\n",
vd.vd_lbn,
vd.vd_nbytes,
name
);
}
exit(0);
}
erc = dvh_open(&dvh, filename);
if ((erc != EFS_ERR_OK) && !outfile) {
errx(1, "couldn't find volume header in '%s'", filename);
} else if (erc != EFS_ERR_OK) {
/* is it iso9660? */
CdioList_t *a;
CdioListNode_t *b;
iso9660_t *ctx;
queue_t q, dirq;
int rc;
struct qent_s *qe;
cdio_loglevel_default = CDIO_LOG_ERROR;
rc = tar_create(outfile);
if (rc) {
errx(1, "while creating tar");
}
q = queue_init();
queue_add_head(q, strdup(""));
ctx = iso9660_open(filename);
if (!ctx)
errx(1, "couldn't open '%s'", filename);
while ((qe = queue_dequeue(q))) {
a = iso9660_ifs_readdir(ctx, qe->path);
dirq = queue_init();
if (a) {
_CDIO_LIST_FOREACH(b, a) {
iso9660_stat_t *st;
char *path;
st = (iso9660_stat_t *) _cdio_list_node_data(b);
path = mkpath(qe->path, st->filename);
if (strcmp(st->filename, ".") && strcmp(st->filename, "..")) {
if (!qflag && path) {
printf("%s\n", path);
}
switch (st->type) {
case _STAT_DIR:
queue_add_head(dirq, path);
break;
case _STAT_FILE:
tar_emit_from_iso9660(ctx, path);
free(path);
break;
default:
free(path);
break;
}
} else {
free(path);
}
}
iso9660_filelist_free(a);
a = NULL;
}
free(qe->path);
free(qe);
queue_add_queue_head(q, dirq);
}
queue_free(q);
iso9660_close(ctx);
tar_close();
ctx = NULL;
return 0;
} /* end iso9660 branch */
if (Xflag) {
int fileNum;
struct dvh_vd_s vd;
for (fileNum = 0; fileNum < NVDIR; fileNum++) {
vd = dvh_getFileInfo(dvh, fileNum);
if (vd.vd_lbn != 0) {
void *filedata;
char filename[VDNAMESIZE + 1];
FILE *f = NULL;
strncpy(filename, vd.vd_name, VDNAMESIZE);
filename[VDNAMESIZE] = '\0';
filedata = dvh_readFile(dvh, fileNum);
if (filedata)
f = fopen(filename, "wb");
if (f) {
size_t sRc;
sRc = fwrite(filedata, vd.vd_nbytes, 1, f);
fclose(f);
if (sRc != 1) {
unlink(filename);
err(1, "while writing file `%s'", filename);
} else if (!qflag) {
printf("%s\n", filename);
}
}
free(filedata);
}
}
efs_close(efs);
dvh_close(dvh);
return EXIT_SUCCESS;
}
par = dvh_getParSlice(dvh, parnum);
if (!par)
errx(1, "couldn't get par slice %u", parnum);
erc = efs_open(&efs, par);
if (erc != EFS_ERR_OK)
errefs(1, erc, "couldn't open efs in '%s'", filename);
if (outfile) {
rc = tar_create(outfile);
if (rc) err(1, "couldn't create archive '%s'", outfile);
}
if (Wflag) {
printf(" %-30s %s\n\n", "Name", "Description");
}
efs_nftw(efs, "", efs_nftw_callback);
if (outfile) {
rc = tar_close();
if (rc) err(1, "couldn't close archive '%s'", outfile);
}
efs_close(efs);
dvh_close(dvh);
return EXIT_SUCCESS;
}
static void tryhelp(void)
{
(void)fprintf(stderr, "Try `%s -h' for more information.\n",
__progname);
exit(EXIT_FAILURE);
}
static void usage(void)
{
(void)fprintf(stderr,
"Usage: %s [OPTION] [FILE]\n"
"Extract files from the SGI CD image (or EFS file system) in FILE.\n"
"\n"
" -h print this help text\n"
" -l list files without extracting\n"
" -L list partitions and bootfiles from the volume header\n"
" -o ARCHIVE\n"
" create a tar archive instead of extracting\n"
" -p NUM use partition number (default: 7)\n"
" -q do not show file listing while extracting\n"
" -V print program version\n"
" -W scan image for packages and list them\n"
" -X extract bootfiles from the volume headers\n"
"\n"
"Please report any bugs to <[email protected]>.\n"
, __progname
);
exit(EXIT_SUCCESS);
}