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