2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 static char sccsid[] = "@(#)utilities.c 8.5 (Berkeley) 4/28/95";
36 static const char rcsid[] =
40 #include <sys/param.h>
43 #include <ufs/ufs/dinode.h>
44 #include <ufs/ufs/dir.h>
57 * Insure that all the components of a pathname exist.
66 start = strchr(name, '/');
69 for (cp = start; *cp != '\0'; cp++) {
73 ep = lookupname(name);
75 /* Safe; we know the pathname exists in the dump. */
76 ep = addentry(name, pathsearch(name)->d_ino, NODE);
79 ep->e_flags |= NEW|KEEP;
85 * Change a name to a unique temporary name.
88 mktempname(struct entry *ep)
90 char oldname[MAXPATHLEN];
92 if (ep->e_flags & TMPNAME)
93 badentry(ep, "mktempname: called with TMPNAME");
94 ep->e_flags |= TMPNAME;
95 (void) strcpy(oldname, myname(ep));
97 ep->e_name = savename(gentempname(ep));
98 ep->e_namlen = strlen(ep->e_name);
99 renameit(oldname, myname(ep));
103 * Generate a temporary name for an entry.
106 gentempname(struct entry *ep)
108 static char name[MAXPATHLEN];
112 for (np = lookupino(ep->e_ino);
113 np != NULL && np != ep; np = np->e_links)
116 badentry(ep, "not on ino list");
117 (void) sprintf(name, "%s%ld%lu", TMPHDR, i, (u_long)ep->e_ino);
122 * Rename a file or directory.
125 renameit(char *from, char *to)
127 if (!Nflag && rename(from, to) < 0) {
128 fprintf(stderr, "warning: cannot rename %s to %s: %s\n",
129 from, to, strerror(errno));
132 vprintf(stdout, "rename %s to %s\n", from, to);
136 * Create a new node (directory).
139 newnode(struct entry *np)
143 if (np->e_type != NODE)
144 badentry(np, "newnode: not a node");
146 if (!Nflag && mkdir(cp, 0777) < 0 && !uflag) {
147 np->e_flags |= EXISTED;
148 fprintf(stderr, "warning: %s: %s\n", cp, strerror(errno));
151 vprintf(stdout, "Make node %s\n", cp);
155 * Remove an old node (directory).
158 removenode(struct entry *ep)
162 if (ep->e_type != NODE)
163 badentry(ep, "removenode: not a node");
164 if (ep->e_entries != NULL)
165 badentry(ep, "removenode: non-empty directory");
166 ep->e_flags |= REMOVED;
167 ep->e_flags &= ~TMPNAME;
169 if (!Nflag && rmdir(cp) < 0) {
170 fprintf(stderr, "warning: %s: %s\n", cp, strerror(errno));
173 vprintf(stdout, "Remove node %s\n", cp);
180 removeleaf(struct entry *ep)
184 if (ep->e_type != LEAF)
185 badentry(ep, "removeleaf: not a leaf");
186 ep->e_flags |= REMOVED;
187 ep->e_flags &= ~TMPNAME;
189 if (!Nflag && unlink(cp) < 0) {
190 fprintf(stderr, "warning: %s: %s\n", cp, strerror(errno));
193 vprintf(stdout, "Remove leaf %s\n", cp);
200 linkit(char *existing, char *new, int type)
203 /* if we want to unlink first, do it now so *link() won't fail */
207 if (type == SYMLINK) {
208 if (!Nflag && symlink(existing, new) < 0) {
210 "warning: cannot create symbolic link %s->%s: %s\n",
211 new, existing, strerror(errno));
214 } else if (type == HARDLINK) {
217 if (!Nflag && (ret = link(existing, new)) < 0) {
221 * Most likely, the schg flag is set. Clear the
222 * flags and try again.
224 if (stat(existing, &s) == 0 && s.st_flags != 0 &&
225 chflags(existing, 0) == 0) {
226 ret = link(existing, new);
227 chflags(existing, s.st_flags);
230 fprintf(stderr, "warning: cannot create "
231 "hard link %s->%s: %s\n",
232 new, existing, strerror(errno));
237 panic("linkit: unknown type %d\n", type);
240 vprintf(stdout, "Create %s link %s->%s\n",
241 type == SYMLINK ? "symbolic" : "hard", new, existing);
249 addwhiteout(char *name)
252 if (!Nflag && mknod(name, S_IFWHT, 0) < 0) {
253 fprintf(stderr, "warning: cannot create whiteout %s: %s\n",
254 name, strerror(errno));
257 vprintf(stdout, "Create whiteout %s\n", name);
265 delwhiteout(struct entry *ep)
269 if (ep->e_type != LEAF)
270 badentry(ep, "delwhiteout: not a leaf");
271 ep->e_flags |= REMOVED;
272 ep->e_flags &= ~TMPNAME;
274 if (!Nflag && undelete(name) < 0) {
275 fprintf(stderr, "warning: cannot delete whiteout %s: %s\n",
276 name, strerror(errno));
279 vprintf(stdout, "Delete whiteout %s\n", name);
283 * find lowest number file (above "start") that needs to be extracted
286 lowerbnd(ino_t start)
290 for ( ; start < maxino; start++) {
291 ep = lookupino(start);
292 if (ep == NULL || ep->e_type == NODE)
294 if (ep->e_flags & (NEW|EXTRACT))
301 * find highest number file (below "start") that needs to be extracted
304 upperbnd(ino_t start)
308 for ( ; start > UFS_ROOTINO; start--) {
309 ep = lookupino(start);
310 if (ep == NULL || ep->e_type == NODE)
312 if (ep->e_flags & (NEW|EXTRACT))
319 * report on a badly formed entry
322 badentry(struct entry *ep, char *msg)
325 fprintf(stderr, "bad entry: %s\n", msg);
326 fprintf(stderr, "name: %s\n", myname(ep));
327 fprintf(stderr, "parent name %s\n", myname(ep->e_parent));
328 if (ep->e_sibling != NULL)
329 fprintf(stderr, "sibling name: %s\n", myname(ep->e_sibling));
330 if (ep->e_entries != NULL)
331 fprintf(stderr, "next entry name: %s\n", myname(ep->e_entries));
332 if (ep->e_links != NULL)
333 fprintf(stderr, "next link name: %s\n", myname(ep->e_links));
334 if (ep->e_next != NULL)
336 "next hashchain name: %s\n", myname(ep->e_next));
337 fprintf(stderr, "entry type: %s\n",
338 ep->e_type == NODE ? "NODE" : "LEAF");
339 fprintf(stderr, "inode number: %lu\n", (u_long)ep->e_ino);
340 panic("flags: %s\n", flagvalues(ep));
344 * Construct a string indicating the active flag bits of an entry.
347 flagvalues(struct entry *ep)
349 static char flagbuf[BUFSIZ];
351 (void) strcpy(flagbuf, "|NIL");
353 if (ep->e_flags & REMOVED)
354 (void) strcat(flagbuf, "|REMOVED");
355 if (ep->e_flags & TMPNAME)
356 (void) strcat(flagbuf, "|TMPNAME");
357 if (ep->e_flags & EXTRACT)
358 (void) strcat(flagbuf, "|EXTRACT");
359 if (ep->e_flags & NEW)
360 (void) strcat(flagbuf, "|NEW");
361 if (ep->e_flags & KEEP)
362 (void) strcat(flagbuf, "|KEEP");
363 if (ep->e_flags & EXISTED)
364 (void) strcat(flagbuf, "|EXISTED");
365 return (&flagbuf[1]);
369 * Check to see if a name is on a dump tape.
372 dirlookup(const char *name)
377 ino = ((dp = pathsearch(name)) == NULL) ? 0 : dp->d_ino;
379 if (ino == 0 || TSTINO(ino, dumpmap) == 0)
380 fprintf(stderr, "%s is not on the tape\n", name);
388 reply(char *question)
393 fprintf(stderr, "%s? [yn] ", question);
394 (void) fflush(stderr);
396 while (c != '\n' && getc(terminal) != '\n')
399 } while (c != 'y' && c != 'n');
406 * handle unexpected inconsistencies
411 panic(const char *fmt, ...)
415 vfprintf(stderr, fmt, ap);
419 if (reply("abort") == GOOD) {
420 if (reply("dump core") == GOOD)