]> pd.if.org Git - zpackage/commitdiff
add foreach_script function
authorNathan Wagner <nw@hydaspes.if.org>
Wed, 7 Nov 2018 01:45:12 +0000 (01:45 +0000)
committerNathan Wagner <nw@hydaspes.if.org>
Wed, 7 Nov 2018 01:45:12 +0000 (01:45 +0000)
Makefile
lib/script_hash.c
zpm-script.c
zpm.h

index a294b3817d937c324377eb4551e931703fce20ed..d018fd3219b6b8203900c26d5b443abae98a45b5 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -135,6 +135,7 @@ zpm-packagehash: zpm-packagehash.o libzpm.a
        $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $< -lzpm -lelf
 
 zpm-foreach-path.o: CFLAGS+=-Wno-unused-parameter
+zpm-script.o: CFLAGS+=-Wno-unused-parameter
 
 zpm-foreach-path: zpm-foreach-path.o libzpm.a sqlite/sqlite3.h
        $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $< -lzpm -lelf
index ebca5508d4c65f80fc335c1d20b51ee8fa89173e..8348b0247c4c2073d3b2794e83089135c8e21998 100644 (file)
@@ -7,6 +7,82 @@
 #include "sqlite3.h"
 #include "zpm.h"
 
+int zpm_foreach_script(struct zpm *zpm, char *pkgstr, char *stage, void *cbd,
+               int (*cb)(void *ud, const char *pkg, const char *stage, const char *hash)) {
+       char *pkgid = 0;
+       char *sql;
+       sqlite3_str *s;
+       sqlite3_stmt *st;
+
+       char *find = "select printf('%s-%s-%d', package, version, release) as pkgid, stage, hash from scripts where true";
+       char *pkgidwhere = " and printf('%s-%s-%d', package, version, release) = %Q";
+       char *stagewhere = " and stage = %Q";
+
+       if (!zpm || !cb) {
+               return 0;
+       }
+       if (zpm->error) {
+               return 0;
+       }
+
+       if (pkgstr) {
+               pkgid = zpm_findpkg(zpm, pkgstr, 0);
+               if (!pkgid) {
+                       return 0;
+               }
+       }
+
+       s = sqlite3_str_new(zpm->db);
+       sqlite3_str_appendall(s, find);
+
+       if (pkgid)  {
+               sqlite3_str_appendf(s, pkgidwhere, pkgid);
+       }
+
+       if (stage) {
+               sqlite3_str_appendf(s, stagewhere, stage);
+       }
+
+       sql = sqlite3_str_value(s);
+       if (!sql) {
+               sqlite3_str_finish(s);
+               zpm->error = 1;
+               if (pkgid) {
+                       free(pkgid);
+               }
+               return 0;
+       }
+
+       st = zpm_dbquery(zpm, sql);
+       int rv, cbrv = 0;
+       while ((rv = sqlite3_step(st)) == SQLITE_ROW) {
+               if (cb) {
+                       const char *sstage, *spkgid, *shash;
+                       int cbrv;
+
+                       spkgid = (const char *)sqlite3_column_text(st, 0);
+                       sstage = (const char *)sqlite3_column_text(st, 1);
+                       shash = (const char *)sqlite3_column_text(st, 2);
+
+                       cbrv = cb(cbd, spkgid, sstage, shash);
+                       if (cbrv) {
+                               break;
+                       }
+               }
+       }
+
+       if (rv != SQLITE_DONE) {
+               zpm->dbresult = rv;
+               zpm->error = 1;
+       }
+       
+       if (pkgid) {
+               free(pkgid);
+       }
+
+       return cbrv;
+}
+
 int zpm_script_set(struct zpm *zpm, char *pkgstr, char *phase, char *hash) {
        char package[64];
        char version[32];
index 359970abd567d7934d641864c8e4e034e7c8d706..a91f2ac3195974c030a0fc5b28c9645e319feaf6 100644 (file)
@@ -119,6 +119,12 @@ int run(char *program, char **args, char *output, int *status) {
 #define SOFT 1
 #define HARD 2
 
+static int list_scripts(void *ud, const char *pkg, const char *stage,
+               const char *hash) {
+       printf("%s %s %.8s\n", pkg, stage, hash);
+       return 0;
+}
+
 int main(int ac, char **av){
        struct zpm zpm;
        int rv;
@@ -248,7 +254,9 @@ int main(int ac, char **av){
                        fail = HARD;
                }
        } else if (mode == LIST) {
-               if (!zpm_script_hash(&zpm, pkgid, phase, hash)) {
+               if (!phase) {
+                       zpm_foreach_script(&zpm, pkgid, phase, 0, list_scripts);
+               } else if (!zpm_script_hash(&zpm, pkgid, phase, hash)) {
                        fail = SOFT;
                } else if (scriptishash) {
                        printf("%s\n", hash);
diff --git a/zpm.h b/zpm.h
index adc69bba4376d03459d656ac70bf5a05d20e1c7e..106314e958356e40de4fbcea04f67bb42a35571b 100644 (file)
--- a/zpm.h
+++ b/zpm.h
@@ -180,6 +180,8 @@ void *data, char **errmsg);
 
 int zpm_script_hash(struct zpm *zpm, char *pkgstr, char *phase, char *hash);
 int zpm_script_set(struct zpm *zpm, char *pkgstr, char *phase, char *hash);
+int zpm_foreach_script(struct zpm *zpm, char *pkgstr, char *stage, void *cbd,
+               int (*cb)(void *ud, const char *pkg, const char *stage, const char *hash));
 
 int zpm_package_hash(struct zpm *zpm, char *pkgid, char *hash);
 int zpm_package_sethash(struct zpm *zpm, char *pkgid, char *hash);