]> git.wh0rd.org - dump.git/blob - restore/utilities.c
LFS compatibility.
[dump.git] / restore / utilities.c
1 /*
2 * Ported to Linux's Second Extended File System as part of the
3 * dump and restore backup suit
4 * Remy Card <card@Linux.EU.Org>, 1994-1997
5 * Stelian Pop <pop@noos.fr>, 1999-2000
6 * Stelian Pop <pop@noos.fr> - AlcĂ´ve <www.alcove.fr>, 2000
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.
40 */
41
42 #ifndef lint
43 static const char rcsid[] =
44 "$Id: utilities.c,v 1.11 2000/12/21 11:14:54 stelian Exp $";
45 #endif /* not lint */
46
47 #include <config.h>
48 #include <sys/param.h>
49 #include <sys/stat.h>
50
51 #ifdef __linux__
52 #include <sys/time.h>
53 #include <linux/ext2_fs.h>
54 #include <bsdcompat.h>
55 #else /* __linux__ */
56 #include <ufs/ufs/dinode.h>
57 #include <ufs/ufs/dir.h>
58 #endif /* __linux__ */
59
60 #include <errno.h>
61 #include <compaterr.h>
62 #include <stdio.h>
63 #include <string.h>
64 #include <unistd.h>
65
66 #ifdef __linux__
67 #include <ext2fs/ext2fs.h>
68 #endif
69
70 #include "restore.h"
71 #include "extern.h"
72
73 /*
74 * Insure that all the components of a pathname exist.
75 */
76 void
77 pathcheck(char *name)
78 {
79 register char *cp;
80 struct entry *ep;
81 char *start;
82
83 start = strchr(name, '/');
84 if (start == 0)
85 return;
86 for (cp = start; *cp != '\0'; cp++) {
87 if (*cp != '/')
88 continue;
89 *cp = '\0';
90 ep = lookupname(name);
91 if (ep == NULL) {
92 /* Safe; we know the pathname exists in the dump. */
93 ep = addentry(name, pathsearch(name)->d_ino, NODE);
94 newnode(ep);
95 }
96 ep->e_flags |= NEW|KEEP;
97 *cp = '/';
98 }
99 }
100
101 /*
102 * Change a name to a unique temporary name.
103 */
104 void
105 mktempname(struct entry *ep)
106 {
107 char oldname[MAXPATHLEN];
108
109 if (ep->e_flags & TMPNAME)
110 badentry(ep, "mktempname: called with TMPNAME");
111 ep->e_flags |= TMPNAME;
112 (void) strcpy(oldname, myname(ep));
113 freename(ep->e_name);
114 ep->e_name = savename(gentempname(ep));
115 ep->e_namlen = strlen(ep->e_name);
116 renameit(oldname, myname(ep));
117 }
118
119 /*
120 * Generate a temporary name for an entry.
121 */
122 char *
123 gentempname(struct entry *ep)
124 {
125 static char name[MAXPATHLEN];
126 struct entry *np;
127 long i = 0;
128
129 for (np = lookupino(ep->e_ino);
130 np != NULL && np != ep; np = np->e_links)
131 i++;
132 if (np == NULL)
133 badentry(ep, "not on ino list");
134 (void) snprintf(name, sizeof(name), "%s%ld%lu", TMPHDR, i, (unsigned long)ep->e_ino);
135 return (name);
136 }
137
138 /*
139 * Rename a file or directory.
140 */
141 void
142 renameit(char *from, char *to)
143 {
144 if (!Nflag && rename(from, to) < 0) {
145 warn("cannot rename %s to %s", from, to);
146 return;
147 }
148 Vprintf(stdout, "rename %s to %s\n", from, to);
149 }
150
151 /*
152 * Create a new node (directory).
153 */
154 void
155 newnode(struct entry *np)
156 {
157 char *cp;
158 if (np->e_type != NODE)
159 badentry(np, "newnode: not a node");
160 cp = myname(np);
161 if (command == 'C') return;
162
163 if (!Nflag && mkdir(cp, 0777) < 0 && !uflag) {
164 np->e_flags |= EXISTED;
165 warn("%s", cp);
166 return;
167 }
168 Vprintf(stdout, "Make node %s\n", cp);
169 }
170
171 /*
172 * Remove an old node (directory).
173 */
174 void
175 removenode(struct entry *ep)
176 {
177 char *cp;
178
179 if (ep->e_type != NODE)
180 badentry(ep, "removenode: not a node");
181 if (ep->e_entries != NULL)
182 badentry(ep, "removenode: non-empty directory");
183 ep->e_flags |= REMOVED;
184 ep->e_flags &= ~TMPNAME;
185 cp = myname(ep);
186 if (!Nflag && rmdir(cp) < 0) {
187 warn("%s", cp);
188 return;
189 }
190 Vprintf(stdout, "Remove node %s\n", cp);
191 }
192
193 /*
194 * Remove a leaf.
195 */
196 void
197 removeleaf(struct entry *ep)
198 {
199 char *cp;
200
201 if (command == 'C') return;
202
203 if (ep->e_type != LEAF)
204 badentry(ep, "removeleaf: not a leaf");
205 ep->e_flags |= REMOVED;
206 ep->e_flags &= ~TMPNAME;
207 cp = myname(ep);
208 if (!Nflag && unlink(cp) < 0) {
209 warn("%s", cp);
210 return;
211 }
212 Vprintf(stdout, "Remove leaf %s\n", cp);
213 }
214
215 /*
216 * Create a link.
217 */
218 int
219 linkit(char *existing, char *new, int type)
220 {
221
222 /* if we want to unlink first, do it now so *link() won't fail */
223 if (uflag && !Nflag)
224 (void)unlink(new);
225
226 if (type == SYMLINK) {
227 if (!Nflag && symlink(existing, new) < 0) {
228 warn("cannot create symbolic link %s->%s",
229 new, existing);
230 return (FAIL);
231 }
232 } else if (type == HARDLINK) {
233 int ret;
234
235 if (!Nflag && (ret = link(existing, new)) < 0) {
236
237 #ifndef __linux__
238 struct stat s;
239
240 /*
241 * Most likely, the schg flag is set. Clear the
242 * flags and try again.
243 */
244 if (stat(existing, &s) == 0 && s.st_flags != 0 &&
245 chflags(existing, 0) == 0) {
246 ret = link(existing, new);
247 chflags(existing, s.st_flags);
248 }
249 #else
250 unsigned long s;
251
252 /*
253 * Most likely, the immutable or append-only attribute
254 * is set. Clear the attributes and try again.
255 */
256 if (fgetflags (existing, &s) != -1 &&
257 fsetflags (existing, 0) != -1) {
258 ret = link(existing, new);
259 fsetflags(existing, s);
260 }
261 #endif
262 if (ret < 0) {
263 warn("warning: cannot create hard link %s->%s",
264 new, existing);
265 return (FAIL);
266 }
267 }
268 } else {
269 panic("linkit: unknown type %d\n", type);
270 return (FAIL);
271 }
272 Vprintf(stdout, "Create %s link %s->%s\n",
273 type == SYMLINK ? "symbolic" : "hard", new, existing);
274 return (GOOD);
275 }
276
277 #ifndef __linux__
278 /*
279 * Create a whiteout.
280 */
281 int
282 addwhiteout(char *name)
283 {
284
285 if (!Nflag && mknod(name, S_IFWHT, 0) < 0) {
286 warn("cannot create whiteout %s", name);
287 return (FAIL);
288 }
289 Vprintf(stdout, "Create whiteout %s\n", name);
290 return (GOOD);
291 }
292
293 /*
294 * Delete a whiteout.
295 */
296 void
297 delwhiteout(struct entry *ep)
298 {
299 char *name;
300
301 if (ep->e_type != LEAF)
302 badentry(ep, "delwhiteout: not a leaf");
303 ep->e_flags |= REMOVED;
304 ep->e_flags &= ~TMPNAME;
305 name = myname(ep);
306 if (!Nflag && undelete(name) < 0) {
307 warn("cannot delete whiteout %s", name);
308 return;
309 }
310 Vprintf(stdout, "Delete whiteout %s\n", name);
311 }
312 #endif
313
314 /*
315 * find lowest number file (above "start") that needs to be extracted
316 */
317 ino_t
318 lowerbnd(ino_t start)
319 {
320 register struct entry *ep;
321
322 for ( ; start < maxino; start++) {
323 ep = lookupino(start);
324 if (ep == NULL || ep->e_type == NODE)
325 continue;
326 if (ep->e_flags & (NEW|EXTRACT))
327 return (start);
328 }
329 return (start);
330 }
331
332 /*
333 * find highest number file (below "start") that needs to be extracted
334 */
335 ino_t
336 upperbnd(ino_t start)
337 {
338 register struct entry *ep;
339
340 for ( ; start > ROOTINO; start--) {
341 ep = lookupino(start);
342 if (ep == NULL || ep->e_type == NODE)
343 continue;
344 if (ep->e_flags & (NEW|EXTRACT))
345 return (start);
346 }
347 return (start);
348 }
349
350 /*
351 * report on a badly formed entry
352 */
353 void
354 badentry(struct entry *ep, const char *msg)
355 {
356
357 fprintf(stderr, "bad entry: %s\n", msg);
358 fprintf(stderr, "name: %s\n", myname(ep));
359 fprintf(stderr, "parent name %s\n", myname(ep->e_parent));
360 if (ep->e_sibling != NULL)
361 fprintf(stderr, "sibling name: %s\n", myname(ep->e_sibling));
362 if (ep->e_entries != NULL)
363 fprintf(stderr, "next entry name: %s\n", myname(ep->e_entries));
364 if (ep->e_links != NULL)
365 fprintf(stderr, "next link name: %s\n", myname(ep->e_links));
366 if (ep->e_next != NULL)
367 fprintf(stderr,
368 "next hashchain name: %s\n", myname(ep->e_next));
369 fprintf(stderr, "entry type: %s\n",
370 ep->e_type == NODE ? "NODE" : "LEAF");
371 fprintf(stderr, "inode number: %lu\n", (unsigned long)ep->e_ino);
372 panic("flags: %s\n", flagvalues(ep));
373 }
374
375 /*
376 * Construct a string indicating the active flag bits of an entry.
377 */
378 char *
379 flagvalues(struct entry *ep)
380 {
381 static char flagbuf[BUFSIZ];
382
383 (void) strcpy(flagbuf, "|NIL");
384 flagbuf[0] = '\0';
385 if (ep->e_flags & REMOVED)
386 (void) strcat(flagbuf, "|REMOVED");
387 if (ep->e_flags & TMPNAME)
388 (void) strcat(flagbuf, "|TMPNAME");
389 if (ep->e_flags & EXTRACT)
390 (void) strcat(flagbuf, "|EXTRACT");
391 if (ep->e_flags & NEW)
392 (void) strcat(flagbuf, "|NEW");
393 if (ep->e_flags & KEEP)
394 (void) strcat(flagbuf, "|KEEP");
395 if (ep->e_flags & EXISTED)
396 (void) strcat(flagbuf, "|EXISTED");
397 return (&flagbuf[1]);
398 }
399
400 /*
401 * Check to see if a name is on a dump tape.
402 */
403 ino_t
404 dirlookup(const char *name)
405 {
406 struct direct *dp;
407 ino_t ino;
408
409 ino = ((dp = pathsearch(name)) == NULL) ? 0 : dp->d_ino;
410
411 if (ino == 0 || TSTINO(ino, dumpmap) == 0)
412 fprintf(stderr, "%s is not on the tape\n", name);
413 return (ino);
414 }
415
416 /*
417 * Elicit a reply.
418 */
419 int
420 reply(const char *question)
421 {
422 char c;
423
424 do {
425 fprintf(stderr, "%s? [yn] ", question);
426 (void) fflush(stderr);
427 c = getc(terminal);
428 while (c != '\n' && getc(terminal) != '\n')
429 if (feof(terminal))
430 return (FAIL);
431 } while (c != 'y' && c != 'n');
432 if (c == 'y')
433 return (GOOD);
434 return (FAIL);
435 }
436
437 /*
438 * handle unexpected inconsistencies
439 */
440 #ifdef __STDC__
441 #include <stdarg.h>
442 #else
443 #include <varargs.h>
444 #endif
445
446 void
447 #ifdef __STDC__
448 panic(const char *fmt, ...)
449 #else
450 panic(fmt, va_alist)
451 char *fmt;
452 va_dcl
453 #endif
454 {
455 va_list ap;
456 #ifdef __STDC__
457 va_start(ap, fmt);
458 #else
459 va_start(ap);
460 #endif
461
462 vfprintf(stderr, fmt, ap);
463 if (yflag)
464 return;
465 if (reply("abort") == GOOD) {
466 if (reply("dump core") == GOOD)
467 abort();
468 exit(1);
469 }
470 }