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