]> pd.if.org Git - zpackage/commitdiff
skip compression if we already have the contents
authorNathan Wagner <nw@hydaspes.if.org>
Sat, 17 Sep 2016 07:02:59 +0000 (02:02 -0500)
committerNathan Wagner <nw@hydaspes.if.org>
Sat, 17 Sep 2016 07:22:45 +0000 (02:22 -0500)
lib/zpm.c

index 37c7e7b565fc14288ce96d18318812e29f523f34..011dba77234f2f4099709d407484a8a73e448618 100644 (file)
--- a/lib/zpm.c
+++ b/lib/zpm.c
@@ -322,7 +322,7 @@ int zpm_import(struct zpm *pkg, char *path, uint32_t flags, char *hash) {
        unsigned char tmp[32];
        hash_state md;
        sqlite3_stmt *ifile;
-       int haverow,havedata;
+       int haverow = 0,havedata = 0;
        int j,rc,type;
        char hashbuf[65];
 
@@ -366,15 +366,8 @@ int zpm_import(struct zpm *pkg, char *path, uint32_t flags, char *hash) {
                sprintf(hash+j*2, "%02x", (unsigned)tmp[j]);
        }
        hash[64] = 0;
-//     fprintf(stderr, "file %s: %s\n", path, hash);
+       fprintf(stderr, "file %s: %s\n", path, hash);
 
-       /* compress */
-       outbuf = compresslzma(content, sbuf.st_size, &outlen);
-//     fprintf(stderr, "compressed to %zu\n", outlen);
-
-       /* don't need the original file now */
-       munmap(content, sbuf.st_size);
-       close(fd);
 
        /* prepare and bind */
        /* TODO check null */
@@ -383,7 +376,7 @@ int zpm_import(struct zpm *pkg, char *path, uint32_t flags, char *hash) {
        rc = sqlite3_prepare(db, "select size, content is not null from files where hash = ?", -1, &ifile,0);
        if (rc != SQLITE_OK) {
                SQLERROR(sqlite3_errmsg(db));
-               return 1;
+               return 0;
        }
 
        /* hash, filename */
@@ -422,15 +415,27 @@ int zpm_import(struct zpm *pkg, char *path, uint32_t flags, char *hash) {
        sqlite3_finalize(ifile);
 
        if (!havedata) {
+               /* compress */
+               outbuf = compresslzma(content, sbuf.st_size, &outlen);
+               if (!outbuf) {
+                       fprintf(stderr, "compresslzma failed\n");
+                       return 0;
+               }
+               fprintf(stderr, "compressed to %zu\n", outlen);
+               /* don't need the original file now */
+               munmap(content, sbuf.st_size);
+               close(fd);
+
                /* start a transaction */
                // do that outside of here 
                //zpm_begin(pkg);
 
                /* insert */
                if (haverow) {
+                       fprintf(stderr, "adding file data\n");
                        rc = sqlite3_prepare(db, "update files set size = ?, content = ? where hash = ?", -1, &ifile,0);
                } else {
-//                     fprintf(stderr, "missing file data\n");
+                       fprintf(stderr, "creating new data row\n");
                        rc = sqlite3_prepare(db, "insert into files (size, content, hash) values (?,?,?)", -1, &ifile,0);
                }
                if (rc != SQLITE_OK) {
@@ -473,8 +478,13 @@ int zpm_import(struct zpm *pkg, char *path, uint32_t flags, char *hash) {
                /* commit */
                //zpm_commit(pkg);
 
+       } else {
+               /* don't need the original file now */
+               munmap(content, sbuf.st_size);
+               close(fd);
        }
 
+
        /* if package and not nopackage flag, add to package */
        if (pkg->pkgname && (!ZPM_NOPACKAGE)) {
                /* TODO */