From: Nathan Wagner Date: Wed, 7 Nov 2018 01:45:12 +0000 (+0000) Subject: add foreach_script function X-Git-Tag: v0.2.16~26 X-Git-Url: https://pd.if.org/git/?p=zpackage;a=commitdiff_plain;h=c02e8d831122a804f675a2f106b2e23af235be58 add foreach_script function --- diff --git a/Makefile b/Makefile index a294b38..d018fd3 100644 --- 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 diff --git a/lib/script_hash.c b/lib/script_hash.c index ebca550..8348b02 100644 --- a/lib/script_hash.c +++ b/lib/script_hash.c @@ -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]; diff --git a/zpm-script.c b/zpm-script.c index 359970a..a91f2ac 100644 --- a/zpm-script.c +++ b/zpm-script.c @@ -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 adc69bb..106314e 100644 --- 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);