]> pd.if.org Git - zpackage/commitdiff
rewrite zpm-add in C v0.4.3
authorNathan Wagner <nw@hydaspes.if.org>
Wed, 12 Dec 2018 21:55:34 +0000 (21:55 +0000)
committerNathan Wagner <nw@hydaspes.if.org>
Wed, 12 Dec 2018 21:55:34 +0000 (21:55 +0000)
Removed the logic to try to figure out a package path, zpm-add
now uses the normal default, -f option, or environment variable
to specify the package file.

Makefile
lib/foreach_path.c
t/add.t
zpm-add
zpm-search.c
zpm.h

index 8e4b94228663e3b648a84e413d264522f1dc6032..886c8eee5e7a1012bb1a072d5fcaee8428c8c867 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -29,11 +29,12 @@ curdir=$(shell pwd)
 ZPKGBIN=zpm-addfile zpm-extract zpm-init zpm-vercmp zpm-stat zpm-hash \
        zpm-findpkg zpm-shell zpm-soneed zpm-foreach-path zpm-parse \
        zpm-script zpm-soname zpm-syncfs zpm-packagehash zpm-verify \
-       zpm-elftype zpm-quote zpm-note zpm-search
+       zpm-elftype zpm-quote zpm-note zpm-search zpm-add
 
 SCRIPTS=zpm zpm-install zpm-merge zpm-list zpm-preserve zpm-test zpm-log \
        zpm-contents zpm-uninstall zpm-pathmod zpm-rmpackage zpm-newpackage \
-       zpm-pkg zpm-add zpm-pkgfile zpm-gc zpm-repo zpm-update zpm-confgit
+       zpm-pkg zpm-pkgfile zpm-gc zpm-repo zpm-update zpm-confgit
+
 MANPAGES=doc/zpm.8 $(addprefix doc/zpm-, list.8 contents.8 hash.8 quote.8 pathmod.8 note.8 vercmp.8 repo.8)
 COMPILED=$(ZPKGBIN)
 PROGRAMS=$(SCRIPTS) $(COMPILED)
@@ -157,6 +158,9 @@ zpm-syncfs: zpm-syncfs.o libzpm.a libelf.a
 zpm-parse: zpm-parse.o lib/parse.o
        $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $+
 
+zpm-add: zpm-add.o lib/parse.o libzpm.a
+       $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $+ -lzpm -lelf
+
 zpm-quote: zpm-quote.o
        $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $<
 
index ec6e07ed77866dd9984d5dc0180e806b1b19810b..25081013db325a2a359ed712e8dd73a8aa3dbe8e 100644 (file)
@@ -1,4 +1,4 @@
-#define _POSIX_C_SOURCE 2
+#define _POSIX_C_SOURCE 200809L
 #include <stdlib.h>
 #include <unistd.h>
 #include <sys/types.h>
@@ -9,6 +9,147 @@
 #include "sqlite3.h"
 #include "zpm.h"
 
