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