]> pd.if.org Git - zpackage/blob - lib/zpm.c
added db initialization and version detection
[zpackage] / lib / zpm.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <sys/mman.h>
7 #include <unistd.h>
8 #include <fcntl.h>
9
10 #include "zpm.h"
11
12 #include "sha256.h"
13 #if 0
14 struct zpm {
15         sqlite3 *db;
16         char *path; /* path to package file */
17         char *version;
18         int release;
19         char *pkgname;
20         time_t installed; /* install time, 0 for not installed */
21 };
22
23 struct zpm_file {
24         char *path;
25         int mode;
26         uint32_t filetype;
27         char *tags;
28         char *owner;
29         char *group;
30         char *hash; /* could be fixed length */
31         time_t mtime;
32         struct zpm_file *next; /* so you can make a linked list */
33 };
34
35 /* NULL?  Create? */
36 /* associate with a package ? if only one?  first? */
37 int zpm_open(struct zpm *pkg, char *path);
38 int zpm_pkgname(char *base, char *version, int release); /* construct a package file name */
39
40 /* flags for preserving mode, owner, etc */
41 /* puts hash of import in hash */
42 /* path can be a hash, with an "INTERNAL" flag, i.e. internally import */
43 #define ZPM_MODE 0x1
44 #define ZPM_OWNER 0x2
45 #define ZPM_MTIME 0x4
46 #define ZPM_INTERNAL 0x8
47 #define ZPM_NOBLOB 0x10
48 /* don't run scripts on install */
49 #define ZPM_NOSCRIPTS 0x10
50 /* don't associate the file with a package, just do a raw insert */
51 /* otherwise, associate it with the current package */
52 #define ZPM_NOPACKAGE 0x20
53
54 int zpm_import(struct zpm *zp, char *path, uint32_t flags, uint8_t *hash);
55
56 /* link and unlink hashes to packages */
57 int zpm_link(struct zpm *pkg, char *path, char *hash, struct zpm_file *fileinfo);
58 int zpm_unlink(struct zpm *pkg, char *path);
59
60 /* tag a file.  relative to "current package" */
61 int zpm_tag(struct zpm *zp, char *path, char *tags);
62 /* should this be broken up into separage functions ? */
63 int zpm_md(struct zpm *zp, char *path, int mode, char *owner, char *group, time_t mtime);
64
65 /* export hash to dest */
66 int zpm_extract(struct zpm *pkg, char *hash, char *path, int mode);
67
68 /* export path to dest */
69 int zpm_export(struct zpm *zp, char *path, uint32_t flags, char *dest);
70
71 int zpm_close(struct zpm *zp);
72
73 /* attach a signature to a package */
74 int zpm_sign(struct zpm *z, size_t s, void *signature);
75
76 /* set the package info to the nth package, -1 to return count? */
77 /* further import/exports and such will be relative to this package */
78 int zpm_package(struct zpm *zp, int n);
79
80 /* get file information */
81 int zpm_stat(struct zpm *z, struct zpm_file *f, int n);
82
83 /* will also set the package context to the new package */
84 int zpm_newpkg(struct zpm *z, char *base, char *version, int release);
85
86 /* transactions */
87 int zpm_begin(struct zpm *z);
88 int zpm_commit(struct zpm *z);
89 int zpm_rollback(struct zpm *z);
90
91 /* higher level operations */
92
93 /* install or uninstall the package */
94 /* flag for keeping the blobs in local */
95 /* what about tag filtering */
96 int zpm_install(struct zpm *z, struct zpm *local, uint32_t flags);
97 int zpm_uninstall(struct zpm *local);
98
99 /* slurp up the actual blobs */
100 /* what about versioning them if they change */
101 int zpm_preserve(struct zpm *local);
102
103 /* check file integrity */
104 int zpm_checkinstall(struct zpm *local);
105
106 int zpm_merge(struct zpm *z, struct zpm *src, uint32_t flags);
107
108 void uncompresslzma(void *buf, size_t bufsize, FILE *out);
109 #define SQLERROR(x) fprintf(stderr, "%s %d: %s\n", __func__, __LINE__, (x))
110 #endif
111
112 static char *dupstr(char *s) {
113         size_t n;
114         char *d;
115
116         n = strlen(s);
117         d = malloc(n+1);
118         if (d) {
119                 d = strcpy(d, s);
120         }
121         return d;
122 }
123
124 #if 0
125 int zpm_newpkg(struct zpm *z, char *base, char *version, int release) {
126         char *sql = "insert or ignore into packages (package,version,release) values (?,?,?)";
127         int rc;
128         sqlite3_stmt *ifile;
129
130         rc = sqlite3_prepare(db, sql, -1, &ifile,0);
131         if (rc != SQLITE_OK) {
132                 SQLERROR(sqlite3_errmsg(db));
133                 return 0;
134         }
135         rc = sqlite3_bind_text(ifile, 1, base, strlen(base), SQLITE_STATIC);
136         if (rc != SQLITE_OK) {
137                 SQLERROR(sqlite3_errmsg(db));
138                 fprintf(stderr, "cant bind package name\n");
139                 zpm_rollback(pkg);
140                 return 0;
141         }
142         sqlite3_bind_text(ifile, 2, version, strlen(version), SQLITE_STATIC);
143         sqlite3_bind_int(ifile, 3, release)
144
145         rc = sqlite3_step(ifile);
146
147         if (rc != SQLITE_DONE) {
148                 SQLERROR(sqlite3_errmsg(db));
149                 sqlite3_finalize(ifile);
150                 return 0;
151         }
152         sqlite3_finalize(ifile);
153         z->pkg = dupstr(base);
154         z->version = dupstr(version);
155         z->release = release;
156 }
157 #endif
158
159 int zpm_begin(struct zpm *z) {
160         char *errstr = 0;
161         sqlite3_exec(z->db, "begin;", NULL, NULL, &errstr);
162         if (errstr) {
163                 fprintf(stderr, "sqlite begin error: %s\n", errstr);
164                 sqlite3_free(errstr);
165                 return 0;
166         }
167         return 1;
168 }
169
170 int zpm_commit(struct zpm *z) {
171         char *errstr = 0;
172         sqlite3_exec(z->db, "commit;", NULL, NULL, &errstr);
173         if (errstr) {
174                 fprintf(stderr, "sqlite commit error: %s\n", errstr);
175                 sqlite3_free(errstr);
176                 return 0;
177         }
178         return 1;
179 }
180
181 int zpm_rollback(struct zpm *z) {
182         char *errstr = 0;
183         sqlite3_exec(z->db, "rollback;", NULL, NULL, &errstr);
184         if (errstr) {
185                 fprintf(stderr, "sqlite rollback error: %s\n", errstr);
186                 sqlite3_free(errstr);
187                 return 0;
188         }
189         return 1;
190 }
191
192 int zpm_db_set_pragma(struct zpm *db, int pragma, int value) {
193         int rc;
194         char *sql;
195         sqlite3_stmt *s;
196
197         switch (pragma) {
198                 case 1: sql = "pragma application_id = ?;"; break;
199                 case 2: sql = "pragma user_version = ?;"; break;
200                 default: return -1; break;
201         }
202
203         rc = sqlite3_prepare_v2(db->db, sql, -1, &s, 0);
204
205         if (rc != SQLITE_OK) {
206                 SQLERROR(sqlite3_errmsg(db->db));
207                 return -1;
208         }
209
210         sqlite3_bind_int(s, 1, value);
211         if (rc != SQLITE_OK) {
212                 SQLERROR(sqlite3_errmsg(db->db));
213                 fprintf(stderr, "cant bind pragma value\n");
214                 sqlite3_finalize(s);
215                 return -1;
216         }
217         rc = sqlite3_step(s);
218         if (rc != SQLITE_DONE) {
219                 SQLERROR(sqlite3_errmsg(db->db));
220                 fprintf(stderr, "cant set pragma\n");
221                 sqlite3_finalize(s);
222                 return -1;
223         }
224
225         sqlite3_finalize(s);
226         return value;
227 }
228
229 int zpm_db_pragma(struct zpm *db, int pragma) {
230         int rc;
231         int value = -1;
232         char *sql = 0;
233         sqlite3_stmt *s;
234
235         switch (pragma) {
236                 case 1: sql = "pragma application_id;"; break;
237                 case 2: sql = "pragma user_version;"; break;
238                 default: return -1; break;
239         }
240
241         rc = sqlite3_prepare_v2(db->db, sql, -1, &s, 0);
242
243         if (rc != SQLITE_OK) {
244                 SQLERROR(sqlite3_errmsg(db->db));
245                 fprintf(stderr, "%s, errnum = %d\n", sqlite3_errmsg(db->db), rc);
246                 /* TODO just abort? */
247                 return -1;
248         }
249
250         rc = sqlite3_step(s);
251         if (rc == SQLITE_ROW) {
252                 value = sqlite3_column_int(s, 0);
253         }
254
255         sqlite3_finalize(s);
256         return value;
257 }
258
259 static
260 #include "newdb.c"
261
262 int zpm_db_initialize(struct zpm *pkg) {
263         fprintf(stderr, "initializing zpm database\n");
264         switch (sqlite3_exec(pkg->db, createdb, (int (*)(void *,int,char **,char **))0, NULL, NULL)) {
265                 case SQLITE_OK: break;
266                 default:
267                         SQLERROR(sqlite3_errmsg(pkg->db));
268                         return 0;
269                         break;
270         }
271         return 1;
272 }
273
274 /* NULL?  Create? */
275 int zpm_open(struct zpm *pkg, char *path) {
276         int rc;
277         char *errstr = 0;
278         sqlite3 *db = 0;
279         int appid, dbver;
280
281         pkg->db = 0;
282         pkg->path = 0;
283         pkg->version = 0;
284         pkg->release = 0;
285         pkg->pkgname = 0;
286         pkg->installed = 0;
287
288         rc = sqlite3_open(path, &db);
289         if (rc) {
290                 SQLERROR(sqlite3_errmsg(db));
291                 sqlite3_close(db);
292                 return 0;
293         }
294         pkg->db   = db;
295         pkg->path = dupstr(path);
296
297         appid = zpm_db_pragma(pkg, 1);
298         dbver = zpm_db_pragma(pkg, 2);
299
300         fprintf(stderr, "db appid = %x, dbver = %d\n", appid, dbver);
301         switch (appid) {
302                 case 0: if (!zpm_db_initialize(pkg)) {
303                                 sqlite3_close(db);
304                                 return 1;
305                         };
306                         break;
307                 case 0x5a504442: break;
308                 default:
309                         fprintf(stderr, "unknown database type\n");
310                         sqlite3_close(db);
311                         return 0;
312                         break;
313         }
314         if (dbver > 1) {
315                 fprintf(stderr, "version %d zpm db detected, this program only works with version 1 databases\n", dbver);
316                         sqlite3_close(db);
317                         return 0;
318         }
319
320         sqlite3_exec(pkg->db, "pragma foreign_keys = ON;", NULL, NULL, &errstr);
321         if (errstr) {
322                 fprintf(stderr, "sqlite foreign key error: %s\n", errstr);
323                 sqlite3_free(errstr);
324                 sqlite3_close(db);
325                 return 0;
326         }
327
328
329         /* TODO if this is a new database, create structures */
330
331         /* get a package. what if more than one? what if none? */
332         return 1;
333 }
334
335 int zpm_close(struct zpm *pkg) {
336         if (pkg) {
337                 sqlite3_close(pkg->db);
338                 free(pkg->path);
339         }
340         /* TODO free any malloced names and such */
341         return 1;
342 }
343
344 int zpm_extract(struct zpm *pkg, char *hash, char *path, int mode) {
345         int rc;
346
347         int blobsize;
348         int64_t size;
349         void *xzdata;
350         int type;
351         FILE *out;
352         sqlite3_stmt *ifile;
353
354         /* TODO check null */
355         sqlite3 *db = pkg->db;
356
357         rc = sqlite3_prepare(db, "select size, content from files where hash = ?", -1, &ifile,0);
358         if (rc != SQLITE_OK) {
359                 SQLERROR(sqlite3_errmsg(db));
360                 return 0;
361         }
362
363         /* hash, filename */
364
365         sqlite3_bind_text(ifile, 1, hash, 64, SQLITE_STATIC);
366
367         rc = sqlite3_step(ifile);
368
369         if (rc == SQLITE_DONE) {
370                 /* didn't find a row */
371                 sqlite3_finalize(ifile);
372                 sqlite3_close(db);
373                 fprintf(stderr, "no such hash\n");
374                 return 0;
375         }
376         /* either way we're done with this now */
377
378         if (rc != SQLITE_ROW) {
379                 SQLERROR(sqlite3_errmsg(db));
380                 sqlite3_finalize(ifile);
381                 sqlite3_close(db);
382                 return 0;
383         }
384
385         type = sqlite3_column_type(ifile, 0);
386         if (type == SQLITE_NULL) {
387                 fprintf(stderr, "no file size\n");
388                 sqlite3_finalize(ifile);
389                 sqlite3_close(db);
390                 return 0;
391         }
392         type = sqlite3_column_type(ifile, 1);
393         if (type == SQLITE_NULL) {
394                 fprintf(stderr, "no file data\n");
395                 sqlite3_finalize(ifile);
396                 sqlite3_close(db);
397                 return 0;
398         }
399         size = sqlite3_column_int64(ifile, 0);
400         xzdata = (void *)sqlite3_column_blob(ifile, 1);
401         blobsize = sqlite3_column_bytes(ifile, 1);
402
403         out = fopen(path, "w");
404         if (!out) {
405                 fprintf(stderr, "can't open output file %s\n", path);
406                 sqlite3_finalize(ifile);
407                 sqlite3_close(db);
408                 return 5;
409         }
410         //fwrite(xzdata, blobsize, 1, stdout);
411
412         fprintf(stderr, "uncompressing %d bytes at %p, expect %lld\n", blobsize, xzdata, (long long int)size);
413         uncompresslzma(xzdata, blobsize, out);
414         fclose(out);
415
416         sqlite3_finalize(ifile);
417
418         return 0;
419         
420 }
421
422 #if 1
423 int zpm_import(struct zpm *pkg, char *path, uint32_t flags, char *hash) {
424         int fd;
425         void *content;
426         struct stat sbuf;
427         unsigned char tmp[32];
428         hash_state md;
429         sqlite3_stmt *ifile;
430         int haverow = 0,havedata = 0;
431         int j,rc,type;
432         char hashbuf[65];
433
434         /* xz compress it */
435         size_t outlen = 0;
436         void *outbuf;
437
438         if (!pkg || !pkg->db || !path) {
439                 return 0;
440         }
441
442         /* use local if caller didn't pass in room */
443         if (!hash) {
444                 hash = hashbuf;
445         }
446
447         /* mmap the file */
448         fd = open(path, O_RDONLY);
449         if (fd == -1) {
450                 return 0;
451         }
452         if (fstat(fd, &sbuf) == -1) {
453                 return 0;
454         }
455         /* not a regular file? */
456         if (!S_ISREG(sbuf.st_mode)) {
457                 /* TODO this is ok, just stored differently */
458                 return 0;
459         }
460
461         content = mmap(0, sbuf.st_size, PROT_READ,MAP_PRIVATE, fd, 0);
462         if (!content) {
463                 return 0;
464         }
465
466         /* get hash */
467         sha256_init(&md);
468         sha256_process(&md, content, sbuf.st_size);
469         sha256_done(&md, tmp);
470         for (j=0;j<32;j++) {
471                 sprintf(hash+j*2, "%02x", (unsigned)tmp[j]);
472         }
473         hash[64] = 0;
474         fprintf(stderr, "file %s: %s\n", path, hash);
475
476
477         /* prepare and bind */
478         /* TODO check null */
479         sqlite3 *db = pkg->db;
480
481         rc = sqlite3_prepare_v2(db, "select size, content is not null from files where hash = ?", -1, &ifile,0);
482         if (rc != SQLITE_OK) {
483                 SQLERROR(sqlite3_errmsg(db));
484                 return 0;
485         }
486
487         /* hash, filename */
488
489         sqlite3_bind_text(ifile, 1, hash, 64, SQLITE_STATIC);
490
491         rc = sqlite3_step(ifile);
492
493         if (rc != SQLITE_DONE) {
494                 if (rc != SQLITE_ROW) {
495                         /* didn't find a row */
496                         SQLERROR(sqlite3_errmsg(db));
497                         zpm_rollback(pkg);
498                         return 0;
499                 }
500                 haverow = 1;
501 //              fprintf(stderr, "have row for hash\n");
502                 type = sqlite3_column_type(ifile, 0);
503                 if (type == SQLITE_NULL) {
504                         /* TODO assert, this shouldn't be possible? */
505                         fprintf(stderr, "no file size\n");
506                         sqlite3_finalize(ifile);
507                         return 0;
508                 }
509                 type = sqlite3_column_type(ifile, 1);
510                 if (type == SQLITE_NULL) {
511                         /* TODO assert, this shouldn't be possible? */
512                         fprintf(stderr, "no file data\n");
513                         sqlite3_finalize(ifile);
514                         return 0;
515                         /* which is fine, just need to update the row then */
516                 }
517                 havedata = sqlite3_column_int(ifile, 1);
518         }
519
520         sqlite3_finalize(ifile);
521
522         if (!havedata) {
523                 /* compress */
524                 outbuf = compresslzma(content, sbuf.st_size, &outlen);
525                 if (!outbuf) {
526                         fprintf(stderr, "compresslzma failed\n");
527                         return 0;
528                 }
529                 fprintf(stderr, "compressed to %zu\n", outlen);
530                 /* don't need the original file now */
531                 munmap(content, sbuf.st_size);
532                 close(fd);
533
534                 /* start a transaction */
535                 // do that outside of here 
536                 //zpm_begin(pkg);
537
538                 /* insert */
539                 if (haverow) {
540                         fprintf(stderr, "adding file data\n");
541                         rc = sqlite3_prepare(db, "update files set size = ?, content = ? where hash = ?", -1, &ifile,0);
542                 } else {
543                         fprintf(stderr, "creating new data row\n");
544                         rc = sqlite3_prepare(db, "insert into files (size, content, hash) values (?,?,?)", -1, &ifile,0);
545                 }
546                 if (rc != SQLITE_OK) {
547                         SQLERROR(sqlite3_errmsg(db));
548                         fprintf(stderr, "cant prepare data\n");
549                         zpm_rollback(pkg);
550                         return 0;
551                 }
552
553                 sqlite3_bind_int64(ifile, 1, (sqlite3_int64)sbuf.st_size);
554                 if (rc != SQLITE_OK) {
555                         SQLERROR(sqlite3_errmsg(db));
556                         fprintf(stderr, "cant bind size\n");
557                         zpm_rollback(pkg);
558                         return 0;
559                 }
560                 sqlite3_bind_blob64(ifile, 2, outbuf, (sqlite3_int64)outlen, SQLITE_STATIC);
561                 if (rc != SQLITE_OK) {
562                         SQLERROR(sqlite3_errmsg(db));
563                         fprintf(stderr, "cant bind content\n");
564                         zpm_rollback(pkg);
565                         return 0;
566                 }
567                 sqlite3_bind_text(ifile, 3, hash, 64, SQLITE_STATIC);
568                 if (rc != SQLITE_OK) {
569                         SQLERROR(sqlite3_errmsg(db));
570                         fprintf(stderr, "cant bind hash\n");
571                         zpm_rollback(pkg);
572                         return 0;
573                 }
574                 rc = sqlite3_step(ifile);
575                 if (rc != SQLITE_DONE) {
576                         SQLERROR(sqlite3_errmsg(db));
577                         sqlite3_finalize(ifile);
578                         zpm_rollback(pkg);
579                         return 0;
580                 }
581                 sqlite3_finalize(ifile);
582
583                 /* commit */
584                 //zpm_commit(pkg);
585
586         } else {
587                 /* don't need the original file now */
588                 munmap(content, sbuf.st_size);
589                 close(fd);
590         }
591
592
593         /* if package and not nopackage flag, add to package */
594         if (pkg->pkgname && (!ZPM_NOPACKAGE)) {
595                 /* TODO */
596         }
597
598         /* return */
599         return 1;
600 }
601 #endif
602
603 #if 0
604 int main(int ac, char **av){
605         sqlite3 *db = 0;
606         int rc;
607
608         int blobsize;
609         int64_t size;
610         void *xzdata;
611         int type;
612         FILE *out;
613         sqlite3_stmt *ifile;
614
615         char *hash;
616         char *filename;
617
618         if (ac < 3) {
619                 fprintf(stderr, "usage: db hash file\n");
620                 return 1;
621         }
622
623         rc = sqlite3_open(av[1], &db);
624         if (rc) {
625                 SQLERROR(sqlite3_errmsg(db));
626                 sqlite3_close(db);
627                 return 1;
628         }
629
630 }
631 #endif
632
633 #if 0
634 Packages are sqlite databases
635
636 get application id and userver
637
638 Primitive operations:
639
640 add path to repo
641 associate path with package
642 associate blob with path?
643 add blob to repo
644 * extract blob to a path
645 compare blob to filesystem path
646 create package with info
647
648 Extra primitives:
649
650 record elf information about blob
651 compress blob
652 uncompress blob
653 sign a package?  What are we verifying?
654 #endif