]> pd.if.org Git - zpackage/blob - lib/zpm.c
remove unused elf tables, add elf summary view
[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                 }
337         }
338 }
339
340 int zpm_init(struct zpm *pkg, char *path) {
341         int rc;
342         sqlite3 *db = 0;
343         int appid;
344
345         if (!pkg) {
346                 return 0;
347         }
348
349         zpm_clearmem(pkg);
350
351         rc = sqlite3_open_v2(path, &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
352         if (rc) {
353                 pkg->error = 1;
354                 pkg->dbresult = rc;
355                 zpm_set_db_errmsg(pkg, sqlite3_errstr(rc));
356                 if (db) {
357                         sqlite3_close(db);
358                 }
359                 fprintf(stderr, "error (%d): %s: %s\n", rc,
360                                 pkg->dberrmsg, path);
361
362                 return 0;
363         }
364         pkg->db   = db;
365         pkg->path = strdup(path);
366
367         appid = zpm_db_pragma(pkg, 1);
368
369         switch (appid) {
370                 case 0: if (!zpm_db_initialize(pkg)) {
371                                 sqlite3_close(db);
372                                 return 1;
373                         };
374                         break;
375                 case 0x5a504442:
376                         /* already initialized */
377                         break;
378                 default:
379                         fprintf(stderr, "unknown database type\n");
380                         sqlite3_close(db);
381                         return 0;
382                         break;
383         }
384
385         if (!setupdb(pkg)) {
386                 sqlite3_close(db);
387                 pkg->db = 0;
388                 return 0;
389         }
390         zpm_addvercmp(pkg);
391
392         return 1;
393 }
394
395 int zpm_open(struct zpm *zpm, char *path) {
396         int rc;
397         sqlite3 *db = 0;
398
399         zpm_clearmem(zpm);
400
401         rc = sqlite3_open_v2(path, &db, SQLITE_OPEN_READWRITE, NULL);
402         if (rc) {
403                 SQLERROR(sqlite3_errmsg(db));
404                 sqlite3_close(db);
405                 zpm->error = 1;
406                 fprintf(stderr, "path = %s\n", path);
407                 return 0;
408         }
409         zpm->db   = db;
410         zpm->path = strdup(path);
411
412         if (!setupdb(zpm)) {
413                 sqlite3_close(db);
414                 zpm->db = 0;
415                 zpm->error = 1;
416                 return 0;
417         }
418         zpm_addvercmp(zpm);
419
420         return 1;
421 }
422
423 int zpm_close(struct zpm *pkg) {
424         if (pkg) {
425                 sqlite3_close(pkg->db);
426                 free(pkg->path);
427         }
428         /* TODO free any malloced names and such */
429         return 1;
430 }
431
432 static int zpm_sqlite_vercmp(void *not_used, int lena, const void *a,
433                 int lenb, const void *b) {
434         /* not sure what the ints are, possibly string lengths */
435         if (not_used != 0) fprintf(stderr, "sqlite vercmp not_used = %p\n",
436                         not_used);
437         if (lena == 0 && lenb > 0) return 1;
438         return zpm_vercmp(a, b);
439 }
440
441 int zpm_addvercmp(struct zpm *pkg) {
442         return sqlite3_create_collation(
443                         pkg->db, "vercmp", SQLITE_UTF8, NULL,
444                         zpm_sqlite_vercmp
445                         );
446 }
447
448 int zpm_extract(struct zpm *pkg, char *hash, char *path, int mode) {
449         int rc;
450
451         int blobsize;
452         //int64_t size;
453         void *xzdata;
454         int type;
455         FILE *out;
456         sqlite3_stmt *ifile;
457
458         /* TODO check null */
459         sqlite3 *db = pkg->db;
460
461         rc = sqlite3_prepare(db, "select size, content from files where hash = ?", -1, &ifile,0);
462         if (rc != SQLITE_OK) {
463                 SQLERROR(sqlite3_errmsg(db));
464                 return 0;
465         }
466
467         /* hash, filename */
468
469         sqlite3_bind_text(ifile, 1, hash, 64, SQLITE_STATIC);
470
471         rc = sqlite3_step(ifile);
472
473         if (rc == SQLITE_DONE) {
474                 /* didn't find a row */
475                 sqlite3_finalize(ifile);
476                 sqlite3_close(db);
477                 fprintf(stderr, "no such hash: %s\n", hash);
478                 return 0;
479         }
480         /* either way we're done with this now */
481
482         if (rc != SQLITE_ROW) {
483                 SQLERROR(sqlite3_errmsg(db));
484                 sqlite3_finalize(ifile);
485                 sqlite3_close(db);
486                 return 0;
487         }
488
489         type = sqlite3_column_type(ifile, 0);
490         if (type == SQLITE_NULL) {
491                 fprintf(stderr, "no file size\n");
492                 sqlite3_finalize(ifile);
493                 sqlite3_close(db);
494                 return 0;
495         }
496         type = sqlite3_column_type(ifile, 1);
497         if (type == SQLITE_NULL) {
498                 fprintf(stderr, "no file data\n");
499                 sqlite3_finalize(ifile);
500                 sqlite3_close(db);
501                 return 0;
502         }
503         //size = sqlite3_column_int64(ifile, 0);
504         xzdata = (void *)sqlite3_column_blob(ifile, 1);
505         blobsize = sqlite3_column_bytes(ifile, 1);
506
507         if (strcmp(path, "-")) {
508                 out = fopen(path, "w");
509         } else {
510                 out = stdout;
511         }
512         if (!out) {
513                 fprintf(stderr, "can't open output file %s\n", path);
514                 sqlite3_finalize(ifile);
515                 sqlite3_close(db);
516                 return 0;
517         }
518         //fwrite(xzdata, blobsize, 1, stdout);
519
520         //fprintf(stderr, "uncompressing %d bytes at %p, expect %lld\n", blobsize, xzdata, (long long int)size);
521         uncompresslzma(xzdata, blobsize, out);
522         fclose(out);
523         chmod(path, mode);
524
525         sqlite3_finalize(ifile);
526
527         return 1;
528 }
529
530 static int run_for_hash(sqlite3 *db, char *sql, char *hash) {
531         int rc;
532         sqlite3_stmt *ifile;
533
534         rc = sqlite3_prepare_v2(db, sql, -1, &ifile, 0);
535         if (rc != SQLITE_OK) {
536                 SQLERROR(sqlite3_errmsg(db));
537                 return 0;
538         }
539
540         /* hash, filename */
541
542         sqlite3_bind_text(ifile, 1, hash, 64, SQLITE_STATIC);
543         do {
544                 rc = sqlite3_step(ifile);
545         } while (rc == SQLITE_ROW);
546
547         sqlite3_finalize(ifile);
548
549         return rc == SQLITE_DONE;
550 }
551
552 #define SQLERP(db, msg) fprintf(stderr, "%s: %s\n", msg, sqlite3_errmsg(db))
553
554 static int set_elf_info(sqlite3 *db, char *hash, char *content, size_t length) {
555         if (length >= sizeof (Elf64_Ehdr) && libelf_iself(content)) {
556                 char *strtab;
557                 Elf64_Dyn *dyn;
558                 int i;
559                 Elf64_Phdr *phdr;
560                 Elf64_Ehdr *hdr;
561                 sqlite3_stmt *ifile;
562                 int rc;
563
564                 /* clear existing for this hash */
565
566                 if (!run_for_hash(db, "delete from elflibraries where file = ?", hash)) {
567                         SQLERP(db, "error clearing elf library");
568                         return 0;
569                 }
570
571                 if (!run_for_hash(db, "delete from elfneeded where file = ?", hash)) {
572                         SQLERP(db, "error clearing elf needed");
573                         return 0;
574                 }
575
576                 hdr = libelf_header(content);
577                 /* if lib, set soname */
578                 if (libelf_type(content) == ET_DYN) {
579                         char *soname = libelf_soname(content);
580                         if (soname) {
581
582                                 sqlite3_prepare_v2(db, "insert into elflibraries (file,soname) values (?,?)",-1, &ifile, 0);
583                                 sqlite3_bind_text(ifile,1,hash,64,SQLITE_STATIC);
584                                 sqlite3_bind_text(ifile,2,soname,-1,SQLITE_STATIC);
585                                 rc = sqlite3_step(ifile);
586                                 if (rc != SQLITE_DONE) {
587                                         SQLERROR(sqlite3_errmsg(db));
588                                         sqlite3_finalize(ifile);
589                                         fprintf(stderr, "error setting library soname\n");
590                                         return 0;
591                                 }
592                                 sqlite3_finalize(ifile);
593                         } else {
594                                 fprintf(stderr, "can't find soname\n");
595                         }
596                 }
597
598                 /* if exe, set neededs */
599                 if (libelf_type(content) == ET_EXEC) {
600                         Elf64_Shdr *dsect = 0;
601                         char *elf;
602
603                         elf = (char *)content;
604                         /* find program header table */
605                         for (i = 0; i < hdr->e_phnum; i++) {
606                                 phdr = (Elf64_Phdr *)(elf + hdr->e_phoff + i * hdr->e_phentsize);
607                                 if (phdr->p_type == PT_DYNAMIC) {
608                                         dsect = (Elf64_Shdr *)(elf + phdr->p_offset);
609                                 }
610                         }
611                         if (!dsect) {
612                                 /* no dynamic section found */
613                                 return 1;
614                         }
615
616 #if 0
617                         dyn = (Elf64_Dyn *)(elf + dsect->sh_offset);
618                         if (!dyn) {
619                                 exit(9);
620                         }
621 #endif
622                         dyn = (Elf64_Dyn *)dsect;
623
624                         dsect = libelf_section(elf, SHT_DYNAMIC);
625                         Elf64_Shdr *strsect;
626
627                         strsect = libelf_section_n(elf, dsect->sh_link);
628                         strtab = elf + strsect->sh_offset;
629
630                         sqlite3_prepare_v2(db, "insert into elfneeded (file,needed) values (?,?)",-1, &ifile, 0);
631                         sqlite3_bind_text(ifile,1,hash,64,SQLITE_STATIC);
632                         while (dyn->d_tag != DT_NULL) {
633                                 if (dyn->d_tag == DT_NEEDED) {
634                                         char *need;
635                                         int rc;
636
637                                         need = strtab + dyn->d_un.d_val;
638                                         if (strlen(need) == 0) continue;
639                                         sqlite3_bind_text(ifile,2,need,strlen(need),SQLITE_STATIC);
640 #if 0
641                                         fprintf(stderr, "%s needs %s\n", hash, need);
642 #endif
643                                         rc = sqlite3_step(ifile);
644                                         if (rc != SQLITE_DONE) {
645                                                 SQLERP(db, "error setting needed library");
646                                                 sqlite3_finalize(ifile);
647                                                 return 0;
648                                         }
649                                         sqlite3_reset(ifile);
650                                 }
651                                 dyn++;
652                         }
653                         sqlite3_finalize(ifile);
654                 }
655         }
656         return 1;
657 }
658
659 int zpm_import(struct zpm *zpm, char *path, uint32_t flags, char *hash) {
660         int fd;
661         void *content = 0;
662         struct stat sbuf;
663         unsigned char tmp[32];
664         struct sha256_state md;
665         sqlite3_stmt *ifile = 0;
666         int haverow = 0,havedata = 0;
667         int j,rc,type;
668         char hashbuf[65];
669
670         /* xz compress it */
671         size_t outlen = 0;
672         void *outbuf;
673
674         if (!zpm || !zpm->db || !path) {
675                 return 0;
676         }
677
678         /* use local if caller didn't pass in room */
679         if (!hash) {
680                 hash = hashbuf;
681         }
682
683         if (flags) {
684                 fprintf(stderr, "zpm_import unused flags = %d\n", flags);
685         }
686         /* mmap the file */
687         fd = open(path, O_RDONLY);
688         if (fd == -1) {
689                 zpm->error = errno;
690                 fprintf(stderr, "%s can't open %s: %s\n", __FUNCTION__, path,strerror(errno));
691                 return 0;
692         }
693         if (fstat(fd, &sbuf) == -1) {
694                 zpm->error = errno;
695                 fprintf(stderr, "%s can't fstat %s: %s\n", __FUNCTION__, path,strerror(errno));
696                 return 0;
697         }
698         /* not a regular file? */
699         if (!S_ISREG(sbuf.st_mode)) {
700                 char *ftype;
701                 switch (sbuf.st_mode & S_IFMT) {
702                         case S_IFSOCK: ftype = "socket"; break;
703                         case S_IFLNK : ftype = "symlink"; break;
704                         case S_IFBLK : ftype = "block device"; break;
705                         case S_IFDIR : ftype = "directory"; break;
706                         case S_IFCHR : ftype = "character device"; break;
707                         case S_IFIFO : ftype = "fifo"; break;
708                         default: ftype = "unknown file type"; break;
709                 }
710                 /* TODO this is ok, just stored differently */
711                 fprintf(stderr, "%s can't import %s file: %s\n", __FUNCTION__, ftype, path);
712                 zpm->error = EINVAL;
713                 return 0;
714         }
715
716         content = mmap(0, sbuf.st_size, PROT_READ,MAP_PRIVATE, fd, 0);
717         close(fd);
718         if (!content) {
719                 zpm->error = errno;
720                 fprintf(stderr, "%s can't mmap %s: %s\n", __FUNCTION__, path,strerror(errno));
721                 return 0;
722         }
723
724         /* get hash */
725         sha256_init(&md);
726         sha256_process(&md, content, sbuf.st_size);
727         sha256_done(&md, tmp);
728         for (j=0;j<32;j++) {
729                 sprintf(hash+j*2, "%02x", (unsigned)tmp[j]);
730         }
731         hash[64] = 0;
732
733         /* TODO check null */
734         sqlite3 *db = zpm->db;
735
736         /* prepare and bind */
737
738         rc = sqlite3_prepare_v2(db, "select size, content is not null from files where hash = ?", -1, &ifile,0);
739         if (rc != SQLITE_OK) {
740                 SQLERROR(sqlite3_errmsg(db));
741                 munmap(content, sbuf.st_size);
742                 return 0;
743         }
744
745         /* hash, filename */
746
747         sqlite3_bind_text(ifile, 1, hash, 64, SQLITE_STATIC);
748
749         rc = sqlite3_step(ifile);
750
751         if (rc != SQLITE_DONE) {
752                 if (rc != SQLITE_ROW) {
753                         /* didn't find a row */
754                         SQLERROR(sqlite3_errmsg(db));
755                 munmap(content, sbuf.st_size);
756                         return 0;
757                 }
758                 haverow = 1;
759                 type = sqlite3_column_type(ifile, 0);
760                 if (type == SQLITE_NULL) {
761                         /* TODO assert, this shouldn't be possible? */
762                         fprintf(stderr, "no file size\n");
763                         sqlite3_finalize(ifile);
764                 munmap(content, sbuf.st_size);
765                         return 0;
766                 }
767                 type = sqlite3_column_type(ifile, 1);
768                 if (type == SQLITE_NULL) {
769                         /* TODO assert, this shouldn't be possible? */
770                         fprintf(stderr, "no file data\n");
771                         sqlite3_finalize(ifile);
772                 munmap(content, sbuf.st_size);
773                         return 0;
774                         /* which is fine, just need to update the row then */
775                 }
776                 havedata = sqlite3_column_int(ifile, 1);
777         }
778
779         sqlite3_finalize(ifile);
780
781         if (!havedata) {
782                 /* compress */
783                 outbuf = compresslzma(content, sbuf.st_size, &outlen);
784                 if (!outbuf) {
785                         fprintf(stderr, "compresslzma failed\n");
786                         munmap(content, sbuf.st_size);
787                         return 0;
788                 }
789
790                 /* insert */
791                 if (haverow) {
792                         rc = sqlite3_prepare_v2(db, "update files set size = ?, content = ? where hash = ?", -1, &ifile,0);
793                 } else {
794                         rc = sqlite3_prepare_v2(db, "insert into files (size, content, hash) values (?,?,?)", -1, &ifile,0);
795                 }
796
797                 if (rc != SQLITE_OK) {
798                         SQLERROR(sqlite3_errmsg(db));
799                         fprintf(stderr, "cant prepare data\n");
800                         munmap(content, sbuf.st_size);
801                         return 0;
802                 }
803
804                 rc = sqlite3_bind_int64(ifile, 1, (sqlite3_int64)sbuf.st_size);
805                 if (rc != SQLITE_OK) {
806                         SQLERROR(sqlite3_errmsg(db));
807                         sqlite3_finalize(ifile);
808                         fprintf(stderr, "cant bind size\n");
809                         munmap(content, sbuf.st_size);
810                         return 0;
811                 }
812
813                 rc = sqlite3_bind_blob64(ifile, 2, outbuf,
814                                 (sqlite3_int64)outlen, SQLITE_STATIC);
815                 if (rc != SQLITE_OK) {
816                         SQLERROR(sqlite3_errmsg(db));
817                         sqlite3_finalize(ifile);
818                         fprintf(stderr, "cant bind content\n");
819                         munmap(content, sbuf.st_size);
820                         return 0;
821                 }
822
823                 rc = sqlite3_bind_text(ifile, 3, hash, 64, SQLITE_STATIC);
824                 if (rc != SQLITE_OK) {
825                         SQLERROR(sqlite3_errmsg(db));
826                         fprintf(stderr, "cant bind hash\n");
827                         sqlite3_finalize(ifile);
828                         munmap(content, sbuf.st_size);
829                         return 0;
830                 }
831
832                 rc = sqlite3_step(ifile);
833                 if (rc != SQLITE_DONE) {
834                         SQLERROR(sqlite3_errmsg(db));
835                         sqlite3_finalize(ifile);
836                         munmap(content, sbuf.st_size);
837                         return 0;
838                 }
839                 sqlite3_finalize(ifile);
840
841                 /* don't need the original file now */
842         }
843
844         if (!set_elf_info(zpm->db, hash, content, sbuf.st_size)) {
845                 fprintf(stderr, "setting elf info failed\n");
846                 munmap(content, sbuf.st_size);
847                 return 0;
848         }
849
850         munmap(content, sbuf.st_size);
851
852         /* return */
853         return 1;
854 }