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, absorb;
46 char *pkglist; /* space separated */
51 struct timespec times[2];
55 printf("usage: zpm $scriptname [-fncC] args ...\n");
58 static int seterror(struct config *conf, char *msgfmt, ...) {
65 vsnprintf(msg, sizeof msg, msgfmt, ap);
69 if (conf->log->errmsg) {
70 free(conf->log->errmsg);
73 conf->log->errmsg = strdup(msg);
76 fprintf(stderr, "%s\n", msg);
79 return conf->errabort;
82 static int setsyserr(struct config *conf, char *msgfmt, ...) {
90 printed = vsnprintf(msg, sizeof msg, msgfmt, ap);
94 /* nothing we can really do */
95 return conf->errabort;
98 if ((size_t)printed < sizeof msg) {
99 snprintf(msg+printed, sizeof msg - printed, ": %s",
104 if (conf->log->errmsg) {
105 free(conf->log->errmsg);
108 conf->log->errmsg = strdup(msg);
111 fprintf(stderr, "%s\n", msg);
114 return conf->errabort;
117 static int exists(char *path, mode_t *mode) {
120 if (lstat(path, &st) == -1) {
123 if (mode) *mode = st.st_mode;
127 /* TODO maintain a list of already created directories */
128 static int create_leading_dirs(char *path) {
131 char pcopy[ZPM_PATH_MAX];
136 delim = strrchr(pcopy, '/');
137 if (!delim) return 1; /* not an error, but no leading dirs */
139 /* cut off last component */
148 delim = strchr(s, '/');
154 /* try to create the directory, if it exists
155 * and is a directory or a symlink, that's ok
156 * should be (eventually) a symlink to a directory
157 * so we want stat here, not lstat
159 if (mkdir(pcopy, 0755) == -1) {
162 if (stat(pcopy, &st) == -1) {
166 switch (st.st_mode & S_IFMT) {
186 static char *column(char *col, int ncols, char **vals, char **cols) {
190 for (i=0; i < ncols; i++) {
191 // fprintf(stderr, "checking '%s' = '%s'\n", cols[i], vals[i]);
193 if (!strcmp(col, cols[i])) {
201 #define COL(x) column(x, ncols, vals, cols)
202 #define SYSERR(x) do { conf->log->error = 2; return conf->errabort; } while (0)
205 static char *ops[] = { "new", "remove", "update", 0 };
213 static int getop(char *opstr) {
216 if (!opstr) return 0;
217 for (i=0;ops[i];i++) {
218 if (!strcmp(opstr, ops[i])) {
225 static int report_conflicts(void *f, int ncols, char **vals, char **cols) {
226 struct config *conf = f;
227 char *path, *hash, *pkg, *conflict_type, *mds;
231 conflict_type = COL("conflict");
232 if (!strcmp(conflict_type, "hash")) {
234 fprintf(stderr, "hash conflict: package %s path %s hash %.8s\n",
237 if (!strcmp(conflict_type, "md")) {
239 fprintf(stderr, "md conflict: package %s path %s md %s\n",
242 fprintf(stderr, "%s conflict: package %s path %s\n",
243 conflict_type, pkg, path);
250 static int check_existing(void *f, int ncols, char **vals, char **cols) {
251 struct config *conf = f;
257 return seterror(conf, "no path");
261 printf("checkfor %s\n", path);
267 fprintf(stderr, "check for existing %s\n", path);
270 if (lstat(path, &st) == 0) {
271 fprintf(stderr, "%s exists\n", path);
275 /* not an error, file shouldn't exist*/
278 fprintf(stderr, "unable to check %s: %s\n",
279 path, strerror(errno));
287 static int remove_files(void *f, int ncols, char **vals, char **cols) {
288 struct config *conf = f;
294 if (!dest) return seterror(conf,"no file dest");
297 char *ftype = COL("filetype");
301 case 'd': printf("rmdir %s\n", dest); break;
302 default: printf("unlink %s\n", dest); break;
308 if (lstat(dest, &st) == -1) {
309 return seterror(conf,"can't stat");
312 if (S_ISDIR(st.st_mode)) {
313 flags = AT_REMOVEDIR;
315 /* TODO check that expected filetype matches actual filetype */
318 fprintf(stderr, "%s(%s)\n", flags ? "rmdir" : "unlink", dest);
323 if (unlinkat(AT_FDCWD, dest, flags) == -1) {
328 return seterror(conf, "can't unlink");
335 #define MARK fprintf(stderr, "%s %d: mark\n", __func__, __LINE__)
337 #define D_NOEXIST 0x1
342 #define D_EISDIR 0x20
346 #define D_MTIME 0x200
347 #define D_OHASH 0x400
348 #define D_ERROR 0x1000
349 #define D_STATERROR 0x2000
350 #define D_RLERROR 0x4000
352 /* 1 = file doesn't exist, 2 = file is a directory, target isn't */
353 /* 4 == ftype different */
354 /* 8 = hash different when both are regular files */
355 static unsigned int file_compare(struct nitem *n, struct stat *st) {
356 int etype = 0, stat_type;
357 char ehash[ZPM_HASH_STRLEN+1];
358 unsigned int diff = 0;
363 case 'd': etype = S_IFDIR; diff |= D_ISDIR ; break;
364 case 'r': etype = S_IFREG; break;
365 case 'l': etype = S_IFLNK; break;
366 default: etype = 0; break;
370 /* new file, so check type, hash, etc */
371 if (lstat(n->dest, st) == 0) {
372 stat_type = st->st_mode & S_IFMT;
373 if (stat_type != etype) {
376 if (stat_type == S_IFDIR) {
380 if (n->hash && etype == S_IFREG && stat_type == S_IFREG) {
381 zpm_hash(n->dest, ehash, 0);
382 if (strcmp(n->hash, ehash) != 0) {
385 if (n->ohash && strcmp(n->ohash, ehash) != 0) {
389 if (n->hash && etype == S_IFLNK && stat_type == S_IFLNK) {
390 lsize = readlink(n->dest, link, sizeof link);
391 if (lsize == -1 || lsize == sizeof link) {
394 } else if (strcmp(n->target, link) != 0) {
398 if (n->uid != st->st_uid) {
402 if (n->gid != st->st_gid) {
406 if (n->mode != (st->st_mode & 07777)) {
412 case ENOENT: diff |= D_NOEXIST; break;
413 default: diff |= (D_STATERROR|D_ERROR); break;
420 static int read_item(struct config *conf, int ncols, char **vals, char **cols,
425 struct nitem zero = { 0 };
431 seterror(conf, "can't determine op");
437 seterror(conf, "can't determine op");
441 n->path = COL("path");
443 seterror(conf, "no file path");
446 if (strlen(n->path) == 0) {
447 seterror(conf, "zero length path not allowed");
451 /* TODO config to dishonor setuid/setgid */
452 n->dest = COL("dest");
454 seterror(conf, "no file dest");
458 if (strlen(n->dest) == 0) {
459 seterror(conf, "zero length dest not allowed");
466 seterror(conf, "can't determine mode");
470 n->mode = strtoul(val, NULL, 8);
472 val = COL("configuration");
474 seterror(conf, "can't determine config status");
477 n->configuration = strtoul(val, NULL, 10);
479 val = COL("filetype");
480 if (!val || strlen(val) == 0) {
481 seterror(conf, "can't determine file type");
486 /* these can be null */
487 n->ohash = COL("ohash");
489 n->omds = COL("omds");
490 n->pkglist = COL("pkglist");
492 if (n->ftype == 'r') {
493 n->hash = COL("hash");
495 seterror(conf, "can't get hash");
498 } else if (n->ftype == 'l') {
499 n->target = COL("target");
501 seterror(conf, "can't get target");
504 if (strlen(n->target) == 0) {
505 seterror(conf, "zero length target not allowed");
512 val = COL("username");
514 seterror(conf, "no username");
519 seterror(conf, "no passwd entry");
527 if (conf->setgroup) {
528 val = COL("groupname");
530 seterror(conf, "no groupname");
535 seterror(conf, "no group entry");
544 double mtime = strtod(COL("mtime"),NULL);
546 mtime = (double)time(NULL);
549 n->mtime = (time_t)mtime;
551 n->times[0].tv_sec = 0;
552 n->times[0].tv_nsec = UTIME_OMIT;
553 n->times[1].tv_sec = (time_t)llrint(floor(mtime));
554 n->times[1].tv_nsec = lrint(floor(fmod(mtime,1.0)*1000000000));
559 static int remove_dir(struct config *conf, char *path) {
564 setsyserr(conf, "can't rmdir %s", path);
570 static int remove_existing(struct config *conf, char *path) {
575 setsyserr(conf, "can't unlink %s", path);
581 static int set_md(struct config *conf, struct nitem *item) {
586 printf("chmod %o %s\n", item->mode, item->dest);
587 if (conf->setuser && conf->setgroup) {
588 printf("chown %d:%d %s\n", item->uid, item->gid,
591 printf("mtime %.0f %s\n", (double)item->mtime, item->dest);
596 rv = chmod(item->dest, item->mode);
599 setsyserr(conf, "can't chmod %o %s", item->mode, item->dest);
600 return conf->errabort;
603 if (conf->setuser && conf->setgroup) {
604 rv = chown(item->dest, item->uid, item->gid);
606 setsyserr(conf, "can't chown %s", item->dest);
607 return conf->errabort;
611 rv = utimensat(AT_FDCWD, item->dest, item->times, AT_SYMLINK_NOFOLLOW);
613 setsyserr(conf, "can't set mtime %.0f %s", (double)item->mtime,
615 return conf->errabort;
620 /* install a file or create a directory or symlink. path should not exist
623 /* flags: 1 = set md, 2 = create leading dirs, 4 = unlink existing file,
624 * 8 = rmdir existing dir, 16 = return true/false
628 #define INS_UNLINK 0x4
629 #define INS_RMDIR 0x8
631 #define INS_ZPMNEW 0x20
632 static int install(struct config *conf, struct nitem *item, unsigned int flags) {
636 int mkleading = (flags & 2);
637 int setmd = (flags & 1);
638 int unlink_file = (flags & 4);
639 int rm_dir = (flags & 8);
640 int failure = conf->errabort;
650 printf("unlink %s\n", item->dest);
652 printf("rmdir %s\n", item->dest);
655 printf("install %c%o %d:%d %s -> %s\n", item->ftype,
656 item->mode, item->uid, item->gid, item->path,
662 source = conf->src ? conf->src : conf->log;
665 rv = remove_existing(conf, item->dest);
667 rv = remove_dir(conf, item->dest);
675 rv = create_leading_dirs(item->dest);
677 setsyserr(conf, "can't create leading dirs for %s", item->dest);
682 if (item->ftype == 'r') {
683 rv = zpm_extract(source, item->hash, item->dest, item->mode);
685 seterror(conf, "can't extract %s", item->dest);
691 switch (item->ftype) {
692 case 'd': rv = mkdir(item->dest, item->mode);
694 case 'l': rv = symlink(item->target, item->dest);
701 setsyserr(conf, "installing %s failed", item->dest);
706 return set_md(conf, item) == 0 ? success : failure;
712 static int install_files(void *f, int ncols, char **vals, char **cols) {
713 struct config *conf = f;
715 struct stat existing;
719 /* TODO put the result row in a hash table. May not actually
722 if (!read_item(conf, ncols, vals, cols, &nitem)) {
723 fprintf(stderr, "can't read item\n");
724 return conf->errabort;
727 if (conf->verbose && !conf->dryrun) {
728 fprintf(stderr, "%s '%c' %s\n", nitem.opstr, nitem.ftype,
732 unsigned int diffs = file_compare(&nitem, &existing);
733 if (diffs >= D_ERROR) {
734 return seterror(conf, "can't check %s", nitem.dest);
738 * exist & same type & md same & hash same: do nothing, but warn bug
739 * exist & same type & md diff & hash same: fix md
740 * exist & same type & md same & hash diff: replace
741 * exist & same type & md diff & hash diff: replace & fix
742 * no exist: install and warn
743 * dir & not dir : remove, mkdir
744 * not dir & not dir & diff type: remove, install
745 * not dir & dir : remove dir if empty, error if not empty, install
748 * no exist: create leading dirs, install
750 * exist & same type & md same & hash same & accept or over: do nothing
751 * exist & same & md diff or hash diff & overwrite : update
752 * exist & same & md diff or hash diff & accept : error, can't accept
753 * exist & same & md diff or hash diff & not accept : error
755 * exist & different type & not overwrite : error
756 * not dir & not dir & overwrite : remove and install
757 * not dir & dir & overwrite: remove empty or error, install
758 * dir & dir & overwrite: fix md
759 * dir & not dir & overwrite: remove and mkdir
761 int exist = (!(diffs & D_NOEXIST));
762 int sametype = (!(diffs & D_TYPE));
763 int mdsame = (!(diffs & D_MD));
764 int hashsame = (!(diffs & D_HASH));
765 int isdir = (diffs & D_ISDIR);
766 int eisdir = (diffs & D_EISDIR);
767 int accept = conf->absorb;
768 int overwrite = conf->overwrite;
769 int installing = (nitem.op == OP_NEW);
770 update = (nitem.op == OP_UPDATE);
774 /* warn, it should exist */
775 fprintf(stderr, "%s missing, installing", nitem.dest);
776 return install(conf, &nitem, 3);
779 if (nitem.configuration) {
780 /* ohash == nhash, not an update */
781 /* fhash == ohash, just update */
782 /* fhash != ohash, install as dest.zpmnew, warn */
783 /* TODO handle replacing config file
784 * with config directory */
785 /* We don't have the information as to which
786 * package this is a config file for, so
787 * we'll need to look it up.
789 if (diffs & D_OHASH) {
790 if (strlen(nitem.dest) > sizeof dest - 8) {
791 return seterror(conf,"config file path too long for install as %s.zpmnew", nitem.dest);
793 fprintf(stderr, "installing as .zpmnew\n");
794 sprintf(dest, "%s.zpmnew", nitem.dest);
799 /* file exists in filesystem */
801 if (mdsame && hashsame) {
802 /* warn, bug in logic. This shouldn't occur,
803 * because if there is nothing to do, it
804 * shouldn't be listed as an update
806 /* could be an update. We're checking against
807 * what's actually on disk, not what was
808 * expected to have been on disk. So, if
809 * the admin has modified the file, or if
810 * it had been installed ignoring the user
811 * and group, it might be correct on disk
812 * but not as in the local database
814 /* TODO detect whether this a logic bug or
815 * an on-disk difference
818 fprintf(stderr, "%s should not be an update\n", nitem.dest);
819 fprintf(stderr, "old hash: %s\n", nitem.ohash);
820 fprintf(stderr, "new hash: %s\n", nitem.hash);
821 fprintf(stderr, "old mds: %s\n", nitem.omds);
822 fprintf(stderr, "new mds: %s\n", nitem.mds);
827 if (!mdsame && hashsame) {
829 return set_md(conf, &nitem);
831 if (mdsame && !hashsame) {
833 return install(conf, &nitem, 3);
835 if (!mdsame && !hashsame) {
837 return install(conf, &nitem, 3);
841 /* file exists, and is not the same type */
843 if (isdir && !eisdir) {
844 /* remove existing */
846 return install(conf, &nitem, 7);
848 if (!isdir && eisdir) {
849 /* remove dir, or error */
851 return install(conf, &nitem, 11);
853 if (!isdir && !isdir) {
854 /* necessarily !sametype, sametype handled above */
855 /* remove existing */
857 return install(conf, &nitem, 7);
859 /* error, should not be possible, assert(0)? */
864 return install(conf, &nitem, 3);
867 /* file exists in filesystem */
869 if (mdsame && hashsame && (accept || overwrite)) {
871 if (conf->dryrun || conf->verbose) {
872 fprintf(stderr, "accepting existing file: %s\n", nitem.dest);
876 if (mdsame && hashsame && !(accept || overwrite)) {
878 return seterror(conf, "will not accept or overwrite existing file: %s", nitem.dest);
880 if (mdsame && !hashsame && overwrite) {
882 return install(conf, &nitem, eisdir ? 11 : 7);
884 if (mdsame && !hashsame && !overwrite) {
885 /* accept doesn't matter, since it's
886 * not an acceptable file */
888 return seterror(conf, "%s (hashdiff): %s", accept ? "existing file not acceptable" : "file exists", nitem.dest);
890 if (!mdsame && hashsame && overwrite) {
892 return set_md(conf, &nitem);
894 if (!mdsame && hashsame && !overwrite) {
895 /* accept doesn't matter, since it's
896 * not an acceptable file */
898 return seterror(conf, "%s (mddiff): %s", accept ? "existing file not acceptable" : "file exists", nitem.dest);
900 if (!mdsame && !hashsame && overwrite) {
902 return install(conf, &nitem, eisdir ? 11 : 7);
904 if (!mdsame && !hashsame && !overwrite) {
905 /* accept doesn't matter, since it's
906 * not an acceptable file */
908 return seterror(conf, "%s (md+hash): %s", accept ? "existing file not acceptable" : "file exists", nitem.dest);
910 /* TODO error, should be impossible */
911 return seterror(conf, "impossible state reached");
914 /* file exists, and is not the same type */
917 return seterror(conf, "%s (difftype): %s", accept ? "existing file not acceptable" : "file exists", nitem.dest);
920 /* not the same type, but ok to overwrite */
922 /* remove existing */
923 return install(conf, &nitem, 7);
926 /* existing path is a directory */
929 /* impossible, if isdir and eisdir, would
933 return set_md(conf, &nitem);
935 /* remove empty dir or error */
937 return install(conf, &nitem, 11);
939 /* if we get here, we missed a case */
941 return seterror(conf, "impossible state 2 reached");
944 /* TODO extra verbose print perms, mtime, etc, probably ls -l
948 printf("%s\n", nitem.path);
954 static void check_conflicts(struct config *conf, char *conflict_type,
955 int (callback)(void *, int, char **, char **)) {
961 s = sqlite3_str_new(conf->log->db);
962 sqlite3_str_appendall(s, "select *, ");
964 sqlite3_str_appendf(s, "printf('%%s/%%s',rtrim(%Q,'/'),ltrim(path,'/'))", conf->rootdir);
966 sqlite3_str_appendf(s, "printf('/%%s', trim(path, '/'))");
968 sqlite3_str_appendall(s, " as dest from syncconflicts");
971 sqlite3_str_appendf(s," where conflict = %Q", conflict_type);
974 sqlite3_str_appendall(s," order by length(path) desc, path desc,pkgid collate vercmp desc, conflict desc");
976 sqlite3_str_appendall(s," order by length(path), path, pkgid collate vercmp, conflict");
980 sql = sqlite3_str_value(s);
982 rv = zpm_exec(conf->log, sql, callback, conf, &errmsg);
984 sqlite3_str_finish(s);
987 fprintf(stderr, "exec fail: %s\n", sqlite3_errstr(rv));
989 fprintf(stderr, "database error: %s\n", errmsg);
992 if (conf->log->error == 1) {
993 fprintf(stderr, "unable to allocate memory\n");
995 fprintf(stderr, "zpm_exec failure: %s\n",
996 conf->log->errmsg ? conf->log->errmsg : "unknown");
999 if (conf->log->errmsg) {
1000 fprintf(stderr, "error: %s\n", conf->log->errmsg);
1002 if (conf->errors && conf->exitonerror) {
1003 zpm_close(conf->log);
1004 zpm_close(conf->src);
1007 /* TODO final report function in conf var */
1010 static void runstage(struct config *conf, char *stage,
1011 int (callback)(void *, int, char **, char **)) {
1017 s = sqlite3_str_new(conf->log->db);
1018 sqlite3_str_appendall(s, "select *, ");
1019 if (conf->rootdir) {
1020 sqlite3_str_appendf(s, "printf('%%s/%%s',rtrim(%Q,'/'),ltrim(path,'/'))", conf->rootdir);
1022 sqlite3_str_appendf(s, "printf('/%%s', trim(path, '/'))");
1024 sqlite3_str_appendall(s, " as dest from syncinfo");
1027 sqlite3_str_appendf(s," where op = %Q", stage);
1029 if (conf->reverse) {
1030 sqlite3_str_appendall(s," order by length(path) desc, path desc");
1033 sql = sqlite3_str_value(s);
1035 rv = zpm_exec(conf->log, sql, callback, conf, &errmsg);
1037 sqlite3_str_finish(s);
1040 fprintf(stderr, "exec fail: %s\n", sqlite3_errstr(rv));
1042 fprintf(stderr, "database error: %s\n", errmsg);
1045 if (conf->log->error == 1) {
1046 fprintf(stderr, "unable to allocate memory\n");
1048 fprintf(stderr, "zpm_exec failure: %s\n",
1049 conf->log->errmsg ? conf->log->errmsg : "unknown");
1053 if (conf->log->errmsg) {
1054 fprintf(stderr, "error: %s\n", conf->log->errmsg);
1057 if (conf->errors && conf->exitonerror) {
1058 zpm_close(conf->log);
1059 zpm_close(conf->src);
1062 /* TODO final report function in conf var */
1065 int main(int ac, char **av){
1069 char *pkgdbfile = 0, *localdbfile = 0;
1088 if (geteuid() != 0) {
1093 localdbfile = ZPM_LOCAL_DB;
1094 if ((s = getenv("ZPMDB"))) {
1095 /* TODO does this need to be copied ? */
1099 if ((s = getenv("ZPM_ROOT_DIR"))) {
1100 /* TODO does this need to be copied ? */
1105 * -d localdb or ZPMDB * or /var/lib/zpm/zpm.db, or die
1106 * -f 'package database', otherwise regular default of env
1107 * ZPM_PACKAGE_FILE, or use pkgdb if otherwise not found
1108 * -R root of pkg, will just chdir there
1110 * args are pkgid triple, but will do a pkg find on the pkgdb
1113 while ((opt = getopt(ac, av, "f:d:c:nCR:vOA")) != -1) {
1115 case 'd': localdbfile = optarg; break;
1116 case 'f': pkgdbfile = optarg; break;
1117 case 'n': conf.dryrun = 1; break;
1118 case 'v': conf.verbose++; break;
1119 case 'C': conf.errabort = 0; break;
1120 case 'R': conf.rootdir = optarg; break;
1121 case 'N': conf.setuser = 0; conf.setgroup = 0; break;
1122 case 'O': conf.overwrite = 1; break;
1123 case 'A': conf.absorb = 1; break;
1131 /* verify root dir exists */
1132 if (conf.rootdir && !exists(conf.rootdir, NULL)) {
1133 fprintf(stderr, "rootdir %s does not exist\n", conf.rootdir);
1136 if (!zpm_open(&localdb, localdbfile)) {
1137 fprintf(stderr, "can't open zpm db %s\n", localdbfile);
1140 conf.log = &localdb;
1143 if (!zpm_open(&pkgdb, pkgdbfile)) {
1144 fprintf(stderr, "can't open src db %s\n", localdbfile);
1151 /* TODO find pkgid from arg */
1153 /* TODO set conf var to finalize error reporting */
1155 fprintf(stderr, "syncing filesystem %s (ldb %s) from %s\n",
1156 conf.rootdir ? conf.rootdir : "/",
1157 localdbfile, pkgdbfile);
1161 conf.exitonerror = 0;
1162 check_conflicts(&conf, NULL, report_conflicts);
1164 if (conf.conflicts) {
1165 fprintf(stderr, "%d conflicts reported, aborting sync\n",
1169 /* no point in running it if we're just going to
1170 * overwrite everything
1172 if (!conf.overwrite && !conf.absorb && !conf.dryrun) {
1173 runstage(&conf, "new", check_existing);
1177 fprintf(stderr, "beginning %ssync\n", conf.dryrun ?
1180 /* have to do the removes first otherwise
1181 * old files may conflict with update file
1185 conf.exitonerror = conf.dryrun ? 0 : 1;
1186 conf.errabort = conf.dryrun ? 0 : 1;
1189 fprintf(stderr, "removing old files\n");
1191 runstage(&conf, "remove", remove_files);
1194 fprintf(stderr, "updating files\n");
1196 runstage(&conf, "update", install_files);
1198 fprintf(stderr, "installing files\n");
1200 runstage(&conf, "new", install_files);
1204 zpm_close(&localdb);
1205 zpm_close(conf.src);
1206 return conf.errors ? 1 : 0;