]>
git.wh0rd.org - dump.git/blob - restore/symtab.c
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. 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.
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
43 static const char rcsid
[] =
44 "$Id: symtab.c,v 1.20 2002/11/15 09:25:42 stelian Exp $";
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".
57 #include <sys/param.h>
63 #ifdef HAVE_EXT2FS_EXT2_FS_H
64 #include <ext2fs/ext2_fs.h>
66 #include <linux/ext2_fs.h>
68 #include <bsdcompat.h>
70 #include <ufs/ufs/dinode.h>
71 #endif /* __linux__ */
74 #include <compaterr.h>
82 #include <ext2fs/ext2fs.h>
89 * The following variables define the inode symbol table.
90 * The primary hash table is dynamically allocated based on
91 * the number of inodes in the file system (maxino), scaled by
92 * HASHFACTOR. The variable "entry" points to the hash table;
93 * the variable "entrytblsize" indicates its size (in entries).
96 static struct entry
**entry
;
97 static long entrytblsize
;
99 static void addino
__P((dump_ino_t
, struct entry
*));
100 static struct entry
*lookupparent
__P((char *));
101 static void removeentry
__P((struct entry
*));
104 * Look up an entry by inode number
107 lookupino(dump_ino_t inum
)
111 if (inum
< WINO
|| inum
>= maxino
)
113 for (ep
= entry
[inum
% entrytblsize
]; ep
!= NULL
; ep
= ep
->e_next
)
114 if (ep
->e_ino
== inum
)
120 * Add an entry into the entry table
123 addino(dump_ino_t inum
, struct entry
*np
)
127 if (inum
< WINO
|| inum
>= maxino
)
128 panic("addino: out of range %d\n", inum
);
129 epp
= &entry
[inum
% entrytblsize
];
134 for (np
= np
->e_next
; np
!= NULL
; np
= np
->e_next
)
135 if (np
->e_ino
== inum
)
136 badentry(np
, "duplicate inum");
140 * Delete an entry from the entry table
143 deleteino(dump_ino_t inum
)
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
) {
154 *prev
= next
->e_next
;
157 prev
= &next
->e_next
;
159 panic("deleteino: %d not found\n", inum
);
163 * Look up an entry by name
166 lookupname(char *name
)
170 char buf
[MAXPATHLEN
];
173 for (ep
= lookupino(ROOTINO
); ep
!= NULL
; ep
= ep
->e_entries
) {
174 for (np
= buf
; *cp
!= '/' && *cp
!= '\0' &&
175 np
< &buf
[sizeof(buf
)]; )
177 if (np
== &buf
[sizeof(buf
)])
180 for ( ; ep
!= NULL
; ep
= ep
->e_sibling
)
181 if (strcmp(ep
->e_name
, buf
) == 0)
192 * Look up the parent of a pathname
194 static struct entry
*
195 lookupparent(char *name
)
200 tailindex
= strrchr(name
, '/');
201 if (tailindex
== NULL
)
204 ep
= lookupname(name
);
208 if (ep
->e_type
!= NODE
)
209 panic("%s is not a directory\n", name
);
214 * Determine the current pathname of a node or leaf
217 myname(struct entry
*ep
)
220 static char namebuf
[MAXPATHLEN
];
222 for (cp
= &namebuf
[MAXPATHLEN
- 2]; cp
> &namebuf
[ep
->e_namlen
]; ) {
224 memmove(cp
, ep
->e_name
, (size_t)ep
->e_namlen
);
225 if (ep
== lookupino(ROOTINO
))
230 panic("%s: pathname too long\n", cp
);
235 * Unused symbol table entries are linked together on a free list
236 * headed by the following pointer.
238 static struct entry
*freelist
= NULL
;
241 * add an entry to the symbol table
244 addentry(char *name
, dump_ino_t inum
, int type
)
246 struct entry
*np
, *ep
;
248 if (freelist
!= NULL
) {
250 freelist
= np
->e_next
;
251 memset(np
, 0, sizeof(struct entry
));
253 np
= (struct entry
*)calloc(1, sizeof(struct entry
));
255 errx(1, "no memory to extend symbol table");
257 np
->e_type
= type
& ~LINK
;
258 ep
= lookupparent(name
);
260 if (inum
!= ROOTINO
|| lookupino(ROOTINO
) != NULL
)
261 panic("bad name to addentry %s\n", name
);
262 np
->e_name
= savename(name
);
263 np
->e_namlen
= strlen(name
);
268 np
->e_name
= savename(strrchr(name
, '/') + 1);
269 np
->e_namlen
= strlen(np
->e_name
);
271 np
->e_sibling
= ep
->e_entries
;
274 ep
= lookupino(inum
);
276 panic("link to non-existent name\n");
278 np
->e_links
= ep
->e_links
;
280 } else if (inum
!= 0) {
281 if (lookupino(inum
) != NULL
)
282 panic("duplicate entry\n");
289 * delete an entry from the symbol table
292 freeentry(struct entry
*ep
)
297 if (ep
->e_flags
!= REMOVED
)
298 badentry(ep
, "not marked REMOVED");
299 if (ep
->e_type
== NODE
) {
300 if (ep
->e_links
!= NULL
)
301 badentry(ep
, "freeing referenced directory");
302 if (ep
->e_entries
!= NULL
)
303 badentry(ep
, "freeing non-empty directory");
305 if (ep
->e_ino
!= 0) {
306 np
= lookupino(ep
->e_ino
);
308 badentry(ep
, "lookupino failed");
312 if (ep
->e_links
!= NULL
)
313 addino(inum
, ep
->e_links
);
315 for (; np
!= NULL
; np
= np
->e_links
) {
316 if (np
->e_links
== ep
) {
317 np
->e_links
= ep
->e_links
;
322 badentry(ep
, "link not found");
326 freename(ep
->e_name
);
327 ep
->e_next
= freelist
;
332 * Relocate an entry in the tree structure
335 moveentry(struct entry
*ep
, char *newname
)
340 np
= lookupparent(newname
);
342 badentry(ep
, "cannot move ROOT");
343 if (np
!= ep
->e_parent
) {
346 ep
->e_sibling
= np
->e_entries
;
349 cp
= strrchr(newname
, '/') + 1;
350 freename(ep
->e_name
);
351 ep
->e_name
= savename(cp
);
352 ep
->e_namlen
= strlen(cp
);
353 if (strcmp(gentempname(ep
), ep
->e_name
) == 0)
354 ep
->e_flags
|= TMPNAME
;
356 ep
->e_flags
&= ~TMPNAME
;
360 * Remove an entry in the tree structure
363 removeentry(struct entry
*ep
)
368 if (np
->e_entries
== ep
) {
369 np
->e_entries
= ep
->e_sibling
;
371 for (np
= np
->e_entries
; np
!= NULL
; np
= np
->e_sibling
) {
372 if (np
->e_sibling
== ep
) {
373 np
->e_sibling
= ep
->e_sibling
;
378 badentry(ep
, "cannot find entry in parent list");
383 * Table of unused string entries, sorted by length.
385 * Entries are allocated in STRTBLINCR sized pieces so that names
386 * of similar lengths can use the same entry. The value of STRTBLINCR
387 * is chosen so that every entry has at least enough space to hold
388 * a "struct strtbl" header. Thus every entry can be linked onto an
389 * appropriate free list.
391 * NB. The macro "allocsize" below assumes that "struct strhdr"
392 * has a size that is a power of two.
398 #define STRTBLINCR (sizeof(struct strhdr))
399 #define allocsize(size) (((size) + 1 + STRTBLINCR - 1) & ~(STRTBLINCR - 1))
401 static struct strhdr strtblhdr
[allocsize(MAXNAMLEN
) / STRTBLINCR
];
404 * Allocate space for a name. It first looks to see if it already
405 * has an appropriate sized entry, and if not allocates a new one.
417 np
= strtblhdr
[len
/ STRTBLINCR
].next
;
419 strtblhdr
[len
/ STRTBLINCR
].next
= np
->next
;
422 cp
= malloc((unsigned)allocsize(len
));
424 errx(1, "no space for string table");
426 (void) strcpy(cp
, name
);
431 * Free space for a name. The resulting entry is linked onto the
432 * appropriate free list.
437 struct strhdr
*tp
, *np
;
439 tp
= &strtblhdr
[strlen(name
) / STRTBLINCR
];
440 np
= (struct strhdr
*)name
;
446 * Useful quantities placed at the end of a dumped symbol table.
448 struct symtableheader
{
451 int32_t entrytblsize
;
460 * dump a snapshot of the symbol table
463 dumpsymtable(char *filename
, long checkpt
)
465 struct entry
*ep
, *tep
;
467 struct entry temp
, *tentry
;
468 long mynum
= 1, stroff
= 0;
470 struct symtableheader hdr
;
472 Vprintf(stdout
, "Check pointing the restore\n");
475 if ((fd
= fopen(filename
, "w")) == NULL
) {
477 panic("cannot create save file %s for symbol table\n",
482 * Assign indices to each entry
483 * Write out the string entries
485 for (i
= WINO
; i
<= maxino
; i
++) {
486 for (ep
= lookupino(i
); ep
!= NULL
; ep
= ep
->e_links
) {
487 ep
->e_index
= mynum
++;
488 (void) fwrite(ep
->e_name
, sizeof(char),
489 (int)allocsize(ep
->e_namlen
), fd
);
493 * Convert pointers to indexes, and output
497 for (i
= WINO
; i
<= maxino
; i
++) {
498 for (ep
= lookupino(i
); ep
!= NULL
; ep
= ep
->e_links
) {
499 memmove(tep
, ep
, sizeof(struct entry
));
500 tep
->e_name
= (char *)stroff
;
501 stroff
+= allocsize(ep
->e_namlen
);
502 tep
->e_parent
= (struct entry
*)ep
->e_parent
->e_index
;
503 if (ep
->e_links
!= NULL
)
505 (struct entry
*)ep
->e_links
->e_index
;
506 if (ep
->e_sibling
!= NULL
)
508 (struct entry
*)ep
->e_sibling
->e_index
;
509 if (ep
->e_entries
!= NULL
)
511 (struct entry
*)ep
->e_entries
->e_index
;
512 if (ep
->e_next
!= NULL
)
514 (struct entry
*)ep
->e_next
->e_index
;
515 (void) fwrite((char *)tep
, sizeof(struct entry
), 1, fd
);
519 * Convert entry pointers to indexes, and output
521 for (i
= 0; (long)i
< entrytblsize
; i
++) {
522 if (entry
[i
] == NULL
)
525 tentry
= (struct entry
*)entry
[i
]->e_index
;
526 (void) fwrite((char *)&tentry
, sizeof(struct entry
*), 1, fd
);
530 hdr
.entrytblsize
= entrytblsize
;
531 hdr
.stringsize
= stroff
;
532 hdr
.dumptime
= dumptime
;
533 hdr
.dumpdate
= dumpdate
;
536 (void) fwrite((char *)&hdr
, sizeof(struct symtableheader
), 1, fd
);
539 panic("output error to file %s writing symbol table\n",
546 * Initialize a symbol table from a file
549 initsymtable(char *filename
)
554 struct entry
*baseep
, *lep
;
555 struct symtableheader hdr
;
560 Vprintf(stdout
, "Initialize symbol table.\n");
561 if (filename
== NULL
) {
562 entrytblsize
= maxino
/ HASHFACTOR
;
563 entry
= (struct entry
**)
564 calloc((unsigned)entrytblsize
, sizeof(struct entry
*));
565 if (entry
== (struct entry
**)NULL
)
566 errx(1, "no memory for entry table");
567 ep
= addentry(".", ROOTINO
, NODE
);
571 if ((fd
= open(filename
, O_RDONLY
, 0)) < 0) {
573 errx(1, "cannot open symbol table file %s", filename
);
575 if (fstat(fd
, &stbuf
) < 0) {
577 errx(1, "cannot stat symbol table file %s", filename
);
579 tblsize
= stbuf
.st_size
- sizeof(struct symtableheader
);
580 base
= calloc(sizeof(char), (unsigned)tblsize
);
582 errx(1, "cannot allocate space for symbol table");
583 if (read(fd
, base
, (int)tblsize
) < 0 ||
584 read(fd
, (char *)&hdr
, sizeof(struct symtableheader
)) < 0) {
586 errx(1, "cannot read symbol table file %s", filename
);
591 * For normal continuation, insure that we are using
592 * the next incremental tape
594 if (hdr
.dumpdate
!= dumptime
)
595 errx(1, "Incremental tape too %s",
596 (hdr
.dumpdate
< dumptime
) ? "low" : "high");
600 * For restart, insure that we are using the same tape
602 curfile
.action
= SKIP
;
603 dumptime
= hdr
.dumptime
;
604 dumpdate
= hdr
.dumpdate
;
607 newtapebuf(hdr
.ntrec
);
611 panic("initsymtable called from command %c\n", command
);
614 resizemaps(maxino
, hdr
.maxino
);
616 entrytblsize
= hdr
.entrytblsize
;
617 entry
= (struct entry
**)
618 (base
+ tblsize
- (entrytblsize
* sizeof(struct entry
*)));
619 baseep
= (struct entry
*)(base
+ hdr
.stringsize
- sizeof(struct entry
));
620 lep
= (struct entry
*)entry
;
621 for (i
= 0; i
< entrytblsize
; i
++) {
622 if (entry
[i
] == NULL
)
624 entry
[i
] = &baseep
[(long)entry
[i
]];
626 for (ep
= &baseep
[1]; ep
< lep
; ep
++) {
627 ep
->e_name
= base
+ (long)ep
->e_name
;
628 ep
->e_parent
= &baseep
[(long)ep
->e_parent
];
629 if (ep
->e_sibling
!= NULL
)
630 ep
->e_sibling
= &baseep
[(long)ep
->e_sibling
];
631 if (ep
->e_links
!= NULL
)
632 ep
->e_links
= &baseep
[(long)ep
->e_links
];
633 if (ep
->e_entries
!= NULL
)
634 ep
->e_entries
= &baseep
[(long)ep
->e_entries
];
635 if (ep
->e_next
!= NULL
)
636 ep
->e_next
= &baseep
[(long)ep
->e_next
];