#include "restore.h"
#undef MAXNAMLEN
#include <dirent.h>
static char *keyval(int);
static void removexattrs(struct entry *);
static void movexattrs(char *, char *);
long
listfile(char *name, ino_t ino, int type)
{
long descend = hflag ? GOOD : FAIL;
if (BIT(ino, dumpmap) == 0) {
return (descend);
}
vprintf(stdout, "%s", type == LEAF ? gettext("leaf") : gettext("dir "));
(void) fprintf(stdout, "%10lu\t%s\n", 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 (BIT(ino, dumpmap) == 0) {
if (mflag) {
dprintf(stdout, gettext(
"%s: not on the volume\n"), name);
} else {
dprintf(stdout, gettext(
"inode %llu: not on the volume\n"),
(u_longlong_t)ino);
}
return (descend);
}
if (!mflag) {
(void) snprintf(buf, sizeof (buf), "./%llu", (u_longlong_t)ino);
buf[sizeof (buf) - 1] = '\0';
name = buf;
if (type == NODE) {
(void) genliteraldir(name, ino);
return (descend);
}
}
ep = lookupino(ino);
if (ep != NIL) {
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 (BIT(ino, dumpmap) == 0) {
return (descend);
}
ep = lookupino(ino);
if (ep != NIL) {
ep->e_flags &= ~NEW;
}
return (descend);
}
static struct entry *removelist;
void
removeoldleaves(void)
{
struct entry *ep;
ino_t i;
vprintf(stdout, gettext("Mark entries to be removed.\n"));
for (i = ROOTINO + 1; i < maxino; i++) {
if (BIT(i, clrimap))
continue;
ep = lookupino(i);
if (ep == NIL)
continue;
while (ep != NIL) {
dprintf(stdout, gettext("%s: REMOVE\n"), myname(ep));
removexattrs(ep->e_xattrs);
if (ep->e_type == LEAF) {
removeleaf(ep);
freeentry(ep);
} else {
mktempname(ep);
deleteino(ep->e_ino);
ep->e_next = removelist;
removelist = ep;
}
ep = ep->e_links;
}
}
}
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 (BIT(ino, dumpmap))
key |= ONTAPE;
np = lookupname(name);
if (np != NIL) {
key |= NAMEFND;
ip = lookupino(np->e_ino);
if (ip == NULL) {
(void) fprintf(stderr,
gettext("corrupted symbol table\n"));
done(1);
}
if (ip != np)
lookuptype = LINK;
}
ip = lookupino(ino);
if (ip != NIL) {
key |= INOFND;
for (ep = ip->e_links; ep != NIL; ep = ep->e_links) {
if (ep == np) {
if (ip->e_flags & EXTRACT) {
if (key & NAMEFND) {
removeleaf(np);
freeentry(np);
np = NIL;
key &= ~NAMEFND;
}
ep->e_flags |= NEW;
} else {
ip = ep;
}
break;
}
}
}
if (((key & (INOFND|NAMEFND)) == (INOFND|NAMEFND)) && ip != np) {
if (lookuptype == LINK) {
removeleaf(np);
freeentry(np);
} else {
dprintf(stdout,
gettext("name/inode conflict, mktempname %s\n"),
myname(np));
mktempname(np);
}
np = NIL;
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 || key == (ONTAPE|NAMEFND)) {
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;
(void) fprintf(stderr, gettext(
"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 (type == LEAF) {
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, gettext("cannot KEEP and change modes"));
break;
}
if (ip->e_type == LEAF) {
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, gettext("[%s] %s: Extraneous name\n"),
keyval(key), name);
descend = FAIL;
break;
case 0:
(void) fprintf(stderr,
gettext("%s: (inode %lu) not found on volume\n"),
name, ino);
break;
case INOFND|NAMEFND|MODECHG:
case NAMEFND|MODECHG:
case INOFND|MODECHG:
(void) fprintf(stderr, "[%s] %s: %s\n",
keyval(key), name, gettext("inconsistent state"));
done(1);
case ONTAPE|MODECHG:
case MODECHG:
default:
(void) fprintf(stderr, "[%s] %s: %s\n",
keyval(key), name, gettext("impossible state"));
done(1);
}
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, gettext("Find unreferenced names.\n"));
for (i = ROOTINO; i < maxino; i++) {
ep = lookupino(i);
if (ep == NIL || ep->e_type == LEAF || BIT(i, dumpmap) == 0)
continue;
for (np = ep->e_entries; np != NIL; np = np->e_sibling) {
if (np->e_flags == 0) {
dprintf(stdout, gettext(
"%s: remove unreferenced name\n"),
myname(np));
removeleaf(np);
freeentry(np);
}
}
}
for (ep = removelist; ep != NIL; ep = ep->e_next) {
for (np = ep->e_entries; np != NIL; np = np->e_sibling) {
if (np->e_type == LEAF) {
if (np->e_flags != 0)
badentry(np, gettext(
"unreferenced with flags"));
dprintf(stdout, gettext(
"%s: remove unreferenced name\n"),
myname(np));
removeleaf(np);
freeentry(np);
}
}
}
}
void
removeoldnodes(void)
{
struct entry *ep, **prev;
long change;
vprintf(stdout, gettext("Remove old nodes (directories).\n"));
do {
change = 0;
prev = &removelist;
for (ep = removelist; ep != NIL; ep = *prev) {
if (ep->e_entries != NIL) {
prev = &ep->e_next;
continue;
}
*prev = ep->e_next;
removenode(ep);
freeentry(ep);
change++;
}
} while (change);
for (ep = removelist; ep != NIL; ep = ep->e_next)
badentry(ep, gettext("cannot remove, non-empty"));
}
void
createleaves(char *symtabfile)
{
struct entry *ep;
char name[MAXCOMPLEXLEN];
ino_t first;
int curvol;
if (command == 'R') {
vprintf(stdout, gettext("Continue extraction of new leaves\n"));
} else {
vprintf(stdout, gettext("Extract new leaves.\n"));
dumpsymtable(symtabfile, volno);
}
first = lowerbnd(ROOTINO);
curvol = volno;
while (curfile.ino < maxino) {
first = lowerbnd(first);
while (first < curfile.ino) {
ep = lookupino(first);
if (ep == NIL) {
(void) fprintf(stderr,
gettext("%d: bad first\n"), first);
done(1);
}
(void) fprintf(stderr,
gettext("%s: not found on volume\n"),
myname(ep));
ep->e_flags &= ~(NEW|EXTRACT);
first = lowerbnd(first);
}
if (first != curfile.ino) {
(void) fprintf(stderr,
gettext("expected next file %d, got %d\n"),
first, curfile.ino);
skipfile();
goto next;
}
ep = lookupino(curfile.ino);
if (ep == NIL) {
(void) fprintf(stderr,
gettext("unknown file on volume\n"));
done(1);
}
if ((ep->e_flags & (NEW|EXTRACT)) == 0)
badentry(ep, gettext("unexpected file on volume"));
if ((ep->e_flags & EXTRACT) != 0) {
char *sname = savename(ep->e_name);
complexcpy(name, myname(ep), MAXCOMPLEXLEN);
mktempname(ep);
(void) extractfile(name);
movexattrs(myname(ep), name);
removeleaf(ep);
freename(ep->e_name);
ep->e_name = sname;
ep->e_namlen = strlen(ep->e_name);
ep->e_flags &= ~REMOVED;
} else {
(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;
int curvol, nextvol;
vprintf(stdout, gettext("Extract requested files\n"));
first = lowerbnd(ROOTINO);
last = upperbnd(maxino - 1);
nextvol = volnumber(first);
if (nextvol == 0) {
curfile.action = SKIP;
getvol(1);
skipmaps();
skipdirs();
}
for (;;) {
first = lowerbnd(first);
last = upperbnd(last);
if (first > last)
return;
if (nextvol > 0) {
nextvol = volnumber(first);
if (nextvol != volno) {
curfile.action = UNKNOWN;
getvol(nextvol);
skipmaps();
}
}
while (curfile.ino > last) {
curfile.action = SKIP;
getvol(0);
skipmaps();
skipdirs();
}
next = lowerbnd(curfile.ino);
do {
curvol = volno;
while (next > curfile.ino && volno == curvol)
skipfile();
skipmaps();
skipdirs();
} while (volno == curvol + 1);
if (volno != curvol)
continue;
while (next < curfile.ino) {
ep = lookupino(next);
if (ep == NIL) {
(void) fprintf(stderr,
gettext("corrupted symbol table\n"));
done(1);
}
(void) fprintf(stderr,
gettext("%s: not found on volume\n"),
myname(ep));
ep->e_flags &= ~NEW;
next = lowerbnd(next);
}
if (next == curfile.ino && next <= last) {
ep = lookupino(next);
if (ep == NIL) {
(void) fprintf(stderr,
gettext("corrupted symbol table\n"));
done(1);
}
(void) extractfile(myname(ep));
ep->e_flags &= ~NEW;
if (volno != curvol)
skipmaps();
}
}
}
void
createlinks(void)
{
struct entry *np, *ep;
ino_t i;
int dfd;
char *to, *from;
int saverr;
vprintf(stdout, gettext("Add links\n"));
for (i = ROOTINO; i < maxino; i++) {
ep = lookupino(i);
if (ep == NIL)
continue;
to = savename(myname(ep));
for (np = ep->e_links; np != NIL; np = np->e_links) {
if ((np->e_flags & NEW) == 0)
continue;
resolve(myname(np), &dfd, &from);
if (dfd != AT_FDCWD) {
if (fchdir(dfd) < 0) {
saverr = errno;
(void) fprintf(stderr,
gettext(
"%s->%s: link failed: %s\n"),
from, to, strerror(saverr));
(void) close(dfd);
continue;
}
}
if (ep->e_type == NODE) {
(void) lf_linkit(to, from, SYMLINK);
} else {
(void) lf_linkit(to, from, HARDLINK);
}
np->e_flags &= ~NEW;
if (dfd != AT_FDCWD) {
fchdir(savepwd);
(void) close(dfd);
}
}
freename(to);
}
}
void
checkrestore(void)
{
struct entry *ep;
ino_t i;
vprintf(stdout, gettext("Check the symbol table.\n"));
for (i = ROOTINO; i < maxino; i++) {
for (ep = lookupino(i); ep != NIL; ep = ep->e_links) {
ep->e_flags &= ~KEEP;
if (ep->e_type == NODE) {
ep->e_flags &= ~(NEW|EXISTED);
}
if ((ep->e_flags & ~(XATTR|XATTRROOT)) != 0)
badentry(ep, gettext("incomplete operations"));
}
}
}
long
verifyfile(char *name, ino_t ino, int type)
{
struct entry *np, *ep;
long descend = GOOD;
ep = lookupname(name);
if (ep == NIL) {
(void) fprintf(stderr,
gettext("Warning: missing name %s\n"), name);
return (FAIL);
}
np = lookupino(ino);
if (np != ep)
descend = FAIL;
for (; np != NIL; np = np->e_links)
if (np == ep)
break;
if (np == NIL) {
(void) fprintf(stderr, gettext("missing inumber %d\n"), ino);
done(1);
}
if (ep->e_type == LEAF && type != LEAF)
badentry(ep, gettext("type should be LEAF"));
return (descend);
}
static void
removexattrs(struct entry *ep)
{
struct entry *np = ep;
if (ep == NIL)
return;
for (np = ep->e_entries; np != NIL; np = np->e_sibling) {
if (np->e_type == NODE) {
removexattrs(np);
} else {
np->e_flags |= REMOVED;
freeentry(np);
}
}
ep->e_flags |= REMOVED;
freeentry(ep);
}
static void
movexattrs(char *orig, char *targ)
{
char *to, *from;
int fromfd, fromdir, tofd, todir, tfd;
DIR *dirp = NULL;
struct dirent *dp = NULL;
fromfd = tofd = fromdir = todir = tfd = -1;
resolve(orig, &tfd, &from);
if (tfd == AT_FDCWD && pathconf(orig, _PC_XATTR_EXISTS) != 1) {
return;
}
if ((fromfd = openat64(tfd, from, O_RDONLY|O_NONBLOCK)) == -1) {
fprintf(stderr, gettext("%s: cannot move attributes: "), from);
perror("");
if (tfd != AT_FDCWD) (void) close(tfd);
goto out;
}
if (fpathconf(fromfd, _PC_XATTR_EXISTS) != 1) {
if (tfd != AT_FDCWD) (void) close(tfd);
goto out;
}
if ((fromdir = openat64(fromfd, ".",
O_RDONLY|O_NONBLOCK|O_XATTR)) == -1) {
fprintf(stderr, gettext("%s: cannot access attributes: "),
from);
perror("");
if (tfd != AT_FDCWD)
(void) close(tfd);
goto out;
}
if (tfd != AT_FDCWD) (void) close(tfd);
resolve(targ, &tfd, &to);
if ((tofd = openat64(tfd, to, O_RDONLY|O_NONBLOCK)) == -1 ||
(todir = openat64(tofd, ".", O_RDONLY|O_NONBLOCK|O_XATTR)) == -1) {
fprintf(stderr, gettext("%s: cannot create attributes: "), to);
perror("");
goto out;
}
if (tfd != AT_FDCWD) (void) close(tfd);
(void) close(tofd);
if ((tfd = dup(fromdir)) == -1 ||
(dirp = fdopendir(tfd)) == NULL) {
fprintf(stderr,
gettext("%s: cannot allocate DIR structure to attribute "
"directory: "), from);
perror("");
if (tfd != -1) (void) close(tfd);
goto out;
}
while ((dp = readdir(dirp)) != NULL) {
if ((dp->d_name[0] == '.' && dp->d_name[1] == '\0') ||
(dp->d_name[0] == '.' && dp->d_name[1] == '.' &&
dp->d_name[2] == '\0'))
continue;
if ((renameat(fromdir, dp->d_name, todir, dp->d_name)) == -1) {
fprintf(stderr,
gettext("%s: cannot move attribute %s: "),
from, dp->d_name);
goto out;
}
}
out:
if (fromfd != -1)
(void) close(fromfd);
if (tofd != -1)
(void) close(tofd);
if (dirp != NULL)
(void) closedir(dirp);
if (fromdir != -1)
(void) close(fromdir);
if (todir != -1)
(void) close(todir);
}