1 #define _POSIX_C_SOURCE 200809L
18 /* needed for S_IFMT and AT_FDCWD */
27 struct zpm *log; /* logging db will be attached as "log" */
31 int errabort, errors, verbose, dryrun, conflicts;
32 int setuser, setgroup;
33 int reverse, exitonerror;
34 int overwrite, accept, acceptdir, ignoredirmd;
35 unsigned long ops_total, ops_completed;
36 unsigned long ops_remove, ops_remove_completed;
37 unsigned long ops_update, ops_update_completed;
38 unsigned long ops_install, ops_install_completed;
39 int progress; /* type of progress meter */
52 char *pkglist; /* space separated */
56 int configuration, oldwasconf;
57 struct timespec times[2];
61 printf("usage: zpm $scriptname [-fncC] args ...\n");
64 static void warn(char *fmt, ...) {
68 vfprintf(stderr, fmt, args);
70 fprintf(stderr, "\n");
73 static void pdots(int len, int ch, int was, int now, int total) {
74 was = len * was / total;
78 now = len * now / total;
85 static int seterror(struct config *conf, char *msgfmt, ...) {
92 vsnprintf(msg, sizeof msg, msgfmt, ap);
96 if (conf->log->errmsg) {
97 free(conf->log->errmsg);
100 conf->log->errmsg = strdup(msg);
103 fprintf(stderr, "%s\n", msg);
106 return conf->errabort;
109 static int setsyserr(struct config *conf, char *msgfmt, ...) {
116 va_start(ap, msgfmt);
117 printed = vsnprintf(msg, sizeof msg, msgfmt, ap);
121 /* nothing we can really do */
122 return conf->errabort;
125 if ((size_t)printed < sizeof msg) {
126 snprintf(msg+printed, sizeof msg - printed, ": %s",
131 if (conf->log->errmsg) {
132 free(conf->log->errmsg);
135 conf->log->errmsg = strdup(msg);
138 fprintf(stderr, "%s\n", msg);
141 return conf->errabort;
144 static int exists(char *path, mode_t *mode) {
147 if (lstat(path, &st) == -1) {
150 if (mode) *mode = st.st_mode;
154 /* TODO maintain a list of already created directories */
155 static int create_leading_dirs(char *path) {
158 char pcopy[ZPM_PATH_MAX];
163 delim = strrchr(pcopy, '/');
164 if (!delim) return 1; /* not an error, but no leading dirs */
166 /* cut off last component */
175 delim = strchr(s, '/');
181 /* try to create the directory, if it exists
182 * and is a directory or a symlink, that's ok
183 * should be (eventually) a symlink to a directory
184 * so we want stat here, not lstat
186 if (mkdir(pcopy, 0755) == -1) {
189 if (stat(pcopy, &st) == -1) {
193 switch (st.st_mode & S_IFMT) {
213 static char *column(char *col, int ncols, char **vals, char **cols) {
217 for (i=0; i < ncols; i++) {
218 // fprintf(stderr, "checking '%s' = '%s'\n", cols[i], vals[i]);
220 if (!strcmp(col, cols[i])) {
228 #define COL(x) column(x, ncols, vals, cols)
229 #define SYSERR(x) do { conf->log->error = 2; return conf->errabort; } while (0)
232 /* TODO handle other ops properly */
233 static char *ops[] = { "new", "remove", "update", 0 };
241 static int getop(char *opstr) {
244 if (!opstr) return 0;
245 for (i=0;ops[i];i++) {
246 if (!strcmp(opstr, ops[i])) {
253 static int report_conflicts(void *f, int ncols, char **vals, char **cols) {
254 struct config *conf = f;
255 char *path, *hash, *pkg, *conflict_type, *mds;
259 conflict_type = COL("conflict");
260 if (!strcmp(conflict_type, "hash")) {
262 fprintf(stderr, "hash conflict: package %s path %s hash %.8s\n",
265 if (!strcmp(conflict_type, "md")) {
267 fprintf(stderr, "md conflict: package %s path %s md %s\n",
270 fprintf(stderr, "%s conflict: package %s path %s\n",
271 conflict_type, pkg, path);
278 static int read_item(struct config *conf, int ncols, char **vals, char **cols,
284 struct nitem zero = { 0 };
290 seterror(conf, "can't determine op");
296 seterror(conf, "can't determine op");
300 n->path = COL("path");
302 seterror(conf, "no file path");
305 if (strlen(n->path) == 0) {
306 seterror(conf, "zero length path not allowed");
310 /* TODO config to dishonor setuid/setgid */
311 n->dest = COL("dest");
313 seterror(conf, "no file dest");
317 if (strlen(n->dest) == 0) {
318 seterror(conf, "zero length dest not allowed");
325 seterror(conf, "can't determine mode");
329 n->mode = strtoul(val, NULL, 8);
331 val = COL("configuration");
333 seterror(conf, "can't determine config status");
336 lval = strtol(val, NULL, 10);
338 n->configuration = ((lval & 1) != 0);
339 n->oldwasconf = ((lval & 2) != 0);
341 val = COL("filetype");
342 if (!val || strlen(val) == 0) {
343 seterror(conf, "can't determine file type");
348 /* these can be null */
349 n->ohash = COL("ohash");
351 n->omds = COL("omds");
352 n->pkglist = COL("pkglist");
354 if (n->ftype == 'r') {
355 n->hash = COL("hash");
357 seterror(conf, "can't get hash");
360 } else if (n->ftype == 'l') {
361 n->target = COL("target");
363 seterror(conf, "can't get target");
366 if (strlen(n->target) == 0) {
367 seterror(conf, "zero length target not allowed");
374 val = COL("username");
376 seterror(conf, "no username");
381 seterror(conf, "no passwd entry");
389 if (conf->setgroup) {
390 val = COL("groupname");
392 seterror(conf, "no groupname");
397 seterror(conf, "no group entry for %s", val);
406 double mtime = strtod(COL("mtime"),NULL);
408 mtime = (double)time(NULL);
411 n->mtime = (time_t)mtime;
413 n->times[0].tv_sec = 0;
414 n->times[0].tv_nsec = UTIME_OMIT;
415 n->times[1].tv_sec = (time_t)llrint(floor(mtime));
416 n->times[1].tv_nsec = lrint(floor(fmod(mtime,1.0)*1000000000));
421 /* file does not exist */
422 #define D_NOEXIST 0x1
423 /* files are different types */
425 /* metadata is different */
427 /* content or link target is different */
429 /* file to be installed is a directory */
431 /* path on disk is a directory */
432 #define D_EISDIR 0x20
433 /* usernames different */
435 /* group names different */
437 /* file mode is different */
439 /* mtimes are different */
440 #define D_MTIME 0x200
441 /* the hash of the file we are supposedly replacing is different than
442 * the the hash of the file on disk
444 #define D_OHASH 0x400
445 /* file exists, and is a directory, and is empty */
446 #define D_ISEMPTY 0x800
447 /* an error occurred trying to compare the file (other than it doesn't exist */
448 #define D_ERROR 0x1000
449 /* there was a stat error */
450 #define D_STATERROR 0x2000
451 /* there was an error calling readlink */
452 #define D_RLERROR 0x4000
454 static int dir_is_empty(char *path) {
475 /* 1 = file doesn't exist, 2 = file is a directory, target isn't */
476 /* 4 == ftype different */
477 /* 8 = hash different when both are regular files */
478 static unsigned int file_compare(struct nitem *n, struct stat *st) {
479 int etype = 0, stat_type;
480 char ehash[ZPM_HASH_STRLEN+1];
481 unsigned int diff = 0;
486 case 'd': etype = S_IFDIR; diff |= D_ISDIR ; break;
487 case 'r': etype = S_IFREG; break;
488 case 'l': etype = S_IFLNK; break;
489 default: etype = 0; break;
493 /* new file, so check type, hash, etc */
494 if (lstat(n->dest, st) == 0) {
495 stat_type = st->st_mode & S_IFMT;
496 if (stat_type != etype) {
499 if (stat_type == S_IFDIR) {
501 if (dir_is_empty(n->dest)) {
506 if (n->hash && etype == S_IFREG && stat_type == S_IFREG) {
507 zpm_hash(n->dest, ehash, 0);
508 if (strcmp(n->hash, ehash) != 0) {
511 if (n->ohash && strcmp(n->ohash, ehash) != 0) {
515 if (n->hash && etype == S_IFLNK && stat_type == S_IFLNK) {
516 lsize = readlink(n->dest, link, sizeof link);
518 if (lsize == -1 || lsize == sizeof link) {
523 if (strcmp(n->target, link) != 0) {
528 if (n->uid != st->st_uid) {
532 if (n->gid != st->st_gid) {
536 if (n->mode != (st->st_mode & 07777)) {
542 case ENOENT: diff |= D_NOEXIST; break;
543 default: diff |= (D_STATERROR|D_ERROR); break;
551 /* 0 = not acceptable
552 * 1 = accept and create/update/remove
554 * 3 = remove and overwrite
555 * 4 = update metadata
557 static int acceptable(struct config *conf, unsigned int diffs, int op) {
558 int exist = (!(diffs & D_NOEXIST));
559 int sametype = (!(diffs & D_TYPE));
560 int mdsame = (!(diffs & D_MD));
561 int hashsame = (!(diffs & D_HASH));
562 int isdir = (diffs & D_ISDIR);
565 return op == OP_REMOVE ? 2 : 1;
568 if (op == OP_UPDATE) {
569 return sametype ? 4 : 3;
572 if (op == OP_REMOVE) {
576 /* the hard cases, should be installing new, but already exists */
579 return conf->overwrite ? 3 : 0;
582 if (mdsame && (conf->accept || conf->overwrite)) {
587 if (mdsame || conf->ignoredirmd) {
588 return conf->acceptdir ? 2 : 0;
590 if (conf->overwrite) {
595 if (hashsame && (conf->accept || conf->overwrite)) {
599 return conf->overwrite ? 3 : 0;
602 static int check_existing(void *f, int ncols, char **vals, char **cols) {
603 struct config *conf = f;
607 if (!read_item(conf, ncols, vals, cols, &nitem)) {
608 fprintf(stderr, "can't read item\n");
609 return conf->errabort;
612 if (conf->verbose > 1) {
613 fprintf(stderr, "check for existing %s\n", nitem.path);
616 if (lstat(nitem.path, &st) == -1) {
618 /* not an error, file shouldn't exist*/
621 fprintf(stderr, "unable to check %s: %s\n",
622 nitem.path, strerror(errno));
629 unsigned int diffs = file_compare(&nitem, &st);
630 int sametype = (!(diffs & D_TYPE));
632 if (diffs >= D_ERROR) {
633 return seterror(conf, "can't check %s", nitem.dest);
636 if (sametype && nitem.configuration) {
640 int action = acceptable(conf, diffs, nitem.op);
643 fprintf(stderr, "%s exists and is not acceptable\n", nitem.path);
645 fprintf(stderr, "%s exists\n", nitem.path);
653 static void update_progress(struct config *conf, char *op, char *path) {
654 if (!conf->verbose) {
658 if (conf->progress == 0) {
659 pdots(50, '.', conf->ops_completed-1, conf->ops_completed, conf->ops_total);
660 } else if (conf->progress == 1) {
661 size_t len = strlen(path);
666 printf("\r%lu/%lu %.10s %.50s\n", conf->ops_completed,
667 conf->ops_total, op, path+offset);
668 } else if (conf->progress == 2) {
669 printf("%lu/%lu %s %s\n", conf->ops_completed,
670 conf->ops_total, op, path);
675 static int remove_files(void *f, int ncols, char **vals, char **cols) {
676 struct config *conf = f;
681 conf->ops_completed++;
684 char *ftype = COL("filetype");
686 if (!dest) return seterror(conf,"no file dest");
687 if (!ftype) return seterror(conf,"no file type");
689 update_progress(conf, *ftype == 'd' ? "rmdir" : "unlink", dest);
697 if (lstat(dest, &st) == -1) {
700 if (conf->verbose > 1) {
701 fprintf(stderr, "expected file not found: '%s'\n", dest);
705 return seterror(conf, "can't stat %s: %s", dest, strerror(errno));
710 if (S_ISDIR(st.st_mode)) {
711 flags = AT_REMOVEDIR;
713 /* TODO check that expected filetype matches actual filetype */
714 /* alternatively, just use the expected type, skip the stat,
715 * and let it fail if the type is wrong
720 if (unlinkat(AT_FDCWD, dest, flags) == -1) {
724 case ENOTEMPTY: /* fall through */
726 fprintf(stderr, "expected empty directory: %s\n", dest);
729 return seterror(conf, "can't unlink %s: %s", dest, strerror(errno));
736 #define MARK fprintf(stderr, "%s %d: mark\n", __func__, __LINE__)
738 static int remove_dir(struct config *conf, char *path) {
743 setsyserr(conf, "can't rmdir %s", path);
749 static int remove_existing(struct config *conf, char *path) {
754 setsyserr(conf, "can't unlink %s", path);
760 static int set_md(struct config *conf, struct nitem *item) {
765 if (item->ftype != 'l') {
766 printf("chmod %o %s\n", item->mode, item->dest);
768 if (conf->setuser && conf->setgroup) {
769 printf("lchown %d:%d %s\n", item->uid, item->gid,
772 printf("mtime %.0f %s\n", (double)item->mtime, item->dest);
777 if (conf->setuser && conf->setgroup) {
778 rv = lchown(item->dest, item->uid, item->gid);
780 setsyserr(conf, "can't lchown %s", item->dest);
781 return conf->errabort;
785 /* have to chmod after the chown, setuid bits may (and will)
786 * be cleared after a chown
788 /* can't chmod a symlink */
789 if (item->ftype != 'l') {
790 rv = chmod(item->dest, item->mode);
793 setsyserr(conf, "can't chmod %o %s", item->mode, item->dest);
794 return conf->errabort;
798 rv = utimensat(AT_FDCWD, item->dest, item->times, AT_SYMLINK_NOFOLLOW);
800 setsyserr(conf, "can't set mtime %.0f %s", (double)item->mtime,
802 return conf->errabort;
807 /* install a file or create a directory or symlink. path should not exist
810 /* flags: 1 = set md, 2 = create leading dirs, 4 = unlink existing file,
811 * 8 = rmdir existing dir, 16 = return true/false
815 #define INS_UNLINK 0x4
816 #define INS_RMDIR 0x8
818 #define INS_ZPMNEW 0x20
819 static int install(struct config *conf, struct nitem *item, unsigned int flags) {
823 int mkleading = (flags & 2);
824 int setmd = (flags & 1);
825 int unlink_file = (flags & 4);
826 int rm_dir = (flags & 8);
827 int failure = conf->errabort;
830 if (flags & INS_RTF) {
837 printf("unlink %s\n", item->dest);
839 printf("rmdir %s\n", item->dest);
842 printf("install %c%o %d:%d %s", item->ftype,
843 item->mode, item->uid, item->gid,
845 if (item->ftype == 'l') {
846 printf(" -> %s", item->target);
853 source = conf->src ? conf->src : conf->log;
856 rv = create_leading_dirs(item->dest);
858 setsyserr(conf, "can't create leading dirs for %s", item->dest);
864 rv = remove_existing(conf, item->dest);
866 rv = remove_dir(conf, item->dest);
874 switch (item->ftype) {
875 case 'r': rv = zpm_extract(source, item->hash, item->dest, item->mode);
876 if (rv == 0) rv = -1;
878 case 'd': rv = mkdir(item->dest, item->mode);
880 case 'l': rv = symlink(item->target, item->dest);
887 switch (item->ftype) {
889 seterror(conf, "can't extract %s", item->dest);
892 setsyserr(conf, "install mkdir(\"%s\") failed", item->dest);
895 setsyserr(conf, "install symlink(\"%s\") failed", item->dest);
898 setsyserr(conf, "installing %s failed", item->dest);
903 return set_md(conf, item) == 0 ? success : failure;
909 static int save_config_file(struct config *conf, struct nitem *n, char *msgfmt) {
910 char hash[ZPM_HASH_STRLEN+1];
913 msgfmt = "saved config file %.8s";
916 if (zpm_import(conf->log, n->dest, 0, hash)) {
917 zpm_note_add(conf->log, n->pkglist, n->path, hash, msgfmt, hash);
919 warn("unable to import existing config file %s", n->dest);
928 * figure out what the difference is for a config file, only called
929 * for an update of a configuration file
930 * return -1 on an error
931 * return 1 if the new file should not be installed
932 * return 0 if the new file should be installed
934 static int adjust_for_config(struct nitem *n, unsigned int diffs) {
936 if (!n->oldwasconf) {
940 int sametype = (!(diffs & D_TYPE));
941 int isdir = (diffs & D_ISDIR);
942 int eisdir = (diffs & D_EISDIR);
944 /* what if old was a directory? */
945 if (!n->configuration) {
946 /* replacing conf with non-conf */
947 /* absorb file, mark todo */
951 /* both old and new are config files */
952 if (isdir && sametype) {
953 /* both config directories, can only be changing
954 * metadata, so no adjustment needed
964 /* replacing old conf directory with a conf file.
965 * nothing needs to be done, if the directory
966 * is empty, it's ok to remove. if it's not empty,
967 * the install will fail
972 /* replacing old file with new file */
973 /* new is same as on disk */
974 if (!(diffs & D_HASH)) {
978 /* new is different than on disk, but on disk is same as old */
979 if (!(diffs & D_OHASH)) {
988 static int config_handler(void *f, int ncols, char **vals, char **cols) {
989 struct config *conf = f;
991 struct stat existing;
997 if (!read_item(conf, ncols, vals, cols, &nitem)) {
998 fprintf(stderr, "can't read item\n");
1000 return conf->errabort;
1003 unsigned int diffs = file_compare(&nitem, &existing);
1004 if (diffs >= D_ERROR) {
1005 return seterror(conf, "can't check %s", nitem.dest);
1008 int exist = (!(diffs & D_NOEXIST));
1009 int sametype = (!(diffs & D_TYPE));
1010 //int mdsame = (!(diffs & D_MD));
1011 int hashsame = (!(diffs & D_HASH));
1012 int oldhashsame = (!(diffs & D_OHASH));
1013 int isdir = (diffs & D_ISDIR);
1014 int eisdir = (diffs & D_EISDIR);
1015 update = (nitem.op == OP_UPDATE);
1017 notehash = nitem.hash;
1019 /* if the file doesn't exist in the system, nothing to do */
1020 /* could possibly note if we expected it, but the regular handling
1027 if (nitem.op == OP_UPDATE && !nitem.oldwasconf) {
1028 /* possibly save anyway */
1032 /* so, old was conf, and something exists in the filesystem */
1035 warn("won't %s %s%s, %s exists",
1036 nitem.op == OP_NEW ? "install" : nitem.opstr,
1039 eisdir ? "directory" : "file"
1042 return conf->errabort;
1045 /* all below are same type of file */
1046 /* what about sametype, but old was different type */
1052 /* save or note cases */
1054 if (nitem.op == OP_REMOVE) {
1055 save ="saved removed config file %.8s";
1058 if (nitem.op == OP_UPDATE) {
1059 if (!nitem.configuration) {
1060 /* replacing config with non-config */
1061 save = "replacing configuration file %.8s with non-configuration file";
1062 } else if (oldhashsame) {
1063 /* config file hasn't changed from old default,
1064 * so go ahead and install the new one
1066 save = "replaced old default config (%.8s) with new one.";
1067 notehash = nitem.ohash;
1069 note = "kept existing config file. new default version is %.8s";
1074 if (nitem.op == OP_NEW && !hashsame) {
1075 note = "config file already existed. would have installed %.8s";
1080 * save files, add notes
1082 if (!conf->dryrun) {
1084 warn("saving config file: %s (root %s)", nitem.path, conf->rootdir ? conf->rootdir : "/");
1085 save_config_file(conf, &nitem, save);
1088 zpm_note_add(conf->log, nitem.pkglist, nitem.path, nitem.hash,
1093 fprintf(stderr, "dry run: %s %s: ", nitem.pkglist,
1095 warn(save, notehash);
1098 fprintf(stderr, "dry run: %s %s: ", nitem.pkglist,
1100 warn(note, notehash);
1108 static void handle_config_files(struct config *conf) {
1114 s = sqlite3_str_new(conf->log->db);
1115 sqlite3_str_appendall(s, "select *, ");
1116 if (conf->rootdir) {
1117 sqlite3_str_appendf(s, "printf('%%s/%%s',rtrim(%Q,'/'),ltrim(path,'/'))", conf->rootdir);
1119 sqlite3_str_appendf(s, "printf('/%%s', trim(path, '/'))");
1121 sqlite3_str_appendall(s, " as dest from syncinfo");
1123 sqlite3_str_appendall(s," where configuration > 0 and op in ('new','update','remove')");
1125 if (conf->reverse) {
1126 sqlite3_str_appendall(s," order by length(path) desc, path desc");
1129 sql = sqlite3_str_value(s);
1131 rv = zpm_exec(conf->log, sql, config_handler, conf, &errmsg);
1133 sqlite3_str_finish(s);
1136 fprintf(stderr, "exec fail: %s\n", sqlite3_errstr(rv));
1138 fprintf(stderr, "database error: %s\n", errmsg);
1141 if (conf->log->error == 1) {
1142 fprintf(stderr, "unable to allocate memory\n");
1144 fprintf(stderr, "zpm_exec failure: %s\n",
1145 conf->log->errmsg ? conf->log->errmsg : "unknown");
1149 if (conf->errors && conf->exitonerror) {
1150 zpm_close(conf->log);
1151 zpm_close(conf->src);
1156 static int install_files(void *f, int ncols, char **vals, char **cols) {
1157 struct config *conf = f;
1159 struct stat existing;
1162 /* put the result row in a hash table? May not actually
1165 if (!read_item(conf, ncols, vals, cols, &nitem)) {
1166 fprintf(stderr, "can't read item\n");
1167 return conf->errabort;
1172 used = sqlite3_memory_used()/1024/1024;
1173 high = sqlite3_memory_highwater(0)/1024/1024;
1174 fprintf(stderr, "memory = %ld MB / %ld MB\n", used, high);
1177 sprintf(action, "%.8s %c", nitem.opstr, nitem.ftype);
1178 conf->ops_completed++;
1179 update_progress(conf, action, nitem.dest);
1181 unsigned int diffs = file_compare(&nitem, &existing);
1182 if (diffs >= D_ERROR) {
1183 return seterror(conf, "can't check %s", nitem.dest);
1187 * exist & same type & md same & hash same: do nothing, but warn bug
1188 * exist & same type & md diff & hash same: fix md
1189 * exist & same type & md same & hash diff: replace
1190 * exist & same type & md diff & hash diff: replace & fix
1191 * no exist: install and warn
1192 * dir & not dir : remove, mkdir
1193 * not dir & not dir & diff type: remove, install
1194 * not dir & dir : remove dir if empty, error if not empty, install
1197 * no exist: create leading dirs, install
1199 * exist & same type & md same & hash same & accept or over: do nothing
1200 * exist & same & md diff or hash diff & overwrite : update
1201 * exist & same & md diff or hash diff & accept : error, can't accept
1202 * exist & same & md diff or hash diff & not accept : error
1204 * exist & different type & not overwrite : error
1205 * not dir & not dir & overwrite : remove and install
1206 * not dir & dir & overwrite: remove empty or error, install
1207 * dir & dir & overwrite: fix md
1208 * dir & not dir & overwrite: remove and mkdir
1210 int exist = (!(diffs & D_NOEXIST));
1211 int sametype = (!(diffs & D_TYPE));
1212 int mdsame = (!(diffs & D_MD));
1213 int hashsame = (!(diffs & D_HASH));
1214 int ohashsame = (!(diffs & D_OHASH));
1215 int isdir = (diffs & D_ISDIR);
1216 int eisdir = (diffs & D_EISDIR);
1217 int accept = conf->accept;
1218 int overwrite = conf->overwrite;
1219 int installing = (nitem.op == OP_NEW);
1220 update = (nitem.op == OP_UPDATE);
1222 /* if a config file doesn't exist on disk, go ahead and do
1223 * whatever you were going to do, this logic here just
1224 * needs to determine if we should skip what we were going to do
1226 * if the old item was a configuration item and the new one isn't, it
1227 * will have been saved earlier, so we can just go ahead. so we only
1228 * test for an existing file, where the item is a configuration file
1230 if (nitem.configuration && exist) {
1231 if (!sametype && !conf->overwrite) {
1232 return seterror(conf, "configuration file exists with different type: %s", nitem.dest);
1239 if (isdir && !mdsame) return 0;
1240 if (!isdir && !ohashsame) return 0;
1245 /* warn, it should exist */
1246 fprintf(stderr, "%s missing, installing\n", nitem.dest);
1247 return install(conf, &nitem, 3);
1250 /* file exists in filesystem */
1252 if (mdsame && hashsame) {
1253 /* warn, bug in logic. This shouldn't occur,
1254 * because if there is nothing to do, it
1255 * shouldn't be listed as an update
1257 /* could be an update. We're checking against
1258 * what's actually on disk, not what was
1259 * expected to have been on disk. So, if
1260 * the admin has modified the file, or if
1261 * it had been installed ignoring the user
1262 * and group, it might be correct on disk
1263 * but not as in the local database
1266 /* TODO detect whether this a logic bug or
1267 * an on-disk difference
1270 fprintf(stderr, "%s should not be an update\n", nitem.dest);
1271 fprintf(stderr, "old hash: %s\n", nitem.ohash);
1272 fprintf(stderr, "new hash: %s\n", nitem.hash);
1273 fprintf(stderr, "old mds: %s\n", nitem.omds);
1274 fprintf(stderr, "new mds: %s\n", nitem.mds);
1279 if (!mdsame && hashsame) {
1281 return set_md(conf, &nitem);
1285 /* doesn't matter on the md */
1286 int flags = INS_MD | INS_CLD;
1287 if (nitem.ftype == 'l') {
1288 flags |= INS_UNLINK;
1290 return install(conf, &nitem, flags);
1294 /* file exists, and is not the same type */
1296 if (isdir && !eisdir) {
1297 /* remove existing */
1299 return install(conf, &nitem, 7);
1301 if (!isdir && eisdir) {
1302 /* remove dir, or error */
1304 return install(conf, &nitem, 11);
1306 if (!isdir && !isdir) {
1307 /* necessarily !sametype, sametype handled above */
1308 /* remove existing */
1310 return install(conf, &nitem, 7);
1312 /* error, should not be possible, assert(0)? */
1313 fprintf(stderr,"impossible state: %s:%d\n", __func__, __LINE__);
1318 return install(conf, &nitem, 3);
1321 /* file exists in filesystem */
1323 if (mdsame && hashsame && (accept || overwrite)) {
1325 if (conf->dryrun || conf->verbose > 1) {
1326 fprintf(stderr, "accept %s: %s\n",
1327 eisdir ? "directory" : "file", nitem.dest);
1332 if (mdsame && isdir && conf->acceptdir) {
1336 if (!mdsame && isdir && conf->ignoredirmd) {
1337 if (conf->verbose > 1) {
1338 fprintf(stderr, "ignoring directory metadata difference: %s\n", nitem.dest);
1343 if (mdsame && hashsame && !(accept || overwrite)) {
1345 return seterror(conf, "file exists: %s", nitem.dest);
1348 if (mdsame && !hashsame && overwrite) {
1350 return install(conf, &nitem, eisdir ? 11 : 7);
1353 if (nitem.configuration && accept) {
1354 /* accept a changed config file */
1355 if (conf->dryrun || conf->verbose) {
1356 fprintf(stderr, "accept %smodified config %s: %s\n", (!mdsame || !hashsame) ? "" : "un",
1357 eisdir ? "directory" : "file", nitem.dest);
1362 if (mdsame && !hashsame && !overwrite) {
1363 /* accept doesn't matter, since it's
1364 * not an acceptable file */
1366 if (nitem.ftype == 'l') {
1369 lsize = readlink(nitem.dest, link, sizeof link);
1370 if (lsize == -1 || (size_t)lsize >= sizeof link) {
1371 return seterror(conf, "%s (linkdiff): expecting %s -> %s, unable to read link", accept ? "existing file not acceptable" : "file exists", nitem.dest, nitem.target, link);
1374 /* links must be different */
1375 return seterror(conf, "%s (linkdiff): expecting %s -> %s, have -> %s", accept ? "existing file not acceptable" : "file exists", nitem.dest, nitem.target, link);
1378 return seterror(conf, "%s (hashdiff): %s", accept ? "existing file not acceptable" : "file exists", nitem.dest);
1381 if (!mdsame && hashsame && overwrite) {
1383 return set_md(conf, &nitem);
1385 if (!mdsame && hashsame && !overwrite) {
1386 /* accept doesn't matter, since it's
1387 * not an acceptable file */
1389 return seterror(conf, "%s (mddiff): %s", accept ? "existing file not acceptable" : "file exists", nitem.dest);
1391 if (!mdsame && !hashsame && overwrite) {
1393 return install(conf, &nitem, eisdir ? 11 : 7);
1395 if (!mdsame && !hashsame && !overwrite) {
1396 /* accept doesn't matter, since it's
1397 * not an acceptable file */
1399 return seterror(conf, "%s (md+hash): %s", accept ? "existing file not acceptable" : "file exists", nitem.dest);
1401 /* error, should be impossible */
1402 return seterror(conf, "impossible state reached");
1405 /* file exists, and is not the same type */
1408 return seterror(conf, "%s (difftype): %s", accept ? "existing file not acceptable" : "file exists", nitem.dest);
1411 /* not the same type, but ok to overwrite */
1413 /* remove existing */
1414 return install(conf, &nitem, 7);
1417 /* existing path is a directory */
1420 /* impossible, if isdir and eisdir, would be same type
1422 return set_md(conf, &nitem);
1424 /* remove empty dir or error */
1426 return install(conf, &nitem, 11);
1428 /* if we get here, we missed a case */
1429 return seterror(conf, "impossible state 2 reached");
1432 /* TODO extra verbose print perms, mtime, etc, probably ls -l format
1434 if (conf->verbose) {
1435 printf("%s\n", nitem.path);
1441 static void check_conflicts(struct config *conf, char *conflict_type,
1442 int (callback)(void *, int, char **, char **)) {
1448 s = sqlite3_str_new(conf->log->db);
1449 sqlite3_str_appendall(s, "select *, ");
1450 if (conf->rootdir) {
1451 sqlite3_str_appendf(s, "printf('%%s/%%s',rtrim(%Q,'/'),ltrim(path,'/'))", conf->rootdir);
1453 sqlite3_str_appendf(s, "printf('/%%s', trim(path, '/'))");
1455 sqlite3_str_appendall(s, " as dest from syncconflicts");
1457 if (conflict_type) {
1458 sqlite3_str_appendf(s," where conflict = %Q", conflict_type);
1460 if (conf->reverse) {
1461 sqlite3_str_appendall(s," order by length(path) desc, path desc,pkgid collate vercmp desc, conflict desc");
1463 sqlite3_str_appendall(s," order by length(path), path, pkgid collate vercmp, conflict");
1467 sql = sqlite3_str_value(s);
1469 rv = zpm_exec(conf->log, sql, callback, conf, &errmsg);
1471 sqlite3_str_finish(s);
1474 fprintf(stderr, "exec fail: %s\n", sqlite3_errstr(rv));
1476 fprintf(stderr, "database error: %s\n", errmsg);
1479 if (conf->log->error == 1) {
1480 fprintf(stderr, "unable to allocate memory\n");
1482 fprintf(stderr, "zpm_exec failure: %s\n",
1483 conf->log->errmsg ? conf->log->errmsg : "unknown");
1486 if (conf->log->errmsg) {
1487 fprintf(stderr, "error: %s\n", conf->log->errmsg);
1489 if (conf->errors && conf->exitonerror) {
1490 zpm_close(conf->log);
1491 zpm_close(conf->src);
1494 /* TODO final report function in conf var */
1497 static void runstage(struct config *conf, char *stage,
1498 int (callback)(void *, int, char **, char **)) {
1504 s = sqlite3_str_new(conf->log->db);
1505 sqlite3_str_appendall(s, "select *, ");
1506 if (conf->rootdir) {
1507 sqlite3_str_appendf(s, "printf('%%s/%%s',rtrim(%Q,'/'),ltrim(path,'/'))", conf->rootdir);
1509 sqlite3_str_appendf(s, "printf('/%%s', trim(path, '/'))");
1511 sqlite3_str_appendall(s, " as dest from syncinfo");
1514 sqlite3_str_appendf(s," where op = %Q", stage);
1516 if (conf->reverse) {
1517 sqlite3_str_appendall(s," order by length(path) desc, path desc");
1520 sql = sqlite3_str_value(s);
1522 rv = zpm_exec(conf->log, sql, callback, conf, &errmsg);
1524 sqlite3_str_finish(s);
1527 fprintf(stderr, "exec fail: %s\n", sqlite3_errstr(rv));
1529 fprintf(stderr, "database error: %s\n", errmsg);
1532 if (conf->log->error == 1) {
1533 fprintf(stderr, "unable to allocate memory\n");
1535 fprintf(stderr, "zpm_exec failure: %s\n",
1536 conf->log->errmsg ? conf->log->errmsg : "unknown");
1540 if (conf->log->errmsg) {
1541 fprintf(stderr, "error: %s\n", conf->log->errmsg);
1544 if (conf->errors && conf->exitonerror) {
1545 zpm_close(conf->log);
1546 zpm_close(conf->src);
1549 /* TODO final report function in conf var */
1552 static void count_ops(struct config *conf) {
1553 conf->ops_remove = zpm_db_int(conf->log, "select count(*) from syncinfo where op = 'remove'");
1554 conf->ops_update = zpm_db_int(conf->log, "select count(*) from syncinfo where op = 'update'");
1555 conf->ops_install = zpm_db_int(conf->log, "select count(*) from syncinfo where op = 'new'");
1556 conf->ops_total = conf->ops_remove + conf->ops_update + conf->ops_install;
1559 int main(int ac, char **av) {
1563 char *pkgdbfile = 0, *localdbfile = 0;
1566 struct config conf = { 0 };
1583 if (geteuid() != 0) {
1588 localdbfile = ZPM_LOCAL_DB;
1589 if ((s = getenv("ZPMDB"))) {
1593 if ((s = getenv("ZPM_ROOT_DIR"))) {
1598 * -d localdb or ZPMDB * or /var/lib/zpm/zpm.db, or die
1599 * -f 'package database', otherwise regular default of env
1600 * ZPM_PACKAGE_FILE, or use pkgdb if otherwise not found
1601 * -R root of pkg, will just chdir there
1603 * args are pkgid triple, but will do a pkg find on the pkgdb
1606 while ((opt = getopt(ac, av, "f:d:c:nCR:vqOAMDp")) != -1) {
1608 case 'd': localdbfile = optarg; break;
1609 case 'f': pkgdbfile = optarg; break;
1610 case 'n': conf.dryrun = 1; break;
1611 case 'v': conf.verbose++; break;
1612 case 'q': conf.verbose--; break;
1613 case 'C': conf.errabort = 0; break;
1614 case 'R': conf.rootdir = optarg; break;
1615 case 'N': conf.setuser = 0; conf.setgroup = 0; break;
1616 case 'O': conf.overwrite = 1; break;
1617 case 'A': conf.accept = 1; break;
1618 case 'M': conf.ignoredirmd = 1;
1619 case 'D': conf.acceptdir = 0;
1620 case 'p': conf.progress++;
1628 /* verify root dir exists */
1629 if (conf.rootdir && !exists(conf.rootdir, NULL)) {
1630 fprintf(stderr, "rootdir %s does not exist\n", conf.rootdir);
1633 if (!zpm_open(&localdb, localdbfile)) {
1634 fprintf(stderr, "can't open zpm db %s\n", localdbfile);
1637 conf.log = &localdb;
1640 /* TODO open read-only */
1641 if (!zpm_open(&pkgdb, pkgdbfile)) {
1642 fprintf(stderr, "can't open src db %s\n", localdbfile);
1649 /* TODO set conf var to finalize error reporting */
1651 fprintf(stderr, "syncing filesystem %s (ldb %s) from %s\n",
1652 conf.rootdir ? conf.rootdir : "/",
1653 localdbfile, pkgdbfile);
1657 conf.exitonerror = 0;
1658 check_conflicts(&conf, NULL, report_conflicts);
1660 if (conf.conflicts) {
1661 fprintf(stderr, "%d conflicts reported, aborting sync\n",
1665 /* no point in running it if we're just going to
1666 * overwrite everything
1668 if (!conf.overwrite && !conf.accept && !conf.dryrun) {
1669 runstage(&conf, "new", check_existing);
1673 fprintf(stderr, "beginning %ssync\n", conf.dryrun ?
1678 handle_config_files(&conf);
1681 /* have to do the removes first otherwise
1682 * old files may conflict with update file
1686 conf.exitonerror = conf.dryrun ? 0 : 1;
1687 conf.errabort = conf.dryrun ? 0 : 1;
1689 fprintf(stderr, "file ops: %lu\n", conf.ops_total);
1690 if (conf.ops_remove > 0) {
1692 fprintf(stderr, "removing %lu file%s\n", conf.ops_remove, conf.ops_remove > 1 ? "s" : "");
1695 conf.ops_completed = 0;
1696 conf.ops_total = conf.ops_remove;
1697 runstage(&conf, "remove", remove_files);
1698 if (conf.verbose && conf.progress < 2) {
1699 fprintf(stderr, " done\n");
1704 if (conf.ops_update > 0) {
1706 fprintf(stderr, "updating %lu file%s\n", conf.ops_update, conf.ops_update > 1 ? "s" : "");
1709 conf.ops_completed = 0;
1710 conf.ops_total = conf.ops_update;
1711 runstage(&conf, "update", install_files);
1712 if (conf.verbose && conf.progress < 2) {
1713 fprintf(stderr, " done\n");
1718 if (conf.ops_install > 0) {
1720 fprintf(stderr, "installing %lu file%s\n", conf.ops_install, conf.ops_install > 1 ? "s" : "");
1723 conf.ops_completed = 0;
1724 conf.ops_total = conf.ops_install;
1725 runstage(&conf, "new", install_files);
1726 if (conf.verbose && conf.progress < 2) {
1727 fprintf(stderr, " done\n");
1734 zpm_close(&localdb);
1735 zpm_close(conf.src);
1736 return conf.errors ? 1 : 0;