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