]> git.wh0rd.org - dump.git/blob - restore/symtab.c
Added CVS Id.
[dump.git] / restore / symtab.c
1 /*
2 * Ported to Linux's Second Extended File System as part of the
3 * dump and restore backup suit
4 * Remy Card <card@Linux.EU.Org>, 1994-1997
5 * Stelian Pop <pop@cybercable.fr>, 1999
6 *
7 */
8
9 /*
10 * Copyright (c) 1983, 1993
11 * The Regents of the University of California. All rights reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by the University of
24 * California, Berkeley and its contributors.
25 * 4. Neither the name of the University nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * SUCH DAMAGE.
40 *
41 * $Id: symtab.c,v 1.5 1999/10/11 13:31:13 stelian Exp $
42 */
43
44 /*
45 * These routines maintain the symbol table which tracks the state
46 * of the file system being restored. They provide lookup by either
47 * name or inode number. They also provide for creation, deletion,
48 * and renaming of entries. Because of the dynamic nature of pathnames,
49 * names should not be saved, but always constructed just before they
50 * are needed, by calling "myname".
51 */
52
53 #include <sys/param.h>
54 #include <sys/stat.h>
55
56 #ifdef __linux__
57 #include <sys/time.h>
58 #include <linux/ext2_fs.h>
59 #include <bsdcompat.h>
60 #else /* __linux__ */
61 #include <ufs/ufs/dinode.h>
62 #endif /* __linux__ */
63
64 #include <errno.h>
65 #include <compaterr.h>
66 #include <fcntl.h>
67 #include <stdio.h>
68 #include <stdlib.h>
69 #include <string.h>
70 #include <unistd.h>
71
72 #ifdef __linux__
73 #include <ext2fs/ext2fs.h>
74 #endif
75
76 #include "restore.h"
77 #include "extern.h"
78
79 /*
80 * The following variables define the inode symbol table.
81 * The primary hash table is dynamically allocated based on
82 * the number of inodes in the file system (maxino), scaled by
83 * HASHFACTOR. The variable "entry" points to the hash table;
84 * the variable "entrytblsize" indicates its size (in entries).
85 */
86 #define HASHFACTOR 5
87 static struct entry **entry;
88 static long entrytblsize;
89
90 static void addino __P((ino_t, struct entry *));
91 static struct entry *lookupparent __P((char *));
92 static void removeentry __P((struct entry *));
93
94 /*
95 * Look up an entry by inode number
96 */
97 struct entry *
98 lookupino(ino_t inum)
99 {
100 register struct entry *ep;
101
102 if (inum < WINO || inum >= maxino)
103 return (NULL);
104 for (ep = entry[inum % entrytblsize]; ep != NULL; ep = ep->e_next)
105 if (ep->e_ino == inum)
106 return (ep);
107 return (NULL);
108 }
109
110 /*
111 * Add an entry into the entry table
112 */
113 static void
114 addino(ino_t inum, struct entry *np)
115 {
116 struct entry **epp;
117
118 if (inum < WINO || inum >= maxino)
119 panic("addino: out of range %d\n", inum);
120 epp = &entry[inum % entrytblsize];
121 np->e_ino = inum;
122 np->e_next = *epp;
123 *epp = np;
124 if (dflag)
125 for (np = np->e_next; np != NULL; np = np->e_next)
126 if (np->e_ino == inum)
127 badentry(np, "duplicate inum");
128 }
129
130 /*
131 * Delete an entry from the entry table
132 */
133 void
134 deleteino(ino_t inum)
135 {
136 register struct entry *next;
137 struct entry **prev;
138
139 if (inum < WINO || inum >= maxino)
140 panic("deleteino: out of range %d\n", inum);
141 prev = &entry[inum % entrytblsize];
142 for (next = *prev; next != NULL; next = next->e_next) {
143 if (next->e_ino == inum) {
144 next->e_ino = 0;
145 *prev = next->e_next;
146 return;
147 }
148 prev = &next->e_next;
149 }
150 panic("deleteino: %d not found\n", inum);
151 }
152
153 /*
154 * Look up an entry by name
155 */
156 struct entry *
157 lookupname(char *name)
158 {
159 register struct entry *ep;
160 register char *np, *cp;
161 char buf[MAXPATHLEN];
162
163 cp = name;
164 for (ep = lookupino(ROOTINO); ep != NULL; ep = ep->e_entries) {
165 for (np = buf; *cp != '/' && *cp != '\0' &&
166 np < &buf[sizeof(buf)]; )
167 *np++ = *cp++;
168 if (np == &buf[sizeof(buf)])
169 break;
170 *np = '\0';
171 for ( ; ep != NULL; ep = ep->e_sibling)
172 if (strcmp(ep->e_name, buf) == 0)
173 break;
174 if (ep == NULL)
175 break;
176 if (*cp++ == '\0')
177 return (ep);
178 }
179 return (NULL);
180 }
181
182 /*
183 * Look up the parent of a pathname
184 */
185 static struct entry *
186 lookupparent(char *name)
187 {
188 struct entry *ep;
189 char *tailindex;
190
191 tailindex = strrchr(name, '/');
192 if (tailindex == NULL)
193 return (NULL);
194 *tailindex = '\0';
195 ep = lookupname(name);
196 *tailindex = '/';
197 if (ep == NULL)
198 return (NULL);
199 if (ep->e_type != NODE)
200 panic("%s is not a directory\n", name);
201 return (ep);
202 }
203
204 /*
205 * Determine the current pathname of a node or leaf
206 */
207 char *
208 myname(struct entry *ep)
209 {
210 register char *cp;
211 static char namebuf[MAXPATHLEN];
212
213 for (cp = &namebuf[MAXPATHLEN - 2]; cp > &namebuf[ep->e_namlen]; ) {
214 cp -= ep->e_namlen;
215 memmove(cp, ep->e_name, (size_t)ep->e_namlen);
216 if (ep == lookupino(ROOTINO))
217 return (cp);
218 *(--cp) = '/';
219 ep = ep->e_parent;
220 }
221 panic("%s: pathname too long\n", cp);
222 return(cp);
223 }
224
225 /*
226 * Unused symbol table entries are linked together on a free list
227 * headed by the following pointer.
228 */
229 static struct entry *freelist = NULL;
230
231 /*
232 * add an entry to the symbol table
233 */
234 struct entry *
235 addentry(char *name, ino_t inum, int type)
236 {
237 register struct entry *np, *ep;
238
239 if (freelist != NULL) {
240 np = freelist;
241 freelist = np->e_next;
242 memset(np, 0, sizeof(struct entry));
243 } else {
244 np = (struct entry *)calloc(1, sizeof(struct entry));
245 if (np == NULL)
246 panic("no memory to extend symbol table\n");
247 }
248 np->e_type = type & ~LINK;
249 ep = lookupparent(name);
250 if (ep == NULL) {
251 if (inum != ROOTINO || lookupino(ROOTINO) != NULL)
252 panic("bad name to addentry %s\n", name);
253 np->e_name = savename(name);
254 np->e_namlen = strlen(name);
255 np->e_parent = np;
256 addino(ROOTINO, np);
257 return (np);
258 }
259 np->e_name = savename(strrchr(name, '/') + 1);
260 np->e_namlen = strlen(np->e_name);
261 np->e_parent = ep;
262 np->e_sibling = ep->e_entries;
263 ep->e_entries = np;
264 if (type & LINK) {
265 ep = lookupino(inum);
266 if (ep == NULL)
267 panic("link to non-existent name\n");
268 np->e_ino = inum;
269 np->e_links = ep->e_links;
270 ep->e_links = np;
271 } else if (inum != 0) {
272 if (lookupino(inum) != NULL)
273 panic("duplicate entry\n");
274 addino(inum, np);
275 }
276 return (np);
277 }
278
279 /*
280 * delete an entry from the symbol table
281 */
282 void
283 freeentry(struct entry *ep)
284 {
285 register struct entry *np;
286 ino_t inum;
287
288 if (ep->e_flags != REMOVED)
289 badentry(ep, "not marked REMOVED");
290 if (ep->e_type == NODE) {
291 if (ep->e_links != NULL)
292 badentry(ep, "freeing referenced directory");
293 if (ep->e_entries != NULL)
294 badentry(ep, "freeing non-empty directory");
295 }
296 if (ep->e_ino != 0) {
297 np = lookupino(ep->e_ino);
298 if (np == NULL)
299 badentry(ep, "lookupino failed");
300 if (np == ep) {
301 inum = ep->e_ino;
302 deleteino(inum);
303 if (ep->e_links != NULL)
304 addino(inum, ep->e_links);
305 } else {
306 for (; np != NULL; np = np->e_links) {
307 if (np->e_links == ep) {
308 np->e_links = ep->e_links;
309 break;
310 }
311 }
312 if (np == NULL)
313 badentry(ep, "link not found");
314 }
315 }
316 removeentry(ep);
317 freename(ep->e_name);
318 ep->e_next = freelist;
319 freelist = ep;
320 }
321
322 /*
323 * Relocate an entry in the tree structure
324 */
325 void
326 moveentry(struct entry *ep, char *newname)
327 {
328 struct entry *np;
329 char *cp;
330
331 np = lookupparent(newname);
332 if (np == NULL)
333 badentry(ep, "cannot move ROOT");
334 if (np != ep->e_parent) {
335 removeentry(ep);
336 ep->e_parent = np;
337 ep->e_sibling = np->e_entries;
338 np->e_entries = ep;
339 }
340 cp = strrchr(newname, '/') + 1;
341 freename(ep->e_name);
342 ep->e_name = savename(cp);
343 ep->e_namlen = strlen(cp);
344 if (strcmp(gentempname(ep), ep->e_name) == 0)
345 ep->e_flags |= TMPNAME;
346 else
347 ep->e_flags &= ~TMPNAME;
348 }
349
350 /*
351 * Remove an entry in the tree structure
352 */
353 static void
354 removeentry(struct entry *ep)
355 {
356 register struct entry *np;
357
358 np = ep->e_parent;
359 if (np->e_entries == ep) {
360 np->e_entries = ep->e_sibling;
361 } else {
362 for (np = np->e_entries; np != NULL; np = np->e_sibling) {
363 if (np->e_sibling == ep) {
364 np->e_sibling = ep->e_sibling;
365 break;
366 }
367 }
368 if (np == NULL)
369 badentry(ep, "cannot find entry in parent list");
370 }
371 }
372
373 /*
374 * Table of unused string entries, sorted by length.
375 *
376 * Entries are allocated in STRTBLINCR sized pieces so that names
377 * of similar lengths can use the same entry. The value of STRTBLINCR
378 * is chosen so that every entry has at least enough space to hold
379 * a "struct strtbl" header. Thus every entry can be linked onto an
380 * appropriate free list.
381 *
382 * NB. The macro "allocsize" below assumes that "struct strhdr"
383 * has a size that is a power of two.
384 */
385 struct strhdr {
386 struct strhdr *next;
387 };
388
389 #define STRTBLINCR (sizeof(struct strhdr))
390 #define allocsize(size) (((size) + 1 + STRTBLINCR - 1) & ~(STRTBLINCR - 1))
391
392 static struct strhdr strtblhdr[allocsize(NAME_MAX) / STRTBLINCR];
393
394 /*
395 * Allocate space for a name. It first looks to see if it already
396 * has an appropriate sized entry, and if not allocates a new one.
397 */
398 char *
399 savename(char *name)
400 {
401 struct strhdr *np;
402 long len;
403 char *cp;
404
405 if (name == NULL)
406 panic("bad name\n");
407 len = strlen(name);
408 np = strtblhdr[len / STRTBLINCR].next;
409 if (np != NULL) {
410 strtblhdr[len / STRTBLINCR].next = np->next;
411 cp = (char *)np;
412 } else {
413 cp = malloc((unsigned)allocsize(len));
414 if (cp == NULL)
415 panic("no space for string table\n");
416 }
417 (void) strcpy(cp, name);
418 return (cp);
419 }
420
421 /*
422 * Free space for a name. The resulting entry is linked onto the
423 * appropriate free list.
424 */
425 void
426 freename(char *name)
427 {
428 struct strhdr *tp, *np;
429
430 tp = &strtblhdr[strlen(name) / STRTBLINCR];
431 np = (struct strhdr *)name;
432 np->next = tp->next;
433 tp->next = np;
434 }
435
436 /*
437 * Useful quantities placed at the end of a dumped symbol table.
438 */
439 struct symtableheader {
440 int32_t volno;
441 int32_t stringsize;
442 int32_t entrytblsize;
443 time_t dumptime;
444 time_t dumpdate;
445 ino_t maxino;
446 int32_t ntrec;
447 };
448
449 /*
450 * dump a snapshot of the symbol table
451 */
452 void
453 dumpsymtable(char *filename, long checkpt)
454 {
455 register struct entry *ep, *tep;
456 register ino_t i;
457 struct entry temp, *tentry;
458 long mynum = 1, stroff = 0;
459 FILE *fd;
460 struct symtableheader hdr;
461
462 Vprintf(stdout, "Check pointing the restore\n");
463 if (Nflag)
464 return;
465 if ((fd = fopen(filename, "w")) == NULL) {
466 warn("fopen");
467 panic("cannot create save file %s for symbol table\n",
468 filename);
469 }
470 clearerr(fd);
471 /*
472 * Assign indices to each entry
473 * Write out the string entries
474 */
475 for (i = WINO; i <= maxino; i++) {
476 for (ep = lookupino(i); ep != NULL; ep = ep->e_links) {
477 ep->e_index = mynum++;
478 (void) fwrite(ep->e_name, sizeof(char),
479 (int)allocsize(ep->e_namlen), fd);
480 }
481 }
482 /*
483 * Convert pointers to indexes, and output
484 */
485 tep = &temp;
486 stroff = 0;
487 for (i = WINO; i <= maxino; i++) {
488 for (ep = lookupino(i); ep != NULL; ep = ep->e_links) {
489 memmove(tep, ep, sizeof(struct entry));
490 tep->e_name = (char *)stroff;
491 stroff += allocsize(ep->e_namlen);
492 tep->e_parent = (struct entry *)ep->e_parent->e_index;
493 if (ep->e_links != NULL)
494 tep->e_links =
495 (struct entry *)ep->e_links->e_index;
496 if (ep->e_sibling != NULL)
497 tep->e_sibling =
498 (struct entry *)ep->e_sibling->e_index;
499 if (ep->e_entries != NULL)
500 tep->e_entries =
501 (struct entry *)ep->e_entries->e_index;
502 if (ep->e_next != NULL)
503 tep->e_next =
504 (struct entry *)ep->e_next->e_index;
505 (void) fwrite((char *)tep, sizeof(struct entry), 1, fd);
506 }
507 }
508 /*
509 * Convert entry pointers to indexes, and output
510 */
511 for (i = 0; i < entrytblsize; i++) {
512 if (entry[i] == NULL)
513 tentry = NULL;
514 else
515 tentry = (struct entry *)entry[i]->e_index;
516 (void) fwrite((char *)&tentry, sizeof(struct entry *), 1, fd);
517 }
518 hdr.volno = checkpt;
519 hdr.maxino = maxino;
520 hdr.entrytblsize = entrytblsize;
521 hdr.stringsize = stroff;
522 hdr.dumptime = dumptime;
523 hdr.dumpdate = dumpdate;
524 hdr.ntrec = ntrec;
525 (void) fwrite((char *)&hdr, sizeof(struct symtableheader), 1, fd);
526 if (ferror(fd)) {
527 warn("fwrite");
528 panic("output error to file %s writing symbol table\n",
529 filename);
530 }
531 (void) fclose(fd);
532 }
533
534 /*
535 * Initialize a symbol table from a file
536 */
537 void
538 initsymtable(char *filename)
539 {
540 char *base;
541 long tblsize;
542 register struct entry *ep;
543 struct entry *baseep, *lep;
544 struct symtableheader hdr;
545 struct stat stbuf;
546 register long i;
547 int fd;
548
549 Vprintf(stdout, "Initialize symbol table.\n");
550 if (filename == NULL) {
551 entrytblsize = maxino / HASHFACTOR;
552 entry = (struct entry **)
553 calloc((unsigned)entrytblsize, sizeof(struct entry *));
554 if (entry == (struct entry **)NULL)
555 panic("no memory for entry table\n");
556 ep = addentry(".", ROOTINO, NODE);
557 ep->e_flags |= NEW;
558 return;
559 }
560 if ((fd = open(filename, O_RDONLY, 0)) < 0) {
561 warn("open");
562 panic("cannot open symbol table file %s\n", filename);
563 }
564 if (fstat(fd, &stbuf) < 0) {
565 warn("stat");
566 panic("cannot stat symbol table file %s\n", filename);
567 }
568 tblsize = stbuf.st_size - sizeof(struct symtableheader);
569 base = calloc(sizeof(char), (unsigned)tblsize);
570 if (base == NULL)
571 panic("cannot allocate space for symbol table\n");
572 if (read(fd, base, (int)tblsize) < 0 ||
573 read(fd, (char *)&hdr, sizeof(struct symtableheader)) < 0) {
574 warn("read");
575 panic("cannot read symbol table file %s\n", filename);
576 }
577 switch (command) {
578 case 'r':
579 /*
580 * For normal continuation, insure that we are using
581 * the next incremental tape
582 */
583 if (hdr.dumpdate != dumptime)
584 errx(1, "Incremental tape too %s",
585 (hdr.dumpdate < dumptime) ? "low" : "high");
586 break;
587 case 'R':
588 /*
589 * For restart, insure that we are using the same tape
590 */
591 curfile.action = SKIP;
592 dumptime = hdr.dumptime;
593 dumpdate = hdr.dumpdate;
594 if (!bflag)
595 newtapebuf(hdr.ntrec);
596 getvol(hdr.volno);
597 break;
598 default:
599 panic("initsymtable called from command %c\n", command);
600 break;
601 }
602 maxino = hdr.maxino;
603 entrytblsize = hdr.entrytblsize;
604 entry = (struct entry **)
605 (base + tblsize - (entrytblsize * sizeof(struct entry *)));
606 baseep = (struct entry *)(base + hdr.stringsize - sizeof(struct entry));
607 lep = (struct entry *)entry;
608 for (i = 0; i < entrytblsize; i++) {
609 if (entry[i] == NULL)
610 continue;
611 entry[i] = &baseep[(long)entry[i]];
612 }
613 for (ep = &baseep[1]; ep < lep; ep++) {
614 ep->e_name = base + (long)ep->e_name;
615 ep->e_parent = &baseep[(long)ep->e_parent];
616 if (ep->e_sibling != NULL)
617 ep->e_sibling = &baseep[(long)ep->e_sibling];
618 if (ep->e_links != NULL)
619 ep->e_links = &baseep[(long)ep->e_links];
620 if (ep->e_entries != NULL)
621 ep->e_entries = &baseep[(long)ep->e_entries];
622 if (ep->e_next != NULL)
623 ep->e_next = &baseep[(long)ep->e_next];
624 }
625 }