]> pd.if.org Git - zpackage/blob - lib/zpm.c
print reason for output error on extract
[zpackage] / lib / zpm.c
1 #define _POSIX_C_SOURCE 200809L
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <sys/mman.h>
9 #include <unistd.h>
10 #include <fcntl.h>
11 #include <errno.h>
12
13 #include "zpm.h"
14 #include "elf.h"
15
16 #include "sha256.h"
17
18 #if 0
19 struct zpm {
20         sqlite3 *db;
21         char *path; /* path to package file */
22         char *version;
23         int release;
24         char *pkgname;
25         time_t installed; /* install time, 0 for not installed */
26 };
27
28 struct zpm_file {
29         char *path;
30         int mode;
31         uint32_t filetype;
32         char *tags;
33         char *owner;
34         char *group;
35         char *hash; /* could be fixed length */
36         time_t mtime;
37         struct zpm_file *next; /* so you can make a linked list */
38 };
39
40 /* NULL?  Create? */
41 /* associate with a package ? if only one?  first? */
42 int zpm_open(struct zpm *pkg, char *path);
43 int zpm_pkgname(char *base, char *version, int release); /* construct a package file name */
44
45 /* flags for preserving mode, owner, etc */
46 /* puts hash of import in hash */
47 /* path can be a hash, with an "INTERNAL" flag, i.e. internally import */
48 #define ZPM_MODE 0x1
49 #define ZPM_OWNER 0x2
50 #define ZPM_MTIME 0x4
51 #define ZPM_INTERNAL 0x8
52 #define ZPM_NOBLOB 0x10
53 /* don't run scripts on install */
54 #define ZPM_NOSCRIPTS 0x10
55 /* don't associate the file with a package, just do a raw insert */
56 /* otherwise, associate it with the current package */
57 #define ZPM_NOPACKAGE 0x20
58
59 int zpm_import(struct zpm *zp, char *path, uint32_t flags, uint8_t *hash);
60
61 /* link and unlink hashes to packages */
62 int zpm_link(struct zpm *pkg, char *path, char *hash, struct zpm_file *fileinfo);
63 int zpm_unlink(struct zpm *pkg, char *path);
64
65 /* tag a file.  relative to "current package" */
66 int zpm_tag(struct zpm *zp, char *path, char *tags);
67 /* should this be broken up into separage functions ? */
68 int zpm_md(struct zpm *zp, char *path, int mode, char *owner, char *group, time_t mtime);
69
70 /* export hash to dest */
71 int zpm_extract(struct zpm *pkg, char *hash, char *path, int mode);
72
73 /* export path to dest */
74 int zpm_export(struct zpm *zp, char *path, uint32_t flags, char *dest);
75
76 int zpm_close(struct zpm *zp);
77
78 /* attach a signature to a package */
79 int zpm_sign(struct zpm *z, size_t s, void *signature);
80
81 /* set the package info to the nth package, -1 to return count? */
82 /* further import/exports and such will be relative to this package */
83 int zpm_package(struct zpm *zp, int n);
84
85 /* get file information */
86 int zpm_stat(struct zpm *z, struct zpm_file *f, int n);
87
88 /* will also set the package context to the new package */
89 int zpm_newpkg(struct zpm *z, char *base, char *version, int release);
90
91 /* transactions */
92 int zpm_begin(struct zpm *z);
93 int zpm_commit(struct zpm *z);
94 int zpm_rollback(struct zpm *z);
95
96 /* higher level operations */
97
98 /* install or uninstall the package */
99 /* flag for keeping the blobs in local */
100 /* what about tag filtering */
101 int zpm_install(struct zpm *z, struct zpm *local, uint32_t flags);
102 int zpm_uninstall(struct zpm *local);
103
104 /* slurp up the actual blobs */
105 /* what about versioning them if they change */
106 int zpm_preserve(struct zpm *local);
107
108 /* check file integrity */
109 int zpm_checkinstall(struct zpm *local);
110
111 int zpm_merge(struct zpm *z, struct zpm *src, uint32_t flags);
112
113 void uncompresslzma(void *buf, size_t bufsize, FILE *out);
114 #define SQLERROR(x) fprintf(stderr, "%s %d: %s\n", __func__, __LINE__, (x))
115 #endif
116
117 #if 0
118 int zpm_newpkg(struct zpm *z, char *base, char *version, int release) {
119         char *sql = "insert or ignore into packages (package,version,release) values (?,?,?)";
120         int rc;
121         sqlite3_stmt *ifile;
122
123         rc = sqlite3_prepare(db, sql, -1, &ifile,0);
124         if (rc != SQLITE_OK) {
125                 SQLERROR(sqlite3_errmsg(db));
126                 return 0;
127         }
128         rc = sqlite3_bind_text(ifile, 1, base, strlen(base), SQLITE_STATIC);
129         if (rc != SQLITE_OK) {
130                 SQLERROR(sqlite3_errmsg(db));
131                 fprintf(stderr, "cant bind package name\n");
132                 zpm_rollback(pkg);
133                 return 0;
134         }
135         sqlite3_bind_text(ifile, 2, version, strlen(version), SQLITE_STATIC);
136         sqlite3_bind_int(ifile, 3, release)
137
138         rc = sqlite3_step(ifile);
139
140         if (rc != SQLITE_DONE) {
141                 SQLERROR(sqlite3_errmsg(db));
142                 sqlite3_finalize(ifile);
143                 return 0;
144         }
145         sqlite3_finalize(ifile);
146         z->pkg = dupstr(base);
147         z->version = dupstr(version);
148         z->release = release;
149 }
150 #endif
151
152 int zpm_begin(struct zpm *z) {
153         char *errstr = 0;
154         sqlite3_exec(z->db, "begin;", NULL, NULL, &errstr);
155         if (errstr) {
156                 fprintf(stderr, "sqlite begin error: %s\n", errstr);
157                 sqlite3_free(errstr);
158                 return 0;
159         }
160         return 1;
161 }
162
163 int zpm_commit(struct zpm *z) {
164         char *errstr = 0;
165         sqlite3_exec(z->db, "commit;", NULL, NULL, &errstr);
166         if (errstr) {
167                 fprintf(stderr, "sqlite commit error: %s\n", errstr);
168                 sqlite3_free(errstr);
169                 return 0;
170         }
171         return 1;
172 }
173
174 /* wrapper for sqlite3_exec */
175 int zpm_exec(struct zpm *z, const char *sql, int(*callback)(void *, int, char **, char**), void *arg, char **errmsg) {
176         return sqlite3_exec(z->db, sql, callback, arg, errmsg);
177 }
178
179 int zpm_rollback(struct zpm *z) {
180         char *errstr = 0;
181         sqlite3_exec(z->db, "rollback;", NULL, NULL, &errstr);
182         if (errstr) {
183                 fprintf(stderr, "sqlite rollback error: %s\n", errstr);
184                 sqlite3_free(errstr);
185                 return 0;
186         }
187         return 1;
188 }
189
190 int zpm_db_set_pragma(struct zpm *db, int pragma, int value) {
191         int rc;
192         char *sql;
193         sqlite3_stmt *s;
194
195         switch (pragma) {
196                 case 1: sql = "pragma application_id = ?;"; break;
197                 case 2: sql = "pragma user_version = ?;"; break;
198                 default: return -1; break;
199         }
200
201         rc = sqlite3_prepare_v2(db->db, sql, -1, &s, 0);
202
203         if (rc != SQLITE_OK) {
204                 SQLERROR(sqlite3_errmsg(db->db));
205                 return -1;
206         }
207
208         sqlite3_bind_int(s, 1, value);
209         if (rc != SQLITE_OK) {
210                 SQLERROR(sqlite3_errmsg(db->db));
211                 fprintf(stderr, "cant bind pragma value\n");
212                 sqlite3_finalize(s);
213                 return -1;
214         }
215         rc = sqlite3_step(s);
216         if (rc != SQLITE_DONE) {
217                 SQLERROR(sqlite3_errmsg(db->db));
218                 fprintf(stderr, "cant set pragma\n");
219                 sqlite3_finalize(s);
220                 return -1;
221         }
222
223         sqlite3_finalize(s);
224         return value;
225 }
226
227 int zpm_db_pragma(struct zpm *db, int pragma) {
228         int rc;
229         int value = -1;
230         char *sql = 0;
231         sqlite3_stmt *s;
232
233         switch (pragma) {
234                 case 1: sql = "pragma application_id;"; break;
235                 case 2: sql = "pragma user_version;"; break;
236                 default: return -1; break;
237         }
238
239         rc = sqlite3_prepare_v2(db->db, sql, -1, &s, 0);
240
241         if (rc != SQLITE_OK) {
242                 SQLERROR(sqlite3_errmsg(db->db));
243                 fprintf(stderr, "%s, errnum = %d\n", sqlite3_errmsg(db->db), rc);
244                 /* TODO just abort? */
245                 return -1;
246         }
247
248         rc = sqlite3_step(s);
249         if (rc == SQLITE_ROW) {
250                 value = sqlite3_column_int(s, 0);
251         }
252
253         sqlite3_finalize(s);
254         return value;
255 }
256
257 static
258 #include "newdb.c"
259
260 int zpm_db_initialize(struct zpm *pkg) {
261         char *error;
262         switch (sqlite3_exec(pkg->db, createdb, (int (*)(void *,int,char **,char **))0, NULL, &error)) {
263                 case SQLITE_OK: break;
264                 default:
265                         SQLERROR(sqlite3_errmsg(pkg->db));
266                         fprintf(stderr, "error: %s\n", error);
267                         sqlite3_free(error);
268                         zpm_rollback(pkg);
269                         return 0;
270                         break;
271         }
272         return 1;
273 }
274
275
276 /* assumes pkg->db is set */
277 static int setupdb(struct zpm *zpm) {
278         char *errstr = 0;
279         int appid, dbver;
280
281         zpm->error = 0;
282
283         appid = zpm_db_pragma(zpm, 1);
284         dbver = zpm_db_pragma(zpm, 2);
285
286         if (appid != 0x5a504442) {
287                 fprintf(stderr, "unknown database type\n");
288                 zpm->error = 1;
289                 return 0;
290         }
291
292         if (dbver > 1) {
293                 fprintf(stderr, "version %d zpm db detected, this program only works with version 1 databases\n", dbver);
294                 zpm->error = 1;
295                 return 0;
296         }
297
298         sqlite3_exec(zpm->db, "pragma foreign_keys = ON;", NULL, NULL, &errstr);
299
300         if (errstr) {
301                 free(zpm->errmsg);
302                 zpm->errmsg = strdup(errstr);
303                 fprintf(stderr, "sqlite foreign key error: %s\n", errstr);
304                 sqlite3_free(errstr);
305                 zpm->error = 1;
306                 return 0;
307         }
308
309         /* TODO add vercmp */
310
311         return 1;
312 }
313
314 struct zpm *zpm_clearmem(struct zpm *zpm) {
315         if (!zpm) {
316                 zpm = malloc(sizeof *zpm);
317         }
318
319         if (zpm) {
320                 *zpm = (struct zpm){0};
321         }
322
323         return zpm;
324 }
325
326 static void zpm_set_db_errmsg(struct zpm *zpm, const char *msg) {
327         if (zpm) {
328                 if (zpm->dberrmsg) {
329                         free(zpm->dberrmsg);
330                 }
331                 if (msg) {
332                         zpm->dberrmsg = strdup(msg);
333                         if (!zpm->dberrmsg) {
334                                 zpm->error = 1;
335                         }
336                 } else {
337                         zpm->dberrmsg = 0;
338                 }
339         }
340 }
341
342 int zpm_init(struct zpm *pkg, char *path) {
343         int rc;
344         sqlite3 *db = 0;
345         int appid;
346
347         if (!pkg) {
348                 return 0;
349         }
350
351         zpm_clearmem(pkg);
352
353         rc = sqlite3_open_v2(path, &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
354         if (rc) {
355                 pkg->error = 1;
356                 pkg->dbresult = rc;
357                 zpm_set_db_errmsg(pkg, sqlite3_errstr(rc));
358                 if (db) {
359                         sqlite3_close(db);
360                 }
361                 fprintf(stderr, "error (%d): %s: %s\n", rc,
362                                 pkg->dberrmsg ? pkg->dberrmsg : "null", path);
363
364                 return 0;
365         }
366         pkg->db   = db;
367         pkg->path = strdup(path);
368
369         appid = zpm_db_pragma(pkg, 1);
370
371         switch (appid) {
372                 case 0: if (!zpm_db_initialize(pkg)) {
373                                 sqlite3_close(db);
374                                 return 1;
375                         };
376                         break;
377                 case 0x5a504442:
378                         /* already initialized */
379                         break;
380                 default:
381                         fprintf(stderr, "unknown database type\n");
382                         sqlite3_close(db);
383                         return 0;
384                         break;
385         }
386
387         if (!setupdb(pkg)) {
388                 sqlite3_close(db);
389                 pkg->db = 0;
390                 return 0;
391         }
392         zpm_addvercmp(pkg);
393
394         return 1;
395 }
396
397 int zpm_open(struct zpm *zpm, char *path) {
398         int rc;
399         sqlite3 *db = 0;
400
401         zpm_clearmem(zpm);
402
403         rc = sqlite3_open_v2(path, &db, SQLITE_OPEN_READWRITE, NULL);
404         if (rc) {
405                 SQLERROR(sqlite3_errmsg(db));
406                 sqlite3_close(db);
407                 zpm->error = 1;
408                 fprintf(stderr, "path = %s\n", path);
409                 return 0;
410         }
411         zpm->db   = db;
412         zpm->path = strdup(path);
413
414         if (!setupdb(zpm)) {
415                 sqlite3_close(db);
416                 zpm->db = 0;
417                 zpm->error = 1;
418                 return 0;
419         }
420         zpm_addvercmp(zpm);
421
422         return 1;
423 }
424
425 int zpm_close(struct zpm *pkg) {
426         if (pkg) {
427                 sqlite3_close(pkg->db);
428                 free(pkg->path);
429         }
430         /* TODO free any malloced names and such */
431         return 1;
432 }
433
434 static int zpm_sqlite_vercmp(void *not_used, int lena, const void *a,
435                 int lenb, const void *b) {
436         /* not sure what the ints are, possibly string lengths */
437         if (not_used != 0) fprintf(stderr, "sqlite vercmp not_used = %p\n",
438                         not_used);
439         if (lena == 0 && lenb > 0) return 1;
440         return zpm_vercmp(a, b);
441 }
442
443 int zpm_addvercmp(struct zpm *pkg) {
444         return sqlite3_create_collation(
445                         pkg->db, "vercmp", SQLITE_UTF8, NULL,
446                         zpm_sqlite_vercmp
447                         );
448 }
449
450 int zpm_extract(struct zpm *pkg, char *hash, char *path, int mode) {
451         int rc;
452
453         int blobsize;
454         //int64_t size;
455         void *xzdata;
456         int type;
457         FILE *out;
458         sqlite3_stmt *ifile;
459
460         /* TODO check null */
461         sqlite3 *db = pkg->db;
462
463         rc = sqlite3_prepare(db, "select size, content from files where hash = ?", -1, &ifile,0);
464         if (rc != SQLITE_OK) {
465                 SQLERROR(sqlite3_errmsg(db));
466                 return 0;
467         }
468
469         /* hash, filename */
470
471         sqlite3_bind_text(ifile, 1, hash, 64, SQLITE_STATIC);
472
473         rc = sqlite3_step(ifile);
474
475         if (rc == SQLITE_DONE) {
476                 /* didn't find a row */
477                 sqlite3_finalize(ifile);
478                 sqlite3_close(db);
479                 fprintf(stderr, "no such hash: %s\n", hash);
480                 return 0;
481         }
482         /* either way we're done with this now */
483
484         if (rc != SQLITE_ROW) {
485                 SQLERROR(sqlite3_errmsg(db));
486                 sqlite3_finalize(ifile);
487                 sqlite3_close(db);
488                 return 0;
489         }
490
491         type = sqlite3_column_type(ifile, 0);
492         if (type == SQLITE_NULL) {
493                 fprintf(stderr, "no file size\n");
494                 sqlite3_finalize(ifile);
495                 sqlite3_close(db);
496                 return 0;
497         }
498         type = sqlite3_column_type(ifile, 1);
499         if (type == SQLITE_NULL) {
500                 fprintf(stderr, "no file data\n");
501                 sqlite3_finalize(ifile);
502                 sqlite3_close(db);
503                 return 0;
504         }
505         //size = sqlite3_column_int64(ifile, 0);
506         xzdata = (void *)sqlite3_column_blob(ifile, 1);
507         blobsize = sqlite3_column_bytes(ifile, 1);
508
509         if (strcmp(path, "-")) {
510                 out = fopen(path, "w");
511         } else {
512                 out = stdout;
513         }
514         if (!out) {
515                 fprintf(stderr, "can't open output file %s: %s\n", path,
516                                 strerror(errno));
517                 sqlite3_finalize(ifile);
518                 sqlite3_close(db);
519                 return 0;
520         }
521         //fwrite(xzdata, blobsize, 1, stdout);
522
523         //fprintf(stderr, "uncompressing %d bytes at %p, expect %lld\n", blobsize, xzdata, (long long int)size);
524         uncompresslzma(xzdata, blobsize, out);
525         fclose(out);
526         chmod(path, mode);
527
528         sqlite3_finalize(ifile);
529
530         return 1;
531 }
532
533 static int run_for_hash(sqlite3 *db, char *sql, char *hash) {
534         int rc;
535         sqlite3_stmt *ifile;
536
537         rc = sqlite3_prepare_v2(db, sql, -1, &ifile, 0);
538         if (rc != SQLITE_OK) {
539                 SQLERROR(sqlite3_errmsg(db));
540                 return 0;
541         }
542
543         /* hash, filename */
544
545         sqlite3_bind_text(ifile, 1, hash, 64, SQLITE_STATIC);
546         do {
547                 rc = sqlite3_step(ifile);
548         } while (rc == SQLITE_ROW);
549
550         sqlite3_finalize(ifile);
551
552         return rc == SQLITE_DONE;
553 }
554
555 #define SQLERP(db, msg) fprintf(stderr, "%s: %s\n", msg, sqlite3_errmsg(db))
556
557 static int set_elf_info(sqlite3 *db, char *hash, char *content, size_t length) {
558         if (length >= sizeof (Elf64_Ehdr) && libelf_iself(content)) {
559                 char *strtab;
560                 Elf64_Dyn *dyn;
561                 int i;
562                 Elf64_Phdr *phdr;
563                 Elf64_Ehdr *hdr;
564                 sqlite3_stmt *ifile;
565                 int rc;
566
567                 /* clear existing for this hash */
568
569                 if (!run_for_hash(db, "delete from elflibraries where file = ?", hash)) {
570                         SQLERP(db, "error clearing elf library");
571                         return 0;
572                 }
573
574                 if (!run_for_hash(db, "delete from elfneeded where file = ?", hash)) {
575                         SQLERP(db, "error clearing elf needed");
576                         return 0;
577                 }
578
579                 hdr = libelf_header(content);
580                 /* if lib, set soname */
581                 if (libelf_type(content) == ET_DYN) {
582                         char *soname = libelf_soname(content);
583                         if (soname) {
584
585                                 sqlite3_prepare_v2(db, "insert into elflibraries (file,soname) values (?,?)",-1, &ifile, 0);
586                                 sqlite3_bind_text(ifile,1,hash,64,SQLITE_STATIC);
587                                 sqlite3_bind_text(ifile,2,soname,-1,SQLITE_STATIC);
588                                 rc = sqlite3_step(ifile);
589                                 if (rc != SQLITE_DONE) {
590                                         SQLERROR(sqlite3_errmsg(db));
591                                         sqlite3_finalize(ifile);
592                                         fprintf(stderr, "error setting library soname\n");
593                                         return 0;
594                                 }
595                                 sqlite3_finalize(ifile);
596                         } else {
597                                 fprintf(stderr, "can't find soname\n");
598                         }
599                 }
600
601                 /* if exe, set neededs */
602                 if (libelf_type(content) == ET_EXEC) {
603                         Elf64_Shdr *dsect = 0;
604                         char *elf;
605
606                         elf = (char *)content;
607                         /* find program header table */
608                         for (i = 0; i < hdr->e_phnum; i++) {
609                                 phdr = (Elf64_Phdr *)(elf + hdr->e_phoff + i * hdr->e_phentsize);
610                                 if (phdr->p_type == PT_DYNAMIC) {
611                                         dsect = (Elf64_Shdr *)(elf + phdr->p_offset);
612                                 }
613                         }
614                         if (!dsect) {
615                                 /* no dynamic section found */
616                                 return 1;
617                         }
618
619 #if 0
620                         dyn = (Elf64_Dyn *)(elf + dsect->sh_offset);
621                         if (!dyn) {
622                                 exit(9);
623                         }
624 #endif
625                         dyn = (Elf64_Dyn *)dsect;
626
627                         dsect = libelf_section(elf, SHT_DYNAMIC);
628                         Elf64_Shdr *strsect;
629
630                         strsect = libelf_section_n(elf, dsect->sh_link);
631                         strtab = elf + strsect->sh_offset;
632
633                         sqlite3_prepare_v2(db, "insert into elfneeded (file,needed) values (?,?)",-1, &ifile, 0);
634                         sqlite3_bind_text(ifile,1,hash,64,SQLITE_STATIC);
635                         while (dyn->d_tag != DT_NULL) {
636                                 if (dyn->d_tag == DT_NEEDED) {
637                                         char *need;
638                                         int rc;
639
640                                         need = strtab + dyn->d_un.d_val;
641                                         if (strlen(need) == 0) continue;
642                                         sqlite3_bind_text(ifile,2,need,strlen(need),SQLITE_STATIC);
643 #if 0
644                                         fprintf(stderr, "%s needs %s\n", hash, need);
645 #endif
646                                         rc = sqlite3_step(ifile);
647                                         if (rc != SQLITE_DONE) {
648                                                 SQLERP(db, "error setting needed library");
649                                                 sqlite3_finalize(ifile);
650                                                 return 0;
651                                         }
652                                         sqlite3_reset(ifile);
653                                 }
654                                 dyn++;
655                         }
656                         sqlite3_finalize(ifile);
657                 }
658         }
659         return 1;
660 }
661
662 int zpm_import(struct zpm *zpm, char *path, uint32_t flags, char *hash) {
663         int fd;
664         void *content = 0;
665         struct stat sbuf;
666         unsigned char tmp[32];
667         struct sha256_state md;
668         sqlite3_stmt *ifile = 0;
669         int haverow = 0,havedata = 0;
670         int j,rc,type;
671         char hashbuf[65];
672
673         /* xz compress it */
674         size_t outlen = 0;
675         void *outbuf;
676
677         if (!zpm || !zpm->db || !path) {
678                 return 0;
679         }
680
681         /* use local if caller didn't pass in room */
682         if (!hash) {
683                 hash = hashbuf;
684         }
685
686         if (flags) {
687                 fprintf(stderr, "zpm_import unused flags = %d\n", flags);
688         }
689         /* mmap the file */
690         fd = open(path, O_RDONLY);
691         if (fd == -1) {
692                 zpm->error = errno;
693                 fprintf(stderr, "%s can't open %s: %s\n", __FUNCTION__, path,strerror(errno));
694                 return 0;
695         }
696         if (fstat(fd, &sbuf) == -1) {
697                 zpm->error = errno;
698                 fprintf(stderr, "%s can't fstat %s: %s\n", __FUNCTION__, path,strerror(errno));
699                 return 0;
700         }
701         /* not a regular file? */
702         if (!S_ISREG(sbuf.st_mode)) {
703                 char *ftype;
704                 switch (sbuf.st_mode & S_IFMT) {
705                         case S_IFSOCK: ftype = "socket"; break;
706                         case S_IFLNK : ftype = "symlink"; break;
707                         case S_IFBLK : ftype = "block device"; break;
708                         case S_IFDIR : ftype = "directory"; break;
709                         case S_IFCHR : ftype = "character device"; break;
710                         case S_IFIFO : ftype = "fifo"; break;
711                         default: ftype = "unknown file type"; break;
712                 }
713                 /* TODO this is ok, just stored differently */
714                 fprintf(stderr, "%s can't import %s file: %s\n", __FUNCTION__, ftype, path);
715                 zpm->error = EINVAL;
716                 return 0;
717         }
718
719         content = mmap(0, sbuf.st_size, PROT_READ,MAP_PRIVATE, fd, 0);
720         close(fd);
721         if (!content) {
722                 zpm->error = errno;
723                 fprintf(stderr, "%s can't mmap %s: %s\n", __FUNCTION__, path,strerror(errno));
724                 return 0;
725         }
726
727         /* get hash */
728         sha256_init(&md);
729         sha256_process(&md, content, sbuf.st_size);
730         sha256_done(&md, tmp);
731         for (j=0;j<32;j++) {
732                 sprintf(hash+j*2, "%02x", (unsigned)tmp[j]);
733         }
734         hash[64] = 0;
735
736         /* TODO check null */
737         sqlite3 *db = zpm->db;
738
739         /* prepare and bind */
740
741         rc = sqlite3_prepare_v2(db, "select size, content is not null from files where hash = ?", -1, &ifile,0);
742         if (rc != SQLITE_OK) {
743                 SQLERROR(sqlite3_errmsg(db));
744                 munmap(content, sbuf.st_size);
745                 return 0;
746         }
747
748         /* hash, filename */
749
750         sqlite3_bind_text(ifile, 1, hash, 64, SQLITE_STATIC);
751
752         rc = sqlite3_step(ifile);
753
754         if (rc != SQLITE_DONE) {
755                 if (rc != SQLITE_ROW) {
756                         /* didn't find a row */
757                         SQLERROR(sqlite3_errmsg(db));
758                 munmap(content, sbuf.st_size);
759                         return 0;
760                 }
761                 haverow = 1;
762                 type = sqlite3_column_type(ifile, 0);
763                 if (type == SQLITE_NULL) {
764                         /* TODO assert, this shouldn't be possible? */
765                         fprintf(stderr, "no file size\n");
766                         sqlite3_finalize(ifile);
767                 munmap(content, sbuf.st_size);
768                         return 0;
769                 }
770                 type = sqlite3_column_type(ifile, 1);
771                 if (type == SQLITE_NULL) {
772                         /* TODO assert, this shouldn't be possible? */
773                         fprintf(stderr, "no file data\n");
774                         sqlite3_finalize(ifile);
775                 munmap(content, sbuf.st_size);
776                         return 0;
777                         /* which is fine, just need to update the row then */
778                 }
779                 havedata = sqlite3_column_int(ifile, 1);
780         }
781
782         sqlite3_finalize(ifile);
783
784         if (!havedata) {
785                 /* compress */
786                 outbuf = compresslzma(content, sbuf.st_size, &outlen);
787                 if (!outbuf) {
788                         fprintf(stderr, "compresslzma failed\n");
789                         munmap(content, sbuf.st_size);
790                         return 0;
791                 }
792
793                 /* insert */
794                 if (haverow) {
795                         rc = sqlite3_prepare_v2(db, "update files set size = ?, content = ? where hash = ?", -1, &ifile,0);
796                 } else {
797                         rc = sqlite3_prepare_v2(db, "insert into files (size, content, hash) values (?,?,?)", -1, &ifile,0);
798                 }
799
800                 if (rc != SQLITE_OK) {
801                         SQLERROR(sqlite3_errmsg(db));
802                         fprintf(stderr, "cant prepare data\n");
803                         munmap(content, sbuf.st_size);
804                         return 0;
805                 }
806
807                 rc = sqlite3_bind_int64(ifile, 1, (sqlite3_int64)sbuf.st_size);
808                 if (rc != SQLITE_OK) {
809                         SQLERROR(sqlite3_errmsg(db));
810                         sqlite3_finalize(ifile);
811                         fprintf(stderr, "cant bind size\n");
812                         munmap(content, sbuf.st_size);
813                         return 0;
814                 }
815
816                 rc = sqlite3_bind_blob64(ifile, 2, outbuf,
817                                 (sqlite3_int64)outlen, SQLITE_STATIC);
818                 if (rc != SQLITE_OK) {
819                         SQLERROR(sqlite3_errmsg(db));
820                         sqlite3_finalize(ifile);
821                         fprintf(stderr, "cant bind content\n");
822                         munmap(content, sbuf.st_size);
823                         return 0;
824                 }
825
826                 rc = sqlite3_bind_text(ifile, 3, hash, 64, SQLITE_STATIC);
827                 if (rc != SQLITE_OK) {
828                         SQLERROR(sqlite3_errmsg(db));
829                         fprintf(stderr, "cant bind hash\n");
830                         sqlite3_finalize(ifile);
831                         munmap(content, sbuf.st_size);
832                         return 0;
833                 }
834
835                 rc = sqlite3_step(ifile);
836                 if (rc != SQLITE_DONE) {
837                         SQLERROR(sqlite3_errmsg(db));
838                         sqlite3_finalize(ifile);
839                         munmap(content, sbuf.st_size);
840                         return 0;
841                 }
842                 sqlite3_finalize(ifile);
843
844                 /* don't need the original file now */
845         }
846
847         if (!set_elf_info(zpm->db, hash, content, sbuf.st_size)) {
848                 fprintf(stderr, "setting elf info failed\n");
849                 munmap(content, sbuf.st_size);
850                 return 0;
851         }
852
853         munmap(content, sbuf.st_size);
854
855         /* return */
856         return 1;
857 }