bb0d2443da48bbf25871f606158b9a48efbf14e7
[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/volser.h>
41 #include <rx/rxkad.h>
42
43 #include "dumpscan.h"
44 #include "dumpscan_errs.h"
45 #include "xf_errs.h"
46
47 extern int optind;
48 extern char *optarg;
49
50 char *argv0;
51 static char *input_path;
52 static int quiet, verbose, error_count;
53
54 static dump_parser dp;
55
56
57 /* Print a usage message and exit */
58 static void
59 usage(int status, char *msg)
60 {
61     if (msg)
62         fprintf(stderr, "%s: %s\n", argv0, msg);
63     fprintf(stderr, "Usage: %s [options] [file]\n", argv0);
64     fprintf(stderr, "  -h     Print this help message\n");
65     fprintf(stderr, "  -q     Quiet mode (don't print errors)\n");
66     fprintf(stderr, "  -v     Verbose mode\n");
67     exit(status);
68 }
69
70
71 /* Parse the command-line options */
72 static void
73 parse_options(int argc, char **argv)
74 {
75     int c;
76
77     /* Set the program name */
78     if ((argv0 = strrchr(argv[0], '/')) != NULL)
79         argv0++;
80     else
81         argv0 = argv[0];
82
83     /* Initialize options */
84     input_path = 0;
85     quiet = verbose = 0;
86
87     /* Initialize other stuff */
88     error_count = 0;
89
90     /* Parse the options */
91     while ((c = getopt(argc, argv, "hqv")) != EOF) {
92         switch (c) {
93         case 'q':
94             quiet = 1;
95             continue;
96         case 'v':
97             verbose = 1;
98             continue;
99         case 'h':
100             usage(0, 0);
101         default:
102             usage(1, "Invalid option!");
103         }
104     }
105
106     if (quiet && verbose)
107         usage(1, "Can't specify both -q and -v");
108
109     /* Parse non-option arguments */
110     if (argc - optind > 1)
111         usage(1, "Too many arguments!");
112     input_path = (argc == optind) ? "-" : argv[optind];
113 }
114
115
116 /* A callback to count and print errors */
117 static afs_uint32
118 my_error_cb(afs_uint32 code, int fatal, void *ref, char *msg, ...)
119 {
120     va_list alist;
121
122     error_count++;
123     if (!quiet) {
124         va_start(alist, msg);
125         afs_com_err_va(argv0, code, msg, alist);
126         va_end(alist);
127     }
128     return 0;
129 }
130
131
132 /* Main program */
133 int
134 main(int argc, char **argv)
135 {
136     XFILE input_file;
137     afs_uint32 r;
138
139     parse_options(argc, argv);
140     initialize_acfg_error_table();
141     initialize_AVds_error_table();
142     initialize_rxk_error_table();
143     initialize_u_error_table();
144     initialize_vl_error_table();
145     initialize_vols_error_table();
146     initialize_xFil_error_table();
147     r = xfopen(&input_file, O_RDONLY, input_path);
148     if (r) {
149         afs_com_err(argv0, r, "opening %s", input_path);
150         exit(2);
151     }
152
153     memset(&dp, 0, sizeof(dp));
154     dp.cb_error = my_error_cb;
155     dp.print_flags = DSPRINT_DIR;
156     if (input_file.is_seekable)
157         dp.flags |= DSFLAG_SEEK;
158
159     r = ParseDirectory(&input_file, &dp, 0, 1);
160     xfclose(&input_file);
161
162     if (verbose && error_count)
163         fprintf(stderr, "*** %d errors\n", error_count);
164     if (r && !quiet)
165         fprintf(stderr, "*** FAILED: %s\n", afs_error_message(r));
166     return 0;
167 }