-
Notifications
You must be signed in to change notification settings - Fork 1
/
null-search.c
189 lines (164 loc) · 5.02 KB
/
null-search.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
* CMUCS AFStools
* dumpscan - routines for scanning and manipulating AFS volume dumps
*
* Copyright (c) 1998, 2001 Carnegie Mellon University
* All Rights Reserved.
*
* Permission to use, copy, modify and distribute this software and its
* documentation is hereby granted, provided that both the copyright
* notice and this permission notice appear in all copies of the
* software, derivative works or modified versions, and any portions
* thereof, and that both notices appear in supporting documentation.
*
* CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
* CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
* ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
*
* Carnegie Mellon requests users of this software to return to
*
* Software Distribution Coordinator or Software_Distribution@CS.CMU.EDU
* School of Computer Science
* Carnegie Mellon University
* Pittsburgh PA 15213-3890
*
* any improvements or extensions that they make and grant Carnegie Mellon
* the rights to redistribute these changes.
*/
/* null-search.c - search for nulls in data files */
#include <sys/fcntl.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <afs/stds.h>
#include <afs/com_err.h>
#include <rx/rxkad.h>
#include <ubik.h>
#include <afs/cellconfig.h>
#include <afs/afsint.h>
#include <afs/volser.h>
#include <afs/vlserver.h>
#include "dumpscan.h"
char *argv0;
static char *input_path = 0;
static int quiet = 0, showpaths = 0, searchcount = 1;
static int error_count = 0, bad_count = 0;
static path_hashinfo phi;
static dump_parser dp;
/* Print a usage message and exit */
static void usage(int status, char *msg)
{
if (msg) fprintf(stderr, "%s: %s\n", argv0, msg);
fprintf(stderr, "Usage: %s [options] [file]\n", argv0);
fprintf(stderr, " -h Print this help message\n");
fprintf(stderr, " -p Print paths of bad vnodes\n");
fprintf(stderr, " -q Quiet mode (don't print errors)\n");
exit(status);
}
/* Parse the command-line options */
static void parse_options(int argc, char **argv)
{
int c;
if (argv0 = strrchr(argv[0], '/')) argv0++;
else argv0 = argv[0];
/* Parse the options */
while ((c = getopt(argc, argv, "n:hpq")) != EOF) {
switch (c) {
case 'n': searchcount = atoi(optarg); continue;
case 'p': showpaths = 1; continue;
case 'q': quiet = 1; continue;
case 'h': usage(0, 0);
default: usage(1, "Invalid option!");
}
}
if (argc - optind > 1) usage(1, "Too many arguments!");
input_path = (argc == optind) ? "-" : argv[optind];
}
/* A callback to count and print errors */
static afs_uint32 my_error_cb(afs_uint32 code, int fatal, void *ref, char *msg, ...)
{
va_list alist;
error_count++;
if (!quiet) {
va_start(alist, msg);
afs_com_err_va(argv0, code, msg, alist);
va_end(alist);
}
}
/* A callback to process file vnodes */
static afs_uint32 my_file_cb(afs_vnode *v, XFILE *X, void *refcon)
{
static char buf[1024];
afs_uint32 size, nulls, cnulls, maxcnulls, n, r;
char *name = 0;
int i;
nulls = cnulls = maxcnulls = 0;
size = v->size;
if ((r = xfseek(X, &v->d_offset))) return r;
while (size) {
n = (size > 1024) ? 1024 : size;
if (r = xfread(X, buf, n)) return r;
for (i = 0; i < n; i++) {
if (buf[i]) {
if (cnulls > maxcnulls) maxcnulls = cnulls;
cnulls = 0;
} else {
nulls++;
cnulls++;
}
}
size -= n;
}
if (maxcnulls >= searchcount) {
bad_count++;
if (showpaths) Path_Build(X, &phi, v->vnode, &name, 0);
if (name) {
printf("*** BAD %d (%s) - %d nulls, %d consecutive\n",
v->vnode, name, nulls, maxcnulls);
free(name);
} else {
printf("*** BAD %d - %d nulls, %d consecutive\n",
v->vnode, nulls, maxcnulls);
}
}
return r;
}
int main(int argc, char **argv)
{
XFILE input_file;
afs_uint32 r;
parse_options(argc, argv);
initialize_acfg_error_table();
initialize_AVds_error_table();
initialize_rxk_error_table();
initialize_u_error_table();
initialize_vl_error_table();
initialize_vols_error_table();
initialize_xFil_error_table();
r = xfopen(&input_file, O_RDONLY, input_path);
if (r) {
afs_com_err(argv0, r, "opening %s", input_path);
exit(2);
}
memset(&dp, 0, sizeof(dp));
dp.cb_error = my_error_cb;
if (input_file.is_seekable) dp.flags |= DSFLAG_SEEK;
if (showpaths) {
u_int64 where;
memset(&phi, 0, sizeof(phi));
phi.p = &dp;
if ((r = xftell(&input_file, &where))
|| (r = Path_PreScan(&input_file, &phi, 0))
|| (r = xfseek(&input_file, &where))) {
afs_com_err(argv0, r, "- path initialization failed");
xfclose(&input_file);
exit(2);
}
}
dp.cb_vnode_file = my_file_cb;
r = ParseDumpFile(&input_file, &dp);
xfclose(&input_file);
if (error_count) printf("*** %d errors\n", error_count);
if (bad_count) printf("*** %d bad files\n", bad_count);
if (r && !quiet) printf("*** FAILED: %s\n", afs_error_message(r));
}