#include <sys/param.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <dirent.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "makefs.h"
#include "mtree.h"
#include "extern.h"
static void apply_specdir(const char *, NODE *, fsnode *, int);
static void apply_specentry(const char *, NODE *, fsnode *);
static fsnode *create_fsnode(const char *, const char *, const char *,
struct stat *);
static int
cmp(const void *_a, const void *_b)
{
const fsnode * const *a = _a;
const fsnode * const *b = _b;
assert(strcmp((*a)->name, (*b)->name) != 0);
if (strcmp((*a)->name, ".") == 0)
return (-1);
if (strcmp((*b)->name, ".") == 0)
return (1);
return (strcoll((*a)->name, (*b)->name));
}
static fsnode *
sort_dir(fsnode *list)
{
fsnode **array;
fsnode *cur;
size_t nitems, i;
nitems = 0;
for (cur = list; cur != NULL; cur = cur->next)
nitems++;
assert(nitems > 0);
array = malloc(nitems * sizeof(fsnode *));
if (array == NULL)
err(1, "malloc");
for (i = 0, cur = list; cur != NULL; i++, cur = cur->next)
array[i] = cur;
qsort(array, nitems, sizeof(fsnode *), cmp);
for (i = 0; i < nitems; i++) {
array[i]->first = array[0];
array[i]->next = i == nitems - 1 ? NULL : array[i + 1];
}
cur = array[0];
free(array);
return (cur);
}
fsnode *
walk_dir(const char *root, const char *dir, fsnode *parent, fsnode *join)
{
fsnode *first, *cur;
DIR *dirp;
struct dirent *dent;
char path[MAXPATHLEN + 1];
struct stat stbuf;
char *name, *rp;
size_t len;
int dot;
assert(root != NULL);
assert(dir != NULL);
len = snprintf(path, sizeof(path), "%s/%s", root, dir);
if (len >= sizeof(path))
errx(1, "Pathname too long.");
if (debug & DEBUG_WALK_DIR)
printf("walk_dir: %s %p\n", path, parent);
if ((dirp = opendir(path)) == NULL)
err(1, "Can't opendir `%s'", path);
rp = path + strlen(root) + 1;
if (join != NULL) {
first = cur = join;
while (cur->next != NULL)
cur = cur->next;
} else
first = NULL;
while ((dent = readdir(dirp)) != NULL) {
name = dent->d_name;
dot = 0;
if (name[0] == '.')
switch (name[1]) {
case '\0':
if (join != NULL)
continue;
dot = 1;
break;
case '.':
if (name[2] == '\0')
continue;
default:
dot = 0;
}
if (debug & DEBUG_WALK_DIR_NODE)
printf("scanning %s/%s/%s\n", root, dir, name);
if ((size_t)snprintf(path + len, sizeof(path) - len, "/%s",
name) >= sizeof(path) - len)
errx(1, "Pathname too long.");
if (lstat(path, &stbuf) == -1)
err(1, "Can't lstat `%s'", path);
#ifdef S_ISSOCK
if (S_ISSOCK(stbuf.st_mode & S_IFMT)) {
if (debug & DEBUG_WALK_DIR_NODE)
printf(" skipping socket %s\n", path);
continue;
}
#endif
if (join != NULL) {
cur = join->next;
for (;;) {
if (cur == NULL || strcmp(cur->name, name) == 0)
break;
cur = cur->next;
}
if (cur != NULL) {
if (S_ISDIR(cur->type) &&
S_ISDIR(stbuf.st_mode)) {
if (debug & DEBUG_WALK_DIR_NODE)
printf("merging %s with %p\n",
path, cur->child);
cur->child = walk_dir(root, rp, cur,
cur->child);
continue;
}
errx(1, "Can't merge %s `%s' with existing %s",
inode_type(stbuf.st_mode), path,
inode_type(cur->type));
}
}
cur = create_fsnode(root, dir, name, &stbuf);
cur->parent = parent;
cur->next = first;
first = cur;
if (!dot && S_ISDIR(cur->type)) {
cur->child = walk_dir(root, rp, cur, NULL);
continue;
}
if (stbuf.st_nlink > 1) {
fsinode *curino;
curino = link_check(cur->inode);
if (curino != NULL) {
free(cur->inode);
cur->inode = curino;
cur->inode->nlink++;
if (debug & DEBUG_WALK_DIR_LINKCHECK)
printf("link_check: found [%llu, %llu]\n",
(unsigned long long)curino->st.st_dev,
(unsigned long long)curino->st.st_ino);
}
}
if (S_ISLNK(cur->type)) {
char slink[PATH_MAX+1];
int llen;
llen = readlink(path, slink, sizeof(slink) - 1);
if (llen == -1)
err(1, "Readlink `%s'", path);
slink[llen] = '\0';
cur->symlink = estrdup(slink);
}
}
if (closedir(dirp) == -1)
err(1, "Can't closedir `%s/%s'", root, dir);
return (sort_dir(first));
}
static fsnode *
create_fsnode(const char *root, const char *path, const char *name,
struct stat *stbuf)
{
fsnode *cur;
cur = ecalloc(1, sizeof(*cur));
cur->path = estrdup(path);
cur->name = estrdup(name);
cur->inode = ecalloc(1, sizeof(*cur->inode));
cur->root = root;
cur->type = stbuf->st_mode & S_IFMT;
cur->inode->nlink = 1;
cur->inode->st = *stbuf;
if (stampst.st_ino != 0)
set_tstamp(cur);
return (cur);
}
void
free_fsnodes(fsnode *node)
{
fsnode *cur, *next;
assert(node != NULL);
if (node->first == node) {
assert(node->name[0] == '.' && node->name[1] == '\0');
if (node->parent) {
assert(node->parent->child == node);
node = node->parent;
}
}
if (node->first != node) {
for (cur = node->first; cur->next; cur = cur->next) {
if (cur->next == node) {
cur->next = node->next;
node->next = NULL;
break;
}
}
}
for (cur = node; cur != NULL; cur = next) {
next = cur->next;
if (cur->child) {
cur->child->parent = NULL;
free_fsnodes(cur->child);
}
if (cur->inode->nlink-- == 1)
free(cur->inode);
if (cur->symlink)
free(cur->symlink);
free(cur->path);
free(cur->name);
free(cur);
}
}
void
apply_specfile(const char *specfile, const char *dir, fsnode *parent, int speconly)
{
struct timeval start;
FILE *fp;
NODE *root;
assert(specfile != NULL);
assert(parent != NULL);
if (debug & DEBUG_APPLY_SPECFILE)
printf("apply_specfile: %s, %s %p\n", specfile, dir, parent);
if ((fp = fopen(specfile, "r")) == NULL)
err(1, "Can't open `%s'", specfile);
TIMER_START(start);
root = spec(fp);
TIMER_RESULTS(start, "spec");
if (fclose(fp) == EOF)
err(1, "Can't close `%s'", specfile);
if (root == NULL)
errx(1, "Specfile `%s' did not contain a tree", specfile);
assert(strcmp(root->name, ".") == 0);
assert(root->type == F_DIR);
apply_specdir(dir, root, parent, speconly);
free_nodes(root);
}
static void
apply_specdir(const char *dir, NODE *specnode, fsnode *dirnode, int speconly)
{
char path[MAXPATHLEN + 1];
NODE *curnode;
fsnode *curfsnode;
assert(specnode != NULL);
assert(dirnode != NULL);
if (debug & DEBUG_APPLY_SPECFILE)
printf("apply_specdir: %s %p %p\n", dir, specnode, dirnode);
if (specnode->type != F_DIR)
errx(1, "Specfile node `%s/%s' is not a directory",
dir, specnode->name);
if (dirnode->type != S_IFDIR)
errx(1, "Directory node `%s/%s' is not a directory",
dir, dirnode->name);
apply_specentry(dir, specnode, dirnode);
if (speconly) {
fsnode *next;
assert(dirnode->name[0] == '.' && dirnode->name[1] == '\0');
for (curfsnode = dirnode->next; curfsnode != NULL; curfsnode = next) {
next = curfsnode->next;
for (curnode = specnode->child; curnode != NULL;
curnode = curnode->next) {
if (strcmp(curnode->name, curfsnode->name) == 0)
break;
}
if (curnode == NULL) {
if (debug & DEBUG_APPLY_SPECONLY) {
printf("apply_specdir: trimming %s/%s %p\n", dir, curfsnode->name, curfsnode);
}
free_fsnodes(curfsnode);
}
}
}
for (curnode = specnode->child; curnode != NULL;
curnode = curnode->next) {
if (debug & DEBUG_APPLY_SPECENTRY)
printf("apply_specdir: spec %s\n",
curnode->name);
for (curfsnode = dirnode->next; curfsnode != NULL;
curfsnode = curfsnode->next) {
#if 0
if (debug & DEBUG_APPLY_SPECENTRY)
printf("apply_specdir: dirent %s\n",
curfsnode->name);
#endif
if (strcmp(curnode->name, curfsnode->name) == 0)
break;
}
if ((size_t)snprintf(path, sizeof(path), "%s/%s", dir,
curnode->name) >= sizeof(path))
errx(1, "Pathname too long.");
if (curfsnode == NULL) {
struct stat stbuf;
if ((curnode->flags & F_OPT) &&
lstat(path, &stbuf) == -1)
continue;
#define NODETEST(t, m) \
if (!(t)) \
errx(1, "`%s': %s not provided", path, m)
NODETEST(curnode->flags & F_TYPE, "type");
NODETEST(curnode->flags & F_MODE, "mode");
NODETEST(curnode->flags & F_GID ||
curnode->flags & F_GNAME, "group");
NODETEST(curnode->flags & F_UID ||
curnode->flags & F_UNAME, "user");
#undef NODETEST
if (debug & DEBUG_APPLY_SPECFILE)
printf("apply_specdir: adding %s\n",
curnode->name);
memset(&stbuf, 0, sizeof(stbuf));
stbuf.st_mode = nodetoino(curnode->type);
stbuf.st_nlink = 1;
stbuf.st_mtime = stbuf.st_atime =
stbuf.st_ctime = start_time.tv_sec;
#if HAVE_STRUCT_STAT_ST_MTIMENSEC
stbuf.st_mtimensec = stbuf.st_atimensec =
stbuf.st_ctimensec = start_time.tv_nsec;
#endif
curfsnode = create_fsnode(".", ".", curnode->name,
&stbuf);
curfsnode->parent = dirnode->parent;
curfsnode->first = dirnode;
curfsnode->next = dirnode->next;
dirnode->next = curfsnode;
if (curfsnode->type == S_IFDIR) {
curfsnode->child = create_fsnode(".", ".", ".",
&stbuf);
curfsnode->child->parent = curfsnode;
curfsnode->child->first = curfsnode->child;
}
if (curfsnode->type == S_IFLNK) {
assert(curnode->slink != NULL);
curfsnode->symlink = estrdup(curnode->slink);
}
}
apply_specentry(dir, curnode, curfsnode);
if (curnode->type == F_DIR) {
if (curfsnode->type != S_IFDIR)
errx(1, "`%s' is not a directory", path);
assert (curfsnode->child != NULL);
apply_specdir(path, curnode, curfsnode->child, speconly);
}
}
}
static void
apply_specentry(const char *dir, NODE *specnode, fsnode *dirnode)
{
assert(specnode != NULL);
assert(dirnode != NULL);
if (nodetoino(specnode->type) != dirnode->type)
errx(1, "`%s/%s' type mismatch: specfile %s, tree %s",
dir, specnode->name, inode_type(nodetoino(specnode->type)),
inode_type(dirnode->type));
if (debug & DEBUG_APPLY_SPECENTRY)
printf("apply_specentry: %s/%s\n", dir, dirnode->name);
#define ASEPRINT(t, b, o, n) \
if (debug & DEBUG_APPLY_SPECENTRY) \
printf("\t\t\tchanging %s from " b " to " b "\n", \
t, o, n)
if (specnode->flags & (F_GID | F_GNAME)) {
ASEPRINT("gid", "%d",
dirnode->inode->st.st_gid, specnode->st_gid);
dirnode->inode->st.st_gid = specnode->st_gid;
}
if (specnode->flags & F_MODE) {
ASEPRINT("mode", "%#o",
dirnode->inode->st.st_mode & ALLPERMS, specnode->st_mode);
dirnode->inode->st.st_mode &= ~ALLPERMS;
dirnode->inode->st.st_mode |= (specnode->st_mode & ALLPERMS);
}
if (specnode->flags & F_SIZE) {
ASEPRINT("size", "%lld",
(long long)dirnode->inode->st.st_size,
(long long)specnode->st_size);
dirnode->inode->st.st_size = specnode->st_size;
}
if (specnode->flags & F_SLINK) {
assert(dirnode->symlink != NULL);
assert(specnode->slink != NULL);
ASEPRINT("symlink", "%s", dirnode->symlink, specnode->slink);
free(dirnode->symlink);
dirnode->symlink = estrdup(specnode->slink);
}
if (specnode->flags & F_TIME) {
ASEPRINT("time", "%ld",
(long)dirnode->inode->st.st_mtime,
(long)specnode->st_mtimespec.tv_sec);
dirnode->inode->st.st_mtime = specnode->st_mtimespec.tv_sec;
dirnode->inode->st.st_atime = specnode->st_mtimespec.tv_sec;
dirnode->inode->st.st_ctime = start_time.tv_sec;
#if HAVE_STRUCT_STAT_ST_MTIMENSEC
dirnode->inode->st.st_mtimensec = specnode->st_mtimespec.tv_nsec;
dirnode->inode->st.st_atimensec = specnode->st_mtimespec.tv_nsec;
dirnode->inode->st.st_ctimensec = start_time.tv_nsec;
#endif
}
if (specnode->flags & (F_UID | F_UNAME)) {
ASEPRINT("uid", "%d",
dirnode->inode->st.st_uid, specnode->st_uid);
dirnode->inode->st.st_uid = specnode->st_uid;
}
if (specnode->flags & F_FLAGS) {
ASEPRINT("flags", "%#lX",
(unsigned long)FSINODE_ST_FLAGS(*dirnode->inode),
(unsigned long)specnode->st_flags);
FSINODE_ST_FLAGS(*dirnode->inode) = specnode->st_flags;
}
#undef ASEPRINT
dirnode->flags |= FSNODE_F_HASSPEC;
}
void
dump_fsnodes(fsnode *root)
{
fsnode *cur;
char path[MAXPATHLEN + 1];
printf("dump_fsnodes: %s %p\n", root->path, root);
for (cur = root; cur != NULL; cur = cur->next) {
if (snprintf(path, sizeof(path), "%s/%s", cur->path,
cur->name) >= (int)sizeof(path))
errx(1, "Pathname too long.");
if (debug & DEBUG_DUMP_FSNODES_VERBOSE)
printf("cur=%8p parent=%8p first=%8p ",
cur, cur->parent, cur->first);
printf("%7s: %s", inode_type(cur->type), path);
if (S_ISLNK(cur->type)) {
assert(cur->symlink != NULL);
printf(" -> %s", cur->symlink);
} else {
assert (cur->symlink == NULL);
}
if (cur->inode->nlink > 1)
printf(", nlinks=%d", cur->inode->nlink);
putchar('\n');
if (cur->child) {
assert (cur->type == S_IFDIR);
dump_fsnodes(cur->child);
}
}
printf("dump_fsnodes: finished %s/%s\n", root->path, root->name);
}
const char *
inode_type(mode_t mode)
{
if (S_ISREG(mode))
return ("file");
if (S_ISLNK(mode))
return ("symlink");
if (S_ISDIR(mode))
return ("dir");
if (S_ISFIFO(mode))
return ("fifo");
if (S_ISSOCK(mode))
return ("socket");
if (S_ISCHR(mode))
return ("char");
if (S_ISBLK(mode))
return ("block");
return ("unknown");
}
fsinode *
link_check(fsinode *entry)
{
static struct entry {
fsinode *data;
} *htable;
static int htshift;
static int htmask;
static int htused;
int h, h2;
uint64_t tmp;
const uint64_t HTCONST = 11400714819323198485ULL;
const int HTBITS = 64;
assert(entry);
if (htused<<1 >= htmask) {
struct entry *ohtable;
if (!htable)
htshift = 10;
else
htshift++;
htmask = (1 << htshift) - 1;
htused = 0;
ohtable = htable;
htable = ecalloc(htmask+1, sizeof(*htable));
if (ohtable) {
int i;
for (i = 0; i <= htmask>>1; i++)
if (ohtable[i].data)
link_check(ohtable[i].data);
free(ohtable);
}
}
tmp = entry->st.st_dev;
tmp <<= HTBITS>>1;
tmp |= entry->st.st_ino;
tmp *= HTCONST;
h = tmp >> (HTBITS - htshift);
h2 = 1 | ( tmp >> (HTBITS - (htshift<<1) - 1));
while (htable[h].data) {
if ((htable[h].data->st.st_ino == entry->st.st_ino) &&
(htable[h].data->st.st_dev == entry->st.st_dev)) {
return htable[h].data;
}
h = (h + h2) & htmask;
}
htable[h].data = entry;
htused++;
return NULL;
}