2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)utilities.c 8.5 (Berkeley) 4/28/95
30 * $FreeBSD: src/sbin/restore/utilities.c,v 1.8.2.2 2001/07/30 10:30:08 dd Exp $
33 #include <sys/param.h>
36 #include <vfs/ufs/dinode.h>
37 #include <vfs/ufs/dir.h>
49 * Insure that all the components of a pathname exist.
58 start
= strchr(name
, '/');
61 for (cp
= start
; *cp
!= '\0'; cp
++) {
65 ep
= lookupname(name
);
67 /* Safe; we know the pathname exists in the dump. */
68 ep
= addentry(name
, pathsearch(name
)->d_ino
, NODE
);
71 ep
->e_flags
|= NEW
|KEEP
;
77 * Change a name to a unique temporary name.
80 mktempname(struct entry
*ep
)
82 char oldname
[MAXPATHLEN
];
84 if (ep
->e_flags
& TMPNAME
)
85 badentry(ep
, "mktempname: called with TMPNAME");
86 ep
->e_flags
|= TMPNAME
;
87 strcpy(oldname
, myname(ep
));
89 ep
->e_name
= savename(gentempname(ep
));
90 ep
->e_namlen
= strlen(ep
->e_name
);
91 renameit(oldname
, myname(ep
));
95 * Generate a temporary name for an entry.
98 gentempname(struct entry
*ep
)
100 static char name
[MAXPATHLEN
];
104 for (np
= lookupino(ep
->e_ino
);
105 np
!= NULL
&& np
!= ep
; np
= np
->e_links
)
108 badentry(ep
, "not on ino list");
109 sprintf(name
, "%s%ld%lu", TMPHDR
, i
, (u_long
)ep
->e_ino
);
114 * Rename a file or directory.
117 renameit(const char *from
, const char *to
)
119 if (!Nflag
&& rename(from
, to
) < 0) {
120 fprintf(stderr
, "warning: cannot rename %s to %s: %s\n",
121 from
, to
, strerror(errno
));
124 vprintf(stdout
, "rename %s to %s\n", from
, to
);
128 * Create a new node (directory).
131 newnode(struct entry
*np
)
135 if (np
->e_type
!= NODE
)
136 badentry(np
, "newnode: not a node");
138 if (!Nflag
&& mkdir(cp
, 0777) < 0 && !uflag
) {
139 np
->e_flags
|= EXISTED
;
140 fprintf(stderr
, "warning: %s: %s\n", cp
, strerror(errno
));
143 vprintf(stdout
, "Make node %s\n", cp
);
147 * Remove an old node (directory).
150 removenode(struct entry
*ep
)
154 if (ep
->e_type
!= NODE
)
155 badentry(ep
, "removenode: not a node");
156 if (ep
->e_entries
!= NULL
)
157 badentry(ep
, "removenode: non-empty directory");
158 ep
->e_flags
|= REMOVED
;
159 ep
->e_flags
&= ~TMPNAME
;
161 if (!Nflag
&& rmdir(cp
) < 0) {
162 fprintf(stderr
, "warning: %s: %s\n", cp
, strerror(errno
));
165 vprintf(stdout
, "Remove node %s\n", cp
);
172 removeleaf(struct entry
*ep
)
176 if (ep
->e_type
!= LEAF
)
177 badentry(ep
, "removeleaf: not a leaf");
178 ep
->e_flags
|= REMOVED
;
179 ep
->e_flags
&= ~TMPNAME
;
181 if (!Nflag
&& unlink(cp
) < 0) {
182 fprintf(stderr
, "warning: %s: %s\n", cp
, strerror(errno
));
185 vprintf(stdout
, "Remove leaf %s\n", cp
);
192 linkit(char *existing
, char *new, int type
)
195 /* if we want to unlink first, do it now so *link() won't fail */
199 if (type
== SYMLINK
) {
200 if (!Nflag
&& symlink(existing
, new) < 0) {
202 "warning: cannot create symbolic link %s->%s: %s\n",
203 new, existing
, strerror(errno
));
206 } else if (type
== HARDLINK
) {
209 if (!Nflag
&& (ret
= link(existing
, new)) < 0) {
213 * Most likely, the schg flag is set. Clear the
214 * flags and try again.
216 if (stat(existing
, &s
) == 0 && s
.st_flags
!= 0 &&
217 chflags(existing
, 0) == 0) {
218 ret
= link(existing
, new);
219 chflags(existing
, s
.st_flags
);
222 fprintf(stderr
, "warning: cannot create "
223 "hard link %s->%s: %s\n",
224 new, existing
, strerror(errno
));
229 panic("linkit: unknown type %d\n", type
);
232 vprintf(stdout
, "Create %s link %s->%s\n",
233 type
== SYMLINK
? "symbolic" : "hard", new, existing
);
241 addwhiteout(char *name
)
244 if (!Nflag
&& mknod(name
, S_IFWHT
, 0) < 0) {
245 fprintf(stderr
, "warning: cannot create whiteout %s: %s\n",
246 name
, strerror(errno
));
249 vprintf(stdout
, "Create whiteout %s\n", name
);
257 delwhiteout(struct entry
*ep
)
261 if (ep
->e_type
!= LEAF
)
262 badentry(ep
, "delwhiteout: not a leaf");
263 ep
->e_flags
|= REMOVED
;
264 ep
->e_flags
&= ~TMPNAME
;
266 if (!Nflag
&& undelete(name
) < 0) {
267 fprintf(stderr
, "warning: cannot delete whiteout %s: %s\n",
268 name
, strerror(errno
));
271 vprintf(stdout
, "Delete whiteout %s\n", name
);
275 * find lowest number file (above "start") that needs to be extracted
278 lowerbnd(ufs1_ino_t start
)
282 for ( ; start
< maxino
; start
++) {
283 ep
= lookupino(start
);
284 if (ep
== NULL
|| ep
->e_type
== NODE
)
286 if (ep
->e_flags
& (NEW
|EXTRACT
))
293 * find highest number file (below "start") that needs to be extracted
296 upperbnd(ufs1_ino_t start
)
300 for ( ; start
> ROOTINO
; start
--) {
301 ep
= lookupino(start
);
302 if (ep
== NULL
|| ep
->e_type
== NODE
)
304 if (ep
->e_flags
& (NEW
|EXTRACT
))
311 * report on a badly formed entry
314 badentry(struct entry
*ep
, const char *msg
)
317 fprintf(stderr
, "bad entry: %s\n", msg
);
318 fprintf(stderr
, "name: %s\n", myname(ep
));
319 fprintf(stderr
, "parent name %s\n", myname(ep
->e_parent
));
320 if (ep
->e_sibling
!= NULL
)
321 fprintf(stderr
, "sibling name: %s\n", myname(ep
->e_sibling
));
322 if (ep
->e_entries
!= NULL
)
323 fprintf(stderr
, "next entry name: %s\n", myname(ep
->e_entries
));
324 if (ep
->e_links
!= NULL
)
325 fprintf(stderr
, "next link name: %s\n", myname(ep
->e_links
));
326 if (ep
->e_next
!= NULL
)
328 "next hashchain name: %s\n", myname(ep
->e_next
));
329 fprintf(stderr
, "entry type: %s\n",
330 ep
->e_type
== NODE
? "NODE" : "LEAF");
331 fprintf(stderr
, "inode number: %lu\n", (u_long
)ep
->e_ino
);
332 panic("flags: %s\n", flagvalues(ep
));
336 * Construct a string indicating the active flag bits of an entry.
339 flagvalues(struct entry
*ep
)
341 static char flagbuf
[BUFSIZ
];
343 strcpy(flagbuf
, "|NIL");
345 if (ep
->e_flags
& REMOVED
)
346 strcat(flagbuf
, "|REMOVED");
347 if (ep
->e_flags
& TMPNAME
)
348 strcat(flagbuf
, "|TMPNAME");
349 if (ep
->e_flags
& EXTRACT
)
350 strcat(flagbuf
, "|EXTRACT");
351 if (ep
->e_flags
& NEW
)
352 strcat(flagbuf
, "|NEW");
353 if (ep
->e_flags
& KEEP
)
354 strcat(flagbuf
, "|KEEP");
355 if (ep
->e_flags
& EXISTED
)
356 strcat(flagbuf
, "|EXISTED");
357 return (&flagbuf
[1]);
361 * Check to see if a name is on a dump tape.
364 dirlookup(const char *name
)
369 ino
= ((dp
= pathsearch(name
)) == NULL
) ? 0 : dp
->d_ino
;
371 if (ino
== 0 || TSTINO(ino
, dumpmap
) == 0)
372 fprintf(stderr
, "%s is not on the tape\n", name
);
380 reply(const char *question
)
385 fprintf(stderr
, "%s? [yn] ", question
);
388 while (c
!= '\n' && getc(terminal
) != '\n')
391 } while (c
!= 'y' && c
!= 'n');
398 * handle unexpected inconsistencies
403 panic(const char *fmt
, ...)
408 vfprintf(stderr
, fmt
, ap
);
412 if (reply("abort") == GOOD
) {
413 if (reply("dump core") == GOOD
)