]>
git.wh0rd.org - dump.git/blob - restore/symtab.c
5bd5e67c45b8b43958f674ffdf2422fe900c7cc0
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 <stelian@popies.net>, 1999-2000
6 * Stelian Pop <stelian@popies.net> - AlcĂ´ve <www.alcove.com>, 2000-2002
10 * Copyright (c) 1983, 1993
11 * The Regents of the University of California. All rights reserved.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 static const char rcsid
[] =
40 "$Id: symtab.c,v 1.21 2003/03/30 15:40:40 stelian Exp $";
44 * These routines maintain the symbol table which tracks the state
45 * of the file system being restored. They provide lookup by either
46 * name or inode number. They also provide for creation, deletion,
47 * and renaming of entries. Because of the dynamic nature of pathnames,
48 * names should not be saved, but always constructed just before they
49 * are needed, by calling "myname".
53 #include <sys/param.h>
59 #ifdef HAVE_EXT2FS_EXT2_FS_H
60 #include <ext2fs/ext2_fs.h>
62 #include <linux/ext2_fs.h>
64 #include <bsdcompat.h>
66 #include <ufs/ufs/dinode.h>
67 #endif /* __linux__ */
70 #include <compaterr.h>
78 #include <ext2fs/ext2fs.h>
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).
92 static struct entry
**entry
;
93 static long entrytblsize
;
95 static void addino
__P((dump_ino_t
, struct entry
*));
96 static struct entry
*lookupparent
__P((char *));
97 static void removeentry
__P((struct entry
*));
100 * Look up an entry by inode number
103 lookupino(dump_ino_t inum
)
107 if (inum
< WINO
|| inum
>= maxino
)
109 for (ep
= entry
[inum
% entrytblsize
]; ep
!= NULL
; ep
= ep
->e_next
)
110 if (ep
->e_ino
== inum
)
116 * Add an entry into the entry table
119 addino(dump_ino_t inum
, struct entry
*np
)
123 if (inum
< WINO
|| inum
>= maxino
)
124 panic("addino: out of range %d\n", inum
);
125 epp
= &entry
[inum
% entrytblsize
];
130 for (np
= np
->e_next
; np
!= NULL
; np
= np
->e_next
)
131 if (np
->e_ino
== inum
)
132 badentry(np
, "duplicate inum");
136 * Delete an entry from the entry table
139 deleteino(dump_ino_t inum
)
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
) {
150 *prev
= next
->e_next
;
153 prev
= &next
->e_next
;
155 panic("deleteino: %d not found\n", inum
);
159 * Look up an entry by name
162 lookupname(char *name
)
166 char buf
[MAXPATHLEN
];
169 for (ep
= lookupino(ROOTINO
); ep
!= NULL
; ep
= ep
->e_entries
) {
170 for (np
= buf
; *cp
!= '/' && *cp
!= '\0' &&
171 np
< &buf
[sizeof(buf
)]; )
173 if (np
== &buf
[sizeof(buf
)])
176 for ( ; ep
!= NULL
; ep
= ep
->e_sibling
)
177 if (strcmp(ep
->e_name
, buf
) == 0)
188 * Look up the parent of a pathname
190 static struct entry
*
191 lookupparent(char *name
)
196 tailindex
= strrchr(name
, '/');
197 if (tailindex
== NULL
)
200 ep
= lookupname(name
);
204 if (ep
->e_type
!= NODE
)
205 panic("%s is not a directory\n", name
);
210 * Determine the current pathname of a node or leaf
213 myname(struct entry
*ep
)
216 static char namebuf
[MAXPATHLEN
];
218 for (cp
= &namebuf
[MAXPATHLEN
- 2]; cp
> &namebuf
[ep
->e_namlen
]; ) {
220 memmove(cp
, ep
->e_name
, (size_t)ep
->e_namlen
);
221 if (ep
== lookupino(ROOTINO
))
226 panic("%s: pathname too long\n", cp
);
231 * Unused symbol table entries are linked together on a free list
232 * headed by the following pointer.
234 static struct entry
*freelist
= NULL
;
237 * add an entry to the symbol table
240 addentry(char *name
, dump_ino_t inum
, int type
)
242 struct entry
*np
, *ep
;
244 if (freelist
!= NULL
) {
246 freelist
= np
->e_next
;
247 memset(np
, 0, sizeof(struct entry
));
249 np
= (struct entry
*)calloc(1, sizeof(struct entry
));
251 errx(1, "no memory to extend symbol table");
253 np
->e_type
= type
& ~LINK
;
254 ep
= lookupparent(name
);
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
);
264 np
->e_name
= savename(strrchr(name
, '/') + 1);
265 np
->e_namlen
= strlen(np
->e_name
);
267 np
->e_sibling
= ep
->e_entries
;
270 ep
= lookupino(inum
);
272 panic("link to non-existent name\n");
274 np
->e_links
= ep
->e_links
;
276 } else if (inum
!= 0) {
277 if (lookupino(inum
) != NULL
)
278 panic("duplicate entry\n");
285 * delete an entry from the symbol table
288 freeentry(struct entry
*ep
)
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");
301 if (ep
->e_ino
!= 0) {
302 np
= lookupino(ep
->e_ino
);
304 badentry(ep
, "lookupino failed");
308 if (ep
->e_links
!= NULL
)
309 addino(inum
, ep
->e_links
);
311 for (; np
!= NULL
; np
= np
->e_links
) {
312 if (np
->e_links
== ep
) {
313 np
->e_links
= ep
->e_links
;
318 badentry(ep
, "link not found");
322 freename(ep
->e_name
);
323 ep
->e_next
= freelist
;
328 * Relocate an entry in the tree structure
331 moveentry(struct entry
*ep
, char *newname
)
336 np
= lookupparent(newname
);
338 badentry(ep
, "cannot move ROOT");
339 if (np
!= ep
->e_parent
) {
342 ep
->e_sibling
= np
->e_entries
;
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
;
352 ep
->e_flags
&= ~TMPNAME
;
356 * Remove an entry in the tree structure
359 removeentry(struct entry
*ep
)
364 if (np
->e_entries
== ep
) {
365 np
->e_entries
= ep
->e_sibling
;
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
;
374 badentry(ep
, "cannot find entry in parent list");
379 * Table of unused string entries, sorted by length.
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.
387 * NB. The macro "allocsize" below assumes that "struct strhdr"
388 * has a size that is a power of two.
394 #define STRTBLINCR (sizeof(struct strhdr))
395 #define allocsize(size) (((size) + 1 + STRTBLINCR - 1) & ~(STRTBLINCR - 1))
397 static struct strhdr strtblhdr
[allocsize(MAXNAMLEN
) / STRTBLINCR
];
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.
413 np
= strtblhdr
[len
/ STRTBLINCR
].next
;
415 strtblhdr
[len
/ STRTBLINCR
].next
= np
->next
;
418 cp
= malloc((unsigned)allocsize(len
));
420 errx(1, "no space for string table");
422 (void) strcpy(cp
, name
);
427 * Free space for a name. The resulting entry is linked onto the
428 * appropriate free list.
433 struct strhdr
*tp
, *np
;
435 tp
= &strtblhdr
[strlen(name
) / STRTBLINCR
];
436 np
= (struct strhdr
*)name
;
442 * Useful quantities placed at the end of a dumped symbol table.
444 struct symtableheader
{
447 int32_t entrytblsize
;
456 * dump a snapshot of the symbol table
459 dumpsymtable(char *filename
, long checkpt
)
461 struct entry
*ep
, *tep
;
463 struct entry temp
, *tentry
;
464 long mynum
= 1, stroff
= 0;
466 struct symtableheader hdr
;
468 Vprintf(stdout
, "Check pointing the restore\n");
471 if ((fd
= fopen(filename
, "w")) == NULL
) {
473 panic("cannot create save file %s for symbol table\n",
478 * Assign indices to each entry
479 * Write out the string entries
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
);
489 * Convert pointers to indexes, and output
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
)
501 (struct entry
*)ep
->e_links
->e_index
;
502 if (ep
->e_sibling
!= NULL
)
504 (struct entry
*)ep
->e_sibling
->e_index
;
505 if (ep
->e_entries
!= NULL
)
507 (struct entry
*)ep
->e_entries
->e_index
;
508 if (ep
->e_next
!= NULL
)
510 (struct entry
*)ep
->e_next
->e_index
;
511 (void) fwrite((char *)tep
, sizeof(struct entry
), 1, fd
);
515 * Convert entry pointers to indexes, and output
517 for (i
= 0; (long)i
< entrytblsize
; i
++) {
518 if (entry
[i
] == NULL
)
521 tentry
= (struct entry
*)entry
[i
]->e_index
;
522 (void) fwrite((char *)&tentry
, sizeof(struct entry
*), 1, fd
);
526 hdr
.entrytblsize
= entrytblsize
;
527 hdr
.stringsize
= stroff
;
528 hdr
.dumptime
= dumptime
;
529 hdr
.dumpdate
= dumpdate
;
532 (void) fwrite((char *)&hdr
, sizeof(struct symtableheader
), 1, fd
);
535 panic("output error to file %s writing symbol table\n",
542 * Initialize a symbol table from a file
545 initsymtable(char *filename
)
550 struct entry
*baseep
, *lep
;
551 struct symtableheader hdr
;
556 Vprintf(stdout
, "Initialize symbol table.\n");
557 if (filename
== NULL
) {
558 entrytblsize
= maxino
/ HASHFACTOR
;
559 entry
= (struct entry
**)
560 calloc((unsigned)entrytblsize
, sizeof(struct entry
*));
561 if (entry
== (struct entry
**)NULL
)
562 errx(1, "no memory for entry table");
563 ep
= addentry(".", ROOTINO
, NODE
);
567 if ((fd
= open(filename
, O_RDONLY
, 0)) < 0) {
569 errx(1, "cannot open symbol table file %s", filename
);
571 if (fstat(fd
, &stbuf
) < 0) {
573 errx(1, "cannot stat symbol table file %s", filename
);
575 tblsize
= stbuf
.st_size
- sizeof(struct symtableheader
);
576 base
= calloc(sizeof(char), (unsigned)tblsize
);
578 errx(1, "cannot allocate space for symbol table");
579 if (read(fd
, base
, (int)tblsize
) < 0 ||
580 read(fd
, (char *)&hdr
, sizeof(struct symtableheader
)) < 0) {
582 errx(1, "cannot read symbol table file %s", filename
);
587 * For normal continuation, insure that we are using
588 * the next incremental tape
590 if (hdr
.dumpdate
!= dumptime
)
591 errx(1, "Incremental tape too %s",
592 (hdr
.dumpdate
< dumptime
) ? "low" : "high");
596 * For restart, insure that we are using the same tape
598 curfile
.action
= SKIP
;
599 dumptime
= hdr
.dumptime
;
600 dumpdate
= hdr
.dumpdate
;
603 newtapebuf(hdr
.ntrec
);
607 panic("initsymtable called from command %c\n", command
);
610 resizemaps(maxino
, hdr
.maxino
);
612 entrytblsize
= hdr
.entrytblsize
;
613 entry
= (struct entry
**)
614 (base
+ tblsize
- (entrytblsize
* sizeof(struct entry
*)));
615 baseep
= (struct entry
*)(base
+ hdr
.stringsize
- sizeof(struct entry
));
616 lep
= (struct entry
*)entry
;
617 for (i
= 0; i
< entrytblsize
; i
++) {
618 if (entry
[i
] == NULL
)
620 entry
[i
] = &baseep
[(long)entry
[i
]];
622 for (ep
= &baseep
[1]; ep
< lep
; ep
++) {
623 ep
->e_name
= base
+ (long)ep
->e_name
;
624 ep
->e_parent
= &baseep
[(long)ep
->e_parent
];
625 if (ep
->e_sibling
!= NULL
)
626 ep
->e_sibling
= &baseep
[(long)ep
->e_sibling
];
627 if (ep
->e_links
!= NULL
)
628 ep
->e_links
= &baseep
[(long)ep
->e_links
];
629 if (ep
->e_entries
!= NULL
)
630 ep
->e_entries
= &baseep
[(long)ep
->e_entries
];
631 if (ep
->e_next
!= NULL
)
632 ep
->e_next
= &baseep
[(long)ep
->e_next
];