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