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