]> pd.if.org Git - zpackage/blob - zpm-extract.c
fix number of tests in update test
[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 /* more usage:
13  * -t : use a temp file, then move into place, possible reverse the sense
14  *  -u : userid
15  *  -g : groupid
16  *  -m : mode (i.e. final mode)
17         int mode = 0600;
18  *  -l : log all actions
19  *  -d : logging database file, if different
20  *
21  *  check if file exists, if it does, and has the same hash, do
22  *  nothing, unless -f is given
23  */ 
24
25 #if 1
26 int main(int ac, char **av){
27         struct zpm pkg;
28         int rv;
29
30         if (ac < 3) {
31                 fprintf(stderr, "usage: db hash file\n");
32                 return 1;
33         }
34         zpm_open(&pkg, av[1]);
35         rv = zpm_extract(&pkg, av[2], av[3], 0600);
36         zpm_close(&pkg);
37
38         return rv ? 0 : 1;
39 }
40 #else
41
42 int main(int ac, char **av) {
43         sqlite3 *db = 0;
44         int rc;
45
46         int blobsize;
47         int64_t size;
48         void *xzdata;
49         int type;
50         FILE *out;
51         sqlite3_stmt *ifile;
52
53         char *hash;
54         char *filename;
55
56         if (ac < 3) {
57                 fprintf(stderr, "usage: db hash file\n");
58                 return 1;
59         }
60
61         rc = sqlite3_open(av[1], &db);
62         if (rc) {
63                 SQLERROR(sqlite3_errmsg(db));
64                 sqlite3_close(db);
65                 return 1;
66         }
67
68         rc = sqlite3_prepare(db, "select size, content from files where hash = ?", -1, &ifile,0);
69         if (rc != SQLITE_OK) {
70                 SQLERROR(sqlite3_errmsg(db));
71                 return 1;
72         }
73
74         /* hash, filename */
75         hash = av[2];
76         filename = av[3];
77
78         sqlite3_bind_text(ifile, 1, hash, 64, SQLITE_STATIC);
79
80         rc = sqlite3_step(ifile);
81
82         if (rc == SQLITE_DONE) {
83                 /* didn't find a row */
84                 sqlite3_finalize(ifile);
85                 sqlite3_close(db);
86                 fprintf(stderr, "no such hash\n");
87                 return 1;
88         }
89         /* either way we're done with this now */
90
91         if (rc != SQLITE_ROW) {
92                 SQLERROR(sqlite3_errmsg(db));
93                 sqlite3_finalize(ifile);
94                 sqlite3_close(db);
95                 return 2;
96         }
97
98         type = sqlite3_column_type(ifile, 0);
99         if (type == SQLITE_NULL) {
100                 fprintf(stderr, "no file size\n");
101                 sqlite3_finalize(ifile);
102                 sqlite3_close(db);
103                 return 3;
104         }
105         type = sqlite3_column_type(ifile, 1);
106         if (type == SQLITE_NULL) {
107                 fprintf(stderr, "no file data\n");
108                 sqlite3_finalize(ifile);
109                 sqlite3_close(db);
110                 return 4;
111         }
112         size = sqlite3_column_int64(ifile, 0);
113         xzdata = (void *)sqlite3_column_blob(ifile, 1);
114         blobsize = sqlite3_column_bytes(ifile, 1);
115
116         out = fopen(filename, "w");
117         if (!out) {
118                 fprintf(stderr, "can't open output file %s\n", filename);
119                 sqlite3_finalize(ifile);
120                 sqlite3_close(db);
121                 return 5;
122         }
123         //fwrite(xzdata, blobsize, 1, stdout);
124
125         fprintf(stderr, "uncompressing %d bytes at %p, expect %lld\n", blobsize, xzdata, (long long int)size);
126         uncompresslzma(xzdata, blobsize, out);
127         fclose(out);
128
129         sqlite3_finalize(ifile);
130         sqlite3_close(db);
131         return 0;
132 }
133 #endif