3 * dumpscan - routines for scanning and manipulating AFS volume dumps
5 * Copyright (c) 1998 Carnegie Mellon University
8 * Permission to use, copy, modify and distribute this software and its
9 * documentation is hereby granted, provided that both the copyright
10 * notice and this permission notice appear in all copies of the
11 * software, derivative works or modified versions, and any portions
12 * thereof, and that both notices appear in supporting documentation.
14 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
15 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
16 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
18 * Carnegie Mellon requests users of this software to return to
20 * Software Distribution Coordinator or Software_Distribution@CS.CMU.EDU
21 * School of Computer Science
22 * Carnegie Mellon University
23 * Pittsburgh PA 15213-3890
25 * any improvements or extensions that they make and grant Carnegie Mellon
26 * the rights to redistribute these changes.
29 /* afsdump_extract.c - Extract files from an AFS dump */
31 #include <sys/fcntl.h>
32 #include <sys/types.h>
39 #include "dumpscan_errs.h"
41 #define COPYBUFSIZE (256*1024)
47 static char **file_names;
48 static int *file_vnums, name_count, vnum_count;
50 static char *input_path, *target;
51 static int quiet, verbose, error_count, dirs_done, extract_all;
52 static int nomode, use_realpath, use_vnum;
53 static int do_acls, do_headers;
55 static path_hashinfo phi;
56 static dump_parser dp;
58 /* Print a usage message and exit */
60 usage(int status, char *msg)
63 fprintf(stderr, "%s: %s\n", argv0, msg);
64 fprintf(stderr, "Usage: %s [options] dumpfile [dest [files...]]\n",
66 fprintf(stderr, " -A Save ACL's\n");
67 fprintf(stderr, " -H Save headers\n");
68 fprintf(stderr, " -h Print this help message\n");
69 fprintf(stderr, " -i Use vnode numbers\n");
70 fprintf(stderr, " -n Don't actually create files\n");
71 fprintf(stderr, " -p Use real pathnames internally\n");
72 fprintf(stderr, " -q Quiet mode (don't print errors)\n");
73 fprintf(stderr, " -v Verbose mode\n");
74 fprintf(stderr, "The destination directory defaults to .\n");
75 fprintf(stderr, "Files may be vnode numbers or volume-relative paths;\n");
76 fprintf(stderr, "If vnode numbers are used, files will be extracted\n");
78 "a name generated from the vnode number and uniqifier.\n");
79 fprintf(stderr, "If paths are used, -p is implied and files will be\n");
80 fprintf(stderr, "into correctly-named files.\n");
85 /* Parse the command-line options */
87 parse_options(int argc, char **argv)
89 int c, i, i_name, i_vnum;
91 /* Set the program name */
92 if (argv0 = strrchr(argv[0], '/'))
97 /* Initialize options */
99 quiet = verbose = nomode = 0;
100 use_realpath = use_vnum = do_acls = do_headers = extract_all = 0;
102 /* Initialize other stuff */
105 /* Parse the options */
106 while ((c = getopt(argc, argv, "AHhinpqv")) != EOF) {
132 usage(1, "Invalid option!");
136 if (quiet && verbose)
137 usage(1, "Can't specify both -q and -v");
139 /* Parse non-option arguments */
140 if (argc - optind < 1)
141 usage(1, "Dumpfile name required!");
142 input_path = argv[optind];
144 if (argc - optind < 2)
146 target = argv[optind + 1];
148 vnum_count = name_count = 0;
149 if (argc - optind < 3)
154 for (i = 0; i < argc; i++) {
155 if (argv[i][0] == '/')
160 file_names = (char **)malloc(name_count + sizeof(char *));
161 file_vnums = (afs_uint32 *) malloc(vnum_count + sizeof(afs_uint32));
166 for (i = 0; i < argc; i++) {
167 if (argv[i][0] == '/')
168 file_names[i_name++] = argv[i];
170 file_vnums[i_vnum++] = strtol(argv[i], 0, 0);
179 char *x = path, slash;
183 while (*x && *x != '/')
188 if (stat(path, &statbuf)) {
189 if (errno == ENOENT) {
191 printf("> mkdir %s\n", path);
192 if (!mkdir(path, 0755))
213 strcpy(str, "rwxrwxrwx");
214 for (i = 0; i < 9; i++) {
215 if (!(mode & (1 << i)))
219 str[8] = (str[8] == '-') ? 'T' : 't';
221 str[5] = (str[5] == '-') ? 'S' : 's';
223 str[2] = (str[2] == '-') ? 'S' : 's';
228 static char *month[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
229 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
235 time_t clock = time(0);
236 struct tm *now, *then;
239 now = localtime(&clock);
240 then = localtime(&date);
242 diff = now->tm_mon - then->tm_mon;
243 if (then->tm_year == now->tm_year - 1)
245 if (then->tm_year == now->tm_year + 1)
248 if (diff < 5 || diff > 5)
249 sprintf(str, "%3s %2d %4d", month[then->tm_mon], then->tm_mday,
250 then->tm_year + 1900);
252 sprintf(str, "%3s %2d %2d:%2d", month[then->tm_mon], then->tm_mday,
253 then->tm_hour, then->tm_min);
258 /* Should we use this vnode?
259 * Return 0 if no, non-0 if yes
262 usevnode(XFILE * X, afs_uint32 vnum, char *vnodepath)
267 if (extract_all || !strcmp(vnodepath, "/"))
270 for (i = 0; i < vnum_count; i++)
271 if (vnum == file_vnums[i])
274 vl = strlen(vnodepath);
275 /*fprintf(stderr, "++ checking %s\n", vnodepath);*/
276 for (i = 0; i < name_count; i++) {
277 vpl = strlen(file_names[i]);
278 /* fprintf(stderr, " %s\n", file_names[i]);*/
281 r = !strncmp(file_names[i], vnodepath, vpl)
282 && vnodepath[vpl] == '/';
283 } else if (vl < vpl) {
284 r = !strncmp(file_names[i], vnodepath, vl)
285 && file_names[i][vl] == '/';
287 r = !strcmp(file_names[i], vnodepath);
297 copyfile(XFILE * in, XFILE * out, int size)
299 static char buf[COPYBUFSIZE];
303 nr = (size > COPYBUFSIZE) ? COPYBUFSIZE : size;
304 if (r = xfread(in, buf, nr))
306 if (r = xfwrite(out, buf, nr))
314 /* A callback to count and print errors */
316 my_error_cb(afs_uint32 code, int fatal, void *ref, char *msg, ...)
322 va_start(alist, msg);
323 afs_com_err_va(argv0, code, msg, alist);
330 dumphdr_cb(afs_dump_header * hdr, XFILE * X, void *refcon)
337 volhdr_cb(afs_vol_header * hdr, XFILE * X, void *refcon)
344 directory_cb(afs_vnode * v, XFILE * X, void *refcon)
349 /* Should we even use this? */
351 if (r = Path_Build(X, &phi, v->vnode, &vnodepath, !use_realpath))
353 if (!(use = usevnode(X, v->vnode, vnodepath))) {
362 printf("d%s %3d %-11d %11d %s #%d:%d\n", modestr(v->mode),
363 v->nlinks, v->owner, v->size, datestr(v->server_date),
366 printf("d%s %3d %-11d %11d %s %s\n", modestr(v->mode), v->nlinks,
367 v->owner, v->size, datestr(v->server_date), vnodepath);
368 } else if (!quiet && !use_vnum)
369 printf("%s\n", vnodepath);
371 /* Make the directory, if needed */
372 if (!nomode && !use_vnum && use != 2) {
373 if (strcmp(vnodepath, "/")
374 && (r = mkdirp(vnodepath + 1))) {
379 /* XXX do ACL's later */
389 file_cb(afs_vnode * v, XFILE * X, void *refcon)
391 char *vnodepath, vnpx[30];
399 printf("* Extracting files...\n");
402 /* Should we even use this? */
404 if (r = Path_Build(X, &phi, v->vnode, &vnodepath, !use_realpath))
406 if (!(use = usevnode(X, v->vnode, vnodepath))) {
412 sprintf(vnpx, "#%d:%d", v->vnode, v->vuniq);
416 sprintf(vnpx, "#%d:%d", v->vnode, v->vuniq);
422 printf("-%s %3d %-11d %11d %s %s\n", modestr(v->mode), v->nlinks,
423 v->owner, v->size, datestr(v->server_date), vnodepath);
425 printf("%s\n", vnodepath);
429 if ((r = xftell(X, &where))
430 || (r = xfseek(X, &v->d_offset))
432 xfopen_path(&OX, O_RDWR | O_CREAT | O_TRUNC, vnodepath + 1,
438 r = copyfile(X, &OX, v->size);
444 if (!use_vnum && use != 2)
451 symlink_cb(afs_vnode * v, XFILE * X, void *refcon)
453 char *vnodepath, *linktarget, vnpx[30];
460 printf("* Extracting files...\n");
463 /* Should we even use this? */
465 if (r = Path_Build(X, &phi, v->vnode, &vnodepath, !use_realpath))
467 if (!(use = usevnode(X, v->vnode, vnodepath))) {
473 sprintf(vnpx, "#%d:%d", v->vnode, v->vuniq);
477 sprintf(vnpx, "#%d:%d", v->vnode, v->vuniq);
481 if (!(linktarget = (char *)malloc(v->size + 1))) {
482 if (!use_vnum && use != 2)
486 if ((r = xftell(X, &where))
487 || (r = xfseek(X, &v->d_offset))
488 || (r = xfread(X, linktarget, v->size))) {
489 if (!use_vnum && use != 2)
495 linktarget[v->size] = 0;
499 printf("l%s %3d %-11d %11d %s %s -> %s\n", modestr(v->mode),
500 v->nlinks, v->owner, v->size, datestr(v->server_date),
501 vnodepath, linktarget);
503 printf("%s\n", vnodepath);
507 if (symlink(linktarget, vnodepath + 1))
512 if (!use_vnum && use != 2)
519 lose_cb(afs_vnode * v, XFILE * F, void *refcon)
526 printf("* Extracting files...\n");
535 main(int argc, char **argv)
540 parse_options(argc, argv);
541 initialize_acfg_error_table();
542 initialize_AVds_error_table();
543 initialize_rxk_error_table();
544 initialize_u_error_table();
545 initialize_vl_error_table();
546 initialize_vols_error_table();
547 initialize_xFil_error_table();
548 r = xfopen(&input_file, O_RDONLY, input_path);
550 afs_com_err(argv0, r, "opening %s", input_path);
554 memset(&dp, 0, sizeof(dp));
555 dp.cb_error = my_error_cb;
556 if (input_file.is_seekable)
557 dp.flags |= DSFLAG_SEEK;
563 memset(&phi, 0, sizeof(phi));
567 printf("* Building pathname info...\n");
568 if ((r = xftell(&input_file, &where))
569 || (r = Path_PreScan(&input_file, &phi, 1))
570 || (r = xfseek(&input_file, &where))) {
571 afs_com_err(argv0, r, "- path initialization failed");
572 xfclose(&input_file);
577 dp.cb_vnode_dir = directory_cb;
578 dp.cb_vnode_file = file_cb;
579 dp.cb_vnode_link = symlink_cb;
580 dp.cb_vnode_empty = lose_cb;
581 dp.cb_vnode_wierd = lose_cb;
583 dp.cb_dumphdr = dumphdr_cb;
584 dp.cb_volhdr = volhdr_cb;
590 fprintf(stderr, "chdir %s failed: %s\n", target, strerror(errno));
594 r = ParseDumpFile(&input_file, &dp);
596 if (verbose && error_count)
597 fprintf(stderr, "*** %d errors\n", error_count);
599 fprintf(stderr, "*** FAILED: %s\n", afs_error_message(r));