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 static char sccsid[] = "@(#)utilities.c 8.5 (Berkeley) 4/28/95"; 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 <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(name) 58 char *name; 59 { 60 register 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(ep) 87 register struct entry *ep; 88 { 89 char oldname[MAXPATHLEN]; 90 91 if (ep->e_flags & TMPNAME) 92 badentry(ep, "mktempname: called with TMPNAME"); 93 ep->e_flags |= TMPNAME; 94 (void) strcpy(oldname, myname(ep)); 95 freename(ep->e_name); 96 ep->e_name = savename(gentempname(ep)); 97 ep->e_namlen = strlen(ep->e_name); 98 renameit(oldname, myname(ep)); 99 } 100 101 /* 102 * Generate a temporary name for an entry. 103 */ 104 char * 105 gentempname(ep) 106 struct entry *ep; 107 { 108 static char name[MAXPATHLEN]; 109 struct entry *np; 110 long i = 0; 111 112 for (np = lookupino(ep->e_ino); 113 np != NULL && np != ep; np = np->e_links) 114 i++; 115 if (np == NULL) 116 badentry(ep, "not on ino list"); 117 (void) sprintf(name, "%s%d%d", TMPHDR, i, ep->e_ino); 118 return (name); 119 } 120 121 /* 122 * Rename a file or directory. 123 */ 124 void 125 renameit(from, to) 126 char *from, *to; 127 { 128 if (!Nflag && rename(from, to) < 0) { 129 fprintf(stderr, "warning: cannot rename %s to %s: %s\n", 130 from, to, strerror(errno)); 131 return; 132 } 133 vprintf(stdout, "rename %s to %s\n", from, to); 134 } 135 136 /* 137 * Create a new node (directory). 138 */ 139 void 140 newnode(np) 141 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) { 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(ep) 161 register struct entry *ep; 162 { 163 char *cp; 164 165 if (ep->e_type != NODE) 166 badentry(ep, "removenode: not a node"); 167 if (ep->e_entries != NULL) 168 badentry(ep, "removenode: non-empty directory"); 169 ep->e_flags |= REMOVED; 170 ep->e_flags &= ~TMPNAME; 171 cp = myname(ep); 172 if (!Nflag && rmdir(cp) < 0) { 173 fprintf(stderr, "warning: %s: %s\n", cp, strerror(errno)); 174 return; 175 } 176 vprintf(stdout, "Remove node %s\n", cp); 177 } 178 179 /* 180 * Remove a leaf. 181 */ 182 void 183 removeleaf(ep) 184 register struct entry *ep; 185 { 186 char *cp; 187 188 if (ep->e_type != LEAF) 189 badentry(ep, "removeleaf: not a leaf"); 190 ep->e_flags |= REMOVED; 191 ep->e_flags &= ~TMPNAME; 192 cp = myname(ep); 193 if (!Nflag && unlink(cp) < 0) { 194 fprintf(stderr, "warning: %s: %s\n", cp, strerror(errno)); 195 return; 196 } 197 vprintf(stdout, "Remove leaf %s\n", cp); 198 } 199 200 /* 201 * Create a link. 202 */ 203 int 204 linkit(existing, new, type) 205 char *existing, *new; 206 int type; 207 { 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 if (!Nflag && link(existing, new) < 0) { 218 fprintf(stderr, 219 "warning: cannot create hard link %s->%s: %s\n", 220 new, existing, strerror(errno)); 221 return (FAIL); 222 } 223 } else { 224 panic("linkit: unknown type %d\n", type); 225 return (FAIL); 226 } 227 vprintf(stdout, "Create %s link %s->%s\n", 228 type == SYMLINK ? "symbolic" : "hard", new, existing); 229 return (GOOD); 230 } 231 232 /* 233 * Create a whiteout. 234 */ 235 int 236 addwhiteout(name) 237 char *name; 238 { 239 240 if (!Nflag && mknod(name, S_IFWHT, 0) < 0) { 241 fprintf(stderr, "warning: cannot create whiteout %s: %s\n", 242 name, strerror(errno)); 243 return (FAIL); 244 } 245 vprintf(stdout, "Create whiteout %s\n", name); 246 return (GOOD); 247 } 248 249 /* 250 * Delete a whiteout. 251 */ 252 void 253 delwhiteout(ep) 254 register struct entry *ep; 255 { 256 char *name; 257 258 if (ep->e_type != LEAF) 259 badentry(ep, "delwhiteout: not a leaf"); 260 ep->e_flags |= REMOVED; 261 ep->e_flags &= ~TMPNAME; 262 name = myname(ep); 263 if (!Nflag && undelete(name) < 0) { 264 fprintf(stderr, "warning: cannot delete whiteout %s: %s\n", 265 name, strerror(errno)); 266 return; 267 } 268 vprintf(stdout, "Delete whiteout %s\n", name); 269 } 270 271 /* 272 * find lowest number file (above "start") that needs to be extracted 273 */ 274 ino_t 275 lowerbnd(start) 276 ino_t start; 277 { 278 register struct entry *ep; 279 280 for ( ; start < maxino; start++) { 281 ep = lookupino(start); 282 if (ep == NULL || ep->e_type == NODE) 283 continue; 284 if (ep->e_flags & (NEW|EXTRACT)) 285 return (start); 286 } 287 return (start); 288 } 289 290 /* 291 * find highest number file (below "start") that needs to be extracted 292 */ 293 ino_t 294 upperbnd(start) 295 ino_t start; 296 { 297 register struct entry *ep; 298 299 for ( ; start > ROOTINO; start--) { 300 ep = lookupino(start); 301 if (ep == NULL || ep->e_type == NODE) 302 continue; 303 if (ep->e_flags & (NEW|EXTRACT)) 304 return (start); 305 } 306 return (start); 307 } 308 309 /* 310 * report on a badly formed entry 311 */ 312 void 313 badentry(ep, msg) 314 register struct entry *ep; 315 char *msg; 316 { 317 318 fprintf(stderr, "bad entry: %s\n", msg); 319 fprintf(stderr, "name: %s\n", myname(ep)); 320 fprintf(stderr, "parent name %s\n", myname(ep->e_parent)); 321 if (ep->e_sibling != NULL) 322 fprintf(stderr, "sibling name: %s\n", myname(ep->e_sibling)); 323 if (ep->e_entries != NULL) 324 fprintf(stderr, "next entry name: %s\n", myname(ep->e_entries)); 325 if (ep->e_links != NULL) 326 fprintf(stderr, "next link name: %s\n", myname(ep->e_links)); 327 if (ep->e_next != NULL) 328 fprintf(stderr, 329 "next hashchain name: %s\n", myname(ep->e_next)); 330 fprintf(stderr, "entry type: %s\n", 331 ep->e_type == NODE ? "NODE" : "LEAF"); 332 fprintf(stderr, "inode number: %ld\n", ep->e_ino); 333 panic("flags: %s\n", flagvalues(ep)); 334 } 335 336 /* 337 * Construct a string indicating the active flag bits of an entry. 338 */ 339 char * 340 flagvalues(ep) 341 register struct entry *ep; 342 { 343 static char flagbuf[BUFSIZ]; 344 345 (void) strcpy(flagbuf, "|NIL"); 346 flagbuf[0] = '\0'; 347 if (ep->e_flags & REMOVED) 348 (void) strcat(flagbuf, "|REMOVED"); 349 if (ep->e_flags & TMPNAME) 350 (void) strcat(flagbuf, "|TMPNAME"); 351 if (ep->e_flags & EXTRACT) 352 (void) strcat(flagbuf, "|EXTRACT"); 353 if (ep->e_flags & NEW) 354 (void) strcat(flagbuf, "|NEW"); 355 if (ep->e_flags & KEEP) 356 (void) strcat(flagbuf, "|KEEP"); 357 if (ep->e_flags & EXISTED) 358 (void) strcat(flagbuf, "|EXISTED"); 359 return (&flagbuf[1]); 360 } 361 362 /* 363 * Check to see if a name is on a dump tape. 364 */ 365 ino_t 366 dirlookup(name) 367 const char *name; 368 { 369 struct direct *dp; 370 ino_t ino; 371 372 ino = ((dp = pathsearch(name)) == NULL) ? 0 : dp->d_ino; 373 374 if (ino == 0 || TSTINO(ino, dumpmap) == 0) 375 fprintf(stderr, "%s is not on the tape\n", name); 376 return (ino); 377 } 378 379 /* 380 * Elicit a reply. 381 */ 382 int 383 reply(question) 384 char *question; 385 { 386 char c; 387 388 do { 389 fprintf(stderr, "%s? [yn] ", question); 390 (void) fflush(stderr); 391 c = getc(terminal); 392 while (c != '\n' && getc(terminal) != '\n') 393 if (feof(terminal)) 394 return (FAIL); 395 } while (c != 'y' && c != 'n'); 396 if (c == 'y') 397 return (GOOD); 398 return (FAIL); 399 } 400 401 /* 402 * handle unexpected inconsistencies 403 */ 404 #if __STDC__ 405 #include <stdarg.h> 406 #else 407 #include <varargs.h> 408 #endif 409 410 void 411 #if __STDC__ 412 panic(const char *fmt, ...) 413 #else 414 panic(fmt, va_alist) 415 char *fmt; 416 va_dcl 417 #endif 418 { 419 va_list ap; 420 #if __STDC__ 421 va_start(ap, fmt); 422 #else 423 va_start(ap); 424 #endif 425 426 vfprintf(stderr, fmt, ap); 427 if (yflag) 428 return; 429 if (reply("abort") == GOOD) { 430 if (reply("dump core") == GOOD) 431 abort(); 432 done(1); 433 } 434 } 435