#include <sys/types.h>
#include <limits.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <ufs/ufs/dinode.h>
#include "restore.h"
#include "extern.h"
static char *keyval(int);
long
listfile(char *name, ino_t ino, int type)
{
long descend = hflag ? GOOD : FAIL;
if (TSTINO(ino, dumpmap) == 0)
return (descend);
vprintf(stdout, "%s", type == LEAF ? "leaf" : "dir ");
fprintf(stdout, "%10ju\t%s\n", (uintmax_t)ino, name);
return (descend);
}
long
addfile(char *name, ino_t ino, int type)
{
struct entry *ep;
long descend = hflag ? GOOD : FAIL;
char buf[100];
if (TSTINO(ino, dumpmap) == 0) {
dprintf(stdout, "%s: not on the tape\n", name);
return (descend);
}
if (ino == UFS_WINO && command == 'i' && !vflag)
return (descend);
if (!mflag) {
(void) sprintf(buf, "./%ju", (uintmax_t)ino);
name = buf;
if (type == NODE) {
(void) genliteraldir(name, ino);
return (descend);
}
}
ep = lookupino(ino);
if (ep != NULL) {
if (strcmp(name, myname(ep)) == 0) {
ep->e_flags |= NEW;
return (descend);
}
type |= LINK;
}
ep = addentry(name, ino, type);
if (type == NODE)
newnode(ep);
ep->e_flags |= NEW;
return (descend);
}
long
deletefile(char *name, ino_t ino, int type)
{
long descend = hflag ? GOOD : FAIL;
struct entry *ep;
if (TSTINO(ino, dumpmap) == 0)
return (descend);
ep = lookupname(name);
if (ep != NULL) {
ep->e_flags &= ~NEW;
ep->e_flags |= REMOVED;
if (ep->e_type != NODE)
freeentry(ep);
}
return (descend);
}
static struct entry *removelist;
void
removeoldleaves(void)
{
struct entry *ep, *nextep;
ino_t i, mydirino;
vprintf(stdout, "Mark entries to be removed.\n");
if ((ep = lookupino(UFS_WINO))) {
vprintf(stdout, "Delete whiteouts\n");
for ( ; ep != NULL; ep = nextep) {
nextep = ep->e_links;
mydirino = ep->e_parent->e_ino;
if (TSTINO(mydirino, usedinomap) &&
!TSTINO(mydirino, dumpmap))
continue;
delwhiteout(ep);
freeentry(ep);
}
}
for (i = UFS_ROOTINO + 1; i < maxino; i++) {
ep = lookupino(i);
if (ep == NULL)
continue;
if (TSTINO(i, usedinomap))
continue;
for ( ; ep != NULL; ep = ep->e_links) {
dprintf(stdout, "%s: REMOVE\n", myname(ep));
if (ep->e_type == LEAF) {
removeleaf(ep);
freeentry(ep);
} else {
mktempname(ep);
deleteino(ep->e_ino);
ep->e_next = removelist;
removelist = ep;
}
}
}
}
long
nodeupdates(char *name, ino_t ino, int type)
{
struct entry *ep, *np, *ip;
long descend = GOOD;
int lookuptype = 0;
int key = 0;
# define ONTAPE 0x1
# define INOFND 0x2
# define NAMEFND 0x4
# define MODECHG 0x8
if (TSTINO(ino, dumpmap))
key |= ONTAPE;
np = lookupname(name);
if (np != NULL) {
key |= NAMEFND;
ip = lookupino(np->e_ino);
if (ip == NULL)
panic("corrupted symbol table\n");
if (ip != np)
lookuptype = LINK;
}
ip = lookupino(ino);
if (ip != NULL) {
key |= INOFND;
for (ep = ip->e_links; ep != NULL; ep = ep->e_links) {
if (ep == np) {
ip = ep;
break;
}
}
}
if (((key & (INOFND|NAMEFND)) == (INOFND|NAMEFND)) && ip != np) {
if (lookuptype == LINK) {
removeleaf(np);
freeentry(np);
} else {
dprintf(stdout, "name/inode conflict, mktempname %s\n",
myname(np));
mktempname(np);
}
np = NULL;
key &= ~NAMEFND;
}
if ((key & ONTAPE) &&
(((key & INOFND) && ip->e_type != type) ||
((key & NAMEFND) && np->e_type != type)))
key |= MODECHG;
switch (key) {
case INOFND|NAMEFND:
ip->e_flags |= KEEP;
dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
flagvalues(ip));
break;
case ONTAPE|NAMEFND:
case ONTAPE|NAMEFND|MODECHG:
if (lookuptype == LINK) {
removeleaf(np);
freeentry(np);
} else {
mktempname(np);
}
case ONTAPE:
ep = addentry(name, ino, type);
if (type == NODE)
newnode(ep);
ep->e_flags |= NEW|KEEP;
dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
flagvalues(ep));
break;
case ONTAPE|INOFND:
if (type == LEAF && (ip->e_flags & KEEP) == 0)
ip->e_flags |= EXTRACT;
case INOFND:
if ((ip->e_flags & KEEP) == 0) {
renameit(myname(ip), name);
moveentry(ip, name);
ip->e_flags |= KEEP;
dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
flagvalues(ip));
break;
}
if (ip->e_type == NODE) {
descend = FAIL;
fprintf(stderr,
"deleted hard link %s to directory %s\n",
name, myname(ip));
break;
}
ep = addentry(name, ino, type|LINK);
ep->e_flags |= NEW;
dprintf(stdout, "[%s] %s: %s|LINK\n", keyval(key), name,
flagvalues(ep));
break;
case ONTAPE|INOFND|NAMEFND:
if (lookuptype == LINK) {
removeleaf(np);
freeentry(np);
ep = addentry(name, ino, type|LINK);
if (type == NODE)
newnode(ep);
ep->e_flags |= NEW|KEEP;
dprintf(stdout, "[%s] %s: %s|LINK\n", keyval(key), name,
flagvalues(ep));
break;
}
if (type == LEAF && lookuptype != LINK)
np->e_flags |= EXTRACT;
np->e_flags |= KEEP;
dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
flagvalues(np));
break;
case ONTAPE|INOFND|MODECHG:
case ONTAPE|INOFND|NAMEFND|MODECHG:
if (ip->e_flags & KEEP) {
badentry(ip, "cannot KEEP and change modes");
break;
}
if (ip->e_type == LEAF) {
for (ip = lookupino(ino); ip != NULL; ip = ip->e_links) {
if (ip->e_type != LEAF)
badentry(ip, "NODE and LEAF links to same inode");
removeleaf(ip);
freeentry(ip);
}
ip = addentry(name, ino, type);
newnode(ip);
} else {
if ((ip->e_flags & TMPNAME) == 0)
mktempname(ip);
deleteino(ip->e_ino);
ip->e_next = removelist;
removelist = ip;
ip = addentry(name, ino, type);
}
ip->e_flags |= NEW|KEEP;
dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
flagvalues(ip));
break;
case NAMEFND:
dprintf(stdout, "[%s] %s: Extraneous name\n", keyval(key),
name);
descend = FAIL;
break;
case 0:
fprintf(stderr, "%s: (inode %ju) not found on tape\n",
name, (uintmax_t)ino);
break;
case INOFND|NAMEFND|MODECHG:
case NAMEFND|MODECHG:
case INOFND|MODECHG:
fprintf(stderr, "[%s] %s: inconsistent state\n", keyval(key),
name);
break;
case ONTAPE|MODECHG:
case MODECHG:
default:
panic("[%s] %s: impossible state\n", keyval(key), name);
break;
}
return (descend);
}
static char *
keyval(int key)
{
static char keybuf[32];
(void) strcpy(keybuf, "|NIL");
keybuf[0] = '\0';
if (key & ONTAPE)
(void) strcat(keybuf, "|ONTAPE");
if (key & INOFND)
(void) strcat(keybuf, "|INOFND");
if (key & NAMEFND)
(void) strcat(keybuf, "|NAMEFND");
if (key & MODECHG)
(void) strcat(keybuf, "|MODECHG");
return (&keybuf[1]);
}
void
findunreflinks(void)
{
struct entry *ep, *np;
ino_t i;
vprintf(stdout, "Find unreferenced names.\n");
for (i = UFS_ROOTINO; i < maxino; i++) {
ep = lookupino(i);
if (ep == NULL || ep->e_type == LEAF || TSTINO(i, dumpmap) == 0)
continue;
for (np = ep->e_entries; np != NULL; np = np->e_sibling) {
if (np->e_flags == 0) {
dprintf(stdout,
"%s: remove unreferenced name\n",
myname(np));
removeleaf(np);
freeentry(np);
}
}
}
for (ep = removelist; ep != NULL; ep = ep->e_next) {
for (np = ep->e_entries; np != NULL; np = np->e_sibling) {
if (np->e_type == LEAF) {
if (np->e_flags != 0)
badentry(np, "unreferenced with flags");
dprintf(stdout,
"%s: remove unreferenced name\n",
myname(np));
removeleaf(np);
freeentry(np);
}
}
}
}
void
removeoldnodes(void)
{
struct entry *ep, **prev;
long change;
vprintf(stdout, "Remove old nodes (directories).\n");
do {
change = 0;
prev = &removelist;
for (ep = removelist; ep != NULL; ep = *prev) {
if (ep->e_entries != NULL) {
prev = &ep->e_next;
continue;
}
*prev = ep->e_next;
removenode(ep);
freeentry(ep);
change++;
}
} while (change);
for (ep = removelist; ep != NULL; ep = ep->e_next)
badentry(ep, "cannot remove, non-empty");
}
void
createleaves(char *symtabfile)
{
struct entry *ep;
ino_t first;
long curvol;
if (command == 'R') {
vprintf(stdout, "Continue extraction of new leaves\n");
} else {
vprintf(stdout, "Extract new leaves.\n");
dumpsymtable(symtabfile, volno);
}
first = lowerbnd(UFS_ROOTINO);
curvol = volno;
while (curfile.ino < maxino) {
first = lowerbnd(first);
while (first < curfile.ino) {
ep = lookupino(first);
if (ep == NULL)
panic("%ju: bad first\n", (uintmax_t)first);
fprintf(stderr, "%s: not found on tape\n", myname(ep));
ep->e_flags &= ~(NEW|EXTRACT);
first = lowerbnd(first);
}
if (first != curfile.ino) {
fprintf(stderr, "expected next file %ju, got %ju\n",
(uintmax_t)first, (uintmax_t)curfile.ino);
skipfile();
goto next;
}
ep = lookupino(curfile.ino);
if (ep == NULL)
panic("unknown file on tape\n");
if ((ep->e_flags & (NEW|EXTRACT)) == 0)
badentry(ep, "unexpected file on tape");
if ((ep->e_flags & EXTRACT) != 0) {
removeleaf(ep);
ep->e_flags &= ~REMOVED;
}
(void) extractfile(myname(ep));
ep->e_flags &= ~(NEW|EXTRACT);
next:
if (curvol != volno) {
dumpsymtable(symtabfile, volno);
skipmaps();
curvol = volno;
}
}
}
void
createfiles(void)
{
ino_t first, next, last;
struct entry *ep;
long curvol;
vprintf(stdout, "Extract requested files\n");
curfile.action = SKIP;
getvol((long)1);
skipmaps();
skipdirs();
first = lowerbnd(UFS_ROOTINO);
last = upperbnd(maxino - 1);
for (;;) {
curvol = volno;
first = lowerbnd(first);
last = upperbnd(last);
if (first > last)
return;
if (Dflag) {
if (curfile.ino == maxino)
return;
if((ep = lookupino(curfile.ino)) != NULL &&
(ep->e_flags & (NEW|EXTRACT))) {
goto justgetit;
} else {
skipfile();
continue;
}
}
if (curfile.ino > last && curfile.ino < maxino && volno > 1) {
curfile.action = SKIP;
getvol((long)0);
skipmaps();
skipdirs();
continue;
}
if (curfile.ino < maxino) {
next = lowerbnd(curfile.ino);
while (next > curfile.ino && volno == curvol)
skipfile();
if (volno != curvol) {
skipmaps();
skipdirs();
continue;
}
} else {
next = first;
}
while (next < curfile.ino) {
ep = lookupino(next);
if (ep == NULL)
panic("corrupted symbol table\n");
fprintf(stderr, "%s: not found on tape\n", myname(ep));
ep->e_flags &= ~NEW;
next = lowerbnd(next);
}
if (next == curfile.ino && next <= last) {
ep = lookupino(next);
if (ep == NULL)
panic("corrupted symbol table\n");
justgetit:
(void) extractfile(myname(ep));
ep->e_flags &= ~NEW;
if (volno != curvol)
skipmaps();
}
}
}
void
createlinks(void)
{
struct entry *np, *ep;
ino_t i;
char name[BUFSIZ];
if ((ep = lookupino(UFS_WINO))) {
vprintf(stdout, "Add whiteouts\n");
for ( ; ep != NULL; ep = ep->e_links) {
if ((ep->e_flags & NEW) == 0)
continue;
(void) addwhiteout(myname(ep));
ep->e_flags &= ~NEW;
}
}
vprintf(stdout, "Add links\n");
for (i = UFS_ROOTINO; i < maxino; i++) {
ep = lookupino(i);
if (ep == NULL)
continue;
for (np = ep->e_links; np != NULL; np = np->e_links) {
if ((np->e_flags & NEW) == 0)
continue;
(void) strcpy(name, myname(ep));
if (ep->e_type == NODE) {
(void) linkit(name, myname(np), SYMLINK);
} else {
(void) linkit(name, myname(np), HARDLINK);
}
np->e_flags &= ~NEW;
}
}
}
void
checkrestore(void)
{
struct entry *ep;
ino_t i;
vprintf(stdout, "Check the symbol table.\n");
for (i = UFS_WINO; i < maxino; i++) {
for (ep = lookupino(i); ep != NULL; ep = ep->e_links) {
ep->e_flags &= ~KEEP;
if (ep->e_type == NODE)
ep->e_flags &= ~(NEW|EXISTED);
if (ep->e_flags != 0)
badentry(ep, "incomplete operations");
}
}
}
long
verifyfile(char *name, ino_t ino, int type)
{
struct entry *np, *ep;
long descend = GOOD;
ep = lookupname(name);
if (ep == NULL) {
fprintf(stderr, "Warning: missing name %s\n", name);
return (FAIL);
}
np = lookupino(ino);
if (np != ep)
descend = FAIL;
for ( ; np != NULL; np = np->e_links)
if (np == ep)
break;
if (np == NULL)
panic("missing inumber %ju\n", (uintmax_t)ino);
if (ep->e_type == LEAF && type != LEAF)
badentry(ep, "type should be LEAF");
return (descend);
}