+ return 0;
+}
+
+static int report_conflicts(void *f, int ncols, char **vals, char **cols) {
+ struct config *conf = f;
+ char *path, *hash, *pkg, *conflict_type, *mds;
+
+ pkg = COL("pkgid");
+ path = COL("path");
+ conflict_type = COL("conflict");
+ if (!strcmp(conflict_type, "hash")) {
+ hash = COL("hash");
+ fprintf(stderr, "hash conflict: package %s path %s hash %.8s\n",
+ pkg, path, hash);
+ } else
+ if (!strcmp(conflict_type, "md")) {
+ mds = COL("mds");
+ fprintf(stderr, "md conflict: package %s path %s md %s\n",
+ pkg, path, mds);
+ } else {
+ fprintf(stderr, "%s conflict: package %s path %s\n",
+ conflict_type, pkg, path);
+ }
+
+ conf->conflicts++;
+ return 0;
+}
+
+static int read_item(struct config *conf, int ncols, char **vals, char **cols,
+ struct nitem *n) {
+ char *val;
+ long lval;
+ struct passwd *pw;
+ struct group *gr;
+ struct nitem zero = { 0 };
+
+ *n = zero;
+
+ val = COL("op");
+ if (!val) {
+ seterror(conf, "can't determine op");
+ return 0;
+ }
+ n->opstr = val;
+ n->op = getop(val);
+ if (!n->op) {
+ seterror(conf, "can't determine op");
+ return 0;
+ }
+
+ n->path = COL("path");
+ if (!n->path) {
+ seterror(conf, "no file path");
+ return 0;
+ }
+ if (strlen(n->path) == 0) {
+ seterror(conf, "zero length path not allowed");
+ return 0;
+ }
+
+ /* TODO config to dishonor setuid/setgid */
+ n->dest = COL("dest");
+ if (!n->dest) {
+ seterror(conf, "no file dest");
+ return 0;
+ }
+
+ if (strlen(n->dest) == 0) {
+ seterror(conf, "zero length dest not allowed");
+ return 0;
+ }
+
+ val = COL("mode");
+
+ if (!val) {
+ seterror(conf, "can't determine mode");
+ return 0;
+ }
+
+ n->mode = strtoul(val, NULL, 8);
+
+ val = COL("configuration");
+ if (!val) {
+ seterror(conf, "can't determine config status");
+ return 0;
+ }
+ lval = strtol(val, NULL, 10);
+
+ n->configuration = ((lval & 1) != 0);
+ n->oldwasconf = ((lval & 2) != 0);
+
+ val = COL("filetype");
+ if (!val || strlen(val) == 0) {
+ seterror(conf, "can't determine file type");
+ return 0;
+ }
+ n->ftype = *val;
+
+ /* these can be null */
+ n->ohash = COL("ohash");
+ n->mds = COL("mds");
+ n->omds = COL("omds");
+ n->pkglist = COL("pkglist");
+
+ if (n->ftype == 'r') {
+ n->hash = COL("hash");
+ if (!n->hash) {
+ seterror(conf, "can't get hash");
+ return 0;
+ }
+ } else if (n->ftype == 'l') {
+ n->target = COL("target");
+ if (!n->target) {
+ seterror(conf, "can't get target");
+ return 0;
+ }
+ if (strlen(n->target) == 0) {
+ seterror(conf, "zero length target not allowed");
+ return 0;
+ }
+ n->hash = n->target;
+ }
+
+ if (conf->setuser) {
+ val = COL("username");
+ if (!val) {
+ seterror(conf, "no username");
+ return 0;
+ }
+ pw = getpwnam(val);
+ if (!pw) {
+ seterror(conf, "no passwd entry");
+ return 0;
+ }
+ n->uid = pw->pw_uid;
+ } else {
+ n->uid = geteuid();
+ }
+
+ if (conf->setgroup) {
+ val = COL("groupname");
+ if (!val) {
+ seterror(conf, "no groupname");
+ return 0;
+ }
+ gr = getgrnam(val);
+ if (!gr) {
+ seterror(conf, "no group entry for %s", val);
+ return 0;
+ }
+ n->gid = gr->gr_gid;
+ } else {
+ n->gid = getegid();
+ }
+
+ errno = 0;
+ double mtime = strtod(COL("mtime"),NULL);
+ if (errno) {
+ mtime = (double)time(NULL);
+ }
+
+ n->mtime = (time_t)mtime;
+
+ n->times[0].tv_sec = 0;
+ n->times[0].tv_nsec = UTIME_OMIT;
+ n->times[1].tv_sec = (time_t)llrint(floor(mtime));
+ n->times[1].tv_nsec = lrint(floor(fmod(mtime,1.0)*1000000000));
+
+ return 1;
+}
+
+/* file does not exist */
+#define D_NOEXIST 0x1
+/* files are different types */
+#define D_TYPE 0x2
+/* metadata is different */
+#define D_MD 0x4
+/* content or link target is different */
+#define D_HASH 0x8
+/* file to be installed is a directory */
+#define D_ISDIR 0x10
+/* path on disk is a directory */
+#define D_EISDIR 0x20
+/* usernames different */
+#define D_UID 0x40
+/* group names different */
+#define D_GID 0x80
+/* file mode is different */
+#define D_MODE 0x100
+/* mtimes are different */
+#define D_MTIME 0x200
+/* the hash of the file we are supposedly replacing is different than
+ * the the hash of the file on disk
+ */
+#define D_OHASH 0x400
+/* file exists, and is a directory, and is empty */
+#define D_ISEMPTY 0x800
+/* an error occurred trying to compare the file (other than it doesn't exist */
+#define D_ERROR 0x1000
+/* there was a stat error */
+#define D_STATERROR 0x2000
+/* there was an error calling readlink */
+#define D_RLERROR 0x4000
+
+static int dir_is_empty(char *path) {
+ DIR *dir;
+ struct dirent *dp;
+ int empty;
+
+ dir = opendir(path);
+ if (!dir) {
+ return -1;
+ }
+
+ dp = readdir(dir);
+ if (dp) {
+ empty = 0;
+ } else {
+ empty = 1;
+ }
+ closedir(dir);
+
+ return empty;
+}
+
+/* 1 = file doesn't exist, 2 = file is a directory, target isn't */
+/* 4 == ftype different */
+/* 8 = hash different when both are regular files */
+static unsigned int file_compare(struct nitem *n, struct stat *st) {
+ int etype = 0, stat_type;
+ char ehash[ZPM_HASH_STRLEN+1];
+ unsigned int diff = 0;
+ char link[1024];
+ ssize_t lsize;
+
+ switch (n->ftype) {
+ case 'd': etype = S_IFDIR; diff |= D_ISDIR ; break;
+ case 'r': etype = S_IFREG; break;
+ case 'l': etype = S_IFLNK; break;
+ default: etype = 0; break;
+ }
+
+ errno = 0;
+ /* new file, so check type, hash, etc */
+ if (lstat(n->dest, st) == 0) {
+ stat_type = st->st_mode & S_IFMT;
+ if (stat_type != etype) {
+ diff |= D_TYPE;
+ }
+ if (stat_type == S_IFDIR) {
+ diff |= D_EISDIR;
+ if (dir_is_empty(n->dest)) {
+ diff |= D_ISEMPTY;
+ }
+ }
+
+ if (n->hash && etype == S_IFREG && stat_type == S_IFREG) {
+ zpm_hash(n->dest, ehash, 0);
+ if (strcmp(n->hash, ehash) != 0) {
+ diff |= D_HASH;
+ }
+ if (n->ohash && strcmp(n->ohash, ehash) != 0) {
+ diff |= D_OHASH;
+ }
+ }
+ if (n->hash && etype == S_IFLNK && stat_type == S_IFLNK) {
+ lsize = readlink(n->dest, link, sizeof link);
+
+ if (lsize == -1 || lsize == sizeof link) {
+ diff |= D_RLERROR;
+ diff |= D_ERROR;
+ } else {
+ link[lsize] = 0;
+ if (strcmp(n->target, link) != 0) {
+ diff |= D_HASH;
+ }
+ }
+ }
+ if (n->uid != st->st_uid) {
+ diff |= D_UID;
+ diff |= D_MD;
+ }
+ if (n->gid != st->st_gid) {
+ diff |= D_GID;
+ diff |= D_MD;
+ }
+ if (n->mode != (st->st_mode & 07777)) {
+ diff |= D_MODE;
+ diff |= D_MD;
+ }
+ } else {
+ switch(errno) {
+ case ENOENT: diff |= D_NOEXIST; break;
+ default: diff |= (D_STATERROR|D_ERROR); break;
+ }
+ }
+
+ return diff;
+}
+
+
+/* 0 = not acceptable
+ * 1 = accept and create/update/remove
+ * 2 = accept as is
+ * 3 = remove and overwrite
+ * 4 = update metadata
+ */
+static int acceptable(struct config *conf, unsigned int diffs, int op) {
+ int exist = (!(diffs & D_NOEXIST));
+ int sametype = (!(diffs & D_TYPE));
+ int mdsame = (!(diffs & D_MD));
+ int hashsame = (!(diffs & D_HASH));
+ int isdir = (diffs & D_ISDIR);
+
+ if (!exist) {
+ return op == OP_REMOVE ? 2 : 1;
+ }
+
+ if (op == OP_UPDATE) {
+ return sametype ? 4 : 3;
+ }
+
+ if (op == OP_REMOVE) {
+ return 1;
+ }
+
+ /* the hard cases, should be installing new, but already exists */
+
+ if (!sametype) {
+ return conf->overwrite ? 3 : 0;
+ }
+
+ if (mdsame && (conf->accept || conf->overwrite)) {
+ return 1;
+ }
+
+ if (isdir) {
+ if (mdsame || conf->ignoredirmd) {
+ return conf->acceptdir ? 2 : 0;
+ }
+ if (conf->overwrite) {
+ return 4;
+ }
+ }
+
+ if (hashsame && (conf->accept || conf->overwrite)) {
+ return 1;
+ }
+
+ return conf->overwrite ? 3 : 0;
+}
+
+static int check_existing(void *f, int ncols, char **vals, char **cols) {
+ struct config *conf = f;
+ struct stat st;
+ struct nitem nitem;
+
+ if (!read_item(conf, ncols, vals, cols, &nitem)) {
+ fprintf(stderr, "can't read item\n");
+ return conf->errabort;
+ }
+
+ if (conf->verbose > 1) {
+ fprintf(stderr, "check for existing %s\n", nitem.path);
+ }
+
+ if (lstat(nitem.path, &st) == -1) {
+ switch(errno) {
+ /* not an error, file shouldn't exist*/
+ case ENOENT: break;
+ default:
+ fprintf(stderr, "unable to check %s: %s\n",
+ nitem.path, strerror(errno));
+ conf->errors++;
+ break;
+ }
+ return 0;
+ }
+
+ unsigned int diffs = file_compare(&nitem, &st);
+ int sametype = (!(diffs & D_TYPE));
+
+ if (diffs >= D_ERROR) {
+ return seterror(conf, "can't check %s", nitem.dest);
+ }
+
+ if (sametype && nitem.configuration) {
+ return 0;
+ }
+
+ int action = acceptable(conf, diffs, nitem.op);
+ if (!action) {
+ if (conf->accept) {
+ fprintf(stderr, "%s exists and is not acceptable\n", nitem.path);
+ } else {
+ fprintf(stderr, "%s exists\n", nitem.path);
+ }
+ conf->errors++;
+ }
+
+ return 0;
+}
+
+static void update_progress(struct config *conf, char *op, char *path) {
+ if (!conf->verbose) {
+ return;
+ }
+
+ if (conf->progress == 0) {
+ pdots(50, '.', conf->ops_completed-1, conf->ops_completed, conf->ops_total);
+ } else if (conf->progress == 1) {
+ size_t len = strlen(path);
+ int offset = 0;
+ if (len > 50) {
+ offset = len-50;
+ }
+ printf("\r%lu/%lu %.10s %.50s\n", conf->ops_completed,
+ conf->ops_total, op, path+offset);
+ } else if (conf->progress == 2) {
+ printf("%lu/%lu %s %s\n", conf->ops_completed,
+ conf->ops_total, op, path);
+ }
+ fflush(stdout);
+}
+
+static int remove_files(void *f, int ncols, char **vals, char **cols) {
+ struct config *conf = f;
+ char *dest;
+ struct stat st;
+ int flags = 0;
+
+ conf->ops_completed++;
+
+ dest = COL("dest");
+ char *ftype = COL("filetype");
+
+ if (!dest) return seterror(conf,"no file dest");
+ if (!ftype) return seterror(conf,"no file type");
+
+ update_progress(conf, *ftype == 'd' ? "rmdir" : "unlink", dest);
+
+ if (conf->dryrun) {
+ return 0;
+ }
+
+ errno = 0;
+
+ if (lstat(dest, &st) == -1) {
+ switch (errno) {
+ case ENOENT:
+ if (conf->verbose > 1) {
+ fprintf(stderr, "expected file not found: '%s'\n", dest);
+ }
+ break;
+ default:
+ return seterror(conf, "can't stat %s: %s", dest, strerror(errno));
+ }
+ return 0;
+ }
+
+ if (S_ISDIR(st.st_mode)) {
+ flags = AT_REMOVEDIR;
+ }
+ /* TODO check that expected filetype matches actual filetype */
+ /* alternatively, just use the expected type, skip the stat,
+ * and let it fail if the type is wrong
+ */
+
+ errno = 0;
+
+ if (unlinkat(AT_FDCWD, dest, flags) == -1) {
+ switch (errno) {
+ case ENOENT:
+ break;
+ case ENOTEMPTY: /* fall through */
+ case EEXIST:
+ fprintf(stderr, "expected empty directory: %s\n", dest);
+ break;
+ default:
+ return seterror(conf, "can't unlink %s: %s", dest, strerror(errno));
+ }
+ }
+
+ return 0;
+}
+
+#define MARK fprintf(stderr, "%s %d: mark\n", __func__, __LINE__)
+
+static int remove_dir(struct config *conf, char *path) {
+ int rv;
+
+ rv = rmdir(path);
+ if (rv == -1) {
+ setsyserr(conf, "can't rmdir %s", path);
+ return 0;
+ }
+ return 1;
+}
+
+static int remove_existing(struct config *conf, char *path) {
+ int rv;
+
+ rv = unlink(path);
+ if (rv == -1) {
+ setsyserr(conf, "can't unlink %s", path);
+ return 0;
+ }
+ return 1;
+}
+
+static int set_md(struct config *conf, struct nitem *item) {
+ int rv;
+ int success = 0;
+
+ if (conf->dryrun) {
+ if (item->ftype != 'l') {
+ printf("chmod %o %s\n", item->mode, item->dest);
+ }
+ if (conf->setuser && conf->setgroup) {
+ printf("lchown %d:%d %s\n", item->uid, item->gid,
+ item->dest);
+ }
+ printf("mtime %.0f %s\n", (double)item->mtime, item->dest);
+ fflush(stdout);
+ return success;
+ }
+
+ if (conf->setuser && conf->setgroup) {
+ rv = lchown(item->dest, item->uid, item->gid);
+ if (rv == -1) {
+ setsyserr(conf, "can't lchown %s", item->dest);
+ return conf->errabort;
+ }
+ }
+
+ /* have to chmod after the chown, setuid bits may (and will)
+ * be cleared after a chown
+ */
+ /* can't chmod a symlink */
+ if (item->ftype != 'l') {
+ rv = chmod(item->dest, item->mode);
+
+ if (rv == -1) {
+ setsyserr(conf, "can't chmod %o %s", item->mode, item->dest);
+ return conf->errabort;
+ }
+ }
+
+ rv = utimensat(AT_FDCWD, item->dest, item->times, AT_SYMLINK_NOFOLLOW);
+ if (rv == -1) {
+ setsyserr(conf, "can't set mtime %.0f %s", (double)item->mtime,
+ item->dest);
+ return conf->errabort;
+ }
+ return 0;
+}
+
+/* install a file or create a directory or symlink. path should not exist
+ * at this point.
+ */
+/* flags: 1 = set md, 2 = create leading dirs, 4 = unlink existing file,
+ * 8 = rmdir existing dir, 16 = return true/false
+ */
+#define INS_MD 0x1
+#define INS_CLD 0x2
+#define INS_UNLINK 0x4
+#define INS_RMDIR 0x8
+#define INS_RTF 0x10
+#define INS_ZPMNEW 0x20
+static int install(struct config *conf, struct nitem *item, unsigned int flags) {
+ int rv = 1;
+ struct zpm *source;
+
+ int mkleading = (flags & 2);
+ int setmd = (flags & 1);
+ int unlink_file = (flags & 4);
+ int rm_dir = (flags & 8);
+ int failure = conf->errabort;
+ int success = 0;
+
+ if (flags & INS_RTF) {
+ failure = 0;
+ success = 1;
+ }
+
+ if (conf->dryrun) {
+ if (unlink_file) {
+ printf("unlink %s\n", item->dest);
+ } else if (rm_dir) {
+ printf("rmdir %s\n", item->dest);
+ }
+
+ printf("install %c%o %d:%d %s", item->ftype,
+ item->mode, item->uid, item->gid,
+ item->dest);
+ if (item->ftype == 'l') {
+ printf(" -> %s", item->target);
+ }
+ printf("\n");
+ fflush(stdout);
+ return success;
+ }
+
+ source = conf->src ? conf->src : conf->log;
+
+ if (mkleading) {
+ rv = create_leading_dirs(item->dest);
+ if (!rv) {
+ setsyserr(conf, "can't create leading dirs for %s", item->dest);
+ return failure;
+ }
+ }
+
+ if (unlink_file) {
+ rv = remove_existing(conf, item->dest);
+ } else if (rm_dir) {
+ rv = remove_dir(conf, item->dest);
+ }
+
+ if (rv != 1) {
+ return failure;
+ }
+
+ errno = 0;
+ switch (item->ftype) {
+ case 'r': rv = zpm_extract(source, item->hash, item->dest, item->mode);
+ if (rv == 0) rv = -1;
+ break;
+ case 'd': rv = mkdir(item->dest, item->mode);
+ break;
+ case 'l': rv = symlink(item->target, item->dest);
+ break;
+ default: /* error */
+ break;
+ }
+
+ if (rv == -1) {
+ switch (item->ftype) {
+ case 'r':
+ seterror(conf, "can't extract %s", item->dest);
+ break;
+ case 'd':
+ setsyserr(conf, "install mkdir(\"%s\") failed", item->dest);
+ break;
+ case 'l':
+ setsyserr(conf, "install symlink(\"%s\") failed", item->dest);
+ break;
+ }
+ setsyserr(conf, "installing %s failed", item->dest);
+ return failure;
+ }
+
+ if (setmd) {
+ return set_md(conf, item) == 0 ? success : failure;
+ }
+
+ return success;
+}
+
+static int save_config_file(struct config *conf, struct nitem *n, char *msgfmt) {
+ char hash[ZPM_HASH_STRLEN+1];
+
+ if (!msgfmt) {
+ msgfmt = "saved config file %.8s";
+ }
+
+ if (zpm_import(conf->log, n->dest, 0, hash)) {
+ zpm_note_add(conf->log, n->pkglist, n->path, hash, msgfmt, hash);
+ } else {
+ warn("unable to import existing config file %s", n->dest);
+ conf->errors++;
+ return 0;
+ }
+ return 1;
+}
+
+#if 0
+/*
+ * figure out what the difference is for a config file, only called
+ * for an update of a configuration file
+ * return -1 on an error
+ * return 1 if the new file should not be installed
+ * return 0 if the new file should be installed
+ */
+static int adjust_for_config(struct nitem *n, unsigned int diffs) {
+
+ if (!n->oldwasconf) {
+ return 0;
+ }
+
+ int sametype = (!(diffs & D_TYPE));
+ int isdir = (diffs & D_ISDIR);
+ int eisdir = (diffs & D_EISDIR);
+
+ /* what if old was a directory? */
+ if (!n->configuration) {
+ /* replacing conf with non-conf */
+ /* absorb file, mark todo */
+ return 0;
+ }
+
+ /* both old and new are config files */
+ if (isdir && sametype) {
+ /* both config directories, can only be changing
+ * metadata, so no adjustment needed
+ */
+ return 0;
+ }
+
+ if (isdir) {
+ return 0;
+ }
+
+ if (eisdir) {
+ /* replacing old conf directory with a conf file.
+ * nothing needs to be done, if the directory
+ * is empty, it's ok to remove. if it's not empty,
+ * the install will fail
+ */
+ return 0;
+ }
+
+ /* replacing old file with new file */
+ /* new is same as on disk */
+ if (!(diffs & D_HASH)) {
+ return 0;
+ }