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