GC !__DragonFly__ section.
[dragonfly.git] / sbin / restore / utilities.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
1de703da
MD
32 *
33 * @(#)utilities.c 8.5 (Berkeley) 4/28/95
34 * $FreeBSD: src/sbin/restore/utilities.c,v 1.8.2.2 2001/07/30 10:30:08 dd Exp $
7cee7052 35 * $DragonFly: src/sbin/restore/utilities.c,v 1.5 2004/12/18 21:43:40 swildner Exp $
984263bc
MD
36 */
37
984263bc
MD
38#include <sys/param.h>
39#include <sys/stat.h>
40
38a690d7
MD
41#include <vfs/ufs/dinode.h>
42#include <vfs/ufs/dir.h>
984263bc
MD
43
44#include <errno.h>
45#include <stdio.h>
46#include <stdlib.h>
47#include <string.h>
48#include <unistd.h>
49
50#include "restore.h"
51#include "extern.h"
52
53/*
54 * Insure that all the components of a pathname exist.
55 */
56void
b5744197 57pathcheck(char *name)
984263bc
MD
58{
59 register char *cp;
60 struct entry *ep;
61 char *start;
62
63 start = strchr(name, '/');
64 if (start == 0)
65 return;
66 for (cp = start; *cp != '\0'; cp++) {
67 if (*cp != '/')
68 continue;
69 *cp = '\0';
70 ep = lookupname(name);
71 if (ep == NULL) {
72 /* Safe; we know the pathname exists in the dump. */
73 ep = addentry(name, pathsearch(name)->d_ino, NODE);
74 newnode(ep);
75 }
76 ep->e_flags |= NEW|KEEP;
77 *cp = '/';
78 }
79}
80
81/*
82 * Change a name to a unique temporary name.
83 */
84void
b5744197 85mktempname(register struct entry *ep)
984263bc
MD
86{
87 char oldname[MAXPATHLEN];
88
89 if (ep->e_flags & TMPNAME)
90 badentry(ep, "mktempname: called with TMPNAME");
91 ep->e_flags |= TMPNAME;
7cee7052 92 strcpy(oldname, myname(ep));
984263bc
MD
93 freename(ep->e_name);
94 ep->e_name = savename(gentempname(ep));
95 ep->e_namlen = strlen(ep->e_name);
96 renameit(oldname, myname(ep));
97}
98
99/*
100 * Generate a temporary name for an entry.
101 */
102char *
b5744197 103gentempname(struct entry *ep)
984263bc
MD
104{
105 static char name[MAXPATHLEN];
106 struct entry *np;
107 long i = 0;
108
109 for (np = lookupino(ep->e_ino);
110 np != NULL && np != ep; np = np->e_links)
111 i++;
112 if (np == NULL)
113 badentry(ep, "not on ino list");
7cee7052 114 sprintf(name, "%s%ld%lu", TMPHDR, i, (u_long)ep->e_ino);
984263bc
MD
115 return (name);
116}
117
118/*
119 * Rename a file or directory.
120 */
121void
b5744197 122renameit(char *from, char *to)
984263bc
MD
123{
124 if (!Nflag && rename(from, to) < 0) {
125 fprintf(stderr, "warning: cannot rename %s to %s: %s\n",
126 from, to, strerror(errno));
127 return;
128 }
129 vprintf(stdout, "rename %s to %s\n", from, to);
130}
131
132/*
133 * Create a new node (directory).
134 */
135void
b5744197 136newnode(struct entry *np)
984263bc
MD
137{
138 char *cp;
139
140 if (np->e_type != NODE)
141 badentry(np, "newnode: not a node");
142 cp = myname(np);
143 if (!Nflag && mkdir(cp, 0777) < 0 && !uflag) {
144 np->e_flags |= EXISTED;
145 fprintf(stderr, "warning: %s: %s\n", cp, strerror(errno));
146 return;
147 }
148 vprintf(stdout, "Make node %s\n", cp);
149}
150
151/*
152 * Remove an old node (directory).
153 */
154void
b5744197 155removenode(register struct entry *ep)
984263bc
MD
156{
157 char *cp;
158
159 if (ep->e_type != NODE)
160 badentry(ep, "removenode: not a node");
161 if (ep->e_entries != NULL)
162 badentry(ep, "removenode: non-empty directory");
163 ep->e_flags |= REMOVED;
164 ep->e_flags &= ~TMPNAME;
165 cp = myname(ep);
166 if (!Nflag && rmdir(cp) < 0) {
167 fprintf(stderr, "warning: %s: %s\n", cp, strerror(errno));
168 return;
169 }
170 vprintf(stdout, "Remove node %s\n", cp);
171}
172
173/*
174 * Remove a leaf.
175 */
176void
b5744197 177removeleaf(register struct entry *ep)
984263bc
MD
178{
179 char *cp;
180
181 if (ep->e_type != LEAF)
182 badentry(ep, "removeleaf: not a leaf");
183 ep->e_flags |= REMOVED;
184 ep->e_flags &= ~TMPNAME;
185 cp = myname(ep);
186 if (!Nflag && unlink(cp) < 0) {
187 fprintf(stderr, "warning: %s: %s\n", cp, strerror(errno));
188 return;
189 }
190 vprintf(stdout, "Remove leaf %s\n", cp);
191}
192
193/*
194 * Create a link.
195 */
196int
b5744197 197linkit(char *existing, char *new, int type)
984263bc
MD
198{
199
200 /* if we want to unlink first, do it now so *link() won't fail */
201 if (uflag && !Nflag)
7cee7052 202 unlink(new);
984263bc
MD
203
204 if (type == SYMLINK) {
205 if (!Nflag && symlink(existing, new) < 0) {
206 fprintf(stderr,
207 "warning: cannot create symbolic link %s->%s: %s\n",
208 new, existing, strerror(errno));
209 return (FAIL);
210 }
211 } else if (type == HARDLINK) {
212 int ret;
213
214 if (!Nflag && (ret = link(existing, new)) < 0) {
215 struct stat s;
216
217 /*
218 * Most likely, the schg flag is set. Clear the
219 * flags and try again.
220 */
221 if (stat(existing, &s) == 0 && s.st_flags != 0 &&
222 chflags(existing, 0) == 0) {
223 ret = link(existing, new);
224 chflags(existing, s.st_flags);
225 }
226 if (ret < 0) {
227 fprintf(stderr, "warning: cannot create "
228 "hard link %s->%s: %s\n",
229 new, existing, strerror(errno));
230 return (FAIL);
231 }
232 }
233 } else {
234 panic("linkit: unknown type %d\n", type);
235 return (FAIL);
236 }
237 vprintf(stdout, "Create %s link %s->%s\n",
238 type == SYMLINK ? "symbolic" : "hard", new, existing);
239 return (GOOD);
240}
241
242/*
243 * Create a whiteout.
244 */
245int
b5744197 246addwhiteout(char *name)
984263bc
MD
247{
248
249 if (!Nflag && mknod(name, S_IFWHT, 0) < 0) {
250 fprintf(stderr, "warning: cannot create whiteout %s: %s\n",
251 name, strerror(errno));
252 return (FAIL);
253 }
254 vprintf(stdout, "Create whiteout %s\n", name);
255 return (GOOD);
256}
257
258/*
259 * Delete a whiteout.
260 */
261void
b5744197 262delwhiteout(register struct entry *ep)
984263bc
MD
263{
264 char *name;
265
266 if (ep->e_type != LEAF)
267 badentry(ep, "delwhiteout: not a leaf");
268 ep->e_flags |= REMOVED;
269 ep->e_flags &= ~TMPNAME;
270 name = myname(ep);
271 if (!Nflag && undelete(name) < 0) {
272 fprintf(stderr, "warning: cannot delete whiteout %s: %s\n",
273 name, strerror(errno));
274 return;
275 }
276 vprintf(stdout, "Delete whiteout %s\n", name);
277}
278
279/*
280 * find lowest number file (above "start") that needs to be extracted
281 */
282ino_t
b5744197 283lowerbnd(ino_t start)
984263bc
MD
284{
285 register struct entry *ep;
286
287 for ( ; start < maxino; start++) {
288 ep = lookupino(start);
289 if (ep == NULL || ep->e_type == NODE)
290 continue;
291 if (ep->e_flags & (NEW|EXTRACT))
292 return (start);
293 }
294 return (start);
295}
296
297/*
298 * find highest number file (below "start") that needs to be extracted
299 */
300ino_t
b5744197 301upperbnd(ino_t start)
984263bc
MD
302{
303 register struct entry *ep;
304
305 for ( ; start > ROOTINO; start--) {
306 ep = lookupino(start);
307 if (ep == NULL || ep->e_type == NODE)
308 continue;
309 if (ep->e_flags & (NEW|EXTRACT))
310 return (start);
311 }
312 return (start);
313}
314
315/*
316 * report on a badly formed entry
317 */
318void
b5744197 319badentry(register struct entry *ep, char *msg)
984263bc
MD
320{
321
322 fprintf(stderr, "bad entry: %s\n", msg);
323 fprintf(stderr, "name: %s\n", myname(ep));
324 fprintf(stderr, "parent name %s\n", myname(ep->e_parent));
325 if (ep->e_sibling != NULL)
326 fprintf(stderr, "sibling name: %s\n", myname(ep->e_sibling));
327 if (ep->e_entries != NULL)
328 fprintf(stderr, "next entry name: %s\n", myname(ep->e_entries));
329 if (ep->e_links != NULL)
330 fprintf(stderr, "next link name: %s\n", myname(ep->e_links));
331 if (ep->e_next != NULL)
332 fprintf(stderr,
333 "next hashchain name: %s\n", myname(ep->e_next));
334 fprintf(stderr, "entry type: %s\n",
335 ep->e_type == NODE ? "NODE" : "LEAF");
336 fprintf(stderr, "inode number: %lu\n", (u_long)ep->e_ino);
337 panic("flags: %s\n", flagvalues(ep));
338}
339
340/*
341 * Construct a string indicating the active flag bits of an entry.
342 */
343char *
b5744197 344flagvalues(register struct entry *ep)
984263bc
MD
345{
346 static char flagbuf[BUFSIZ];
347
7cee7052 348 strcpy(flagbuf, "|NIL");
984263bc
MD
349 flagbuf[0] = '\0';
350 if (ep->e_flags & REMOVED)
7cee7052 351 strcat(flagbuf, "|REMOVED");
984263bc 352 if (ep->e_flags & TMPNAME)
7cee7052 353 strcat(flagbuf, "|TMPNAME");
984263bc 354 if (ep->e_flags & EXTRACT)
7cee7052 355 strcat(flagbuf, "|EXTRACT");
984263bc 356 if (ep->e_flags & NEW)
7cee7052 357 strcat(flagbuf, "|NEW");
984263bc 358 if (ep->e_flags & KEEP)
7cee7052 359 strcat(flagbuf, "|KEEP");
984263bc 360 if (ep->e_flags & EXISTED)
7cee7052 361 strcat(flagbuf, "|EXISTED");
984263bc
MD
362 return (&flagbuf[1]);
363}
364
365/*
366 * Check to see if a name is on a dump tape.
367 */
368ino_t
b5744197 369dirlookup(const char *name)
984263bc
MD
370{
371 struct direct *dp;
372 ino_t ino;
373
374 ino = ((dp = pathsearch(name)) == NULL) ? 0 : dp->d_ino;
375
376 if (ino == 0 || TSTINO(ino, dumpmap) == 0)
377 fprintf(stderr, "%s is not on the tape\n", name);
378 return (ino);
379}
380
381/*
382 * Elicit a reply.
383 */
384int
b5744197 385reply(char *question)
984263bc
MD
386{
387 int c;
388
389 do {
390 fprintf(stderr, "%s? [yn] ", question);
7cee7052 391 fflush(stderr);
984263bc
MD
392 c = getc(terminal);
393 while (c != '\n' && getc(terminal) != '\n')
394 if (c == EOF)
395 return (FAIL);
396 } while (c != 'y' && c != 'n');
397 if (c == 'y')
398 return (GOOD);
399 return (FAIL);
400}
401
402/*
403 * handle unexpected inconsistencies
404 */
984263bc 405#include <stdarg.h>
984263bc
MD
406
407void
984263bc 408panic(const char *fmt, ...)
984263bc
MD
409{
410 va_list ap;
984263bc 411
b5744197 412 va_start(ap, fmt);
984263bc
MD
413 vfprintf(stderr, fmt, ap);
414 if (yflag)
415 return;
416 if (reply("abort") == GOOD) {
417 if (reply("dump core") == GOOD)
418 abort();
419 done(1);
420 }
421}