1 #define _POSIX_C_SOURCE 200809L
17 /* needed for S_IFMT and AT_FDCWD */
26 struct zpm *log; /* logging db will be attached as "log" */
30 int errabort, errors, verbose, dryrun, conflicts;
31 int setuser, setgroup;
32 int reverse, exitonerror;
33 int overwrite, accept, acceptdir, ignoredirmd;
34 int ops_total, ops_completed;
35 int ops_remove, ops_remove_completed;
36 int ops_update, ops_update_completed;
37 int ops_install, ops_install_completed;
38 int progress; /* type of progress meter */
51 char *pkglist; /* space separated */
55 int configuration, oldwasconf;
56 struct timespec times[2];
60 printf("usage: zpm $scriptname [-fncC] args ...\n");
63 static void pdots(int len, int ch, int was, int now, int total) {
64 was = len * was / total;
68 now = len * now / total;
75 static int seterror(struct config *conf, char *msgfmt, ...) {
82 vsnprintf(msg, sizeof msg, msgfmt, ap);
86 if (conf->log->errmsg) {
87 free(conf->log->errmsg);
90 conf->log->errmsg = strdup(msg);
93 fprintf(stderr, "%s\n", msg);
96 return conf->errabort;
99 static int setsyserr(struct config *conf, char *msgfmt, ...) {
106 va_start(ap, msgfmt);
107 printed = vsnprintf(msg, sizeof msg, msgfmt, ap);
111 /* nothing we can really do */
112 return conf->errabort;
115 if ((size_t)printed < sizeof msg) {
116 snprintf(msg+printed, sizeof msg - printed, ": %s",
121 if (conf->log->errmsg) {
122 free(conf->log->errmsg);
125 conf->log->errmsg = strdup(msg);
128 fprintf(stderr, "%s\n", msg);
131 return conf->errabort;
134 static int exists(char *path, mode_t *mode) {
137 if (lstat(path, &st) == -1) {
140 if (mode) *mode = st.st_mode;
144 /* TODO maintain a list of already created directories */
145 static int create_leading_dirs(char *path) {
148 char pcopy[ZPM_PATH_MAX];
153 delim = strrchr(pcopy, '/');
154 if (!delim) return 1; /* not an error, but no leading dirs */
156 /* cut off last component */
165 delim = strchr(s, '/');
171 /* try to create the directory, if it exists
172 * and is a directory or a symlink, that's ok
173 * should be (eventually) a symlink to a directory
174 * so we want stat here, not lstat
176 if (mkdir(pcopy, 0755) == -1) {
179 if (stat(pcopy, &st) == -1) {
183 switch (st.st_mode & S_IFMT) {
203 static char *column(char *col, int ncols, char **vals, char **cols) {
207 for (i=0; i < ncols; i++) {
208 // fprintf(stderr, "checking '%s' = '%s'\n", cols[i], vals[i]);
210 if (!strcmp(col, cols[i])) {
218 #define COL(x) column(x, ncols, vals, cols)
219 #define SYSERR(x) do { conf->log->error = 2; return conf->errabort; } while (0)
222 static char *ops[] = { "new", "remove", "update", 0 };
230 static int getop(char *opstr) {
233 if (!opstr) return 0;
234 for (i=0;ops[i];i++) {
235 if (!strcmp(opstr, ops[i])) {
242 static int report_conflicts(void *f, int ncols, char **vals, char **cols) {
243 struct config *conf = f;
244 char *path, *hash, *pkg, *conflict_type, *mds;
248 conflict_type = COL("conflict");
249 if (!strcmp(conflict_type, "hash")) {
251 fprintf(stderr, "hash conflict: package %s path %s hash %.8s\n",
254 if (!strcmp(conflict_type, "md")) {
256 fprintf(stderr, "md conflict: package %s path %s md %s\n",
259 fprintf(stderr, "%s conflict: package %s path %s\n",
260 conflict_type, pkg, path);
267 static int read_item(struct config *conf, int ncols, char **vals, char **cols,
273 struct nitem zero = { 0 };
279 seterror(conf, "can't determine op");
285 seterror(conf, "can't determine op");
289 n->path = COL("path");
291 seterror(conf, "no file path");
294 if (strlen(n->path) == 0) {
295 seterror(conf, "zero length path not allowed");
299 /* TODO config to dishonor setuid/setgid */
300 n->dest = COL("dest");
302 seterror(conf, "no file dest");
306 if (strlen(n->dest) == 0) {
307 seterror(conf, "zero length dest not allowed");
314 seterror(conf, "can't determine mode");
318 n->mode = strtoul(val, NULL, 8);
320 val = COL("configuration");
322 seterror(conf, "can't determine config status");
325 lval = strtol(val, NULL, 10);
327 n->configuration = ((lval & 1) != 0);
328 n->oldwasconf = ((lval & 2) != 0);
330 val = COL("filetype");
331 if (!val || strlen(val) == 0) {
332 seterror(conf, "can't determine file type");
337 /* these can be null */
338 n->ohash = COL("ohash");
340 n->omds = COL("omds");
341 n->pkglist = COL("pkglist");
343 if (n->ftype == 'r') {
344 n->hash = COL("hash");
346 seterror(conf, "can't get hash");
349 } else if (n->ftype == 'l') {
350 n->target = COL("target");
352 seterror(conf, "can't get target");
355 if (strlen(n->target) == 0) {
356 seterror(conf, "zero length target not allowed");
363 val = COL("username");
365 seterror(conf, "no username");
370 seterror(conf, "no passwd entry");
378 if (conf->setgroup) {
379 val = COL("groupname");
381 seterror(conf, "no groupname");
386 seterror(conf, "no group entry for %s", val);
395 double mtime = strtod(COL("mtime"),NULL);
397 mtime = (double)time(NULL);
400 n->mtime = (time_t)mtime;
402 n->times[0].tv_sec = 0;
403 n->times[0].tv_nsec = UTIME_OMIT;
404 n->times[1].tv_sec = (time_t)llrint(floor(mtime));
405 n->times[1].tv_nsec = lrint(floor(fmod(mtime,1.0)*1000000000));
410 /* file does not exist */
411 #define D_NOEXIST 0x1
412 /* files are different types */
414 /* metadata is different */
416 /* content or link target is different */
418 /* file to be installed is a directory */
420 /* path on disk is a directory */
421 #define D_EISDIR 0x20
422 /* usernames different */
424 /* group names different */
426 /* file mode is different */
428 /* mtimes are different */
429 #define D_MTIME 0x200
430 /* the hash of the file we are supposedly replacing is different than
431 * the the hash of the file on disk
433 #define D_OHASH 0x400
434 /* an error occurred trying to compare the file (other than it doesn't exist */
435 #define D_ERROR 0x1000
436 /* there was a stat error */
437 #define D_STATERROR 0x2000
438 /* there was an error calling readlink */
439 #define D_RLERROR 0x4000
441 /* 1 = file doesn't exist, 2 = file is a directory, target isn't */
442 /* 4 == ftype different */
443 /* 8 = hash different when both are regular files */
444 static unsigned int file_compare(struct nitem *n, struct stat *st) {
445 int etype = 0, stat_type;
446 char ehash[ZPM_HASH_STRLEN+1];
447 unsigned int diff = 0;
452 case 'd': etype = S_IFDIR; diff |= D_ISDIR ; break;
453 case 'r': etype = S_IFREG; break;
454 case 'l': etype = S_IFLNK; break;
455 default: etype = 0; break;
459 /* new file, so check type, hash, etc */
460 if (lstat(n->dest, st) == 0) {
461 stat_type = st->st_mode & S_IFMT;
462 if (stat_type != etype) {
465 if (stat_type == S_IFDIR) {
469 if (n->hash && etype == S_IFREG && stat_type == S_IFREG) {
470 zpm_hash(n->dest, ehash, 0);
471 if (strcmp(n->hash, ehash) != 0) {
474 if (n->ohash && strcmp(n->ohash, ehash) != 0) {
478 if (n->hash && etype == S_IFLNK && stat_type == S_IFLNK) {
479 lsize = readlink(n->dest, link, sizeof link);
481 if (lsize == -1 || lsize == sizeof link) {
486 if (strcmp(n->target, link) != 0) {
491 if (n->uid != st->st_uid) {
495 if (n->gid != st->st_gid) {
499 if (n->mode != (st->st_mode & 07777)) {
505 case ENOENT: diff |= D_NOEXIST; break;
506 default: diff |= (D_STATERROR|D_ERROR); break;
514 /* 0 = not acceptable
515 * 1 = accept and create/update/remove
517 * 3 = remove and overwrite
518 * 4 = update metadata
520 static int acceptable(struct config *conf, unsigned int diffs, int op) {
521 int exist = (!(diffs & D_NOEXIST));
522 int sametype = (!(diffs & D_TYPE));
523 int mdsame = (!(diffs & D_MD));
524 int hashsame = (!(diffs & D_HASH));
525 int isdir = (diffs & D_ISDIR);
528 return op == OP_REMOVE ? 2 : 1;
531 if (op == OP_UPDATE) {
532 return sametype ? 4 : 3;
535 if (op == OP_REMOVE) {
539 /* the hard cases, should be installing new, but already exists */
542 return conf->overwrite ? 3 : 0;
545 if (mdsame && (conf->accept || conf->overwrite)) {
550 if (mdsame || conf->ignoredirmd) {
551 return conf->acceptdir ? 2 : 0;
553 if (conf->overwrite) {
558 if (hashsame && (conf->accept || conf->overwrite)) {
562 return conf->overwrite ? 3 : 0;
565 static int check_existing(void *f, int ncols, char **vals, char **cols) {
566 struct config *conf = f;
570 if (!read_item(conf, ncols, vals, cols, &nitem)) {
571 fprintf(stderr, "can't read item\n");
572 return conf->errabort;
575 if (conf->verbose > 1) {
576 fprintf(stderr, "check for existing %s\n", nitem.path);
579 if (lstat(nitem.path, &st) == -1) {
581 /* not an error, file shouldn't exist*/
584 fprintf(stderr, "unable to check %s: %s\n",
585 nitem.path, strerror(errno));
592 unsigned int diffs = file_compare(&nitem, &st);
593 if (diffs >= D_ERROR) {
594 return seterror(conf, "can't check %s", nitem.dest);
597 int action = acceptable(conf, diffs, nitem.op);
600 fprintf(stderr, "%s exists and is not acceptable\n", nitem.path);
602 fprintf(stderr, "%s exists\n", nitem.path);
610 static int remove_files(void *f, int ncols, char **vals, char **cols) {
611 struct config *conf = f;
617 if (!dest) return seterror(conf,"no file dest");
620 char *ftype = COL("filetype");
624 case 'd': printf("rmdir %s\n", dest); break;
625 default: printf("unlink %s\n", dest); break;
633 if (lstat(dest, &st) == -1) {
636 /* TODO chatter if verbose */
638 case ENOTEMPTY: /* fall through */
640 /* TODO chatter, or possibly require */
643 return seterror(conf, "can't stat %s: %s", dest, strerror(errno));
648 if (S_ISDIR(st.st_mode)) {
649 flags = AT_REMOVEDIR;
651 /* TODO check that expected filetype matches actual filetype */
654 if (conf->progress == 2) {
655 fprintf(stderr, "%s(%s)\n", flags ? "rmdir" : "unlink", dest);
656 } else if (conf->progress == 1) {
658 pdots(50, '.', conf->ops_completed, conf->ops_completed + 1, conf->ops_total);
659 conf->ops_completed++;
660 conf->ops_completed++;
662 pdots(50, '.', conf->ops_completed, conf->ops_completed + 1, conf->ops_total);
663 conf->ops_completed++;
669 if (unlinkat(AT_FDCWD, dest, flags) == -1) {
674 return seterror(conf, "can't unlink %s: %s", dest, strerror(errno));
681 #define MARK fprintf(stderr, "%s %d: mark\n", __func__, __LINE__)
684 static int remove_dir(struct config *conf, char *path) {
689 setsyserr(conf, "can't rmdir %s", path);
695 static int remove_existing(struct config *conf, char *path) {
700 setsyserr(conf, "can't unlink %s", path);
706 static int set_md(struct config *conf, struct nitem *item) {
711 if (item->ftype != 'l') {
712 printf("chmod %o %s\n", item->mode, item->dest);
714 if (conf->setuser && conf->setgroup) {
715 printf("lchown %d:%d %s\n", item->uid, item->gid,
718 printf("mtime %.0f %s\n", (double)item->mtime, item->dest);
723 /* can't chmod a symlink */
724 if (item->ftype != 'l') {
725 rv = chmod(item->dest, item->mode);
728 setsyserr(conf, "can't chmod %o %s", item->mode, item->dest);
729 return conf->errabort;
733 if (conf->setuser && conf->setgroup) {
734 rv = lchown(item->dest, item->uid, item->gid);
736 setsyserr(conf, "can't lchown %s", item->dest);
737 return conf->errabort;
741 rv = utimensat(AT_FDCWD, item->dest, item->times, AT_SYMLINK_NOFOLLOW);
743 setsyserr(conf, "can't set mtime %.0f %s", (double)item->mtime,
745 return conf->errabort;
750 /* install a file or create a directory or symlink. path should not exist
753 /* flags: 1 = set md, 2 = create leading dirs, 4 = unlink existing file,
754 * 8 = rmdir existing dir, 16 = return true/false
758 #define INS_UNLINK 0x4
759 #define INS_RMDIR 0x8
761 #define INS_ZPMNEW 0x20
762 static int install(struct config *conf, struct nitem *item, unsigned int flags) {
766 int mkleading = (flags & 2);
767 int setmd = (flags & 1);
768 int unlink_file = (flags & 4);
769 int rm_dir = (flags & 8);
770 int failure = conf->errabort;
780 printf("unlink %s\n", item->dest);
782 printf("rmdir %s\n", item->dest);
785 printf("install %c%o %d:%d %s", item->ftype,
786 item->mode, item->uid, item->gid,
788 if (item->ftype == 'l') {
789 printf(" -> %s", item->target);
796 source = conf->src ? conf->src : conf->log;
799 rv = remove_existing(conf, item->dest);
801 rv = remove_dir(conf, item->dest);
809 rv = create_leading_dirs(item->dest);
811 setsyserr(conf, "can't create leading dirs for %s", item->dest);
816 if (item->ftype == 'r') {
817 rv = zpm_extract(source, item->hash, item->dest, item->mode);
819 seterror(conf, "can't extract %s", item->dest);
825 switch (item->ftype) {
826 case 'd': rv = mkdir(item->dest, item->mode);
828 case 'l': rv = symlink(item->target, item->dest);
835 setsyserr(conf, "installing %s failed", item->dest);
840 return set_md(conf, item) == 0 ? success : failure;
847 * figure out what the difference is for a config file, only called
848 * for an update of a configuration file
849 * return -1 on an error
850 * return 1 if the new file should not be installed
851 * return 0 if the new file should be installed
853 static int adjust_for_config(struct config *conf, struct nitem *n, unsigned int
856 if (!n->oldwasconf) {
860 /* TODO what if old was a directory? */
861 if (!n->configuration) {
862 /* replacing conf with non-conf */
863 /* absorb file, mark todo */
864 char hash[ZPM_HASH_STRLEN+1];
867 fprintf(stderr, "importing old conf file\n");
869 if (zpm_import(conf->log, n->dest, 0, hash)) {
870 zpm_note_add(conf->log, n->pkglist, n->dest, hash,
871 "replaced config file with non-config. zpm-cat %.8s", hash);
873 fprintf(stderr, "unable to import existing config file %s\n", n->dest);
877 fprintf(stderr, "dry-run: would replace config file %s with non-config file\n", n->dest);
882 int sametype = (!(diffs & D_TYPE));
883 int isdir = (diffs & D_ISDIR);
884 int eisdir = (diffs & D_EISDIR);
886 /* both old and new are config files */
887 if (isdir && sametype) {
888 /* both config directories, can only be changing
889 * metadata, so no adjustment needed
892 fprintf(stderr, "both config dirs, ok to update\n");
898 char hash[ZPM_HASH_STRLEN+1];
900 /* replacing old file with new directory */
901 /* absorb, make note */
903 if (zpm_import(conf->log, n->dest, 0, hash)) {
904 zpm_note_add(conf->log, n->pkglist, n->dest, hash,
905 "replaced config file with config directory. zpm-cat %.8s", hash);
907 fprintf(stderr, "unable to import existing config file %s\n", n->dest);
911 fprintf(stderr, "dry-run: would replace config file %s with config directory\n", n->dest);
917 /* replacing old conf directory with a conf file.
918 * nothing needs to be done, if the directory
919 * is empty, it's ok to remove. if it's not empty,
920 * the install will fail
925 /* replacing old file with new file */
926 /* new is same as on disk */
927 if (!(diffs & D_HASH)) {
929 fprintf(stderr, "new config file is already on disk, probably shouldn't happen\n");
934 /* new is different than on disk, but on disk is same as old */
935 if (!(diffs & D_OHASH)) {
937 fprintf(stderr, "old config file not changed from default, replacing with new default\n");
939 /* ok to do the update, since same as default */
940 fprintf(stderr, "updating default config %s\n", n->dest);
944 /* new is different than on disk, and disk different than old */
947 fprintf(stderr, "new default config file is different than on disk, and old default was changed, should keep on-disk config\n");
950 zpm_note_add(conf->log, n->pkglist, n->dest, n->hash,
951 "default config file update. zpm-cat %.8s", n->hash);
952 /* TODO check for note error */
954 fprintf(stderr, "dry-run: default config file %s update\n",
961 static int install_files(void *f, int ncols, char **vals, char **cols) {
962 struct config *conf = f;
964 struct stat existing;
967 /* TODO put the result row in a hash table. May not actually
970 if (!read_item(conf, ncols, vals, cols, &nitem)) {
971 fprintf(stderr, "can't read item\n");
972 return conf->errabort;
977 used = sqlite3_memory_used()/1024/1024;
978 high = sqlite3_memory_highwater(0)/1024/1024;
979 fprintf(stderr, "memory = %ld MB / %ld MB\n", used, high);
981 if (conf->verbose && !conf->dryrun) {
982 if (conf->progress == 2) {
983 fprintf(stderr, "%s '%c' %s\n", nitem.opstr, nitem.ftype,
985 } else if (conf->progress == 1) {
987 pdots(50, '.', conf->ops_completed, conf->ops_completed + 1, conf->ops_total);
988 conf->ops_completed++;
989 conf->ops_completed++;
991 pdots(50, '.', conf->ops_completed, conf->ops_completed + 1, conf->ops_total);
992 conf->ops_completed++;
996 unsigned int diffs = file_compare(&nitem, &existing);
997 if (diffs >= D_ERROR) {
998 return seterror(conf, "can't check %s", nitem.dest);
1002 * exist & same type & md same & hash same: do nothing, but warn bug
1003 * exist & same type & md diff & hash same: fix md
1004 * exist & same type & md same & hash diff: replace
1005 * exist & same type & md diff & hash diff: replace & fix
1006 * no exist: install and warn
1007 * dir & not dir : remove, mkdir
1008 * not dir & not dir & diff type: remove, install
1009 * not dir & dir : remove dir if empty, error if not empty, install
1012 * no exist: create leading dirs, install
1014 * exist & same type & md same & hash same & accept or over: do nothing
1015 * exist & same & md diff or hash diff & overwrite : update
1016 * exist & same & md diff or hash diff & accept : error, can't accept
1017 * exist & same & md diff or hash diff & not accept : error
1019 * exist & different type & not overwrite : error
1020 * not dir & not dir & overwrite : remove and install
1021 * not dir & dir & overwrite: remove empty or error, install
1022 * dir & dir & overwrite: fix md
1023 * dir & not dir & overwrite: remove and mkdir
1025 int exist = (!(diffs & D_NOEXIST));
1026 int sametype = (!(diffs & D_TYPE));
1027 int mdsame = (!(diffs & D_MD));
1028 int hashsame = (!(diffs & D_HASH));
1029 int isdir = (diffs & D_ISDIR);
1030 int eisdir = (diffs & D_EISDIR);
1031 int accept = conf->accept;
1032 int overwrite = conf->overwrite;
1033 int installing = (nitem.op == OP_NEW);
1034 update = (nitem.op == OP_UPDATE);
1038 /* warn, it should exist */
1039 fprintf(stderr, "%s missing, installing", nitem.dest);
1040 return install(conf, &nitem, 3);
1043 switch (adjust_for_config(conf, &nitem, diffs)) {
1044 case -1: return conf->errabort; break;
1046 fprintf(stderr, "skipping changed default config file: %s\n", nitem.dest);
1051 /* file exists in filesystem */
1053 if (mdsame && hashsame) {
1054 /* warn, bug in logic. This shouldn't occur,
1055 * because if there is nothing to do, it
1056 * shouldn't be listed as an update
1058 /* could be an update. We're checking against
1059 * what's actually on disk, not what was
1060 * expected to have been on disk. So, if
1061 * the admin has modified the file, or if
1062 * it had been installed ignoring the user
1063 * and group, it might be correct on disk
1064 * but not as in the local database
1066 /* TODO detect whether this a logic bug or
1067 * an on-disk difference
1070 fprintf(stderr, "%s should not be an update\n", nitem.dest);
1071 fprintf(stderr, "old hash: %s\n", nitem.ohash);
1072 fprintf(stderr, "new hash: %s\n", nitem.hash);
1073 fprintf(stderr, "old mds: %s\n", nitem.omds);
1074 fprintf(stderr, "new mds: %s\n", nitem.mds);
1079 if (!mdsame && hashsame) {
1081 return set_md(conf, &nitem);
1083 if (mdsame && !hashsame) {
1085 return install(conf, &nitem, 3);
1087 if (!mdsame && !hashsame) {
1089 return install(conf, &nitem, 3);
1093 /* file exists, and is not the same type */
1095 if (isdir && !eisdir) {
1096 /* remove existing */
1098 return install(conf, &nitem, 7);
1100 if (!isdir && eisdir) {
1101 /* remove dir, or error */
1103 return install(conf, &nitem, 11);
1105 if (!isdir && !isdir) {
1106 /* necessarily !sametype, sametype handled above */
1107 /* remove existing */
1109 return install(conf, &nitem, 7);
1111 /* error, should not be possible, assert(0)? */
1112 fprintf(stderr,"impossible state: %s:%d\n", __func__, __LINE__);
1117 return install(conf, &nitem, 3);
1120 /* file exists in filesystem */
1122 if (mdsame && hashsame && (accept || overwrite)) {
1124 if (conf->dryrun || conf->verbose) {
1125 fprintf(stderr, "accept %s: %s\n",
1126 eisdir ? "directory" : "file", nitem.dest);
1131 if (mdsame && isdir && conf->acceptdir) {
1135 if (!mdsame && isdir && conf->ignoredirmd) {
1136 /* TODO warn ignoring dir md */
1140 if (mdsame && hashsame && !(accept || overwrite)) {
1142 return seterror(conf, "file exists: %s", nitem.dest);
1145 if (mdsame && !hashsame && overwrite) {
1147 return install(conf, &nitem, eisdir ? 11 : 7);
1150 if (nitem.configuration && accept) {
1151 /* accept a changed config file */
1152 if (conf->dryrun || conf->verbose) {
1153 fprintf(stderr, "accept %smodified config %s: %s\n", (!mdsame || !hashsame) ? "" : "un",
1154 eisdir ? "directory" : "file", nitem.dest);
1159 if (mdsame && !hashsame && !overwrite) {
1160 /* accept doesn't matter, since it's
1161 * not an acceptable file */
1163 if (nitem.ftype == 'l') {
1166 lsize = readlink(nitem.dest, link, sizeof link);
1167 if (lsize == -1 || (size_t)lsize >= sizeof link) {
1168 return seterror(conf, "%s (linkdiff): expecting %s -> %s, unable to read link", accept ? "existing file not acceptable" : "file exists", nitem.dest, nitem.target, link);
1171 /* links must be different */
1172 return seterror(conf, "%s (linkdiff): expecting %s -> %s, have -> %s", accept ? "existing file not acceptable" : "file exists", nitem.dest, nitem.target, link);
1175 return seterror(conf, "%s (hashdiff): %s", accept ? "existing file not acceptable" : "file exists", nitem.dest);
1178 if (!mdsame && hashsame && overwrite) {
1180 return set_md(conf, &nitem);
1182 if (!mdsame && hashsame && !overwrite) {
1183 /* accept doesn't matter, since it's
1184 * not an acceptable file */
1186 return seterror(conf, "%s (mddiff): %s", accept ? "existing file not acceptable" : "file exists", nitem.dest);
1188 if (!mdsame && !hashsame && overwrite) {
1190 return install(conf, &nitem, eisdir ? 11 : 7);
1192 if (!mdsame && !hashsame && !overwrite) {
1193 /* accept doesn't matter, since it's
1194 * not an acceptable file */
1196 return seterror(conf, "%s (md+hash): %s", accept ? "existing file not acceptable" : "file exists", nitem.dest);
1198 /* TODO error, should be impossible */
1199 return seterror(conf, "impossible state reached");
1202 /* file exists, and is not the same type */
1205 return seterror(conf, "%s (difftype): %s", accept ? "existing file not acceptable" : "file exists", nitem.dest);
1208 /* not the same type, but ok to overwrite */
1210 /* remove existing */
1211 return install(conf, &nitem, 7);
1214 /* existing path is a directory */
1217 /* impossible, if isdir and eisdir, would
1221 return set_md(conf, &nitem);
1223 /* remove empty dir or error */
1225 return install(conf, &nitem, 11);
1227 /* if we get here, we missed a case */
1229 return seterror(conf, "impossible state 2 reached");
1232 /* TODO extra verbose print perms, mtime, etc, probably ls -l
1235 if (conf->verbose) {
1236 printf("%s\n", nitem.path);
1242 static void check_conflicts(struct config *conf, char *conflict_type,
1243 int (callback)(void *, int, char **, char **)) {
1249 s = sqlite3_str_new(conf->log->db);
1250 sqlite3_str_appendall(s, "select *, ");
1251 if (conf->rootdir) {
1252 sqlite3_str_appendf(s, "printf('%%s/%%s',rtrim(%Q,'/'),ltrim(path,'/'))", conf->rootdir);
1254 sqlite3_str_appendf(s, "printf('/%%s', trim(path, '/'))");
1256 sqlite3_str_appendall(s, " as dest from syncconflicts");
1258 if (conflict_type) {
1259 sqlite3_str_appendf(s," where conflict = %Q", conflict_type);
1261 if (conf->reverse) {
1262 sqlite3_str_appendall(s," order by length(path) desc, path desc,pkgid collate vercmp desc, conflict desc");
1264 sqlite3_str_appendall(s," order by length(path), path, pkgid collate vercmp, conflict");
1268 sql = sqlite3_str_value(s);
1270 rv = zpm_exec(conf->log, sql, callback, conf, &errmsg);
1272 sqlite3_str_finish(s);
1275 fprintf(stderr, "exec fail: %s\n", sqlite3_errstr(rv));
1277 fprintf(stderr, "database error: %s\n", errmsg);
1280 if (conf->log->error == 1) {
1281 fprintf(stderr, "unable to allocate memory\n");
1283 fprintf(stderr, "zpm_exec failure: %s\n",
1284 conf->log->errmsg ? conf->log->errmsg : "unknown");
1287 if (conf->log->errmsg) {
1288 fprintf(stderr, "error: %s\n", conf->log->errmsg);
1290 if (conf->errors && conf->exitonerror) {
1291 zpm_close(conf->log);
1292 zpm_close(conf->src);
1295 /* TODO final report function in conf var */
1298 static void runstage(struct config *conf, char *stage,
1299 int (callback)(void *, int, char **, char **)) {
1305 s = sqlite3_str_new(conf->log->db);
1306 sqlite3_str_appendall(s, "select *, ");
1307 if (conf->rootdir) {
1308 sqlite3_str_appendf(s, "printf('%%s/%%s',rtrim(%Q,'/'),ltrim(path,'/'))", conf->rootdir);
1310 sqlite3_str_appendf(s, "printf('/%%s', trim(path, '/'))");
1312 sqlite3_str_appendall(s, " as dest from syncinfo");
1315 sqlite3_str_appendf(s," where op = %Q", stage);
1317 if (conf->reverse) {
1318 sqlite3_str_appendall(s," order by length(path) desc, path desc");
1321 sql = sqlite3_str_value(s);
1323 rv = zpm_exec(conf->log, sql, callback, conf, &errmsg);
1325 sqlite3_str_finish(s);
1328 fprintf(stderr, "exec fail: %s\n", sqlite3_errstr(rv));
1330 fprintf(stderr, "database error: %s\n", errmsg);
1333 if (conf->log->error == 1) {
1334 fprintf(stderr, "unable to allocate memory\n");
1336 fprintf(stderr, "zpm_exec failure: %s\n",
1337 conf->log->errmsg ? conf->log->errmsg : "unknown");
1341 if (conf->log->errmsg) {
1342 fprintf(stderr, "error: %s\n", conf->log->errmsg);
1345 if (conf->errors && conf->exitonerror) {
1346 zpm_close(conf->log);
1347 zpm_close(conf->src);
1350 /* TODO final report function in conf var */
1353 static void count_ops(struct config *conf) {
1354 conf->ops_remove = zpm_db_int(conf->log, "select count(*) from syncinfo where op = 'remove'");
1355 conf->ops_update = zpm_db_int(conf->log, "select count(*) from syncinfo where op = 'update'");
1356 conf->ops_install = zpm_db_int(conf->log, "select count(*) from syncinfo where op = 'new'");
1357 conf->ops_total = conf->ops_remove + conf->ops_update + conf->ops_install;
1360 int main(int ac, char **av) {
1364 char *pkgdbfile = 0, *localdbfile = 0;
1367 struct config conf = { 0 };
1384 if (geteuid() != 0) {
1389 localdbfile = ZPM_LOCAL_DB;
1390 if ((s = getenv("ZPMDB"))) {
1391 /* TODO does this need to be copied ? */
1395 if ((s = getenv("ZPM_ROOT_DIR"))) {
1396 /* TODO does this need to be copied ? */
1401 * -d localdb or ZPMDB * or /var/lib/zpm/zpm.db, or die
1402 * -f 'package database', otherwise regular default of env
1403 * ZPM_PACKAGE_FILE, or use pkgdb if otherwise not found
1404 * -R root of pkg, will just chdir there
1406 * args are pkgid triple, but will do a pkg find on the pkgdb
1409 while ((opt = getopt(ac, av, "f:d:c:nCR:vOAMDp")) != -1) {
1411 case 'd': localdbfile = optarg; break;
1412 case 'f': pkgdbfile = optarg; break;
1413 case 'n': conf.dryrun = 1; break;
1414 case 'v': conf.verbose++; break;
1415 case 'C': conf.errabort = 0; break;
1416 case 'R': conf.rootdir = optarg; break;
1417 case 'N': conf.setuser = 0; conf.setgroup = 0; break;
1418 case 'O': conf.overwrite = 1; break;
1419 case 'A': conf.accept = 1; break;
1420 case 'M': conf.ignoredirmd = 1;
1421 case 'D': conf.acceptdir = 0;
1422 case 'p': conf.progress++;
1430 /* verify root dir exists */
1431 if (conf.rootdir && !exists(conf.rootdir, NULL)) {
1432 fprintf(stderr, "rootdir %s does not exist\n", conf.rootdir);
1435 if (!zpm_open(&localdb, localdbfile)) {
1436 fprintf(stderr, "can't open zpm db %s\n", localdbfile);
1439 conf.log = &localdb;
1442 /* TODO open read-only */
1443 if (!zpm_open(&pkgdb, pkgdbfile)) {
1444 fprintf(stderr, "can't open src db %s\n", localdbfile);
1451 /* TODO find pkgid from arg */
1453 /* TODO set conf var to finalize error reporting */
1455 fprintf(stderr, "syncing filesystem %s (ldb %s) from %s\n",
1456 conf.rootdir ? conf.rootdir : "/",
1457 localdbfile, pkgdbfile);
1461 conf.exitonerror = 0;
1462 check_conflicts(&conf, NULL, report_conflicts);
1464 if (conf.conflicts) {
1465 fprintf(stderr, "%d conflicts reported, aborting sync\n",
1469 /* no point in running it if we're just going to
1470 * overwrite everything
1472 if (!conf.overwrite && !conf.accept && !conf.dryrun) {
1473 runstage(&conf, "new", check_existing);
1477 fprintf(stderr, "beginning %ssync\n", conf.dryrun ?
1480 /* have to do the removes first otherwise
1481 * old files may conflict with update file
1485 conf.exitonerror = conf.dryrun ? 0 : 1;
1486 conf.errabort = conf.dryrun ? 0 : 1;
1488 fprintf(stderr, "file ops: %d\n", conf.ops_total);
1489 if (conf.ops_remove > 0) {
1491 fprintf(stderr, "removing %d file%s\n", conf.ops_remove, conf.ops_remove > 0 ? "s" : "");
1494 conf.ops_completed = 0;
1495 conf.ops_total = conf.ops_remove;
1496 runstage(&conf, "remove", remove_files);
1497 if (conf.verbose && conf.progress < 2) {
1498 fprintf(stderr, " done\n");
1503 if (conf.ops_update > 0) {
1505 fprintf(stderr, "updating %d file%s\n", conf.ops_update, conf.ops_update > 0 ? "s" : "");
1508 conf.ops_completed = 0;
1509 conf.ops_total = conf.ops_update;
1510 runstage(&conf, "update", install_files);
1511 if (conf.verbose && conf.progress < 2) {
1512 fprintf(stderr, " done\n");
1517 if (conf.ops_install > 0) {
1519 fprintf(stderr, "installing %d file%s\n", conf.ops_install, conf.ops_install > 0 ? "s" : "");
1522 conf.ops_completed = 0;
1523 conf.ops_total = conf.ops_install;
1524 runstage(&conf, "new", install_files);
1525 if (conf.verbose && conf.progress < 2) {
1526 fprintf(stderr, " done\n");
1533 zpm_close(&localdb);
1534 zpm_close(conf.src);
1535 return conf.errors ? 1 : 0;