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