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