110315Smckusick /* Copyright (c) 1983 Regents of the University of California */ 210315Smckusick 310315Smckusick #ifndef lint 4*11312Smckusick static char sccsid[] = "@(#)utilities.c 3.2 (Berkeley) 83/02/27"; 510315Smckusick #endif 610315Smckusick 710315Smckusick #include "restore.h" 810315Smckusick 910315Smckusick /* 1011131Smckusick * Move the contents of a directory to a new directory. 1111131Smckusick */ 1211131Smckusick movecontents(from, to) 1311131Smckusick struct entry *from, *to; 1411131Smckusick { 1511131Smckusick register struct entry *ep; 1611131Smckusick struct entry *np; 1711131Smckusick register char *targetp; 1811131Smckusick char target[BUFSIZ]; 1911131Smckusick 2011131Smckusick strcpy(target, myname(to)); 2111131Smckusick targetp = &target[strlen(target)]; 2211131Smckusick *targetp++ = '/'; 2311131Smckusick for (ep = from->e_entries; ep != NIL; ) { 2411131Smckusick strcpy(targetp, ep->e_name); 2511131Smckusick if (ep->e_flags & TMPNAME) 2611131Smckusick badentry(ep, "movecontents: found TMPNAME"); 2711131Smckusick np = lookupname(target); 2811131Smckusick if (np != NIL) 2911131Smckusick mktempname(np); 3011131Smckusick renameit(myname(ep), target); 3111131Smckusick np = ep->e_sibling; 3211131Smckusick moveentry(ep, target); 3311131Smckusick ep = np; 3411131Smckusick } 3511131Smckusick } 3611131Smckusick 3711131Smckusick /* 3810315Smckusick * Insure that all the components of a pathname exist. 3910315Smckusick */ 4011131Smckusick struct entry * 4111131Smckusick pathcheck(name, type) 4210315Smckusick char *name; 4311131Smckusick char type; 4410315Smckusick { 4510315Smckusick register char *cp; 4610315Smckusick struct entry *ep; 47*11312Smckusick char *start; 4810315Smckusick 4910315Smckusick start = index(name, '/'); 50*11312Smckusick if (start == 0) 5111131Smckusick return (lookupino(ROOTINO)); 5210315Smckusick for (cp = start; *cp != '\0'; cp++) { 5310315Smckusick if (*cp != '/') 5410315Smckusick continue; 5510315Smckusick *cp = '\0'; 5610315Smckusick ep = lookupname(name); 5710315Smckusick if (ep == NIL) { 5811131Smckusick ep = addentry(name, (ino_t)0, NODE); 5911131Smckusick ep->e_flags |= type; 6010315Smckusick newnode(ep); 6110315Smckusick } 6210315Smckusick *cp = '/'; 6310315Smckusick } 6411131Smckusick return (ep); 6510315Smckusick } 6610315Smckusick 6710315Smckusick /* 6810315Smckusick * Change a name to a unique temporary name. 6910315Smckusick */ 7010315Smckusick mktempname(ep) 7110315Smckusick register struct entry *ep; 7210315Smckusick { 7310315Smckusick char oldname[BUFSIZ]; 7410315Smckusick 7510315Smckusick if (ep->e_flags & TMPNAME) 7610315Smckusick badentry(ep, "mktempname: called with TMPNAME"); 7710315Smckusick ep->e_flags |= TMPNAME; 7810315Smckusick strcpy(oldname, myname(ep)); 7910315Smckusick ep->e_name[ep->e_namlen++] = TMPCHAR; 8010315Smckusick ep->e_name[ep->e_namlen] = '\0'; 8110315Smckusick renameit(oldname, myname(ep)); 8210315Smckusick } 8310315Smckusick 8410315Smckusick /* 8510315Smckusick * Rename a file or directory. 8610315Smckusick */ 8710315Smckusick renameit(from, to) 8810315Smckusick char *from, *to; 8910315Smckusick { 9010315Smckusick if (rename(from, to) < 0) { 9110315Smckusick perror("renameit"); 9210315Smckusick panic("Cannot rename %s to %s\n", from, to); 9310315Smckusick } 9410315Smckusick vprintf(stdout, "rename %s to %s\n", from, to); 9510315Smckusick } 9610315Smckusick 9710315Smckusick /* 9810315Smckusick * Create a new node (directory). 9910315Smckusick */ 10010315Smckusick newnode(np) 10110315Smckusick struct entry *np; 10210315Smckusick { 10310315Smckusick char *cp; 10410315Smckusick 10510315Smckusick if (np->e_type != NODE) 10610315Smckusick badentry(np, "newnode: not a node"); 10710315Smckusick cp = myname(np); 108*11312Smckusick if (mkdir(cp, 0777) < 0) { 10910315Smckusick perror("newnode"); 11010315Smckusick panic("Cannot make node %s\n", cp); 11110315Smckusick } 11210315Smckusick vprintf(stdout, "Make node %s\n", cp); 11310315Smckusick } 11410315Smckusick 11510315Smckusick /* 11610315Smckusick * Remove an old node (directory). 11710315Smckusick */ 11810315Smckusick removenode(ep) 11910315Smckusick register struct entry *ep; 12010315Smckusick { 12110315Smckusick char *cp; 12210315Smckusick 12310315Smckusick if (ep->e_type != NODE) 12410315Smckusick badentry(ep, "removenode: not a node"); 12510315Smckusick if (ep->e_entries != NIL) 12610315Smckusick badentry(ep, "removenode: non-empty directory"); 12710315Smckusick cp = myname(ep); 12810315Smckusick if (rmdir(cp) < 0) { 12910315Smckusick perror("removenode"); 13010315Smckusick panic("Cannot remove node %s\n", cp); 13110315Smckusick } 13210315Smckusick ep->e_flags |= REMOVED; 13311131Smckusick ep->e_flags &= ~(TMPNAME|TMPNODE); 13410315Smckusick vprintf(stdout, "Remove node %s\n", cp); 13510315Smckusick } 13610315Smckusick 13710315Smckusick /* 13810315Smckusick * Remove a leaf. 13910315Smckusick */ 14010315Smckusick removeleaf(ep) 14110315Smckusick register struct entry *ep; 14210315Smckusick { 14310315Smckusick char *cp; 14410315Smckusick 14510315Smckusick if (ep->e_type != LEAF) 14610315Smckusick badentry(ep, "removeleaf: not a leaf"); 14710315Smckusick cp = myname(ep); 14810315Smckusick if (unlink(cp) < 0) { 14910315Smckusick perror("removeleaf"); 15010315Smckusick panic("Cannot remove leaf %s\n", cp); 15110315Smckusick } 15210315Smckusick ep->e_flags |= REMOVED; 15310315Smckusick ep->e_flags &= ~TMPNAME; 15410315Smckusick vprintf(stdout, "Remove leaf %s\n", cp); 15510315Smckusick } 15610315Smckusick 15710315Smckusick /* 15810315Smckusick * Create a link. 15910315Smckusick */ 16010315Smckusick linkit(existing, new, type) 16110315Smckusick char *existing, *new; 16210315Smckusick int type; 16310315Smckusick { 16410315Smckusick 16510315Smckusick if (type == SYMLINK) { 16610315Smckusick if (symlink(existing, new) < 0) { 16710315Smckusick perror("linkit"); 16810315Smckusick panic("Cannot create symbolic link %s->%s\n", 16910315Smckusick new, existing); 17010315Smckusick } 17110315Smckusick } else if (type == HARDLINK) { 17210315Smckusick if (link(existing, new) < 0) { 17310315Smckusick perror("linkit"); 17410315Smckusick panic("Cannot create hard link %s->%s\n", 17510315Smckusick new, existing); 17610315Smckusick } 17710315Smckusick } else { 17810315Smckusick panic("linkit: unknown type %d\n", type); 17910315Smckusick } 18010315Smckusick vprintf(stdout, "Create %s link %s->%s\n", 18110315Smckusick type == SYMLINK ? "symbolic" : "hard", new, existing); 18210315Smckusick } 18310315Smckusick 18410315Smckusick /* 18510315Smckusick * find lowest number file (above "start") that needs to be extracted 18610315Smckusick */ 18710315Smckusick ino_t 18810315Smckusick lowerbnd(start) 18910315Smckusick ino_t start; 19010315Smckusick { 19110315Smckusick register struct entry *ep; 19210315Smckusick 19310315Smckusick for ( ; start < maxino; start++) { 19410315Smckusick ep = lookupino(start); 19510315Smckusick if (ep == NIL) 19610315Smckusick continue; 19711131Smckusick if (ep->e_flags & (NEW|EXTRACT|CHANGE)) 19810315Smckusick return (start); 19910315Smckusick } 20010315Smckusick return (start); 20110315Smckusick } 20210315Smckusick 20310315Smckusick /* 20410315Smckusick * find highest number file (below "start") that needs to be extracted 20510315Smckusick */ 20610315Smckusick ino_t 20710315Smckusick upperbnd(start) 20810315Smckusick ino_t start; 20910315Smckusick { 21010315Smckusick register struct entry *ep; 21110315Smckusick 21210315Smckusick for ( ; start > ROOTINO; start--) { 21310315Smckusick ep = lookupino(start); 21410315Smckusick if (ep == NIL) 21510315Smckusick continue; 21611131Smckusick if (ep->e_flags & (NEW|EXTRACT|CHANGE)) 21710315Smckusick return (start); 21810315Smckusick } 21910315Smckusick return (start); 22010315Smckusick } 22110315Smckusick 22210315Smckusick /* 22310315Smckusick * report on a badly formed entry 22410315Smckusick */ 22510315Smckusick badentry(ep, msg) 22610315Smckusick register struct entry *ep; 22710315Smckusick char *msg; 22810315Smckusick { 22910315Smckusick char flagbuf[BUFSIZ]; 23010315Smckusick 23110315Smckusick fprintf(stderr, "bad entry: %s\n", msg); 23210315Smckusick fprintf(stderr, "name: %s\n", myname(ep)); 23311131Smckusick if (ep->e_newname != NULL) 23411131Smckusick fprintf(stderr, "new name: %s\n", ep->e_newname); 23510315Smckusick fprintf(stderr, "parent name %s\n", myname(ep->e_parent)); 23610315Smckusick if (ep->e_sibling != NIL) 23710315Smckusick fprintf(stderr, "sibling name: %s\n", myname(ep->e_sibling)); 23810315Smckusick if (ep->e_entries != NIL) 23910315Smckusick fprintf(stderr, "next entry name: %s\n", myname(ep->e_entries)); 24010315Smckusick if (ep->e_links != NIL) 24110315Smckusick fprintf(stderr, "next link name: %s\n", myname(ep->e_links)); 24210315Smckusick fprintf(stderr, "entry type: %s\n", 24310315Smckusick ep->e_type == NODE ? "NODE" : "LEAF"); 24410315Smckusick fprintf(stderr, "inode number: %ld\n", ep->e_ino); 24510315Smckusick strcpy(flagbuf, "|NIL"); 24610315Smckusick flagbuf[0] = '\0'; 24711131Smckusick if (ep->e_flags & REMOVE) 24811131Smckusick strcat(flagbuf, "|REMOVE"); 24910315Smckusick if (ep->e_flags & REMOVED) 25010315Smckusick strcat(flagbuf, "|REMOVED"); 25111131Smckusick if (ep->e_flags & RENAME) 25211131Smckusick strcat(flagbuf, "|RENAME"); 25310315Smckusick if (ep->e_flags & TMPNAME) 25410315Smckusick strcat(flagbuf, "|TMPNAME"); 25511131Smckusick if (ep->e_flags & TMPNODE) 25611131Smckusick strcat(flagbuf, "|TMPNODE"); 25710315Smckusick if (ep->e_flags & EXTRACT) 25810315Smckusick strcat(flagbuf, "|EXTRACT"); 25911131Smckusick if (ep->e_flags & RENUMBER) 26011131Smckusick strcat(flagbuf, "|RENUMBER"); 26111131Smckusick if (ep->e_flags & CHANGE) 26211131Smckusick strcat(flagbuf, "|CHANGE"); 26310315Smckusick if (ep->e_flags & NEW) 26410315Smckusick strcat(flagbuf, "|NEW"); 26510315Smckusick if (ep->e_flags & KEEP) 26610315Smckusick strcat(flagbuf, "|KEEP"); 26710315Smckusick panic("flags: %s\n", &flagbuf[1]); 26810315Smckusick } 26910315Smckusick 27010315Smckusick /* 27111131Smckusick * respond to interrupts 27210315Smckusick */ 27311131Smckusick onintr() 27410315Smckusick { 27511131Smckusick if (reply("restore interrupted, continue")) 27611131Smckusick return; 27711131Smckusick done(1); 27811131Smckusick } 27910315Smckusick 28011131Smckusick /* 28111131Smckusick * handle unexpected inconsistencies 28211131Smckusick */ 28311131Smckusick /* VARARGS1 */ 28411131Smckusick panic(msg, d1, d2) 28511131Smckusick char *msg; 28611131Smckusick long d1, d2; 28711131Smckusick { 28811131Smckusick 28911131Smckusick fprintf(stderr, msg, d1, d2); 29011131Smckusick if (reply("abort")) 29111131Smckusick abort(); 29210315Smckusick } 29310315Smckusick 29410315Smckusick /* 29510315Smckusick * elicit a reply 29610315Smckusick */ 29710315Smckusick reply(question) 29810315Smckusick char *question; 29910315Smckusick { 30010315Smckusick char c; 30110315Smckusick 30210315Smckusick fprintf(stderr, "%s? ", question); 30310315Smckusick do { 30410315Smckusick fprintf(stderr, "[yn] "); 30510315Smckusick c = getchar(); 30610315Smckusick while (c != '\n' && getchar() != '\n') 30710315Smckusick /* void */; 30810315Smckusick } while (c != 'y' && c != 'n'); 30910315Smckusick if (c == 'y') 31010315Smckusick return (GOOD); 31110315Smckusick return (FAIL); 31210315Smckusick } 313