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_scan.c - General-purpose dump scanner */
31 #include <sys/fcntl.h>
36 #include <afs/param.h>
37 #include <afs/com_err.h>
40 #include "dumpscan_errs.h"
45 extern XFILE repair_output;
46 extern afs_uint32 repair_dumphdr_cb(afs_dump_header *, XFILE *, void *);
47 extern afs_uint32 repair_volhdr_cb(afs_vol_header *, XFILE *, void *);
48 extern afs_uint32 repair_vnode_cb(afs_vnode *, XFILE *, void *);
51 static char *input_path, *gendump_path;
52 static afs_uint32 printflags, repairflags;
53 static int quiet, verbose, error_count;
55 static path_hashinfo phi;
56 static dump_parser dp;
59 /* Print a usage message and exit */
61 usage(int status, char *msg)
64 fprintf(stderr, "%s: %s\n", argv0, msg);
65 fprintf(stderr, "Usage: %s [options] [file]\n", argv0);
66 fprintf(stderr, " -Pxxx Set print options:\n");
67 fprintf(stderr, " B = Print backup system header (if any)\n");
68 fprintf(stderr, " H = Print AFS dump header\n");
69 fprintf(stderr, " V = Print AFS volume header\n");
70 fprintf(stderr, " v = List vnodes\n");
71 fprintf(stderr, " p = Include path to each vnode\n");
72 fprintf(stderr, " i = Include info for each vnode\n");
73 fprintf(stderr, " d = List directory contents\n");
74 fprintf(stderr, " a = List access control lists\n");
75 fprintf(stderr, " g = Print debugging info\n");
76 fprintf(stderr, " -Rxxx Set repair options:\n");
77 fprintf(stderr, " 0 = Skip null tags\n");
78 fprintf(stderr, " b = Seek backward to find skipped tags\n");
79 fprintf(stderr, " d = Resync after vnode data\n");
80 fprintf(stderr, " v = Resync after corrupted vnodes\n");
81 fprintf(stderr, " -h Print this help message\n");
82 fprintf(stderr, " -gxxx Generate a new dump in file xxx\n");
83 fprintf(stderr, " -q Quiet mode (don't print errors)\n");
84 fprintf(stderr, " -v Verbose mode\n");
89 /* Parse the argument given to the -P option.
90 * Returns the resulting * dumpscan print flags (DSPRINT_*).
91 * If an unrecognized flag is used, prints an error message and exits.
94 parse_printflags(char *flags)
96 afs_uint32 result = 0;
99 for (x = flags; *x; x++)
102 result |= DSPRINT_BCKHDR;
105 result |= DSPRINT_DUMPHDR;
108 result |= DSPRINT_VOLHDR;
111 result |= DSPRINT_ITEM;
114 result |= DSPRINT_PATH;
117 result |= DSPRINT_VNODE;
120 result |= DSPRINT_DIR;
123 result |= DSPRINT_ACL;
126 result |= DSPRINT_DEBUG;
129 usage(1, "Invalid print options!");
135 /* Parse the argument given to the -R option.
136 * Returns the resulting * dumpscan repair flags (DSFIX_*).
137 * If an unrecognized flag is used, prints an error message and exits.
140 parse_repairflags(char *flags)
142 afs_uint32 result = 0;
145 for (x = flags; *x; x++)
148 result |= DSFIX_SKIP;
151 result |= DSFIX_RSKIP;
154 result |= DSFIX_VDSYNC;
157 result |= DSFIX_VFSYNC;
160 usage(1, "Invalid repair options!");
166 /* Parse the command-line options */
168 parse_options(int argc, char **argv)
172 /* Set the program name */
173 if ((argv0 = strrchr(argv[0], '/')))
178 /* Initialize options */
179 input_path = gendump_path = 0;
180 printflags = repairflags = 0;
183 /* Initialize other stuff */
186 /* Parse the options */
187 while ((c = getopt(argc, argv, "P:R:g:hqv")) != EOF) {
190 printflags = parse_printflags(optarg);
193 repairflags = parse_repairflags(optarg);
196 gendump_path = optarg;
207 usage(1, "Invalid option!");
211 if (quiet && verbose)
212 usage(1, "Can't specify both -q and -v");
214 /* Parse non-option arguments */
215 if (argc - optind > 1)
216 usage(1, "Too many arguments!");
217 input_path = (argc == optind) ? "-" : argv[optind];
221 /* A callback to count and print errors */
223 my_error_cb(afs_uint32 code, int fatal, void *ref, char *msg, ...)
229 va_start(alist, msg);
230 afs_com_err_va(argv0, code, msg, alist);
237 /* A callback to print the path of a vnode. */
239 print_vnode_path(afs_vnode * v, XFILE * X, void *refcon)
244 /* Do repair, but only for known vnode types */
245 if (gendump_path && (!(v->field_mask & F_VNODE_TYPE)
246 || ((v->type != vFile) && (v->type != vDirectory)
247 && (v->type != vSymlink)))) {
248 r = repair_vnode_cb(v, X, refcon);
252 r = Path_Build(X, &phi, v->vnode, &name, 0);
254 printf(" Path: %s\n", name);
261 /* Setup for generating a repaired dump */
267 r = xfopen(&repair_output, O_RDWR | O_CREAT | O_TRUNC, gendump_path);
271 dp.cb_dumphdr = repair_dumphdr_cb;
272 dp.cb_volhdr = repair_volhdr_cb;
273 dp.cb_vnode_dir = repair_vnode_cb;
274 dp.cb_vnode_file = repair_vnode_cb;
275 dp.cb_vnode_link = repair_vnode_cb;
276 dp.cb_vnode_empty = repair_vnode_cb;
280 extern afs_uint32 DumpDumpEnd(XFILE * OX);
284 main(int argc, char **argv)
289 parse_options(argc, argv);
290 initialize_acfg_error_table();
291 initialize_AVds_error_table();
292 initialize_rxk_error_table();
293 initialize_u_error_table();
294 initialize_vl_error_table();
295 initialize_vols_error_table();
296 initialize_xFil_error_table();
297 r = xfopen(&input_file, O_RDONLY, input_path);
299 afs_com_err(argv0, r, "opening %s", input_path);
303 memset(&dp, 0, sizeof(dp));
304 dp.cb_error = my_error_cb;
305 dp.repair_flags = repairflags;
306 if (input_file.is_seekable)
307 dp.flags |= DSFLAG_SEEK;
311 "Repair modes available only for seekable dumps\n");
312 if (printflags & DSPRINT_PATH)
314 "Path-printing available only for seekable dumps\n");
315 if (repairflags || (printflags & DSPRINT_PATH))
319 if (gendump_path && (r = setup_repair())) {
320 afs_com_err(argv0, r, "setting up repair output");
321 xfclose(&input_file);
325 if (printflags & DSPRINT_PATH) {
328 dp.print_flags = printflags & DSPRINT_DEBUG;
329 memset(&phi, 0, sizeof(phi));
332 if ((r = xftell(&input_file, &where))
333 || (r = Path_PreScan(&input_file, &phi, 0))
334 || (r = xfseek(&input_file, &where))) {
335 afs_com_err(argv0, r, "- path initialization failed");
336 xfclose(&input_file);
340 dp.cb_vnode_dir = print_vnode_path;
341 dp.cb_vnode_file = print_vnode_path;
342 dp.cb_vnode_link = print_vnode_path;
343 dp.cb_vnode_empty = print_vnode_path;
344 dp.cb_vnode_wierd = print_vnode_path;
347 dp.print_flags = printflags;
348 r = ParseDumpFile(&input_file, &dp);
349 xfclose(&input_file);
352 r = DumpDumpEnd(&repair_output);
354 r = xfclose(&repair_output);
356 xfclose(&repair_output);
359 if (verbose && error_count)
360 fprintf(stderr, "*** %d errors\n", error_count);
362 fprintf(stderr, "*** FAILED: %s\n", afs_error_message(r));