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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 1995-2003 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* module:
* debug.c
*
* purpose:
* utility routines for debugging filesync (tracing, diagnostics,
* and error simulation)
*
* contents:
* showflags display a word of flags symbolicly
* dbg_usage printout usage info for -D switch
* err_usage printout usage info for -E switch
* dbg_set_error enable an error simulation
* dbg_check_error check for error simulation
*
*
* note:
* there are numerous flag words and bit fields in this
* program, and it would be horrendous to just print them
* out in hex (in debugging output). These routines use
* a "flaglist" data structure to map between bits and
* character string names or descriptions.
*
* a flaglist is merely a list of paired bits and name strings.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include "filesync.h"
#include "database.h"
#include "debug.h"
/* bits in opt_debug for usage message */
static struct flaglist dbgflags[] =
{ DBG_BASE, "BASE: base include building",
DBG_RULE, "RULE: rule tree building",
DBG_STAT, "STAT: file stats",
DBG_ANAL, "ANAL: difference analysis",
DBG_RECON, "RECO: reconciliation list processing",
DBG_VARS, "VARS: qualification and expansion",
DBG_FILES, "FILE: rule and baseline files",
DBG_LIST, "LIST: tree building",
DBG_EVAL, "EVAL: tree walking",
DBG_IGNORE, "IGNO: ignore list",
DBG_MISC, "MISC: everything else",
0, 0
};
/* bits in opt_debug for dsiplay */
struct flaglist dbgmap[] =
{ DBG_BASE, "BASE",
DBG_RULE, "RULE",
DBG_STAT, "STAT",
DBG_ANAL, "ANAL",
DBG_RECON, "RECO",
DBG_VARS, "VARS",
DBG_FILES, "FILE",
DBG_LIST, "LIST",
DBG_EVAL, "EVAL",
DBG_IGNORE, "IGNO",
DBG_MISC, "MISC",
0, 0
};
/* bits in the rules flag field */
struct flaglist rflags[] =
{ R_IGNORE, "IGNORE",
R_PROGRAM, "PROGRAM",
R_WILD, "WILD",
R_NEW, "NEW",
R_BOGUS, "BOGUS",
R_RESTRICT, "RESTRICT",
0, 0
};
/* bits in the files flag field */
struct flaglist fileflags[] =
{ F_NEW, "new",
F_IN_BASELINE, "base",
F_IN_SOURCE, "srce",
F_IN_DEST, "dest",
F_EVALUATE, "eval",
F_SPARSE, "sparse",
F_REMOVE, "remove",
F_CONFLICT, "conflict",
F_LISTED, "listed",
F_STAT_ERROR, "statfail",
0, 0
};
/* bits in the file src/dst difference mask */
struct flaglist diffmap[] = {
D_CREATE, "create",
D_DELETE, "delete",
D_MTIME, "modtime",
D_SIZE, "size",
D_UID, "uid",
D_GID, "gid",
D_PROT, "modes",
D_LINKS, "links",
D_TYPE, "type",
D_FACLS, "facls",
D_RENAME_TO, "rename2",
D_RENAME_FROM, "renamed",
0, 0
};
/* bits in the exit error code mask */
struct flaglist errmap[] = {
ERR_RESOLVABLE, "resolvable",
ERR_UNRESOLVED, "unresolvable",
ERR_MISSING, "missing files",
ERR_PERM, "permissions",
ERR_FILES, "rule/base errors",
ERR_INVAL, "invalid arguments",
ERR_NOBASE, "bad base dir",
ERR_OTHER, "other",
0, 0
};
/*
* routine:
* showflags
*
* purpose:
* format flags for printing
*
* parameters:
* pointer to map
* mask to be interpreted \
*
* returns:
* pointer to a static buffer
*/
char *
showflags(struct flaglist *map, long mask)
{ int i;
static char outbuf[MAX_NAME];
outbuf[0] = 0;
for (i = 0; map[i].fl_mask; i++)
if (mask & map[i].fl_mask) {
if (outbuf[0])
strcat(outbuf, "|");
strcat(outbuf, map[i].fl_name);
}
return (outbuf);
}
/*
* routines:
* dbg_usage, err_usage
*
* purpose:
* to print out usage messages for the secret debugging flags
*
* returns:
* void
*/
void
dbg_usage(void)
{ int i;
fprintf(stderr, "Usage:\tfilesync -Dmask ...\n");
for (i = 0; dbgflags[i].fl_mask; i++)
fprintf(stderr, "\t0x%04lx .... %s\n",
dbgflags[i].fl_mask, dbgflags[i].fl_name);
fprintf(stderr, "\n");
}
#ifdef DBG_ERRORS
/*
* The -E flag is a debugging feature that enables the user to request
* the simulation of difficult to trigger error conditions in order
* to test out the error handling code in filesync. We maintain a
* registry that specifies a file name and an operation, and an errno
* to be returned if the specified operation is attempted on the
* specified file.
*/
void
err_usage(void)
{
fprintf(stderr, "Usage:\tfilesync -E<errno>,<code>,<filename>\n");
fprintf(stderr, "\ts ... eval stat source\n");
fprintf(stderr, "\tS ... eval stat destination\n");
fprintf(stderr, "\tn ... eval nftw source\n");
fprintf(stderr, "\tN ... eval nftw destination\n");
fprintf(stderr, "\tc ... reconcile copy create\n");
fprintf(stderr, "\to ... reconcile copy open\n");
fprintf(stderr, "\tr ... reconcile copy read/readlink\n");
fprintf(stderr, "\tw ... reconcile copy write\n");
fprintf(stderr, "\tl ... reconcile link/symlink\n");
fprintf(stderr, "\tu ... reconcile unlink\n");
fprintf(stderr, "\td ... reconcile mkdir/mknod\n");
fprintf(stderr, "\tD ... reconcile rmdir\n");
fprintf(stderr, "\tm ... reconcile rename\n");
fprintf(stderr, "\tR ... reconcile restat\n");
fprintf(stderr, "\tp ... reconcile protection (chmod)");
fprintf(stderr, "\ta ... reconcile access control (setfacl)");
fprintf(stderr, "\tO ... reconcile ownership (chown)");
fprintf(stderr, "\tZ ... out of space on target\n");
fprintf(stderr, "\n");
}
/*
* this data structure us used to keep track of the error simulations
* that have been requested.
*/
static struct errsim {
int Errno; /* error number to return */
char code; /* event triggering the error */
char *file; /* file name triggering error */
} errsim[ DBG_MAX_ERR ];
static int num_errs; /* number of simulated errors */
/*
* routine:
* dbg_set_error
*
* purpose:
* note that we have been requested to simulate file access errors
*
* parameters:
* argument string <errno>,<errcode>,<filename>
*
* returns:
* error mask
*/
int
dbg_set_error(char *arg)
{ char *s;
char error_type;
int error_no;
if (num_errs >= DBG_MAX_ERR) {
fprintf(stderr, "ERROR: only %d -E specifications allowed\n",
DBG_MAX_ERR);
return (ERR_INVAL);
}
/* get the error number */
if (!isdigit(arg[0]))
return (ERR_INVAL);
error_no = strtol(arg, &s, 0);
/* get the error condition */
if (*s++ != ',' || !isalpha(*s))
return (ERR_INVAL);
error_type = *s;
/* get the file name */
while (*s && *s != ',') s++;
if (*s++ != ',' || *s == 0)
return (ERR_INVAL);
/* register the error simulation */
errsim[num_errs].Errno = error_no;
errsim[num_errs].code = error_type;
errsim[num_errs].file = s;
if (opt_debug & DBG_MISC)
fprintf(stderr, "MISC: errsim[%d] %c(%s) -> %d\n",
num_errs, error_type, s, error_no);
num_errs++;
return (0);
}
/*
* routine:
* dbg_chk_error
*
* purpose:
* determine whether or not we have been asked to simulate an
* error for a specified file.
*
* parameters:
* file name
*
* returns:
* errno (or zero if no error)
*/
int
dbg_chk_error(const char *name, char code)
{ int i;
for (i = 0; i < num_errs; i++) {
/* see if this code matches any registered condition */
if (code != errsim[i].code)
continue;
/* see if this also matches the file name */
if (!suffix(name, errsim[i].file))
continue;
/* we have a winner */
if (opt_debug & DBG_MISC)
fprintf(stderr, "MISC: trigger %d for file %c(%s)\n",
errsim[i].Errno, code, name);
return (errsim[i].Errno);
}
return (0);
}
#else /* ! DBG_ERRORS */
void
err_usage(void)
{
fprintf(stderr, "ERROR: this filesync does not support -E\n");
}
int
dbg_set_error(char *arg)
{
return (ERR_INVAL);
}
int
dbg_chk_error(const char *name, char code)
{
return (0);
}
#endif
|