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)) {
237 fprintf(stderr, "rmdir %s\n", dest);
239 } else if (S_ISREG(st.st_mode)) {
240 /* TODO conf to import before removal */
242 fprintf(stderr, "unlink(%s)\n", dest);
252 static int install_files(void *f, int ncols, char **vals, char **cols) {
253 struct config *conf = f;
267 /* TODO put the result row in a hash table. May not actually
272 if (op == 0) IERR("invalid operation");
274 /* TODO config to dishonor setuid/setgid */
276 if (!path) IERR("no file path");
277 if (strlen(path) == 0) {
278 IERR("zero length path not allowed");
281 if (!dest) IERR("no file dest");
282 if (strlen(dest) == 0) {
283 IERR("zero length dest not allowed");
288 if (!val) IERR("can't determine mode");
289 mode = strtoul(val, NULL, 8);
291 val = COL("filetype");
292 if (!val || strlen(val) == 0) {
293 IERR("can't determine file type");
299 if (!hash) IERR("can't get hash");
303 fprintf(stderr, "installing '%c' %s\n", ftype, dest);
310 val = COL("username");
311 if (!val) IERR("no username");
313 if (!pw) IERR("no passwd entry");
316 if (conf->setgroup) {
317 val = COL("groupname");
318 if (!val) IERR("no groupname");
320 if (!gr) IERR("no group entry");
325 //printf("cld %s\n", path);
326 printf("new %c%o %d:%d %s -> %s\n", ftype, mode, uid, gid, path, dest);
330 /* TODO should these be owned by the path owner if they don't exist? */
331 /* probably, though they then belong to the package, sort of */
332 if (!create_leading_dirs(dest)) {
333 fprintf(stderr, "unable to create leading directories for %s\n",
339 if (mkdir(dest, mode) == -1) {
342 } else if (ftype == 'r') {
344 source = conf->src ? conf->src : conf->log;
345 if (conf->verbose > 1) {
346 fprintf(stderr, "extracting %8.8s to %s with mode %o\n",
349 if (!zpm_extract(source, hash, dest, mode)) {
350 IERR("can't extract file");
352 } else if (ftype == 'l') {
353 char *target = COL("target");
355 fprintf(stderr, "no target for symlink %s\n", path);
357 return conf->errabort;
360 if (strlen(target) == 0) {
361 IERR("zero length symlink not allowed");
364 if (conf->verbose > 1) {
365 fprintf(stderr, "symlink %s -> %s\n", path, target);
367 if (symlink(target, path) == -1) {
368 IERR("can't symlink");
371 fprintf(stderr, "unhandled filetype %c\n", ftype);
374 if (conf->setuser && conf->setgroup) {
375 if (chown(dest, uid, gid) == -1) {
380 struct timespec times[2] = { 0 };
381 double mtime = strtod(COL("mtime"),NULL);
383 times[0].tv_nsec = UTIME_OMIT;
384 times[1].tv_sec = (time_t)llrint(floor(mtime));
385 times[1].tv_nsec = lrint(floor(fmod(mtime,1.0)*1000000000));
387 utimensat(AT_FDCWD, dest, times, AT_SYMLINK_NOFOLLOW);
390 printf("%s\n", path);
396 static void runstage(struct config *conf, char *stage,
397 int (callback)(void *, int, char **, char **)) {
403 s = sqlite3_str_new(conf->log->db);
404 sqlite3_str_appendall(s, "select *, ");
406 sqlite3_str_appendf(s, "printf('%%s/%%s',rtrim(%Q,'/'),ltrim(path,'/'))", conf->rootdir);
408 sqlite3_str_appendf(s, "printf('/%%s', trim(path, '/'))");
410 sqlite3_str_appendall(s, " as dest from install_status");
413 sqlite3_str_appendf(s," where op = %Q", stage);
416 sqlite3_str_appendall(s," order by length(path) desc, path desc");
419 sql = sqlite3_str_value(s);
420 if (conf->verbose > 2) {
421 fprintf(stderr, "stage query: %s\n", sql);
424 rv = zpm_exec(conf->log, sql, callback, conf, &errmsg);
426 sqlite3_str_finish(s);
429 fprintf(stderr, "exec fail: %s\n", sqlite3_errstr(rv));
431 fprintf(stderr, "database error: %s\n", errmsg);
434 if (conf->log->error == 1) {
435 fprintf(stderr, "unable to allocate memory\n");
437 fprintf(stderr, "zpm_exec failure: %s\n",
438 conf->log->errmsg ? conf->log->errmsg : "unknown");
441 if (conf->log->errmsg) {
442 fprintf(stderr, "error: %s\n", conf->log->errmsg);
444 if (conf->errors && conf->exitonerror) {
445 zpm_close(conf->log);
446 zpm_close(conf->src);
449 /* TODO final report function in conf var */
452 int main(int ac, char **av){
456 char *pkgdbfile = 0, *localdbfile = 0;
472 if (geteuid() != 0) {
477 localdbfile = ZPM_LOCAL_DB;
478 if ((s = getenv("ZPMDB"))) {
479 /* TODO does this need to be copied ? */
483 if ((s = getenv("ZPM_ROOT_DIR"))) {
484 /* TODO does this need to be copied ? */
489 * -d localdb or ZPMDB * or /var/lib/zpm/zpm.db, or die
490 * -f 'package database', otherwise regular default of env
491 * ZPM_PACKAGE_FILE, or use pkgdb if otherwise not found
492 * -R root of pkg, will just chdir there
494 * args are pkgid triple, but will do a pkg find on the pkgdb
497 while ((opt = getopt(ac, av, "f:d:c:nCR:v")) != -1) {
499 case 'd': localdbfile = optarg; break;
500 case 'f': pkgdbfile = optarg; break;
501 case 'n': conf.dryrun = 1; break;
502 case 'v': conf.verbose++; break;
503 case 'C': conf.errabort = 0; break;
504 case 'R': conf.rootdir = optarg; break;
505 case 'N': conf.setuser = 0; conf.setgroup = 0; break;
513 /* verify root dir exists */
514 if (conf.rootdir && !exists(conf.rootdir, NULL)) {
515 fprintf(stderr, "rootdir %s does not exist\n", conf.rootdir);
518 if (!zpm_open(&localdb, localdbfile)) {
519 fprintf(stderr, "can't open zpm db %s\n", localdbfile);
525 if (!zpm_open(&pkgdb, pkgdbfile)) {
526 fprintf(stderr, "can't open src db %s\n", localdbfile);
533 /* TODO find pkgid from arg */
535 /* TODO set conf var to finalize error reporting */
537 fprintf(stderr, "syncing filesystem %s (ldb %s) from %s\n",
538 conf.rootdir ? conf.rootdir : "/",
539 localdbfile, pkgdbfile);
543 conf.exitonerror = 0;
544 runstage(&conf, "conflict", report_conflicts);
545 runstage(&conf, "new", check_existing);
548 conf.exitonerror = 1;
549 runstage(&conf, "new", install_files);
550 runstage(&conf, "update", update_files);
552 runstage(&conf, "remove", remove_files);
558 return conf.errors ? 1 : 0;