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