kernel/usb4bsd: Configure the correct bMaxPacketSize for control endpoints.
[dragonfly.git] / sbin / restore / utilities.c
blob75a3ea8ecf812beda1a157b044e73e2ae02718aa
1 /*
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
7 * are met:
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
27 * SUCH DAMAGE.
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>
34 #include <sys/stat.h>
36 #include <vfs/ufs/dinode.h>
37 #include <vfs/ufs/dir.h>
39 #include <errno.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
45 #include "restore.h"
46 #include "extern.h"
49 * Insure that all the components of a pathname exist.
51 void
52 pathcheck(char *name)
54 char *cp;
55 struct entry *ep;
56 char *start;
58 start = strchr(name, '/');
59 if (start == NULL)
60 return;
61 for (cp = start; *cp != '\0'; cp++) {
62 if (*cp != '/')
63 continue;
64 *cp = '\0';
65 ep = lookupname(name);
66 if (ep == NULL) {
67 /* Safe; we know the pathname exists in the dump. */
68 ep = addentry(name, pathsearch(name)->d_ino, NODE);
69 newnode(ep);
71 ep->e_flags |= NEW|KEEP;
72 *cp = '/';
77 * Change a name to a unique temporary name.
79 void
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));
88 freename(ep->e_name);
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.
97 char *
98 gentempname(struct entry *ep)
100 static char name[MAXPATHLEN];
101 struct entry *np;
102 long i = 0;
104 for (np = lookupino(ep->e_ino);
105 np != NULL && np != ep; np = np->e_links)
106 i++;
107 if (np == NULL)
108 badentry(ep, "not on ino list");
109 sprintf(name, "%s%ld%lu", TMPHDR, i, (u_long)ep->e_ino);
110 return (name);
114 * Rename a file or directory.
116 void
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));
122 return;
124 vprintf(stdout, "rename %s to %s\n", from, to);
128 * Create a new node (directory).
130 void
131 newnode(struct entry *np)
133 char *cp;
135 if (np->e_type != NODE)
136 badentry(np, "newnode: not a node");
137 cp = myname(np);
138 if (!Nflag && mkdir(cp, 0777) < 0 && !uflag) {
139 np->e_flags |= EXISTED;
140 fprintf(stderr, "warning: %s: %s\n", cp, strerror(errno));
141 return;
143 vprintf(stdout, "Make node %s\n", cp);
147 * Remove an old node (directory).
149 void
150 removenode(struct entry *ep)
152 char *cp;
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;
160 cp = myname(ep);
161 if (!Nflag && rmdir(cp) < 0) {
162 fprintf(stderr, "warning: %s: %s\n", cp, strerror(errno));
163 return;
165 vprintf(stdout, "Remove node %s\n", cp);
169 * Remove a leaf.
171 void
172 removeleaf(struct entry *ep)
174 char *cp;
176 if (ep->e_type != LEAF)
177 badentry(ep, "removeleaf: not a leaf");
178 ep->e_flags |= REMOVED;
179 ep->e_flags &= ~TMPNAME;
180 cp = myname(ep);
181 if (!Nflag && unlink(cp) < 0) {
182 fprintf(stderr, "warning: %s: %s\n", cp, strerror(errno));
183 return;
185 vprintf(stdout, "Remove leaf %s\n", cp);
189 * Create a link.
192 linkit(char *existing, char *new, int type)
195 /* if we want to unlink first, do it now so *link() won't fail */
196 if (uflag && !Nflag)
197 unlink(new);
199 if (type == SYMLINK) {
200 if (!Nflag && symlink(existing, new) < 0) {
201 fprintf(stderr,
202 "warning: cannot create symbolic link %s->%s: %s\n",
203 new, existing, strerror(errno));
204 return (FAIL);
206 } else if (type == HARDLINK) {
207 int ret;
209 if (!Nflag && (ret = link(existing, new)) < 0) {
210 struct stat s;
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);
221 if (ret < 0) {
222 fprintf(stderr, "warning: cannot create "
223 "hard link %s->%s: %s\n",
224 new, existing, strerror(errno));
225 return (FAIL);
228 } else {
229 panic("linkit: unknown type %d\n", type);
230 return (FAIL);
232 vprintf(stdout, "Create %s link %s->%s\n",
233 type == SYMLINK ? "symbolic" : "hard", new, existing);
234 return (GOOD);
238 * Create a whiteout.
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));
247 return (FAIL);
249 vprintf(stdout, "Create whiteout %s\n", name);
250 return (GOOD);
254 * Delete a whiteout.
256 void
257 delwhiteout(struct entry *ep)
259 char *name;
261 if (ep->e_type != LEAF)
262 badentry(ep, "delwhiteout: not a leaf");
263 ep->e_flags |= REMOVED;
264 ep->e_flags &= ~TMPNAME;
265 name = myname(ep);
266 if (!Nflag && undelete(name) < 0) {
267 fprintf(stderr, "warning: cannot delete whiteout %s: %s\n",
268 name, strerror(errno));
269 return;
271 vprintf(stdout, "Delete whiteout %s\n", name);
275 * find lowest number file (above "start") that needs to be extracted
277 ufs1_ino_t
278 lowerbnd(ufs1_ino_t start)
280 struct entry *ep;
282 for ( ; start < maxino; start++) {
283 ep = lookupino(start);
284 if (ep == NULL || ep->e_type == NODE)
285 continue;
286 if (ep->e_flags & (NEW|EXTRACT))
287 return (start);
289 return (start);
293 * find highest number file (below "start") that needs to be extracted
295 ufs1_ino_t
296 upperbnd(ufs1_ino_t start)
298 struct entry *ep;
300 for ( ; start > ROOTINO; start--) {
301 ep = lookupino(start);
302 if (ep == NULL || ep->e_type == NODE)
303 continue;
304 if (ep->e_flags & (NEW|EXTRACT))
305 return (start);
307 return (start);
311 * report on a badly formed entry
313 void
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)
327 fprintf(stderr,
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.
338 char *
339 flagvalues(struct entry *ep)
341 static char flagbuf[BUFSIZ];
343 strcpy(flagbuf, "|NIL");
344 flagbuf[0] = '\0';
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.
363 ufs1_ino_t
364 dirlookup(const char *name)
366 struct direct *dp;
367 ufs1_ino_t ino;
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);
373 return (ino);
377 * Elicit a reply.
380 reply(const char *question)
382 int c;
384 do {
385 fprintf(stderr, "%s? [yn] ", question);
386 fflush(stderr);
387 c = getc(terminal);
388 while (c != '\n' && getc(terminal) != '\n')
389 if (c == EOF)
390 return (FAIL);
391 } while (c != 'y' && c != 'n');
392 if (c == 'y')
393 return (GOOD);
394 return (FAIL);
398 * handle unexpected inconsistencies
400 #include <stdarg.h>
402 void
403 panic(const char *fmt, ...)
405 va_list ap;
407 va_start(ap, fmt);
408 vfprintf(stderr, fmt, ap);
409 va_end(ap);
410 if (yflag)
411 return;
412 if (reply("abort") == GOOD) {
413 if (reply("dump core") == GOOD)
414 abort();
415 done(1);