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