]>
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 <pop@cybercable.fr>, 1999
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
41 * $Id: symtab.c,v 1.5 1999/10/11 13:31:13 stelian Exp $
45 * These routines maintain the symbol table which tracks the state
46 * of the file system being restored. They provide lookup by either
47 * name or inode number. They also provide for creation, deletion,
48 * and renaming of entries. Because of the dynamic nature of pathnames,
49 * names should not be saved, but always constructed just before they
50 * are needed, by calling "myname".
53 #include <sys/param.h>
58 #include <linux/ext2_fs.h>
59 #include <bsdcompat.h>
61 #include <ufs/ufs/dinode.h>
62 #endif /* __linux__ */
65 #include <compaterr.h>
73 #include <ext2fs/ext2fs.h>
80 * The following variables define the inode symbol table.
81 * The primary hash table is dynamically allocated based on
82 * the number of inodes in the file system (maxino), scaled by
83 * HASHFACTOR. The variable "entry" points to the hash table;
84 * the variable "entrytblsize" indicates its size (in entries).
87 static struct entry
**entry
;
88 static long entrytblsize
;
90 static void addino
__P((ino_t
, struct entry
*));
91 static struct entry
*lookupparent
__P((char *));
92 static void removeentry
__P((struct entry
*));
95 * Look up an entry by inode number
100 register struct entry
*ep
;
102 if (inum
< WINO
|| inum
>= maxino
)
104 for (ep
= entry
[inum
% entrytblsize
]; ep
!= NULL
; ep
= ep
->e_next
)
105 if (ep
->e_ino
== inum
)
111 * Add an entry into the entry table
114 addino(ino_t inum
, struct entry
*np
)
118 if (inum
< WINO
|| inum
>= maxino
)
119 panic("addino: out of range %d\n", inum
);
120 epp
= &entry
[inum
% entrytblsize
];
125 for (np
= np
->e_next
; np
!= NULL
; np
= np
->e_next
)
126 if (np
->e_ino
== inum
)
127 badentry(np
, "duplicate inum");
131 * Delete an entry from the entry table
134 deleteino(ino_t inum
)
136 register struct entry
*next
;
139 if (inum
< WINO
|| inum
>= maxino
)
140 panic("deleteino: out of range %d\n", inum
);
141 prev
= &entry
[inum
% entrytblsize
];
142 for (next
= *prev
; next
!= NULL
; next
= next
->e_next
) {
143 if (next
->e_ino
== inum
) {
145 *prev
= next
->e_next
;
148 prev
= &next
->e_next
;
150 panic("deleteino: %d not found\n", inum
);
154 * Look up an entry by name
157 lookupname(char *name
)
159 register struct entry
*ep
;
160 register char *np
, *cp
;
161 char buf
[MAXPATHLEN
];
164 for (ep
= lookupino(ROOTINO
); ep
!= NULL
; ep
= ep
->e_entries
) {
165 for (np
= buf
; *cp
!= '/' && *cp
!= '\0' &&
166 np
< &buf
[sizeof(buf
)]; )
168 if (np
== &buf
[sizeof(buf
)])
171 for ( ; ep
!= NULL
; ep
= ep
->e_sibling
)
172 if (strcmp(ep
->e_name
, buf
) == 0)
183 * Look up the parent of a pathname
185 static struct entry
*
186 lookupparent(char *name
)
191 tailindex
= strrchr(name
, '/');
192 if (tailindex
== NULL
)
195 ep
= lookupname(name
);
199 if (ep
->e_type
!= NODE
)
200 panic("%s is not a directory\n", name
);
205 * Determine the current pathname of a node or leaf
208 myname(struct entry
*ep
)
211 static char namebuf
[MAXPATHLEN
];
213 for (cp
= &namebuf
[MAXPATHLEN
- 2]; cp
> &namebuf
[ep
->e_namlen
]; ) {
215 memmove(cp
, ep
->e_name
, (size_t)ep
->e_namlen
);
216 if (ep
== lookupino(ROOTINO
))
221 panic("%s: pathname too long\n", cp
);
226 * Unused symbol table entries are linked together on a free list
227 * headed by the following pointer.
229 static struct entry
*freelist
= NULL
;
232 * add an entry to the symbol table
235 addentry(char *name
, ino_t inum
, int type
)
237 register struct entry
*np
, *ep
;
239 if (freelist
!= NULL
) {
241 freelist
= np
->e_next
;
242 memset(np
, 0, sizeof(struct entry
));
244 np
= (struct entry
*)calloc(1, sizeof(struct entry
));
246 panic("no memory to extend symbol table\n");
248 np
->e_type
= type
& ~LINK
;
249 ep
= lookupparent(name
);
251 if (inum
!= ROOTINO
|| lookupino(ROOTINO
) != NULL
)
252 panic("bad name to addentry %s\n", name
);
253 np
->e_name
= savename(name
);
254 np
->e_namlen
= strlen(name
);
259 np
->e_name
= savename(strrchr(name
, '/') + 1);
260 np
->e_namlen
= strlen(np
->e_name
);
262 np
->e_sibling
= ep
->e_entries
;
265 ep
= lookupino(inum
);
267 panic("link to non-existent name\n");
269 np
->e_links
= ep
->e_links
;
271 } else if (inum
!= 0) {
272 if (lookupino(inum
) != NULL
)
273 panic("duplicate entry\n");
280 * delete an entry from the symbol table
283 freeentry(struct entry
*ep
)
285 register struct entry
*np
;
288 if (ep
->e_flags
!= REMOVED
)
289 badentry(ep
, "not marked REMOVED");
290 if (ep
->e_type
== NODE
) {
291 if (ep
->e_links
!= NULL
)
292 badentry(ep
, "freeing referenced directory");
293 if (ep
->e_entries
!= NULL
)
294 badentry(ep
, "freeing non-empty directory");
296 if (ep
->e_ino
!= 0) {
297 np
= lookupino(ep
->e_ino
);
299 badentry(ep
, "lookupino failed");
303 if (ep
->e_links
!= NULL
)
304 addino(inum
, ep
->e_links
);
306 for (; np
!= NULL
; np
= np
->e_links
) {
307 if (np
->e_links
== ep
) {
308 np
->e_links
= ep
->e_links
;
313 badentry(ep
, "link not found");
317 freename(ep
->e_name
);
318 ep
->e_next
= freelist
;
323 * Relocate an entry in the tree structure
326 moveentry(struct entry
*ep
, char *newname
)
331 np
= lookupparent(newname
);
333 badentry(ep
, "cannot move ROOT");
334 if (np
!= ep
->e_parent
) {
337 ep
->e_sibling
= np
->e_entries
;
340 cp
= strrchr(newname
, '/') + 1;
341 freename(ep
->e_name
);
342 ep
->e_name
= savename(cp
);
343 ep
->e_namlen
= strlen(cp
);
344 if (strcmp(gentempname(ep
), ep
->e_name
) == 0)
345 ep
->e_flags
|= TMPNAME
;
347 ep
->e_flags
&= ~TMPNAME
;
351 * Remove an entry in the tree structure
354 removeentry(struct entry
*ep
)
356 register struct entry
*np
;
359 if (np
->e_entries
== ep
) {
360 np
->e_entries
= ep
->e_sibling
;
362 for (np
= np
->e_entries
; np
!= NULL
; np
= np
->e_sibling
) {
363 if (np
->e_sibling
== ep
) {
364 np
->e_sibling
= ep
->e_sibling
;
369 badentry(ep
, "cannot find entry in parent list");
374 * Table of unused string entries, sorted by length.
376 * Entries are allocated in STRTBLINCR sized pieces so that names
377 * of similar lengths can use the same entry. The value of STRTBLINCR
378 * is chosen so that every entry has at least enough space to hold
379 * a "struct strtbl" header. Thus every entry can be linked onto an
380 * appropriate free list.
382 * NB. The macro "allocsize" below assumes that "struct strhdr"
383 * has a size that is a power of two.
389 #define STRTBLINCR (sizeof(struct strhdr))
390 #define allocsize(size) (((size) + 1 + STRTBLINCR - 1) & ~(STRTBLINCR - 1))
392 static struct strhdr strtblhdr
[allocsize(NAME_MAX
) / STRTBLINCR
];
395 * Allocate space for a name. It first looks to see if it already
396 * has an appropriate sized entry, and if not allocates a new one.
408 np
= strtblhdr
[len
/ STRTBLINCR
].next
;
410 strtblhdr
[len
/ STRTBLINCR
].next
= np
->next
;
413 cp
= malloc((unsigned)allocsize(len
));
415 panic("no space for string table\n");
417 (void) strcpy(cp
, name
);
422 * Free space for a name. The resulting entry is linked onto the
423 * appropriate free list.
428 struct strhdr
*tp
, *np
;
430 tp
= &strtblhdr
[strlen(name
) / STRTBLINCR
];
431 np
= (struct strhdr
*)name
;
437 * Useful quantities placed at the end of a dumped symbol table.
439 struct symtableheader
{
442 int32_t entrytblsize
;
450 * dump a snapshot of the symbol table
453 dumpsymtable(char *filename
, long checkpt
)
455 register struct entry
*ep
, *tep
;
457 struct entry temp
, *tentry
;
458 long mynum
= 1, stroff
= 0;
460 struct symtableheader hdr
;
462 Vprintf(stdout
, "Check pointing the restore\n");
465 if ((fd
= fopen(filename
, "w")) == NULL
) {
467 panic("cannot create save file %s for symbol table\n",
472 * Assign indices to each entry
473 * Write out the string entries
475 for (i
= WINO
; i
<= maxino
; i
++) {
476 for (ep
= lookupino(i
); ep
!= NULL
; ep
= ep
->e_links
) {
477 ep
->e_index
= mynum
++;
478 (void) fwrite(ep
->e_name
, sizeof(char),
479 (int)allocsize(ep
->e_namlen
), fd
);
483 * Convert pointers to indexes, and output
487 for (i
= WINO
; i
<= maxino
; i
++) {
488 for (ep
= lookupino(i
); ep
!= NULL
; ep
= ep
->e_links
) {
489 memmove(tep
, ep
, sizeof(struct entry
));
490 tep
->e_name
= (char *)stroff
;
491 stroff
+= allocsize(ep
->e_namlen
);
492 tep
->e_parent
= (struct entry
*)ep
->e_parent
->e_index
;
493 if (ep
->e_links
!= NULL
)
495 (struct entry
*)ep
->e_links
->e_index
;
496 if (ep
->e_sibling
!= NULL
)
498 (struct entry
*)ep
->e_sibling
->e_index
;
499 if (ep
->e_entries
!= NULL
)
501 (struct entry
*)ep
->e_entries
->e_index
;
502 if (ep
->e_next
!= NULL
)
504 (struct entry
*)ep
->e_next
->e_index
;
505 (void) fwrite((char *)tep
, sizeof(struct entry
), 1, fd
);
509 * Convert entry pointers to indexes, and output
511 for (i
= 0; i
< entrytblsize
; i
++) {
512 if (entry
[i
] == NULL
)
515 tentry
= (struct entry
*)entry
[i
]->e_index
;
516 (void) fwrite((char *)&tentry
, sizeof(struct entry
*), 1, fd
);
520 hdr
.entrytblsize
= entrytblsize
;
521 hdr
.stringsize
= stroff
;
522 hdr
.dumptime
= dumptime
;
523 hdr
.dumpdate
= dumpdate
;
525 (void) fwrite((char *)&hdr
, sizeof(struct symtableheader
), 1, fd
);
528 panic("output error to file %s writing symbol table\n",
535 * Initialize a symbol table from a file
538 initsymtable(char *filename
)
542 register struct entry
*ep
;
543 struct entry
*baseep
, *lep
;
544 struct symtableheader hdr
;
549 Vprintf(stdout
, "Initialize symbol table.\n");
550 if (filename
== NULL
) {
551 entrytblsize
= maxino
/ HASHFACTOR
;
552 entry
= (struct entry
**)
553 calloc((unsigned)entrytblsize
, sizeof(struct entry
*));
554 if (entry
== (struct entry
**)NULL
)
555 panic("no memory for entry table\n");
556 ep
= addentry(".", ROOTINO
, NODE
);
560 if ((fd
= open(filename
, O_RDONLY
, 0)) < 0) {
562 panic("cannot open symbol table file %s\n", filename
);
564 if (fstat(fd
, &stbuf
) < 0) {
566 panic("cannot stat symbol table file %s\n", filename
);
568 tblsize
= stbuf
.st_size
- sizeof(struct symtableheader
);
569 base
= calloc(sizeof(char), (unsigned)tblsize
);
571 panic("cannot allocate space for symbol table\n");
572 if (read(fd
, base
, (int)tblsize
) < 0 ||
573 read(fd
, (char *)&hdr
, sizeof(struct symtableheader
)) < 0) {
575 panic("cannot read symbol table file %s\n", filename
);
580 * For normal continuation, insure that we are using
581 * the next incremental tape
583 if (hdr
.dumpdate
!= dumptime
)
584 errx(1, "Incremental tape too %s",
585 (hdr
.dumpdate
< dumptime
) ? "low" : "high");
589 * For restart, insure that we are using the same tape
591 curfile
.action
= SKIP
;
592 dumptime
= hdr
.dumptime
;
593 dumpdate
= hdr
.dumpdate
;
595 newtapebuf(hdr
.ntrec
);
599 panic("initsymtable called from command %c\n", command
);
603 entrytblsize
= hdr
.entrytblsize
;
604 entry
= (struct entry
**)
605 (base
+ tblsize
- (entrytblsize
* sizeof(struct entry
*)));
606 baseep
= (struct entry
*)(base
+ hdr
.stringsize
- sizeof(struct entry
));
607 lep
= (struct entry
*)entry
;
608 for (i
= 0; i
< entrytblsize
; i
++) {
609 if (entry
[i
] == NULL
)
611 entry
[i
] = &baseep
[(long)entry
[i
]];
613 for (ep
= &baseep
[1]; ep
< lep
; ep
++) {
614 ep
->e_name
= base
+ (long)ep
->e_name
;
615 ep
->e_parent
= &baseep
[(long)ep
->e_parent
];
616 if (ep
->e_sibling
!= NULL
)
617 ep
->e_sibling
= &baseep
[(long)ep
->e_sibling
];
618 if (ep
->e_links
!= NULL
)
619 ep
->e_links
= &baseep
[(long)ep
->e_links
];
620 if (ep
->e_entries
!= NULL
)
621 ep
->e_entries
= &baseep
[(long)ep
->e_entries
];
622 if (ep
->e_next
!= NULL
)
623 ep
->e_next
= &baseep
[(long)ep
->e_next
];