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 progress; /* type of progress meter */
48 char *pkglist; /* space separated */
52 int configuration, oldwasconf;
53 struct timespec times[2];
57 printf("usage: zpm $scriptname [-fncC] args ...\n");
60 static int seterror(struct config *conf, char *msgfmt, ...) {
67 vsnprintf(msg, sizeof msg, msgfmt, ap);
71 if (conf->log->errmsg) {
72 free(conf->log->errmsg);
75 conf->log->errmsg = strdup(msg);
78 fprintf(stderr, "%s\n", msg);
81 return conf->errabort;
84 static int setsyserr(struct config *conf, char *msgfmt, ...) {
92 printed = vsnprintf(msg, sizeof msg, msgfmt, ap);
96 /* nothing we can really do */
97 return conf->errabort;
100 if ((size_t)printed < sizeof msg) {
101 snprintf(msg+printed, sizeof msg - printed, ": %s",
106 if (conf->log->errmsg) {
107 free(conf->log->errmsg);
110 conf->log->errmsg = strdup(msg);
113 fprintf(stderr, "%s\n", msg);
116 return conf->errabort;
119 static int exists(char *path, mode_t *mode) {
122 if (lstat(path, &st) == -1) {
125 if (mode) *mode = st.st_mode;
129 /* TODO maintain a list of already created directories */
130 static int create_leading_dirs(char *path) {
133 char pcopy[ZPM_PATH_MAX];
138 delim = strrchr(pcopy, '/');
139 if (!delim) return 1; /* not an error, but no leading dirs */
141 /* cut off last component */
150 delim = strchr(s, '/');
156 /* try to create the directory, if it exists
157 * and is a directory or a symlink, that's ok
158 * should be (eventually) a symlink to a directory
159 * so we want stat here, not lstat
161 if (mkdir(pcopy, 0755) == -1) {
164 if (stat(pcopy, &st) == -1) {
168 switch (st.st_mode & S_IFMT) {
188 static char *column(char *col, int ncols, char **vals, char **cols) {
192 for (i=0; i < ncols; i++) {
193 // fprintf(stderr, "checking '%s' = '%s'\n", cols[i], vals[i]);
195 if (!strcmp(col, cols[i])) {
203 #define COL(x) column(x, ncols, vals, cols)
204 #define SYSERR(x) do { conf->log->error = 2; return conf->errabort; } while (0)
207 static char *ops[] = { "new", "remove", "update", 0 };
215 static int getop(char *opstr) {
218 if (!opstr) return 0;
219 for (i=0;ops[i];i++) {
220 if (!strcmp(opstr, ops[i])) {
227 static int report_conflicts(void *f, int ncols, char **vals, char **cols) {
228 struct config *conf = f;
229 char *path, *hash, *pkg, *conflict_type, *mds;
233 conflict_type = COL("conflict");
234 if (!strcmp(conflict_type, "hash")) {
236 fprintf(stderr, "hash conflict: package %s path %s hash %.8s\n",
239 if (!strcmp(conflict_type, "md")) {
241 fprintf(stderr, "md conflict: package %s path %s md %s\n",
244 fprintf(stderr, "%s conflict: package %s path %s\n",
245 conflict_type, pkg, path);
252 static int read_item(struct config *conf, int ncols, char **vals, char **cols,
258 struct nitem zero = { 0 };
264 seterror(conf, "can't determine op");
270 seterror(conf, "can't determine op");
274 n->path = COL("path");
276 seterror(conf, "no file path");
279 if (strlen(n->path) == 0) {
280 seterror(conf, "zero length path not allowed");
284 /* TODO config to dishonor setuid/setgid */
285 n->dest = COL("dest");
287 seterror(conf, "no file dest");
291 if (strlen(n->dest) == 0) {
292 seterror(conf, "zero length dest not allowed");
299 seterror(conf, "can't determine mode");
303 n->mode = strtoul(val, NULL, 8);
305 val = COL("configuration");
307 seterror(conf, "can't determine config status");
310 lval = strtol(val, NULL, 10);
312 n->configuration = ((lval & 1) != 0);
313 n->oldwasconf = ((lval & 2) != 0);
315 val = COL("filetype");
316 if (!val || strlen(val) == 0) {
317 seterror(conf, "can't determine file type");
322 /* these can be null */
323 n->ohash = COL("ohash");
325 n->omds = COL("omds");
326 n->pkglist = COL("pkglist");
328 if (n->ftype == 'r') {
329 n->hash = COL("hash");
331 seterror(conf, "can't get hash");
334 } else if (n->ftype == 'l') {
335 n->target = COL("target");
337 seterror(conf, "can't get target");
340 if (strlen(n->target) == 0) {
341 seterror(conf, "zero length target not allowed");
348 val = COL("username");
350 seterror(conf, "no username");
355 seterror(conf, "no passwd entry");
363 if (conf->setgroup) {
364 val = COL("groupname");
366 seterror(conf, "no groupname");
371 seterror(conf, "no group entry for %s", val);
380 double mtime = strtod(COL("mtime"),NULL);
382 mtime = (double)time(NULL);
385 n->mtime = (time_t)mtime;
387 n->times[0].tv_sec = 0;
388 n->times[0].tv_nsec = UTIME_OMIT;
389 n->times[1].tv_sec = (time_t)llrint(floor(mtime));
390 n->times[1].tv_nsec = lrint(floor(fmod(mtime,1.0)*1000000000));
395 /* file does not exist */
396 #define D_NOEXIST 0x1
397 /* files are different types */
399 /* metadata is different */
401 /* content or link target is different */
403 /* file to be installed is a directory */
405 /* path on disk is a directory */
406 #define D_EISDIR 0x20
407 /* usernames different */
409 /* group names different */
411 /* file mode is different */
413 /* mtimes are different */
414 #define D_MTIME 0x200
415 /* the hash of the file we are supposedly replacing is different than
416 * the the hash of the file on disk
418 #define D_OHASH 0x400
419 /* an error occurred trying to compare the file (other than it doesn't exist */
420 #define D_ERROR 0x1000
421 /* there was a stat error */
422 #define D_STATERROR 0x2000
423 /* there was an error calling readlink */
424 #define D_RLERROR 0x4000
426 /* 1 = file doesn't exist, 2 = file is a directory, target isn't */
427 /* 4 == ftype different */
428 /* 8 = hash different when both are regular files */
429 static unsigned int file_compare(struct nitem *n, struct stat *st) {
430 int etype = 0, stat_type;
431 char ehash[ZPM_HASH_STRLEN+1];
432 unsigned int diff = 0;
437 case 'd': etype = S_IFDIR; diff |= D_ISDIR ; break;
438 case 'r': etype = S_IFREG; break;
439 case 'l': etype = S_IFLNK; break;
440 default: etype = 0; break;
444 /* new file, so check type, hash, etc */
445 if (lstat(n->dest, st) == 0) {
446 stat_type = st->st_mode & S_IFMT;
447 if (stat_type != etype) {
450 if (stat_type == S_IFDIR) {
454 if (n->hash && etype == S_IFREG && stat_type == S_IFREG) {
455 zpm_hash(n->dest, ehash, 0);
456 if (strcmp(n->hash, ehash) != 0) {
459 if (n->ohash && strcmp(n->ohash, ehash) != 0) {
463 if (n->hash && etype == S_IFLNK && stat_type == S_IFLNK) {
464 lsize = readlink(n->dest, link, sizeof link);
466 if (lsize == -1 || lsize == sizeof link) {
471 if (strcmp(n->target, link) != 0) {
476 if (n->uid != st->st_uid) {
480 if (n->gid != st->st_gid) {
484 if (n->mode != (st->st_mode & 07777)) {
490 case ENOENT: diff |= D_NOEXIST; break;
491 default: diff |= (D_STATERROR|D_ERROR); break;
499 /* 0 = not acceptable
500 * 1 = accept and create/update/remove
502 * 3 = remove and overwrite
503 * 4 = update metadata
505 static int acceptable(struct config *conf, unsigned int diffs, int op) {
506 int exist = (!(diffs & D_NOEXIST));
507 int sametype = (!(diffs & D_TYPE));
508 int mdsame = (!(diffs & D_MD));
509 int hashsame = (!(diffs & D_HASH));
510 int isdir = (diffs & D_ISDIR);
513 return op == OP_REMOVE ? 2 : 1;
516 if (op == OP_UPDATE) {
517 return sametype ? 4 : 3;
520 if (op == OP_REMOVE) {
524 /* the hard cases, should be installing new, but already exists */
527 return conf->overwrite ? 3 : 0;
530 if (mdsame && (conf->accept || conf->overwrite)) {
535 if (mdsame || conf->ignoredirmd) {
536 return conf->acceptdir ? 2 : 0;
538 if (conf->overwrite) {
543 if (hashsame && (conf->accept || conf->overwrite)) {
547 return conf->overwrite ? 3 : 0;
550 static int check_existing(void *f, int ncols, char **vals, char **cols) {
551 struct config *conf = f;
555 if (!read_item(conf, ncols, vals, cols, &nitem)) {
556 fprintf(stderr, "can't read item\n");
557 return conf->errabort;
560 if (conf->verbose > 1) {
561 fprintf(stderr, "check for existing %s\n", nitem.path);
564 if (lstat(nitem.path, &st) == -1) {
566 /* not an error, file shouldn't exist*/
569 fprintf(stderr, "unable to check %s: %s\n",
570 nitem.path, strerror(errno));
577 unsigned int diffs = file_compare(&nitem, &st);
578 if (diffs >= D_ERROR) {
579 return seterror(conf, "can't check %s", nitem.dest);
582 int action = acceptable(conf, diffs, nitem.op);
585 fprintf(stderr, "%s exists and is not acceptable\n", nitem.path);
587 fprintf(stderr, "%s exists\n", nitem.path);
595 static int remove_files(void *f, int ncols, char **vals, char **cols) {
596 struct config *conf = f;
602 if (!dest) return seterror(conf,"no file dest");
605 char *ftype = COL("filetype");
609 case 'd': printf("rmdir %s\n", dest); break;
610 default: printf("unlink %s\n", dest); break;
616 if (lstat(dest, &st) == -1) {
617 return seterror(conf,"can't stat");
620 if (S_ISDIR(st.st_mode)) {
621 flags = AT_REMOVEDIR;
623 /* TODO check that expected filetype matches actual filetype */
626 fprintf(stderr, "%s(%s)\n", flags ? "rmdir" : "unlink", dest);
631 if (unlinkat(AT_FDCWD, dest, flags) == -1) {
636 return seterror(conf, "can't unlink");
643 #define MARK fprintf(stderr, "%s %d: mark\n", __func__, __LINE__)
646 static int remove_dir(struct config *conf, char *path) {
651 setsyserr(conf, "can't rmdir %s", path);
657 static int remove_existing(struct config *conf, char *path) {
662 setsyserr(conf, "can't unlink %s", path);
668 static int set_md(struct config *conf, struct nitem *item) {
673 if (item->ftype != 'l') {
674 printf("chmod %o %s\n", item->mode, item->dest);
676 if (conf->setuser && conf->setgroup) {
677 printf("lchown %d:%d %s\n", item->uid, item->gid,
680 printf("mtime %.0f %s\n", (double)item->mtime, item->dest);
685 /* can't chmod a symlink */
686 if (item->ftype != 'l') {
687 rv = chmod(item->dest, item->mode);
690 setsyserr(conf, "can't chmod %o %s", item->mode, item->dest);
691 return conf->errabort;
695 if (conf->setuser && conf->setgroup) {
696 rv = lchown(item->dest, item->uid, item->gid);
698 setsyserr(conf, "can't lchown %s", item->dest);
699 return conf->errabort;
703 rv = utimensat(AT_FDCWD, item->dest, item->times, AT_SYMLINK_NOFOLLOW);
705 setsyserr(conf, "can't set mtime %.0f %s", (double)item->mtime,
707 return conf->errabort;
712 /* install a file or create a directory or symlink. path should not exist
715 /* flags: 1 = set md, 2 = create leading dirs, 4 = unlink existing file,
716 * 8 = rmdir existing dir, 16 = return true/false
720 #define INS_UNLINK 0x4
721 #define INS_RMDIR 0x8
723 #define INS_ZPMNEW 0x20
724 static int install(struct config *conf, struct nitem *item, unsigned int flags) {
728 int mkleading = (flags & 2);
729 int setmd = (flags & 1);
730 int unlink_file = (flags & 4);
731 int rm_dir = (flags & 8);
732 int failure = conf->errabort;
742 printf("unlink %s\n", item->dest);
744 printf("rmdir %s\n", item->dest);
747 printf("install %c%o %d:%d %s", item->ftype,
748 item->mode, item->uid, item->gid,
750 if (item->ftype == 'l') {
751 printf(" -> %s", item->target);
758 source = conf->src ? conf->src : conf->log;
761 rv = remove_existing(conf, item->dest);
763 rv = remove_dir(conf, item->dest);
771 rv = create_leading_dirs(item->dest);
773 setsyserr(conf, "can't create leading dirs for %s", item->dest);
778 if (item->ftype == 'r') {
779 rv = zpm_extract(source, item->hash, item->dest, item->mode);
781 seterror(conf, "can't extract %s", item->dest);
787 switch (item->ftype) {
788 case 'd': rv = mkdir(item->dest, item->mode);
790 case 'l': rv = symlink(item->target, item->dest);
797 setsyserr(conf, "installing %s failed", item->dest);
802 return set_md(conf, item) == 0 ? success : failure;
809 * figure out what the difference is for a config file, only called
810 * for an update of a configuration file
811 * return -1 on an error
812 * return 1 if the new file should not be installed
813 * return 0 if the new file should be installed
815 static int adjust_for_config(struct config *conf, struct nitem *n, unsigned int
818 if (!n->oldwasconf) {
822 /* TODO what if old was a directory? */
823 if (!n->configuration) {
824 /* replacing conf with non-conf */
825 /* absorb file, mark todo */
826 char hash[ZPM_HASH_STRLEN+1];
829 fprintf(stderr, "importing old conf file\n");
831 if (zpm_import(conf->log, n->dest, 0, hash)) {
832 zpm_note_add(conf->log, n->pkglist, n->dest, hash,
833 "replaced config file with non-config. zpm-cat %.8s", hash);
835 fprintf(stderr, "unable to import existing config file %s\n", n->dest);
839 fprintf(stderr, "dry-run: would replace config file %s with non-config file\n", n->dest);
844 int sametype = (!(diffs & D_TYPE));
845 int isdir = (diffs & D_ISDIR);
846 int eisdir = (diffs & D_EISDIR);
848 /* both old and new are config files */
849 if (isdir && sametype) {
850 /* both config directories, can only be changing
851 * metadata, so no adjustment needed
854 fprintf(stderr, "both config dirs, ok to update\n");
860 char hash[ZPM_HASH_STRLEN+1];
862 /* replacing old file with new directory */
863 /* absorb, make note */
865 if (zpm_import(conf->log, n->dest, 0, hash)) {
866 zpm_note_add(conf->log, n->pkglist, n->dest, hash,
867 "replaced config file with config directory. zpm-cat %.8s", hash);
869 fprintf(stderr, "unable to import existing config file %s\n", n->dest);
873 fprintf(stderr, "dry-run: would replace config file %s with config directory\n", n->dest);
879 /* replacing old conf directory with a conf file.
880 * nothing needs to be done, if the directory
881 * is empty, it's ok to remove. if it's not empty,
882 * the install will fail
887 /* replacing old file with new file */
888 /* new is same as on disk */
889 if (!(diffs & D_HASH)) {
891 fprintf(stderr, "new config file is already on disk, probably shouldn't happen\n");
896 /* new is different than on disk, but on disk is same as old */
897 if (!(diffs & D_OHASH)) {
899 fprintf(stderr, "old config file not changed from default, replacing with new default\n");
901 /* ok to do the update, since same as default */
902 fprintf(stderr, "updating default config %s\n", n->dest);
906 /* new is different than on disk, and disk different than old */
909 fprintf(stderr, "new default config file is different than on disk, and old default was changed, should keep on-disk config\n");
912 zpm_note_add(conf->log, n->pkglist, n->dest, n->hash,
913 "default config file update. zpm-cat %.8s", n->hash);
914 /* TODO check for note error */
916 fprintf(stderr, "dry-run: default config file %s update\n",
923 static int install_files(void *f, int ncols, char **vals, char **cols) {
924 struct config *conf = f;
926 struct stat existing;
929 /* TODO put the result row in a hash table. May not actually
932 if (!read_item(conf, ncols, vals, cols, &nitem)) {
933 fprintf(stderr, "can't read item\n");
934 return conf->errabort;
939 used = sqlite3_memory_used()/1024/1024;
940 high = sqlite3_memory_highwater(0)/1024/1024;
941 fprintf(stderr, "memory = %ld MB / %ld MB\n", used, high);
943 if (conf->verbose && !conf->dryrun) {
944 if (conf->progress == 2) {
945 fprintf(stderr, "%s '%c' %s\n", nitem.opstr, nitem.ftype,
947 } else if (conf->progress == 1) {
950 int was = 50 * conf->ops_completed / conf->ops_total;
951 int now = 50 * (conf->ops_completed+1) / conf->ops_total;
952 while (was++ < now) {
953 fprintf(stderr, ".");
955 conf->ops_completed++;
958 int was = 50 * conf->ops_completed / conf->ops_total;
959 int now = 50 * (conf->ops_completed+1) / conf->ops_total;
960 while (was++ < now) {
961 fprintf(stderr, ".");
963 conf->ops_completed++;
967 unsigned int diffs = file_compare(&nitem, &existing);
968 if (diffs >= D_ERROR) {
969 return seterror(conf, "can't check %s", nitem.dest);
973 * exist & same type & md same & hash same: do nothing, but warn bug
974 * exist & same type & md diff & hash same: fix md
975 * exist & same type & md same & hash diff: replace
976 * exist & same type & md diff & hash diff: replace & fix
977 * no exist: install and warn
978 * dir & not dir : remove, mkdir
979 * not dir & not dir & diff type: remove, install
980 * not dir & dir : remove dir if empty, error if not empty, install
983 * no exist: create leading dirs, install
985 * exist & same type & md same & hash same & accept or over: do nothing
986 * exist & same & md diff or hash diff & overwrite : update
987 * exist & same & md diff or hash diff & accept : error, can't accept
988 * exist & same & md diff or hash diff & not accept : error
990 * exist & different type & not overwrite : error
991 * not dir & not dir & overwrite : remove and install
992 * not dir & dir & overwrite: remove empty or error, install
993 * dir & dir & overwrite: fix md
994 * dir & not dir & overwrite: remove and mkdir
996 int exist = (!(diffs & D_NOEXIST));
997 int sametype = (!(diffs & D_TYPE));
998 int mdsame = (!(diffs & D_MD));
999 int hashsame = (!(diffs & D_HASH));
1000 int isdir = (diffs & D_ISDIR);
1001 int eisdir = (diffs & D_EISDIR);
1002 int accept = conf->accept;
1003 int overwrite = conf->overwrite;
1004 int installing = (nitem.op == OP_NEW);
1005 update = (nitem.op == OP_UPDATE);
1009 /* warn, it should exist */
1010 fprintf(stderr, "%s missing, installing", nitem.dest);
1011 return install(conf, &nitem, 3);
1014 switch (adjust_for_config(conf, &nitem, diffs)) {
1015 case -1: return conf->errabort; break;
1017 fprintf(stderr, "skipping changed default config file: %s\n", nitem.dest);
1022 /* file exists in filesystem */
1024 if (mdsame && hashsame) {
1025 /* warn, bug in logic. This shouldn't occur,
1026 * because if there is nothing to do, it
1027 * shouldn't be listed as an update
1029 /* could be an update. We're checking against
1030 * what's actually on disk, not what was
1031 * expected to have been on disk. So, if
1032 * the admin has modified the file, or if
1033 * it had been installed ignoring the user
1034 * and group, it might be correct on disk
1035 * but not as in the local database
1037 /* TODO detect whether this a logic bug or
1038 * an on-disk difference
1041 fprintf(stderr, "%s should not be an update\n", nitem.dest);
1042 fprintf(stderr, "old hash: %s\n", nitem.ohash);
1043 fprintf(stderr, "new hash: %s\n", nitem.hash);
1044 fprintf(stderr, "old mds: %s\n", nitem.omds);
1045 fprintf(stderr, "new mds: %s\n", nitem.mds);
1050 if (!mdsame && hashsame) {
1052 return set_md(conf, &nitem);
1054 if (mdsame && !hashsame) {
1056 return install(conf, &nitem, 3);
1058 if (!mdsame && !hashsame) {
1060 return install(conf, &nitem, 3);
1064 /* file exists, and is not the same type */
1066 if (isdir && !eisdir) {
1067 /* remove existing */
1069 return install(conf, &nitem, 7);
1071 if (!isdir && eisdir) {
1072 /* remove dir, or error */
1074 return install(conf, &nitem, 11);
1076 if (!isdir && !isdir) {
1077 /* necessarily !sametype, sametype handled above */
1078 /* remove existing */
1080 return install(conf, &nitem, 7);
1082 /* error, should not be possible, assert(0)? */
1083 fprintf(stderr,"impossible state: %s:%d\n", __func__, __LINE__);
1088 return install(conf, &nitem, 3);
1091 /* file exists in filesystem */
1093 if (mdsame && hashsame && (accept || overwrite)) {
1095 if (conf->dryrun || conf->verbose) {
1096 fprintf(stderr, "accept %s: %s\n",
1097 eisdir ? "directory" : "file", nitem.dest);
1102 if (mdsame && isdir && conf->acceptdir) {
1106 if (!mdsame && isdir && conf->ignoredirmd) {
1107 /* TODO warn ignoring dir md */
1111 if (mdsame && hashsame && !(accept || overwrite)) {
1113 return seterror(conf, "file exists: %s", nitem.dest);
1116 if (mdsame && !hashsame && overwrite) {
1118 return install(conf, &nitem, eisdir ? 11 : 7);
1121 if (nitem.configuration && accept) {
1122 /* accept a changed config file */
1123 if (conf->dryrun || conf->verbose) {
1124 fprintf(stderr, "accept %smodified config %s: %s\n", (!mdsame || !hashsame) ? "" : "un",
1125 eisdir ? "directory" : "file", nitem.dest);
1130 if (mdsame && !hashsame && !overwrite) {
1131 /* accept doesn't matter, since it's
1132 * not an acceptable file */
1134 if (nitem.ftype == 'l') {
1137 lsize = readlink(nitem.dest, link, sizeof link);
1138 if (lsize == -1 || (size_t)lsize >= sizeof link) {
1139 return seterror(conf, "%s (linkdiff): expecting %s -> %s, unable to read link", accept ? "existing file not acceptable" : "file exists", nitem.dest, nitem.target, link);
1142 /* links must be different */
1143 return seterror(conf, "%s (linkdiff): expecting %s -> %s, have -> %s", accept ? "existing file not acceptable" : "file exists", nitem.dest, nitem.target, link);
1146 return seterror(conf, "%s (hashdiff): %s", accept ? "existing file not acceptable" : "file exists", nitem.dest);
1149 if (!mdsame && hashsame && overwrite) {
1151 return set_md(conf, &nitem);
1153 if (!mdsame && hashsame && !overwrite) {
1154 /* accept doesn't matter, since it's
1155 * not an acceptable file */
1157 return seterror(conf, "%s (mddiff): %s", accept ? "existing file not acceptable" : "file exists", nitem.dest);
1159 if (!mdsame && !hashsame && overwrite) {
1161 return install(conf, &nitem, eisdir ? 11 : 7);
1163 if (!mdsame && !hashsame && !overwrite) {
1164 /* accept doesn't matter, since it's
1165 * not an acceptable file */
1167 return seterror(conf, "%s (md+hash): %s", accept ? "existing file not acceptable" : "file exists", nitem.dest);
1169 /* TODO error, should be impossible */
1170 return seterror(conf, "impossible state reached");
1173 /* file exists, and is not the same type */
1176 return seterror(conf, "%s (difftype): %s", accept ? "existing file not acceptable" : "file exists", nitem.dest);
1179 /* not the same type, but ok to overwrite */
1181 /* remove existing */
1182 return install(conf, &nitem, 7);
1185 /* existing path is a directory */
1188 /* impossible, if isdir and eisdir, would
1192 return set_md(conf, &nitem);
1194 /* remove empty dir or error */
1196 return install(conf, &nitem, 11);
1198 /* if we get here, we missed a case */
1200 return seterror(conf, "impossible state 2 reached");
1203 /* TODO extra verbose print perms, mtime, etc, probably ls -l
1206 if (conf->verbose) {
1207 printf("%s\n", nitem.path);
1213 static void check_conflicts(struct config *conf, char *conflict_type,
1214 int (callback)(void *, int, char **, char **)) {
1220 s = sqlite3_str_new(conf->log->db);
1221 sqlite3_str_appendall(s, "select *, ");
1222 if (conf->rootdir) {
1223 sqlite3_str_appendf(s, "printf('%%s/%%s',rtrim(%Q,'/'),ltrim(path,'/'))", conf->rootdir);
1225 sqlite3_str_appendf(s, "printf('/%%s', trim(path, '/'))");
1227 sqlite3_str_appendall(s, " as dest from syncconflicts");
1229 if (conflict_type) {
1230 sqlite3_str_appendf(s," where conflict = %Q", conflict_type);
1232 if (conf->reverse) {
1233 sqlite3_str_appendall(s," order by length(path) desc, path desc,pkgid collate vercmp desc, conflict desc");
1235 sqlite3_str_appendall(s," order by length(path), path, pkgid collate vercmp, conflict");
1239 sql = sqlite3_str_value(s);
1241 rv = zpm_exec(conf->log, sql, callback, conf, &errmsg);
1243 sqlite3_str_finish(s);
1246 fprintf(stderr, "exec fail: %s\n", sqlite3_errstr(rv));
1248 fprintf(stderr, "database error: %s\n", errmsg);
1251 if (conf->log->error == 1) {
1252 fprintf(stderr, "unable to allocate memory\n");
1254 fprintf(stderr, "zpm_exec failure: %s\n",
1255 conf->log->errmsg ? conf->log->errmsg : "unknown");
1258 if (conf->log->errmsg) {
1259 fprintf(stderr, "error: %s\n", conf->log->errmsg);
1261 if (conf->errors && conf->exitonerror) {
1262 zpm_close(conf->log);
1263 zpm_close(conf->src);
1266 /* TODO final report function in conf var */
1269 static void runstage(struct config *conf, char *stage,
1270 int (callback)(void *, int, char **, char **)) {
1276 s = sqlite3_str_new(conf->log->db);
1277 sqlite3_str_appendall(s, "select *, ");
1278 if (conf->rootdir) {
1279 sqlite3_str_appendf(s, "printf('%%s/%%s',rtrim(%Q,'/'),ltrim(path,'/'))", conf->rootdir);
1281 sqlite3_str_appendf(s, "printf('/%%s', trim(path, '/'))");
1283 sqlite3_str_appendall(s, " as dest from syncinfo");
1286 sqlite3_str_appendf(s," where op = %Q", stage);
1288 if (conf->reverse) {
1289 sqlite3_str_appendall(s," order by length(path) desc, path desc");
1292 sql = sqlite3_str_value(s);
1294 rv = zpm_exec(conf->log, sql, callback, conf, &errmsg);
1296 sqlite3_str_finish(s);
1299 fprintf(stderr, "exec fail: %s\n", sqlite3_errstr(rv));
1301 fprintf(stderr, "database error: %s\n", errmsg);
1304 if (conf->log->error == 1) {
1305 fprintf(stderr, "unable to allocate memory\n");
1307 fprintf(stderr, "zpm_exec failure: %s\n",
1308 conf->log->errmsg ? conf->log->errmsg : "unknown");
1312 if (conf->log->errmsg) {
1313 fprintf(stderr, "error: %s\n", conf->log->errmsg);
1316 if (conf->errors && conf->exitonerror) {
1317 zpm_close(conf->log);
1318 zpm_close(conf->src);
1321 /* TODO final report function in conf var */
1324 static int count_ops(struct config *conf) {
1325 return zpm_db_int(conf->log, "select count(*) from syncinfo where op in ('remove', 'update', 'new')");
1328 int main(int ac, char **av) {
1332 char *pkgdbfile = 0, *localdbfile = 0;
1335 struct config conf = { 0 };
1352 if (geteuid() != 0) {
1357 localdbfile = ZPM_LOCAL_DB;
1358 if ((s = getenv("ZPMDB"))) {
1359 /* TODO does this need to be copied ? */
1363 if ((s = getenv("ZPM_ROOT_DIR"))) {
1364 /* TODO does this need to be copied ? */
1369 * -d localdb or ZPMDB * or /var/lib/zpm/zpm.db, or die
1370 * -f 'package database', otherwise regular default of env
1371 * ZPM_PACKAGE_FILE, or use pkgdb if otherwise not found
1372 * -R root of pkg, will just chdir there
1374 * args are pkgid triple, but will do a pkg find on the pkgdb
1377 while ((opt = getopt(ac, av, "f:d:c:nCR:vOAMDp")) != -1) {
1379 case 'd': localdbfile = optarg; break;
1380 case 'f': pkgdbfile = optarg; break;
1381 case 'n': conf.dryrun = 1; break;
1382 case 'v': conf.verbose++; break;
1383 case 'C': conf.errabort = 0; break;
1384 case 'R': conf.rootdir = optarg; break;
1385 case 'N': conf.setuser = 0; conf.setgroup = 0; break;
1386 case 'O': conf.overwrite = 1; break;
1387 case 'A': conf.accept = 1; break;
1388 case 'M': conf.ignoredirmd = 1;
1389 case 'D': conf.acceptdir = 0;
1390 case 'p': conf.progress++;
1398 /* verify root dir exists */
1399 if (conf.rootdir && !exists(conf.rootdir, NULL)) {
1400 fprintf(stderr, "rootdir %s does not exist\n", conf.rootdir);
1403 if (!zpm_open(&localdb, localdbfile)) {
1404 fprintf(stderr, "can't open zpm db %s\n", localdbfile);
1407 conf.log = &localdb;
1410 /* TODO open read-only */
1411 if (!zpm_open(&pkgdb, pkgdbfile)) {
1412 fprintf(stderr, "can't open src db %s\n", localdbfile);
1419 /* TODO find pkgid from arg */
1421 /* TODO set conf var to finalize error reporting */
1423 fprintf(stderr, "syncing filesystem %s (ldb %s) from %s\n",
1424 conf.rootdir ? conf.rootdir : "/",
1425 localdbfile, pkgdbfile);
1429 conf.exitonerror = 0;
1430 check_conflicts(&conf, NULL, report_conflicts);
1432 if (conf.conflicts) {
1433 fprintf(stderr, "%d conflicts reported, aborting sync\n",
1437 /* no point in running it if we're just going to
1438 * overwrite everything
1440 if (!conf.overwrite && !conf.accept && !conf.dryrun) {
1441 runstage(&conf, "new", check_existing);
1445 fprintf(stderr, "beginning %ssync\n", conf.dryrun ?
1448 /* have to do the removes first otherwise
1449 * old files may conflict with update file
1453 conf.exitonerror = conf.dryrun ? 0 : 1;
1454 conf.errabort = conf.dryrun ? 0 : 1;
1456 conf.ops_total = count_ops(&conf);
1457 fprintf(stderr, "file ops: %d\n", conf.ops_total);
1459 fprintf(stderr, "removing old files\n");
1461 runstage(&conf, "remove", remove_files);
1464 fprintf(stderr, "updating files\n");
1466 runstage(&conf, "update", install_files);
1468 fprintf(stderr, "installing %d files\n", conf.ops_total);
1470 runstage(&conf, "new", install_files);
1471 if (conf.verbose && conf.progress < 2) {
1472 fprintf(stderr, " done\n");
1478 zpm_close(&localdb);
1479 zpm_close(conf.src);
1480 return conf.errors ? 1 : 0;