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