]> pd.if.org Git - zpackage/blobdiff - zpm-extract.c
large commit of C work
[zpackage] / zpm-extract.c
diff --git a/zpm-extract.c b/zpm-extract.c
new file mode 100644 (file)
index 0000000..e050bf2
--- /dev/null
@@ -0,0 +1,118 @@
+#include <stdio.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <errno.h>
+
+#include <sys/mman.h>
+
+#include "zpm.h"
+
+#if 1
+int main(int ac, char **av){
+       struct zpm pkg;
+       int mode = 0644;
+
+       if (ac < 3) {
+               fprintf(stderr, "usage: db hash file\n");
+               return 1;
+       }
+       zpm_open(&pkg, av[1]);
+       zpm_extract(&pkg, av[2], av[3], mode);
+       zpm_close(&pkg);
+}
+#else
+
+int main(int ac, char **av) {
+       sqlite3 *db = 0;
+       int rc;
+
+       int blobsize;
+       int64_t size;
+       void *xzdata;
+       int type;
+       FILE *out;
+       sqlite3_stmt *ifile;
+
+       char *hash;
+       char *filename;
+
+       if (ac < 3) {
+               fprintf(stderr, "usage: db hash file\n");
+               return 1;
+       }
+
+       rc = sqlite3_open(av[1], &db);
+       if (rc) {
+               SQLERROR(sqlite3_errmsg(db));
+               sqlite3_close(db);
+               return 1;
+       }
+
+       rc = sqlite3_prepare(db, "select size, content from files where hash = ?", -1, &ifile,0);
+       if (rc != SQLITE_OK) {
+               SQLERROR(sqlite3_errmsg(db));
+               return 1;
+       }
+
+       /* hash, filename */
+       hash = av[2];
+       filename = av[3];
+
+       sqlite3_bind_text(ifile, 1, hash, 64, SQLITE_STATIC);
+
+       rc = sqlite3_step(ifile);
+
+       if (rc == SQLITE_DONE) {
+               /* didn't find a row */
+               sqlite3_finalize(ifile);
+               sqlite3_close(db);
+               fprintf(stderr, "no such hash\n");
+               return 1;
+       }
+       /* either way we're done with this now */
+
+       if (rc != SQLITE_ROW) {
+               SQLERROR(sqlite3_errmsg(db));
+               sqlite3_finalize(ifile);
+               sqlite3_close(db);
+               return 2;
+       }
+
+       type = sqlite3_column_type(ifile, 0);
+       if (type == SQLITE_NULL) {
+               fprintf(stderr, "no file size\n");
+               sqlite3_finalize(ifile);
+               sqlite3_close(db);
+               return 3;
+       }
+       type = sqlite3_column_type(ifile, 1);
+       if (type == SQLITE_NULL) {
+               fprintf(stderr, "no file data\n");
+               sqlite3_finalize(ifile);
+               sqlite3_close(db);
+               return 4;
+       }
+       size = sqlite3_column_int64(ifile, 0);
+       xzdata = (void *)sqlite3_column_blob(ifile, 1);
+       blobsize = sqlite3_column_bytes(ifile, 1);
+
+       out = fopen(filename, "w");
+       if (!out) {
+               fprintf(stderr, "can't open output file %s\n", filename);
+               sqlite3_finalize(ifile);
+               sqlite3_close(db);
+               return 5;
+       }
+       //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);
+
+       sqlite3_finalize(ifile);
+       sqlite3_close(db);
+       return 0;
+}
+#endif