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 /* pathname.c - Pathname lookup and traversal */
35 #include "dumpscan_errs.h"
37 /* Hash function for a vnode */
38 #define BUCKET_SIZE 32
39 #define vnode_hash(phi,vnode) ((vnode) & ((1 << (phi)->hash_size) - 1))
43 get_vhash_ent(path_hashinfo * phi, afs_uint32 vnode, int make)
45 int key = vnode_hash(phi, vnode);
48 for (vhe = phi->hash_table[key]; vhe && vhe->vnode != vnode;
51 vhe = (vhash_ent *) malloc(sizeof(vhash_ent));
53 memset(vhe, 0, sizeof(vhash_ent));
55 vhe->next = phi->hash_table[key];
56 phi->hash_table[key] = vhe;
64 volhdr_cb(afs_vol_header * hdr, XFILE * X, void *refcon)
66 path_hashinfo *phi = (path_hashinfo *) refcon;
69 if (hdr->field_mask & F_VOLHDR_NFILES) {
70 nfiles = phi->n_vnodes = hdr->nfiles;
71 for (phi->hash_size = 1; nfiles > BUCKET_SIZE;
72 phi->hash_size++, nfiles >>= 1);
73 hsize = (1 << phi->hash_size);
74 phi->hash_table = (vhash_ent **) malloc(hsize * sizeof(vhash_ent *));
77 memset(phi->hash_table, 0, hsize * sizeof(vhash_ent *));
81 (phi->p->cb_error) (DSERR_FMT, 1, phi->p->err_refcon,
82 "File count missing from volume header");
89 vnode_keep(afs_vnode * v, XFILE * X, void *refcon)
91 path_hashinfo *phi = (path_hashinfo *) refcon;
94 if (!phi->hash_table) {
96 (phi->p->cb_error) (DSERR_FMT, 1, phi->p->refcon,
97 "No volume header in dump???");
100 vhe = get_vhash_ent(phi, v->vnode, 1);
103 cp64(vhe->v_offset, v->offset);
104 if (v->field_mask & F_VNODE_PARENT)
105 vhe->parent = v->parent;
106 if (v->field_mask & F_VNODE_DATA) {
107 cp64(vhe->d_offset, v->d_offset);
108 vhe->d_size = v->size;
110 if ((v->field_mask & F_VNODE_TYPE) && v->type == vDirectory)
119 vnode_stop(afs_vnode * v, XFILE * X, void *refcon)
121 path_hashinfo *phi = (path_hashinfo *) refcon;
124 /* If the file is seekable, try to position so we can pick up later... */
125 if (phi->p->flags & DSFLAG_SEEK)
126 if ((r = xfseek(X, &v->offset)))
133 dirent_cb(afs_vnode * v, afs_dir_entry * de, XFILE * X, void *refcon)
135 path_hashinfo *phi = (path_hashinfo *) refcon;
138 if (!phi->hash_table) {
139 if (phi->p->cb_error)
140 (phi->p->cb_error) (DSERR_FMT, 1, phi->p->refcon,
141 "No volume header in dump???");
144 if (!strcmp(de->name, ".") || !strcmp(de->name, ".."))
146 vhe = get_vhash_ent(phi, de->vnode, 1);
149 vhe->parent = v->vnode;
154 /* Prescan the vnodes in a dump file, collecting information that will
155 * be useful in generating and following pathnames.
158 Path_PreScan(XFILE * X, path_hashinfo * phi, int full)
160 dump_parser my_p, *p = phi->p;
162 memset(phi, 0, sizeof(path_hashinfo));
164 memset(&my_p, 0, sizeof(my_p));
165 my_p.refcon = (void *)phi;
166 my_p.cb_volhdr = volhdr_cb;
167 my_p.cb_vnode_dir = vnode_keep;
169 my_p.cb_vnode_file = vnode_keep;
170 my_p.cb_vnode_link = vnode_keep;
171 my_p.cb_vnode_empty = vnode_keep;
172 my_p.cb_vnode_wierd = vnode_keep;
174 my_p.cb_vnode_file = vnode_stop;
175 my_p.cb_vnode_link = vnode_stop;
176 my_p.cb_vnode_empty = vnode_stop;
177 my_p.cb_vnode_wierd = vnode_stop;
179 my_p.err_refcon = p->err_refcon;
180 my_p.cb_error = p->cb_error;
181 my_p.cb_dirent = dirent_cb;
182 my_p.flags = p->flags;
183 my_p.print_flags = p->print_flags;
184 my_p.repair_flags = p->repair_flags;
186 return ParseDumpFile(X, &my_p);
190 /* Free the hash table in a path_hashinfo */
192 Path_FreeHashTable(path_hashinfo * phi)
195 vhash_ent *vhe, *next_vhe;
197 if (phi->hash_table) {
198 size = (1 << phi->hash_size);
199 for (i = 0; i < size; i++)
200 for (vhe = phi->hash_table[i]; vhe; vhe = next_vhe) {
201 next_vhe = vhe->next;
204 free(phi->hash_table);
209 /* Follow a pathname to the vnode it represents */
211 Path_Follow(XFILE * X, path_hashinfo * phi, char *path, vhash_ent * his_vhe)
215 afs_uint32 r, vnum = 1;
219 name = strtok(path, "/");
221 for (name = strtok(path, "/"); name; name = strtok(0, "/")) {
223 if (phi->p->cb_error)
224 (phi->p->cb_error) (ENOTDIR, 1, phi->p->err_refcon,
225 "Not a directory vnode");
228 vhe = get_vhash_ent(phi, vnum, 0);
230 if (phi->p->cb_error)
231 (phi->p->cb_error) (DSERR_FMT, 1, phi->p->err_refcon,
232 "Vnode %d not found in hash table", vnum);
235 if (zero64(vhe->d_offset)) {
236 if (phi->p->cb_error)
237 (phi->p->cb_error) (DSERR_FMT, 1, phi->p->err_refcon,
238 "Directory vnode %d is incomplete", vnum);
241 if ((r = xfseek(X, &vhe->d_offset))) {
242 if (phi->p->cb_error)
243 (phi->p->cb_error) (r, 1, phi->p->err_refcon,
244 "Unable to seek to directory %d", vnum);
248 r = DirectoryLookup(X, phi->p, vhe->d_size, &name, &vnum, 0);
252 if (phi->p->cb_error)
253 (phi->p->cb_error) (ENOENT, 1, phi->p->err_refcon,
258 vhe = get_vhash_ent(phi, vnum, 0);
260 if (phi->p->cb_error)
261 (phi->p->cb_error) (DSERR_FMT, 1, phi->p->err_refcon,
262 "Vnode %d not found in hash table", vnum);
272 Path_Build(XFILE * X, path_hashinfo * phi, afs_uint32 vnode, char **his_path,
276 char *name, *path = 0, fastbuf[12];
278 afs_uint32 parent, r;
282 *his_path = strdup("/");
284 if (phi->p->cb_error)
285 (phi->p->cb_error) (ENOMEM, 1, phi->p->err_refcon,
286 "No memory for pathname of vnode 1");
293 vhe = get_vhash_ent(phi, vnode, 0);
295 if (phi->p->cb_error)
296 (phi->p->cb_error) (DSERR_FMT, 1, phi->p->err_refcon,
297 "Vnode %d not found in hash table", vnode);
301 /* Find the parent */
303 if (phi->p->cb_error)
304 (phi->p->cb_error) (DSERR_FMT, 1, phi->p->err_refcon,
305 "Vnode %d has no parent?", vnode);
310 parent = vhe->parent;
311 vhe = get_vhash_ent(phi, parent, 0);
312 if (phi->p->print_flags & DSPRINT_DEBUG)
313 fprintf(stderr, "Searching for vnode %d in parent %d\n", vnode,
316 if (phi->p->cb_error)
317 (phi->p->cb_error) (DSERR_FMT, 1, phi->p->err_refcon,
318 "Vnode %d not found in hash table",
326 /* Make up a path component from the vnode number */
327 sprintf(fastbuf, "%d", vnode);
330 /* Do a reverse-lookup in the parent directory */
331 if (zero64(vhe->d_offset)) {
332 if (phi->p->cb_error)
333 (phi->p->cb_error) (DSERR_FMT, 1, phi->p->err_refcon,
334 "Directory vnode %d is incomplete",
340 if ((r = xfseek(X, &vhe->d_offset))) {
341 if (phi->p->cb_error)
342 (phi->p->cb_error) (errno, 1, phi->p->err_refcon,
343 "Unable to seek to directory %d",
351 r = DirectoryLookup(X, phi->p, vhe->d_size, &name, &vnode, 0);
355 if (phi->p->cb_error)
356 (phi->p->cb_error) (DSERR_FMT, 1, phi->p->err_refcon,
357 "No entry for vnode %d in directory %d",
367 path = (char *)realloc(path, nl + pl + 2);
369 if (phi->p->cb_error)
370 (phi->p->cb_error) (ENOMEM, 1, phi->p->err_refcon,
371 "No memory for pathname of vnode 1");
379 for (x = name, y = path + 1; *x;)
383 path = (char *)malloc(nl + 2);
385 if (phi->p->cb_error)
386 (phi->p->cb_error) (ENOMEM, 1, phi->p->err_refcon,
387 "No memory for pathname of vnode 1");
391 strcpy(path + 1, name);