1 #define _POSIX_C_SOURCE 200809L
15 /* needed for S_IFMT and AT_FDCWD */
24 struct zpm *log; /* logging db will be attached as "log" */
28 int errabort, errors, verbose, dryrun;
29 int setuser, setgroup;
30 int reverse, exitonerror;
34 printf("usage: zpm $scriptname [-fncC] args ...\n");
37 static int exists(char *path, mode_t *mode) {
40 if (lstat(path, &st) == -1) {
43 if (mode) *mode = st.st_mode;
47 /* TODO maintain a list of already created directories */
48 static int create_leading_dirs(char *path) {
51 char pcopy[ZPM_PATH_MAX];
56 delim = strrchr(pcopy, '/');
57 if (!delim) return 1; /* not an error, but no leading dirs */
59 /* cut off last component */
68 delim = strchr(s, '/');
74 /* try to create the directory, if it exists
75 * and is a directory or a symlink, that's ok
77 if (mkdir(pcopy, 0755) == -1) {
80 if (lstat(pcopy, &st) == -1) {
84 switch (st.st_mode & S_IFMT) {
105 char *column(char *col, int ncols, char **vals, char **cols) {
109 for (i=0; i < ncols; i++) {
110 // fprintf(stderr, "checking '%s' = '%s'\n", cols[i], vals[i]);
112 if (!strcmp(col, cols[i])) {
120 #define IERR(x) do { conf->errors++; conf->log->errmsg = strdup(x); return conf->errabort; } while (0)
121 #define COL(x) column(x, ncols, vals, cols)
122 #define SYSERR(x) do { conf->log->error = 2; return conf->errabort; } while (0)
124 static char *ops[] = { "new", "remove", "update", 0 };
126 static int getop(char *opstr) {
129 if (!opstr) return 0;
130 for (i=0;ops[i];i++) {
131 if (!strcmp(opstr, ops[i])) {
138 static int report_conflicts(void *f, int ncols, char **vals, char **cols) {
139 struct config *conf = f;
147 fprintf(stderr, "%s owned by %s\n", path, pkg);
151 static int check_existing(void *f, int ncols, char **vals, char **cols) {
152 struct config *conf = f;
157 if (!path) IERR("can't check for existing");
160 printf("checkfor %s\n", path);
165 fprintf(stderr, "check for existing %s\n", path);
168 if (lstat(path, &st) == 0) {
169 fprintf(stderr, "%s exists \n", path);
173 /* not an error, file shouldn't exist*/
176 fprintf(stderr, "unable to check %s: %s\n",
177 path, strerror(errno));
185 static int update_files(void *f, int ncols, char **vals, char **cols) {
186 struct config *conf = f;
192 if (!pkg) IERR("can't get pkgid");
194 if (!path) IERR("can't get path");
196 if (!dest) IERR("no file dest");
198 /* hash is different, so no different than install,
199 * but we don't need to create leading directories
203 fprintf(stderr, "update %s\n", dest);
207 fprintf(stderr, "update not implemented: %s", dest);
213 static int remove_files(void *f, int ncols, char **vals, char **cols) {
214 struct config *conf = f;
219 if (!dest) IERR("no file dest");
222 char *ftype = COL("filetype");
226 case 'd': printf("rmdir %s\n", dest); break;
227 default: printf("unlink %s\n", dest); break;
232 if (lstat(dest, &st) == -1) {
236 if (S_ISDIR(st.st_mode)) {
238 fprintf(stderr, "rmdir(%s)\n", dest);
241 } else if (S_ISREG(st.st_mode)) {
242 /* TODO conf to import before removal */
244 fprintf(stderr, "unlink(%s)\n", dest);
246 if (unlink(dest) == -1) {
247 IERR("can't unlink");
250 if (unlink(dest) == -1) {
251 IERR("can't unlink");
258 static int install_files(void *f, int ncols, char **vals, char **cols) {
259 struct config *conf = f;
273 /* TODO put the result row in a hash table. May not actually
278 if (op == 0) IERR("invalid operation");
280 /* TODO config to dishonor setuid/setgid */
282 if (!path) IERR("no file path");
283 if (strlen(path) == 0) {
284 IERR("zero length path not allowed");
287 if (!dest) IERR("no file dest");
288 if (strlen(dest) == 0) {
289 IERR("zero length dest not allowed");
294 if (!val) IERR("can't determine mode");
295 mode = strtoul(val, NULL, 8);
297 val = COL("filetype");
298 if (!val || strlen(val) == 0) {
299 IERR("can't determine file type");
305 if (!hash) IERR("can't get hash");
309 fprintf(stderr, "installing '%c' %s\n", ftype, dest);
316 val = COL("username");
317 if (!val) IERR("no username");
319 if (!pw) IERR("no passwd entry");
322 if (conf->setgroup) {
323 val = COL("groupname");
324 if (!val) IERR("no groupname");
326 if (!gr) IERR("no group entry");
331 //printf("cld %s\n", path);
332 printf("new %c%o %d:%d %s -> %s\n", ftype, mode, uid, gid, path, dest);
336 /* TODO should these be owned by the path owner if they don't exist? */
337 /* probably, though they then belong to the package, sort of */
338 if (!create_leading_dirs(dest)) {
339 fprintf(stderr, "unable to create leading directories for %s\n",
345 if (mkdir(dest, mode) == -1) {
348 } else if (ftype == 'r') {
350 source = conf->src ? conf->src : conf->log;
351 if (conf->verbose > 1) {
352 fprintf(stderr, "extracting %8.8s to %s with mode %o\n",
355 if (!zpm_extract(source, hash, dest, mode)) {
356 IERR("can't extract file");
358 } else if (ftype == 'l') {
359 char *target = COL("target");
361 fprintf(stderr, "no target for symlink %s\n", path);
363 return conf->errabort;
366 if (strlen(target) == 0) {
367 IERR("zero length symlink not allowed");
370 if (conf->verbose > 1) {
371 fprintf(stderr, "symlink %s -> %s\n", path, target);
373 if (symlink(target, path) == -1) {
374 IERR("can't symlink");
377 fprintf(stderr, "unhandled filetype %c\n", ftype);
380 if (conf->setuser && conf->setgroup) {
381 if (chown(dest, uid, gid) == -1) {
386 struct timespec times[2] = { {0}, {0} };
387 double mtime = strtod(COL("mtime"),NULL);
389 times[0].tv_nsec = UTIME_OMIT;
390 times[1].tv_sec = (time_t)llrint(floor(mtime));
391 times[1].tv_nsec = lrint(floor(fmod(mtime,1.0)*1000000000));
393 utimensat(AT_FDCWD, dest, times, AT_SYMLINK_NOFOLLOW);
396 printf("%s\n", path);
402 static void runstage(struct config *conf, char *stage,
403 int (callback)(void *, int, char **, char **)) {
409 s = sqlite3_str_new(conf->log->db);
410 sqlite3_str_appendall(s, "select *, ");
412 sqlite3_str_appendf(s, "printf('%%s/%%s',rtrim(%Q,'/'),ltrim(path,'/'))", conf->rootdir);
414 sqlite3_str_appendf(s, "printf('/%%s', trim(path, '/'))");
416 sqlite3_str_appendall(s, " as dest from install_status");
419 sqlite3_str_appendf(s," where op = %Q", stage);
422 sqlite3_str_appendall(s," order by length(path) desc, path desc");
425 sql = sqlite3_str_value(s);
426 if (conf->verbose > 2) {
427 fprintf(stderr, "stage query: %s\n", sql);
430 rv = zpm_exec(conf->log, sql, callback, conf, &errmsg);
432 sqlite3_str_finish(s);
435 fprintf(stderr, "exec fail: %s\n", sqlite3_errstr(rv));
437 fprintf(stderr, "database error: %s\n", errmsg);
440 if (conf->log->error == 1) {
441 fprintf(stderr, "unable to allocate memory\n");
443 fprintf(stderr, "zpm_exec failure: %s\n",
444 conf->log->errmsg ? conf->log->errmsg : "unknown");
447 if (conf->log->errmsg) {
448 fprintf(stderr, "error: %s\n", conf->log->errmsg);
450 if (conf->errors && conf->exitonerror) {
451 zpm_close(conf->log);
452 zpm_close(conf->src);
455 /* TODO final report function in conf var */
458 int main(int ac, char **av){
462 char *pkgdbfile = 0, *localdbfile = 0;
478 if (geteuid() != 0) {
483 localdbfile = ZPM_LOCAL_DB;
484 if ((s = getenv("ZPMDB"))) {
485 /* TODO does this need to be copied ? */
489 if ((s = getenv("ZPM_ROOT_DIR"))) {
490 /* TODO does this need to be copied ? */
495 * -d localdb or ZPMDB * or /var/lib/zpm/zpm.db, or die
496 * -f 'package database', otherwise regular default of env
497 * ZPM_PACKAGE_FILE, or use pkgdb if otherwise not found
498 * -R root of pkg, will just chdir there
500 * args are pkgid triple, but will do a pkg find on the pkgdb
503 while ((opt = getopt(ac, av, "f:d:c:nCR:v")) != -1) {
505 case 'd': localdbfile = optarg; break;
506 case 'f': pkgdbfile = optarg; break;
507 case 'n': conf.dryrun = 1; break;
508 case 'v': conf.verbose++; break;
509 case 'C': conf.errabort = 0; break;
510 case 'R': conf.rootdir = optarg; break;
511 case 'N': conf.setuser = 0; conf.setgroup = 0; break;
519 /* verify root dir exists */
520 if (conf.rootdir && !exists(conf.rootdir, NULL)) {
521 fprintf(stderr, "rootdir %s does not exist\n", conf.rootdir);
524 if (!zpm_open(&localdb, localdbfile)) {
525 fprintf(stderr, "can't open zpm db %s\n", localdbfile);
531 if (!zpm_open(&pkgdb, pkgdbfile)) {
532 fprintf(stderr, "can't open src db %s\n", localdbfile);
539 /* TODO find pkgid from arg */
541 /* TODO set conf var to finalize error reporting */
543 fprintf(stderr, "syncing filesystem %s (ldb %s) from %s\n",
544 conf.rootdir ? conf.rootdir : "/",
545 localdbfile, pkgdbfile);
549 conf.exitonerror = 0;
550 runstage(&conf, "conflict", report_conflicts);
551 runstage(&conf, "new", check_existing);
554 conf.exitonerror = 1;
555 runstage(&conf, "new", install_files);
556 runstage(&conf, "update", update_files);
558 runstage(&conf, "remove", remove_files);
564 return conf.errors ? 1 : 0;