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" */
29 int errabort, errors, verbose, dryrun;
30 int setuser, setgroup;
31 int reverse, exitonerror;
35 printf("usage: zpm $scriptname [-fncC] args ...\n");
38 static int exists(char *path, mode_t *mode) {
41 if (lstat(path, &st) == -1) {
44 if (mode) *mode = st.st_mode;
48 /* TODO maintain a list of already created directories */
49 static int create_leading_dirs(char *path) {
52 char pcopy[ZPM_PATH_MAX];
57 delim = strrchr(pcopy, '/');
58 if (!delim) return 1; /* not an error, but no leading dirs */
60 /* cut off last component */
69 delim = strchr(s, '/');
75 /* try to create the directory, if it exists
76 * and is a directory or a symlink, that's ok
78 if (mkdir(pcopy, 0755) == -1) {
81 if (lstat(pcopy, &st) == -1) {
85 switch (st.st_mode & S_IFMT) {
106 char *column(char *col, int ncols, char **vals, char **cols) {
110 for (i=0; i < ncols; i++) {
111 // fprintf(stderr, "checking '%s' = '%s'\n", cols[i], vals[i]);
113 if (!strcmp(col, cols[i])) {
121 #define IERR(x) do { conf->errors++; conf->log->errmsg = strdup(x); return conf->errabort; } while (0)
122 #define COL(x) column(x, ncols, vals, cols)
123 #define SYSERR(x) do { conf->log->error = 2; return conf->errabort; } while (0)
125 static char *ops[] = { "new", "remove", "update", 0 };
127 static int getop(char *opstr) {
130 if (!opstr) return 0;
131 for (i=0;ops[i];i++) {
132 if (!strcmp(opstr, ops[i])) {
139 static int report_conflicts(void *f, int ncols, char **vals, char **cols) {
140 struct config *conf = f;
148 fprintf(stderr, "%s owned by %s\n", path, pkg);
152 static int check_existing(void *f, int ncols, char **vals, char **cols) {
153 struct config *conf = f;
158 if (!path) IERR("can't check for existing");
161 printf("checkfor %s\n", path);
166 fprintf(stderr, "check for existing %s\n", path);
169 if (lstat(path, &st) == 0) {
170 fprintf(stderr, "%s exists \n", path);
174 /* not an error, file shouldn't exist*/
177 fprintf(stderr, "unable to check %s: %s\n",
178 path, strerror(errno));
186 static int update_files(void *f, int ncols, char **vals, char **cols) {
187 struct config *conf = f;
193 if (!pkg) IERR("can't get pkgid");
195 if (!path) IERR("can't get path");
197 if (!dest) IERR("no file dest");
199 /* hash is different, so no different than install,
200 * but we don't need to create leading directories
204 fprintf(stderr, "update %s\n", dest);
208 fprintf(stderr, "update not implemented: %s", dest);
214 static int remove_files(void *f, int ncols, char **vals, char **cols) {
215 struct config *conf = f;
220 if (!dest) IERR("no file dest");
223 char *ftype = COL("filetype");
227 case 'd': printf("rmdir %s\n", dest); break;
228 default: printf("unlink %s\n", dest); break;
233 if (lstat(dest, &st) == -1) {
237 if (S_ISDIR(st.st_mode)) {
238 fprintf(stderr, "rmdir %s\n", dest);
240 } else if (S_ISREG(st.st_mode)) {
241 /* TODO conf to import before removal */
243 fprintf(stderr, "unlink(%s)\n", dest);
253 static int install_files(void *f, int ncols, char **vals, char **cols) {
254 struct config *conf = f;
268 /* TODO put the result row in a hash table. May not actually
273 if (op == 0) IERR("invalid operation");
275 /* TODO config to dishonor setuid/setgid */
277 if (!path) IERR("no file path");
278 if (strlen(path) == 0) {
279 IERR("zero length path not allowed");
282 if (!dest) IERR("no file dest");
283 if (strlen(dest) == 0) {
284 IERR("zero length dest not allowed");
289 if (!val) IERR("can't determine mode");
290 mode = strtoul(val, NULL, 8);
292 val = COL("filetype");
293 if (!val || strlen(val) == 0) {
294 IERR("can't determine file type");
300 if (!hash) IERR("can't get hash");
304 fprintf(stderr, "installing '%c' %s\n", ftype, dest);
311 val = COL("username");
312 if (!val) IERR("no username");
314 if (!pw) IERR("no passwd entry");
317 if (conf->setgroup) {
318 val = COL("groupname");
319 if (!val) IERR("no groupname");
321 if (!gr) IERR("no group entry");
326 //printf("cld %s\n", path);
327 printf("new %c%o %d:%d %s -> %s\n", ftype, mode, uid, gid, path, dest);
331 /* TODO should these be owned by the path owner if they don't exist? */
332 /* probably, though they then belong to the package, sort of */
333 if (!create_leading_dirs(dest)) {
334 fprintf(stderr, "unable to create leading directories for %s\n",
340 if (mkdir(dest, mode) == -1) {
343 } else if (ftype == 'r') {
345 source = conf->src ? conf->src : conf->log;
346 if (conf->verbose > 1) {
347 fprintf(stderr, "extracting %8.8s to %s with mode %o\n",
350 if (!zpm_extract(source, hash, dest, mode)) {
351 IERR("can't extract file");
353 } else if (ftype == 'l') {
354 char *target = COL("target");
356 fprintf(stderr, "no target for symlink %s\n", path);
358 return conf->errabort;
361 if (strlen(target) == 0) {
362 IERR("zero length symlink not allowed");
365 if (conf->verbose > 1) {
366 fprintf(stderr, "symlink %s -> %s\n", path, target);
368 if (symlink(target, path) == -1) {
369 IERR("can't symlink");
372 fprintf(stderr, "unhandled filetype %c\n", ftype);
375 if (conf->setuser && conf->setgroup) {
376 if (chown(dest, uid, gid) == -1) {
381 struct timespec times[2] = { 0 };
382 double mtime = strtod(COL("mtime"),NULL);
384 times[0].tv_nsec = UTIME_OMIT;
385 times[1].tv_sec = (time_t)llrint(floor(mtime));
386 times[1].tv_nsec = lrint(floor(fmod(mtime,1.0)*1000000000));
388 utimensat(AT_FDCWD, dest, times, AT_SYMLINK_NOFOLLOW);
391 printf("%s\n", path);
397 static void runstage(struct config *conf, char *stage,
398 int (callback)(void *, int, char **, char **)) {
404 s = sqlite3_str_new(conf->log->db);
405 sqlite3_str_appendall(s, "select *, ");
407 sqlite3_str_appendf(s, "printf('%%s/%%s',rtrim(%Q,'/'),ltrim(path,'/'))", conf->rootdir);
409 sqlite3_str_appendf(s, "printf('/%%s', trim(path, '/'))");
411 sqlite3_str_appendall(s, " as dest from install_status");
414 sqlite3_str_appendf(s," where op = %Q", stage);
417 sqlite3_str_appendall(s," order by length(path) desc, path desc");
420 sql = sqlite3_str_value(s);
421 if (conf->verbose > 2) {
422 fprintf(stderr, "stage query: %s\n", sql);
425 rv = zpm_exec(conf->log, sql, callback, conf, &errmsg);
427 sqlite3_str_finish(s);
430 fprintf(stderr, "exec fail: %s\n", sqlite3_errstr(rv));
432 fprintf(stderr, "database error: %s\n", errmsg);
435 if (conf->log->error == 1) {
436 fprintf(stderr, "unable to allocate memory\n");
438 fprintf(stderr, "zpm_exec failure: %s\n",
439 conf->log->errmsg ? conf->log->errmsg : "unknown");
442 if (conf->log->errmsg) {
443 fprintf(stderr, "error: %s\n", conf->log->errmsg);
445 if (conf->errors && conf->exitonerror) {
446 zpm_close(conf->log);
447 zpm_close(conf->src);
450 /* TODO final report function in conf var */
453 int main(int ac, char **av){
457 char *pkgdbfile = 0, *localdbfile = 0;
473 if (geteuid() != 0) {
478 localdbfile = ZPM_LOCAL_DB;
479 if ((s = getenv("ZPMDB"))) {
480 /* TODO does this need to be copied ? */
484 if ((s = getenv("ZPM_ROOT_DIR"))) {
485 /* TODO does this need to be copied ? */
490 * -d localdb or ZPMDB * or /var/lib/zpm/zpm.db, or die
491 * -f 'package database', otherwise regular default of env
492 * ZPM_PACKAGE_FILE, or use pkgdb if otherwise not found
493 * -R root of pkg, will just chdir there
495 * args are pkgid triple, but will do a pkg find on the pkgdb
498 while ((opt = getopt(ac, av, "f:d:c:nCR:v")) != -1) {
500 case 'd': localdbfile = optarg; break;
501 case 'f': pkgdbfile = optarg; break;
502 case 'n': conf.dryrun = 1; break;
503 case 'v': conf.verbose++; break;
504 case 'C': conf.errabort = 0; break;
505 case 'R': conf.rootdir = optarg; break;
506 case 'N': conf.setuser = 0; conf.setgroup = 0; break;
514 /* verify root dir exists */
515 if (conf.rootdir && !exists(conf.rootdir, NULL)) {
516 fprintf(stderr, "rootdir %s does not exist\n", conf.rootdir);
521 conf.pkgid = av[argn];
524 fprintf(stderr, "must specify pkgid\n");
529 if (!zpm_open(&localdb, localdbfile)) {
530 fprintf(stderr, "can't open zpm db %s\n", localdbfile);
536 if (!zpm_open(&pkgdb, pkgdbfile)) {
537 fprintf(stderr, "can't open src db %s\n", localdbfile);
544 /* TODO find pkgid from arg */
546 /* TODO set conf var to finalize error reporting */
548 fprintf(stderr, "installing %s (ldb %s) from %s\n",
549 conf.pkgid, localdbfile, pkgdbfile);
553 conf.exitonerror = 0;
554 runstage(&conf, "conflict", report_conflicts);
555 runstage(&conf, "new", check_existing);
558 conf.exitonerror = 1;
559 runstage(&conf, "new", install_files);
560 runstage(&conf, "update", update_files);
562 runstage(&conf, "remove", remove_files);
568 return conf.errors ? 1 : 0;