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 <afsconfig.h>
32 #include <afs/param.h>
36 #include <afs/com_err.h>
37 #include <afs/cellconfig.h>
38 #include <afs/vlserver.h>
39 #include <afs/volser.h>
43 #include "dumpscan_errs.h"
46 #define COPYBUFSIZE (256*1024)
52 static char **file_names;
53 static int *file_vnums, name_count, vnum_count;
55 static char *input_path, *target;
56 static int quiet, verbose, error_count, dirs_done, extract_all;
57 static int nomode, use_realpath, use_vnum;
58 static int do_acls, do_headers;
60 static path_hashinfo phi;
61 static dump_parser dp;
63 /* Print a usage message and exit */
65 usage(int status, char *msg)
68 fprintf(stderr, "%s: %s\n", argv0, msg);
69 fprintf(stderr, "Usage: %s [options] dumpfile [dest [files...]]\n",
71 fprintf(stderr, " -A Save ACL's\n");
72 fprintf(stderr, " -H Save headers\n");
73 fprintf(stderr, " -h Print this help message\n");
74 fprintf(stderr, " -i Use vnode numbers\n");
75 fprintf(stderr, " -n Don't actually create files\n");
76 fprintf(stderr, " -p Use real pathnames internally\n");
77 fprintf(stderr, " -q Quiet mode (don't print errors)\n");
78 fprintf(stderr, " -v Verbose mode\n");
79 fprintf(stderr, "The destination directory defaults to .\n");
80 fprintf(stderr, "Files may be vnode numbers or volume-relative paths;\n");
81 fprintf(stderr, "If vnode numbers are used, files will be extracted\n");
83 "a name generated from the vnode number and uniqifier.\n");
84 fprintf(stderr, "If paths are used, -p is implied and files will be\n");
85 fprintf(stderr, "into correctly-named files.\n");
90 /* Parse the command-line options */
92 parse_options(int argc, char **argv)
94 int c, i, i_name, i_vnum;
96 /* Set the program name */
97 if ((argv0 = strrchr(argv[0], '/')))
102 /* Initialize options */
104 quiet = verbose = nomode = 0;
105 use_realpath = use_vnum = do_acls = do_headers = extract_all = 0;
107 /* Initialize other stuff */
110 /* Parse the options */
111 while ((c = getopt(argc, argv, "AHhinpqv")) != EOF) {
137 usage(1, "Invalid option!");
141 if (quiet && verbose)
142 usage(1, "Can't specify both -q and -v");
144 /* Parse non-option arguments */
145 if (argc - optind < 1)
146 usage(1, "Dumpfile name required!");
147 input_path = argv[optind];
149 if (argc - optind < 2)
151 target = argv[optind + 1];
153 vnum_count = name_count = 0;
154 if (argc - optind < 3)
159 for (i = 0; i < argc; i++) {
160 if (argv[i][0] == '/')
165 file_names = malloc(name_count + sizeof(char *));
166 file_vnums = malloc(vnum_count + sizeof(afs_uint32));
171 for (i = 0; i < argc; i++) {
172 if (argv[i][0] == '/')
173 file_names[i_name++] = argv[i];
175 file_vnums[i_vnum++] = strtol(argv[i], 0, 0);
184 char *x = path, slash;
188 while (*x && *x != '/')
193 if (stat(path, &statbuf)) {
194 if (errno == ENOENT) {
196 printf("> mkdir %s\n", path);
197 if (!mkdir(path, 0755))
218 strcpy(str, "rwxrwxrwx");
219 for (i = 0; i < 9; i++) {
220 if (!(mode & (1 << i)))
224 str[8] = (str[8] == '-') ? 'T' : 't';
226 str[5] = (str[5] == '-') ? 'S' : 's';
228 str[2] = (str[2] == '-') ? 'S' : 's';
233 static char *month[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
234 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
240 time_t clock = time(0);
241 struct tm *now, *then;
244 now = localtime(&clock);
245 then = localtime(&date);
247 diff = now->tm_mon - then->tm_mon;
248 if (then->tm_year == now->tm_year - 1)
250 if (then->tm_year == now->tm_year + 1)
253 if (diff < 5 || diff > 5)
254 sprintf(str, "%3s %2d %4d", month[then->tm_mon], then->tm_mday,
255 then->tm_year + 1900);
257 sprintf(str, "%3s %2d %2d:%2d", month[then->tm_mon], then->tm_mday,
258 then->tm_hour, then->tm_min);
263 /* Should we use this vnode?
264 * Return 0 if no, non-0 if yes
267 usevnode(XFILE * X, afs_uint32 vnum, char *vnodepath)
272 if (extract_all || !strcmp(vnodepath, "/"))
275 for (i = 0; i < vnum_count; i++)
276 if (vnum == file_vnums[i])
279 vl = strlen(vnodepath);
280 /*fprintf(stderr, "++ checking %s\n", vnodepath);*/
281 for (i = 0; i < name_count; i++) {
282 vpl = strlen(file_names[i]);
283 /* fprintf(stderr, " %s\n", file_names[i]);*/
286 r = !strncmp(file_names[i], vnodepath, vpl)
287 && vnodepath[vpl] == '/';
288 } else if (vl < vpl) {
289 r = !strncmp(file_names[i], vnodepath, vl)
290 && file_names[i][vl] == '/';
292 r = !strcmp(file_names[i], vnodepath);
302 copyfile(XFILE * in, XFILE * out, int size)
304 static char buf[COPYBUFSIZE];
308 nr = (size > COPYBUFSIZE) ? COPYBUFSIZE : size;
309 if ((r = xfread(in, buf, nr)))
311 if ((r = xfwrite(out, buf, nr)))
319 /* A callback to count and print errors */
321 my_error_cb(afs_uint32 code, int fatal, void *ref, char *msg, ...)
327 va_start(alist, msg);
328 afs_com_err_va(argv0, code, msg, alist);
336 dumphdr_cb(afs_dump_header * hdr, XFILE * X, void *refcon)
343 volhdr_cb(afs_vol_header * hdr, XFILE * X, void *refcon)
350 directory_cb(afs_vnode * v, XFILE * X, void *refcon)
355 /* Should we even use this? */
357 if ((r = Path_Build(X, &phi, v->vnode, &vnodepath, !use_realpath)))
359 if (!(use = usevnode(X, v->vnode, vnodepath))) {
368 printf("d%s %3d %-11d %11d %s #%d:%d\n", modestr(v->mode),
369 v->nlinks, v->owner, v->size, datestr(v->server_date),
372 printf("d%s %3d %-11d %11d %s %s\n", modestr(v->mode), v->nlinks,
373 v->owner, v->size, datestr(v->server_date), vnodepath);
374 } else if (!quiet && !use_vnum)
375 printf("%s\n", vnodepath);
377 /* Make the directory, if needed */
378 if (!nomode && !use_vnum && use != 2) {
379 if (strcmp(vnodepath, "/")
380 && (r = mkdirp(vnodepath + 1))) {
385 /* XXX do ACL's later */
395 file_cb(afs_vnode * v, XFILE * X, void *refcon)
397 char *vnodepath, vnpx[30];
405 printf("* Extracting files...\n");
408 /* Should we even use this? */
410 if ((r = Path_Build(X, &phi, v->vnode, &vnodepath, !use_realpath)))
412 if (!(use = usevnode(X, v->vnode, vnodepath))) {
418 sprintf(vnpx, "#%d:%d", v->vnode, v->vuniq);
422 sprintf(vnpx, "#%d:%d", v->vnode, v->vuniq);
428 printf("-%s %3d %-11d %11d %s %s\n", modestr(v->mode), v->nlinks,
429 v->owner, v->size, datestr(v->server_date), vnodepath);
431 printf("%s\n", vnodepath);
435 if ((r = xftell(X, &where))
436 || (r = xfseek(X, &v->d_offset))
438 xfopen_path(&OX, O_RDWR | O_CREAT | O_TRUNC, vnodepath + 1,
440 if (vnodepath != vnpx)
444 r = copyfile(X, &OX, v->size);
450 if (vnodepath != vnpx)
457 symlink_cb(afs_vnode * v, XFILE * X, void *refcon)
459 char *vnodepath, *linktarget, vnpx[30];
466 printf("* Extracting files...\n");
469 /* Should we even use this? */
471 if ((r = Path_Build(X, &phi, v->vnode, &vnodepath, !use_realpath)))
473 if (!(use = usevnode(X, v->vnode, vnodepath))) {
479 sprintf(vnpx, "#%d:%d", v->vnode, v->vuniq);
483 sprintf(vnpx, "#%d:%d", v->vnode, v->vuniq);
487 if (!(linktarget = malloc(v->size + 1))) {
488 if (vnodepath != vnpx)
492 if ((r = xftell(X, &where))
493 || (r = xfseek(X, &v->d_offset))
494 || (r = xfread(X, linktarget, v->size))) {
495 if (vnodepath != vnpx)
501 linktarget[v->size] = 0;
505 printf("l%s %3d %-11d %11d %s %s -> %s\n", modestr(v->mode),
506 v->nlinks, v->owner, v->size, datestr(v->server_date),
507 vnodepath, linktarget);
509 printf("%s\n", vnodepath);
513 if (symlink(linktarget, vnodepath + 1))
518 if (vnodepath != vnpx)
525 lose_cb(afs_vnode * v, XFILE * F, void *refcon)
530 printf("* Extracting files...\n");
539 main(int argc, char **argv)
544 parse_options(argc, argv);
545 initialize_acfg_error_table();
546 initialize_AVds_error_table();
547 initialize_rxk_error_table();
548 initialize_u_error_table();
549 initialize_vl_error_table();
550 initialize_vols_error_table();
551 initialize_xFil_error_table();
552 r = xfopen(&input_file, O_RDONLY, input_path);
554 afs_com_err(argv0, r, "opening %s", input_path);
558 memset(&dp, 0, sizeof(dp));
559 dp.cb_error = my_error_cb;
560 if (input_file.is_seekable)
561 dp.flags |= DSFLAG_SEEK;
567 memset(&phi, 0, sizeof(phi));
571 printf("* Building pathname info...\n");
572 if ((r = xftell(&input_file, &where))
573 || (r = Path_PreScan(&input_file, &phi, 1))
574 || (r = xfseek(&input_file, &where))) {
575 afs_com_err(argv0, r, "- path initialization failed");
576 xfclose(&input_file);
581 dp.cb_vnode_dir = directory_cb;
582 dp.cb_vnode_file = file_cb;
583 dp.cb_vnode_link = symlink_cb;
584 dp.cb_vnode_empty = lose_cb;
585 dp.cb_vnode_wierd = lose_cb;
587 dp.cb_dumphdr = dumphdr_cb;
588 dp.cb_volhdr = volhdr_cb;
594 fprintf(stderr, "chdir %s failed: %s\n", target, strerror(errno));
598 r = ParseDumpFile(&input_file, &dp);
600 if (verbose && error_count)
601 fprintf(stderr, "*** %d errors\n", error_count);
603 fprintf(stderr, "*** FAILED: %s\n", afs_error_message(r));