Import Upstream version 1.8.5
[hcoop/debian/openafs.git] / src / tools / dumpscan / afsdump_dirlist.c
1 /*
2 * CMUCS AFStools
3 * dumpscan - routines for scanning and manipulating AFS volume dumps
4 *
5 * Copyright (c) 1998 Carnegie Mellon University
6 * All Rights Reserved.
7 *
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.
13 *
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.
17 *
18 * Carnegie Mellon requests users of this software to return to
19 *
20 * Software Distribution Coordinator or Software_Distribution@CS.CMU.EDU
21 * School of Computer Science
22 * Carnegie Mellon University
23 * Pittsburgh PA 15213-3890
24 *
25 * any improvements or extensions that they make and grant Carnegie Mellon
26 * the rights to redistribute these changes.
27 */
28
29 /* afsdump_dirlist.c - List an AFS directory file */
30
31 #include <afsconfig.h>
32 #include <afs/param.h>
33
34 #include <roken.h>
35
36 #include <afs/stds.h>
37 #include <afs/com_err.h>
38 #include <afs/cellconfig.h>
39 #include <afs/vlserver.h>
40 #include <afs/afsint.h>
41 #include <afs/volser.h>
42 #include <rx/rxkad.h>
43
44 #include "dumpscan.h"
45 #include "dumpscan_errs.h"
46 #include "xf_errs.h"
47
48 extern int optind;
49 extern char *optarg;
50
51 char *argv0;
52 static char *input_path;
53 static int quiet, verbose, error_count;
54
55 static dump_parser dp;
56
57
58 /* Print a usage message and exit */
59 static void
60 usage(int status, char *msg)
61 {
62 if (msg)
63 fprintf(stderr, "%s: %s\n", argv0, msg);
64 fprintf(stderr, "Usage: %s [options] [file]\n", argv0);
65 fprintf(stderr, " -h Print this help message\n");
66 fprintf(stderr, " -q Quiet mode (don't print errors)\n");
67 fprintf(stderr, " -v Verbose mode\n");
68 exit(status);
69 }
70
71
72 /* Parse the command-line options */
73 static void
74 parse_options(int argc, char **argv)
75 {
76 int c;
77
78 /* Set the program name */
79 if ((argv0 = strrchr(argv[0], '/')) != NULL)
80 argv0++;
81 else
82 argv0 = argv[0];
83
84 /* Initialize options */
85 input_path = 0;
86 quiet = verbose = 0;
87
88 /* Initialize other stuff */
89 error_count = 0;
90
91 /* Parse the options */
92 while ((c = getopt(argc, argv, "hqv")) != EOF) {
93 switch (c) {
94 case 'q':
95 quiet = 1;
96 continue;
97 case 'v':
98 verbose = 1;
99 continue;
100 case 'h':
101 usage(0, 0);
102 default:
103 usage(1, "Invalid option!");
104 }
105 }
106
107 if (quiet && verbose)
108 usage(1, "Can't specify both -q and -v");
109
110 /* Parse non-option arguments */
111 if (argc - optind > 1)
112 usage(1, "Too many arguments!");
113 input_path = (argc == optind) ? "-" : argv[optind];
114 }
115
116
117 /* A callback to count and print errors */
118 static afs_uint32
119 my_error_cb(afs_uint32 code, int fatal, void *ref, char *msg, ...)
120 {
121 va_list alist;
122
123 error_count++;
124 if (!quiet) {
125 va_start(alist, msg);
126 afs_com_err_va(argv0, code, msg, alist);
127 va_end(alist);
128 }
129 return 0;
130 }
131
132
133 /* Main program */
134 int
135 main(int argc, char **argv)
136 {
137 XFILE input_file;
138 afs_uint32 r;
139
140 parse_options(argc, argv);
141 initialize_acfg_error_table();
142 initialize_AVds_error_table();
143 initialize_rxk_error_table();
144 initialize_u_error_table();
145 initialize_vl_error_table();
146 initialize_vols_error_table();
147 initialize_xFil_error_table();
148 r = xfopen(&input_file, O_RDONLY, input_path);
149 if (r) {
150 afs_com_err(argv0, r, "opening %s", input_path);
151 exit(2);
152 }
153
154 memset(&dp, 0, sizeof(dp));
155 dp.cb_error = my_error_cb;
156 dp.print_flags = DSPRINT_DIR;
157 if (input_file.is_seekable)
158 dp.flags |= DSFLAG_SEEK;
159
160 r = ParseDirectory(&input_file, &dp, 0, 1);
161 xfclose(&input_file);
162
163 if (verbose && error_count)
164 fprintf(stderr, "*** %d errors\n", error_count);
165 if (r && !quiet)
166 fprintf(stderr, "*** FAILED: %s\n", afs_error_message(r));
167 return 0;
168 }