]> pd.if.org Git - zpackage/blobdiff - lib/foreach_path.c
rewrite zpm-add in C
[zpackage] / lib / foreach_path.c
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) {