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