From 3b4ac94a0a91170d19d6281119d697195cce12a3 Mon Sep 17 00:00:00 2001 From: Nathan Wagner Date: Sat, 17 Sep 2016 02:02:59 -0500 Subject: [PATCH] skip compression if we already have the contents --- lib/zpm.c | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/lib/zpm.c b/lib/zpm.c index 37c7e7b..011dba7 100644 --- 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 */ -- 2.40.0