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