]> pd.if.org Git - zpackage/blobdiff - lib/zpm.c
add vercmp functions to libzpm
[zpackage] / lib / zpm.c
index 9d62ad7d7debb32172cb4ee50299c01f7161a467..8e0687542c290f49dbdf0298045a0bb67a158cd1 100644 (file)
--- a/lib/zpm.c
+++ b/lib/zpm.c
@@ -183,6 +183,11 @@ int zpm_commit(struct zpm *z) {
        return 1;
 }
 
+/* wrapper for sqlite3_exec */
+int zpm_exec(struct zpm *z, const char *sql, int(*callback)(void *, int, char **, char**), void *arg, char **errmsg) {
+       return sqlite3_exec(z->db, sql, callback, arg, errmsg);
+}
+
 int zpm_rollback(struct zpm *z) {
        char *errstr = 0;
        sqlite3_exec(z->db, "rollback;", NULL, NULL, &errstr);
@@ -349,6 +354,22 @@ int zpm_close(struct zpm *pkg) {
        return 1;
 }
 
+static int zpm_sqlite_vercmp(void *not_used, int unknown, const void *a,
+               int unk2, const void *b) {
+       /* not sure what the ints are, possibly string lengths */
+       not_used = 0; /* suppress warning */
+       unknown = 0; /* suppress warning */
+       unk2 = 0;
+       return zpm_vercmp(a, b);
+}
+
+int zpm_addvercmp(struct zpm *pkg) {
+       return sqlite3_create_collation(
+                       pkg->db, "vercmp", SQLITE_UTF8, NULL,
+                       zpm_sqlite_vercmp
+                       );
+}
+
 /* set package struct variables, database, environment, then command line */
 int zpm_readopts(struct zpm *pkg, int ac, char **av) {
        char *ev;
@@ -372,7 +393,7 @@ int zpm_readopts(struct zpm *pkg, int ac, char **av) {
 
        /* now, parse the options, return optind so the caller can adjust if needed */
 
-       return 1;
+       return av ? ac : 1;
 }
 
 int zpm_extract(struct zpm *pkg, char *hash, char *path, int mode) {
@@ -434,23 +455,27 @@ int zpm_extract(struct zpm *pkg, char *hash, char *path, int mode) {
        xzdata = (void *)sqlite3_column_blob(ifile, 1);
        blobsize = sqlite3_column_bytes(ifile, 1);
 
-       out = fopen(path, "w");
+       if (strcmp(path, "-")) {
+               out = fopen(path, "w");
+       } else {
+               out = stdout;
+       }
        if (!out) {
                fprintf(stderr, "can't open output file %s\n", path);
                sqlite3_finalize(ifile);
                sqlite3_close(db);
-               return 5;
+               return 0;
        }
        //fwrite(xzdata, blobsize, 1, stdout);
 
        //fprintf(stderr, "uncompressing %d bytes at %p, expect %lld\n", blobsize, xzdata, (long long int)size);
        uncompresslzma(xzdata, blobsize, out);
        fclose(out);
+       chmod(path, mode);
 
        sqlite3_finalize(ifile);
 
-       return 0;
-       
+       return 1;
 }
 
 /* flags 0, close mmap, flags 1, return mmap fd */
@@ -495,7 +520,7 @@ int zpm_hash(char *path, char *hash, uint32_t flags) {
        }
        hash[64] = 0;
        munmap(content, sbuf.st_size);
-       return 1;
+       return flags ? fd : 1;
 }
 
 static sqlite3_stmt *run_for_hash(sqlite3 *db, char *sql, char *hash) {
@@ -709,6 +734,7 @@ int zpm_import(struct zpm *pkg, char *path, uint32_t flags, char *hash) {
                hash = hashbuf;
        }
 
+       flags = 0; /* suppress warning, probably use to follow symlinks */
        /* mmap the file */
        fd = open(path, O_RDONLY);
        if (fd == -1) {