]> pd.if.org Git - zpackage/blob - zpm-extract.c
large commit of C work
[zpackage] / zpm-extract.c
1 #include <stdio.h>
2 #include <sys/types.h>
3 #include <sys/stat.h>
4 #include <fcntl.h>
5 #include <unistd.h>
6 #include <errno.h>
7
8 #include <sys/mman.h>
9
10 #include "zpm.h"
11
12 #if 1
13 int main(int ac, char **av){
14         struct zpm pkg;
15         int mode = 0644;
16
17         if (ac < 3) {
18                 fprintf(stderr, "usage: db hash file\n");
19                 return 1;
20         }
21         zpm_open(&pkg, av[1]);
22         zpm_extract(&pkg, av[2], av[3], mode);
23         zpm_close(&pkg);
24 }
25 #else
26
27 int main(int ac, char **av) {
28         sqlite3 *db = 0;
29         int rc;
30
31         int blobsize;
32         int64_t size;
33         void *xzdata;
34         int type;
35         FILE *out;
36         sqlite3_stmt *ifile;
37
38         char *hash;
39         char *filename;
40
41         if (ac < 3) {
42                 fprintf(stderr, "usage: db hash file\n");
43                 return 1;
44         }
45
46         rc = sqlite3_open(av[1], &db);
47         if (rc) {
48                 SQLERROR(sqlite3_errmsg(db));
49                 sqlite3_close(db);
50                 return 1;
51         }
52
53         rc = sqlite3_prepare(db, "select size, content from files where hash = ?", -1, &ifile,0);
54         if (rc != SQLITE_OK) {
55                 SQLERROR(sqlite3_errmsg(db));
56                 return 1;
57         }
58
59         /* hash, filename */
60         hash = av[2];
61         filename = av[3];
62
63         sqlite3_bind_text(ifile, 1, hash, 64, SQLITE_STATIC);
64
65         rc = sqlite3_step(ifile);
66
67         if (rc == SQLITE_DONE) {
68                 /* didn't find a row */
69                 sqlite3_finalize(ifile);
70                 sqlite3_close(db);
71                 fprintf(stderr, "no such hash\n");
72                 return 1;
73         }
74         /* either way we're done with this now */
75
76         if (rc != SQLITE_ROW) {
77                 SQLERROR(sqlite3_errmsg(db));
78                 sqlite3_finalize(ifile);
79                 sqlite3_close(db);
80                 return 2;
81         }
82
83         type = sqlite3_column_type(ifile, 0);
84         if (type == SQLITE_NULL) {
85                 fprintf(stderr, "no file size\n");
86                 sqlite3_finalize(ifile);
87                 sqlite3_close(db);
88                 return 3;
89         }
90         type = sqlite3_column_type(ifile, 1);
91         if (type == SQLITE_NULL) {
92                 fprintf(stderr, "no file data\n");
93                 sqlite3_finalize(ifile);
94                 sqlite3_close(db);
95                 return 4;
96         }
97         size = sqlite3_column_int64(ifile, 0);
98         xzdata = (void *)sqlite3_column_blob(ifile, 1);
99         blobsize = sqlite3_column_bytes(ifile, 1);
100
101         out = fopen(filename, "w");
102         if (!out) {
103                 fprintf(stderr, "can't open output file %s\n", filename);
104                 sqlite3_finalize(ifile);
105                 sqlite3_close(db);
106                 return 5;
107         }
108         //fwrite(xzdata, blobsize, 1, stdout);
109
110         fprintf(stderr, "uncompressing %d bytes at %p, expect %lld\n", blobsize, xzdata, (long long int)size);
111         uncompresslzma(xzdata, blobsize, out);
112         fclose(out);
113
114         sqlite3_finalize(ifile);
115         sqlite3_close(db);
116         return 0;
117 }
118 #endif