+void zpm_file_init(struct zpm_file *file) {
+       file->package = 0;
+       file->version = 0;
+       file->release = 0;
+       file->path = 0;
+       file->owner = 0;
+       file->group = 0;
+       file->target = 0;
+       file->status = 0;
+       file->hash[0] = 0;
+       file->confhash[0] = 0;
+       file->configuration = 0;
+       file->type = 0;
+       file->mode = 0;
+       file->device = 0;
+}
+
+void zpm_file_free(struct zpm_file *file) {
+       free(file->package);
+       free(file->version);
+       free(file->path);
+       free(file->owner);
+       free(file->group);
+       free(file->target);
+       free(file->status);
+       zpm_file_init(file);
+}
+
+#define getstring(x,n) ((const char *)sqlite3_column_text(x,n))
+
+int zpm_foreach_path_ds(struct zpm *zpm, char *pkgid, char *where,
+int (*callback)(struct zpm *, struct zpm_file *, void *),
+void *data) {
+       char *sql;
+       sqlite3_str *s;
+       sqlite3_stmt *st;
+       struct zpm_file file = { 0 };
+       int rv;
+
+       zpm_file_init(&file);
+
+       if (!zpm || zpm->error || !callback) return 0;
+
+       s = sqlite3_str_new(zpm->db);
+       sqlite3_str_appendall(s, "select package,version,release,path,filetype,mode,username,groupname,configuration,target,device,hash,confhash,status from packagefiles_status where ");
+
+       if (where) {
+               sqlite3_str_appendf(s, "%s", where);
+       } else {
+               sqlite3_str_appendall(s, "true");
+       }
+
+       if (pkgid) {
+               sqlite3_str_appendf(s, " and printf('%%s-%%s-%%s', package, version, release) = %Q", pkgid);
+       }
+
+       sql = sqlite3_str_value(s);
+       if (!sql) {
+               sqlite3_str_finish(s);
+               zpm->error = 1;
+               return 0;
+       }
+
+       st = zpm_dbquery(zpm, sql);
+       sqlite3_str_finish(s);
+
+       while (sqlite3_step(st) == SQLITE_ROW) {
+               const char *val;
+
+               val = getstring(st, 0);
+               file.package = val ? strdup(val) : 0;
+
+               val = getstring(st, 1);
+               file.version = val ? strdup(val) : 0;
+
+               file.release = sqlite3_column_int(st, 2);
+
+               val = getstring(st, 3);
+               file.path = val ? strdup(val) : 0;
+
+               val = getstring(st, 4);
+               file.type = val ? *val : 0;
+
+               val = getstring(st, 5);
+               file.mode = val ? strtol(val, NULL, 8) : 0;
+
+               val = getstring(st, 6);
+               file.owner = val ? strdup(val) : 0;
+
+               val = getstring(st, 7);
+               file.group = val ? strdup(val) : 0;
+
+               file.configuration  = sqlite3_column_int(st, 8);
+
+               val = getstring(st, 9);
+               file.target = val ? strdup(val) : 0;
+
+               val = getstring(st, 10);
+               file.device = val ? strtol(val, NULL, 10) : 0;
+
+               val = getstring(st, 11);
+               if (val) {
+                       strncpy(file.hash, val, ZPM_HASH_STRLEN);
+                       file.hash[ZPM_HASH_STRLEN] = 0;
+               }
+
+               val = getstring(st, 12);
+               if (val) {
+                       strncpy(file.hash, val, ZPM_HASH_STRLEN);
+                       file.confhash[ZPM_HASH_STRLEN] = 0;
+               }
+
+               val = getstring(st, 13);
+               file.status = val ? strdup(val) : 0;
+
+               rv = callback(zpm, &file, data);
+               zpm_file_free(&file);
+
+               if (rv != 0) {
+                       break;
+               }
+       }
+       
+       zpm->dbresult = sqlite3_finalize(st);
+
+       if (rv != 0) {
+               zpm->error = rv;
+               return 0;
+       }
+
+       if (zpm->dbresult != SQLITE_OK) {
+               free(zpm->dberrmsg);
+               zpm->dberrmsg = strdup(sqlite3_errmsg(zpm->db));
+               return 0;
+       }
+
+       zpm->error = 0;
+
+       return 1;
+}
+
 int zpm_foreach_path(struct zpm *zpm, char *pkgid, char *where,
 int (*callback)(void *f, int ncols, char **vals, char **cols),
 void *data, char **errmsg) {
diff --git a/t/add.t b/t/add.t
index cf14f527a8b3b020a11ca3723cb8325a0876c013..891a22d202889b8e47aa87ed1546e50e2d2e4af6 100755 (executable)
--- a/t/add.t
+++ b/t/add.t
@@ -19,10 +19,10 @@ okexit 'making foo'
 pkgid=zpmtest-1.0-1
 PF=zpmtest-1.0-1.zpm
 
-require zpm newpackage -C $pkgid
-require zpm add $pkgid foo
+require -v zpm newpackage -C $pkgid
+require -v zpm add -vvv -f $PF $pkgid foo
 h=$(zpm hash foo)
-require zpm extract -f zpmtest-1.0-1.zpm $h foo2
+require -v zpm extract -f zpmtest-1.0-1.zpm $h foo2
 h2=$(zpm hash foo2)
 okstreq "$h" "$h2" "foo and foo2 hash match"
 
@@ -54,7 +54,7 @@ rm -f $PF
 mkdir subdir
 touch subdir/foo
 require zpm newpackage -f $PF -C $pkgid
-require zpm add -f $PF -S subdir zpmtest subdir/foo
+require -v zpm add -f $PF -S subdir zpmtest subdir/foo
 fn=$(zpm showpkg $PF | awk '{print $4}')
 okstreq "$fn" "/foo" file foo in package prefix striped
 rm -f $PF
diff --git a/zpm-add b/zpm-add
index c910a1cfd30025ae8b82af825f0f0bc43525c4b0..3131c72f9e3c5b9b742a2850d5deae5793d6d068 100755 (executable)
Binary files a/zpm-add and b/zpm-add differ
index a98aa002cd3abe8b570bb6454d4c5de3d2a419ac..9b6ec887cd015f45b88f72e0de32df74d31303a2 100644 (file)
@@ -271,6 +271,10 @@ with pkglibs as (\
      where PL.pkgid = %Q\
      ";
 
+static int afind_strcmp(const void *a, const void *b) {
+       return strcmp(a, b);
+}
+
 void checklibs(struct search_ctl *opts,
                jsw_hash_t *check, jsw_hash_t *forlibs, jsw_hash_t *nfound) {
        char *pkgid = 0, *pkgfile = 0;
@@ -286,8 +290,8 @@ void checklibs(struct search_ctl *opts,
        jsw_atrav_t *i;
        char *soname;
        
-       checked = jsw_anew((cmp_f)strcmp, (dup_f)strdup, (rel_f)free);
-       checked_libs = jsw_anew((cmp_f)strcmp, (dup_f)strdup, (rel_f)free);
+       checked = jsw_anew(afind_strcmp, (dup_f)strdup, (rel_f)free);
+       checked_libs = jsw_anew(afind_strcmp, (dup_f)strdup, (rel_f)free);
 
        while (jsw_hsize(check) > 0) {
                free(pkgid);
@@ -306,11 +310,22 @@ void checklibs(struct search_ctl *opts,
 
                if (jsw_afind(checked, pkgid)) {
                        /* already checked this one */
+                       /* fprintf(stderr, "already checked %s\n", pkgid); */
                        continue;
                }
 
+               fprintf(stderr, "checking libs for %s\n", pkgid);
                /* we do this now so we catch self deps */
-               jsw_ainsert(checked, pkgid);
+               if (!jsw_ainsert(checked, pkgid)) {
+                       fprintf(stderr, "checked insert failed\n");
+                       exit(EXIT_FAILURE);
+               }
+
+               if (!jsw_afind(checked, pkgid)) {
+                       /* already checked this one */
+                       fprintf(stderr, "checked find failed\n");
+                       exit(EXIT_FAILURE);
+               }
 
                /* get the libraries needed by this package */
                if (!zpm_open(&src, pkgfile)) {
@@ -319,7 +334,7 @@ void checklibs(struct search_ctl *opts,
                }
 
                if (needed) jsw_adelete(needed);
-               needed = jsw_anew((cmp_f)strcmp, (dup_f)strdup, (rel_f)free);
+               needed = jsw_anew(afind_strcmp, (dup_f)strdup, (rel_f)free);
                libs = zpm_libraries_needed(&src, pkgid, needed);
                zpm_close(&src);
 
@@ -333,16 +348,18 @@ void checklibs(struct search_ctl *opts,
                        int found;
                        struct pkgloc pkginfo;
 
-                       if (opts->verbose > 1) {
-                               fprintf(stderr, "checking for %s\n", soname);
-                       }
                        /* if it's in checked_libs, we've already looked at this one */
                        if (jsw_afind(checked_libs, soname)) {
                                if (opts->verbose > 1) {
-                                       fprintf(stderr, "already checked for %s\n", soname);
+                                       fprintf(stderr, "already checked %s\n", soname);
                                }
                                continue;
                        }
+#if 0
+                       if (opts->verbose > 1) {
+                               fprintf(stderr, "checking for %s\n", soname);
+                       }
+#endif
 
                        /* haven't found this soname */
                        jsw_ainsert(checked_libs, soname);
@@ -497,15 +514,24 @@ int main(int ac, char *av[]) {
                fprintf(stderr, "\n");
        }
 
-       packages = jsw_hnew(ac,NULL,(cmp_f)strcmp,(keydup_f)strdup,
+       /* direct packages asked for */
+       packages = jsw_hnew(ac,NULL,afind_strcmp,(keydup_f)strdup,
                        (itemdup_f)strdup,free,free);
-       check = jsw_hnew(ac,NULL,(cmp_f)strcmp,(keydup_f)strdup,
+
+       /* packages we need to check for libs */
+       check = jsw_hnew(ac,NULL,afind_strcmp,(keydup_f)strdup,
                        (itemdup_f)strdup,free,free);
-       forlibs = jsw_hnew(ac,NULL,(cmp_f)strcmp,(keydup_f)strdup,
+
+       /* packages we will also need for library dependences */
+       forlibs = jsw_hnew(ac,NULL,afind_strcmp,(keydup_f)strdup,
                        (itemdup_f)strdup,free,free);
-       nolib = jsw_hnew(ac,NULL,(cmp_f)strcmp,(keydup_f)strdup,
+
+       /* libraries we didn't find */
+       nolib = jsw_hnew(ac,NULL,afind_strcmp,(keydup_f)strdup,
                        (itemdup_f)strdup,free,free);
-       nfound = jsw_anew((cmp_f)strcmp, (dup_f)strdup, (rel_f)free);
+
+       /* packages asked for that we can't find */
+       nfound = jsw_anew(afind_strcmp, (dup_f)strdup, (rel_f)free);
 
        int arg;
        for (arg = argn; arg < ac; arg++) {
@@ -522,7 +548,6 @@ int main(int ac, char *av[]) {
                } else {
                        jsw_ainsert(nfound, av[arg]);
                }
-
        }
 
        if (findlibs) {
diff --git a/zpm.h b/zpm.h
index ea9cd6c96ec406073662475e9086a5648b0b72de..fec1aee134963adcc4e3ee338311c6503d267017 100644 (file)
--- a/zpm.h
+++ b/zpm.h
@@ -84,13 +84,30 @@ int zpm_libraries_needed(struct zpm *zpm, char *pkgid, jsw_atree_t *list);
 int zpm_quote(char *value, char *dest, size_t n);
 
 struct zpm_file {
-       char path[ZPM_PATH_MAX];
-       int mode;
+       char *package;
+       char *version;
+       int release;
+
+       char *status;
+       char *path;
+       char *target;
+
+       mode_t mode;
+
        //struct zpm_tree *tags;
-       char owner[32];
-       char group[32];
+       
+       char *owner;
+       char *group;
+       gid_t gid;
+       uid_t uid;
+
+       int configuration;
        time_t mtime;
+       char type;
+       dev_t device;
        char hash[ZPM_HASH_STRLEN+1];
+       char confhash[ZPM_HASH_STRLEN+1];
+       void *data; /* hook for applications to attach data */
        struct zpm_file *next; /* so you can make a linked list */
 };
 
@@ -178,6 +195,10 @@ int zpm_exec(struct zpm *z, const char *sql, int(*callback)(void *, int, char **
 int zpm_foreach_path(struct zpm *zpm, char *pkgid, char *where,
 int (*callback)(void *f, int ncols, char **vals, char **cols),
 void *data, char **errmsg);
+
+int zpm_foreach_path_ds(struct zpm *zpm, char *pkgid, char *where,
+int (*callback)(struct zpm *, struct zpm_file *, void *), void *cbd);
+
 int zpm_foreach_package(struct zpm *zpm, char *where,
 int (*callback)(void *cbdata, int ncols, char **vals, char **cols),
 void *data, char **errmsg);