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 * 4. 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
32 static char sccsid
[] = "@(#)utilities.c 8.5 (Berkeley) 4/28/95";
34 static const char rcsid
[] =
38 #include <sys/param.h>
41 #include <ufs/ufs/dinode.h>
42 #include <ufs/ufs/dir.h>
55 * Insure that all the components of a pathname exist.
64 start
= strchr(name
, '/');
67 for (cp
= start
; *cp
!= '\0'; cp
++) {
71 ep
= lookupname(name
);
73 /* Safe; we know the pathname exists in the dump. */
74 ep
= addentry(name
, pathsearch(name
)->d_ino
, NODE
);
77 ep
->e_flags
|= NEW
|KEEP
;
83 * Change a name to a unique temporary name.
86 mktempname(struct entry
*ep
)
88 char oldname
[MAXPATHLEN
];
90 if (ep
->e_flags
& TMPNAME
)
91 badentry(ep
, "mktempname: called with TMPNAME");
92 ep
->e_flags
|= TMPNAME
;
93 (void) strcpy(oldname
, myname(ep
));
95 ep
->e_name
= savename(gentempname(ep
));
96 ep
->e_namlen
= strlen(ep
->e_name
);
97 renameit(oldname
, myname(ep
));
101 * Generate a temporary name for an entry.
104 gentempname(struct entry
*ep
)
106 static char name
[MAXPATHLEN
];
110 for (np
= lookupino(ep
->e_ino
);
111 np
!= NULL
&& np
!= ep
; np
= np
->e_links
)
114 badentry(ep
, "not on ino list");
115 (void) sprintf(name
, "%s%ld%lu", TMPHDR
, i
, (u_long
)ep
->e_ino
);
120 * Rename a file or directory.
123 renameit(char *from
, char *to
)
125 if (!Nflag
&& rename(from
, to
) < 0) {
126 fprintf(stderr
, "warning: cannot rename %s to %s: %s\n",
127 from
, to
, strerror(errno
));
130 vprintf(stdout
, "rename %s to %s\n", from
, to
);
134 * Create a new node (directory).
137 newnode(struct entry
*np
)
141 if (np
->e_type
!= NODE
)
142 badentry(np
, "newnode: not a node");
144 if (!Nflag
&& mkdir(cp
, 0777) < 0 && !uflag
) {
145 np
->e_flags
|= EXISTED
;
146 fprintf(stderr
, "warning: %s: %s\n", cp
, strerror(errno
));
149 vprintf(stdout
, "Make node %s\n", cp
);
153 * Remove an old node (directory).
156 removenode(struct entry
*ep
)
160 if (ep
->e_type
!= NODE
)
161 badentry(ep
, "removenode: not a node");
162 if (ep
->e_entries
!= NULL
)
163 badentry(ep
, "removenode: non-empty directory");
164 ep
->e_flags
|= REMOVED
;
165 ep
->e_flags
&= ~TMPNAME
;
167 if (!Nflag
&& rmdir(cp
) < 0) {
168 fprintf(stderr
, "warning: %s: %s\n", cp
, strerror(errno
));
171 vprintf(stdout
, "Remove node %s\n", cp
);
178 removeleaf(struct entry
*ep
)
182 if (ep
->e_type
!= LEAF
)
183 badentry(ep
, "removeleaf: not a leaf");
184 ep
->e_flags
|= REMOVED
;
185 ep
->e_flags
&= ~TMPNAME
;
187 if (!Nflag
&& unlink(cp
) < 0) {
188 fprintf(stderr
, "warning: %s: %s\n", cp
, strerror(errno
));
191 vprintf(stdout
, "Remove leaf %s\n", cp
);
198 linkit(char *existing
, char *new, int type
)
201 /* if we want to unlink first, do it now so *link() won't fail */
205 if (type
== SYMLINK
) {
206 if (!Nflag
&& symlink(existing
, new) < 0) {
208 "warning: cannot create symbolic link %s->%s: %s\n",
209 new, existing
, strerror(errno
));
212 } else if (type
== HARDLINK
) {
215 if (!Nflag
&& (ret
= link(existing
, new)) < 0) {
219 * Most likely, the schg flag is set. Clear the
220 * flags and try again.
222 if (stat(existing
, &s
) == 0 && s
.st_flags
!= 0 &&
223 chflags(existing
, 0) == 0) {
224 ret
= link(existing
, new);
225 chflags(existing
, s
.st_flags
);
228 fprintf(stderr
, "warning: cannot create "
229 "hard link %s->%s: %s\n",
230 new, existing
, strerror(errno
));
235 panic("linkit: unknown type %d\n", type
);
238 vprintf(stdout
, "Create %s link %s->%s\n",
239 type
== SYMLINK
? "symbolic" : "hard", new, existing
);
247 addwhiteout(char *name
)
250 if (!Nflag
&& mknod(name
, S_IFWHT
, 0) < 0) {
251 fprintf(stderr
, "warning: cannot create whiteout %s: %s\n",
252 name
, strerror(errno
));
255 vprintf(stdout
, "Create whiteout %s\n", name
);
263 delwhiteout(struct entry
*ep
)
267 if (ep
->e_type
!= LEAF
)
268 badentry(ep
, "delwhiteout: not a leaf");
269 ep
->e_flags
|= REMOVED
;
270 ep
->e_flags
&= ~TMPNAME
;
272 if (!Nflag
&& undelete(name
) < 0) {
273 fprintf(stderr
, "warning: cannot delete whiteout %s: %s\n",
274 name
, strerror(errno
));
277 vprintf(stdout
, "Delete whiteout %s\n", name
);
281 * find lowest number file (above "start") that needs to be extracted
284 lowerbnd(ino_t start
)
288 for ( ; start
< maxino
; start
++) {
289 ep
= lookupino(start
);
290 if (ep
== NULL
|| ep
->e_type
== NODE
)
292 if (ep
->e_flags
& (NEW
|EXTRACT
))
299 * find highest number file (below "start") that needs to be extracted
302 upperbnd(ino_t start
)
306 for ( ; start
> ROOTINO
; start
--) {
307 ep
= lookupino(start
);
308 if (ep
== NULL
|| ep
->e_type
== NODE
)
310 if (ep
->e_flags
& (NEW
|EXTRACT
))
317 * report on a badly formed entry
320 badentry(struct entry
*ep
, char *msg
)
323 fprintf(stderr
, "bad entry: %s\n", msg
);
324 fprintf(stderr
, "name: %s\n", myname(ep
));
325 fprintf(stderr
, "parent name %s\n", myname(ep
->e_parent
));
326 if (ep
->e_sibling
!= NULL
)
327 fprintf(stderr
, "sibling name: %s\n", myname(ep
->e_sibling
));
328 if (ep
->e_entries
!= NULL
)
329 fprintf(stderr
, "next entry name: %s\n", myname(ep
->e_entries
));
330 if (ep
->e_links
!= NULL
)
331 fprintf(stderr
, "next link name: %s\n", myname(ep
->e_links
));
332 if (ep
->e_next
!= NULL
)
334 "next hashchain name: %s\n", myname(ep
->e_next
));
335 fprintf(stderr
, "entry type: %s\n",
336 ep
->e_type
== NODE
? "NODE" : "LEAF");
337 fprintf(stderr
, "inode number: %lu\n", (u_long
)ep
->e_ino
);
338 panic("flags: %s\n", flagvalues(ep
));
342 * Construct a string indicating the active flag bits of an entry.
345 flagvalues(struct entry
*ep
)
347 static char flagbuf
[BUFSIZ
];
349 (void) strcpy(flagbuf
, "|NIL");
351 if (ep
->e_flags
& REMOVED
)
352 (void) strcat(flagbuf
, "|REMOVED");
353 if (ep
->e_flags
& TMPNAME
)
354 (void) strcat(flagbuf
, "|TMPNAME");
355 if (ep
->e_flags
& EXTRACT
)
356 (void) strcat(flagbuf
, "|EXTRACT");
357 if (ep
->e_flags
& NEW
)
358 (void) strcat(flagbuf
, "|NEW");
359 if (ep
->e_flags
& KEEP
)
360 (void) strcat(flagbuf
, "|KEEP");
361 if (ep
->e_flags
& EXISTED
)
362 (void) strcat(flagbuf
, "|EXISTED");
363 return (&flagbuf
[1]);
367 * Check to see if a name is on a dump tape.
370 dirlookup(const char *name
)
375 ino
= ((dp
= pathsearch(name
)) == NULL
) ? 0 : dp
->d_ino
;
377 if (ino
== 0 || TSTINO(ino
, dumpmap
) == 0)
378 fprintf(stderr
, "%s is not on the tape\n", name
);
386 reply(char *question
)
391 fprintf(stderr
, "%s? [yn] ", question
);
392 (void) fflush(stderr
);
394 while (c
!= '\n' && getc(terminal
) != '\n')
397 } while (c
!= 'y' && c
!= 'n');
404 * handle unexpected inconsistencies
409 panic(const char *fmt
, ...)
413 vfprintf(stderr
, fmt
, ap
);
416 if (reply("abort") == GOOD
) {
417 if (reply("dump core") == GOOD
)