]> pd.if.org Git - zpackage/blob - lib/zpm.c
remove stray debug fprintf
[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 int zpm_begin(struct zpm *z) {
19         char *errstr = 0;
20         sqlite3_exec(z->db, "begin;", NULL, NULL, &errstr);
21         if (errstr) {
22                 fprintf(stderr, "sqlite begin error: %s\n", errstr);
23                 sqlite3_free(errstr);
24                 return 0;
25         }
26         return 1;
27 }
28
29 int zpm_commit(struct zpm *z) {
30         char *errstr = 0;
31         sqlite3_exec(z->db, "commit;", NULL, NULL, &errstr);
32         if (errstr) {
33                 fprintf(stderr, "sqlite commit error: %s\n", errstr);
34                 sqlite3_free(errstr);
35                 return 0;
36         }
37         return 1;
38 }
39
40 /* wrapper for sqlite3_exec */
41 int zpm_exec(struct zpm *z, const char *sql, int(*callback)(void *, int, char **, char**), void *arg, char **errmsg) {
42         return sqlite3_exec(z->db, sql, callback, arg, errmsg);
43 }
44
45 int zpm_rollback(struct zpm *z) {
46         char *errstr = 0;
47         sqlite3_exec(z->db, "rollback;", NULL, NULL, &errstr);
48         if (errstr) {
49                 fprintf(stderr, "sqlite rollback error: %s\n", errstr);
50                 sqlite3_free(errstr);
51                 return 0;
52         }
53         return 1;
54 }
55
56 int zpm_readonly(struct zpm *z) {
57         if (z->db && sqlite3_db_readonly(z->db, "main")) {
58                 return 1;
59         }
60         return 0;
61 }
62
63 int zpm_db_set_pragma(struct zpm *db, int pragma, int value) {
64         int rc;
65         char *sql;
66         sqlite3_stmt *s;
67
68         switch (pragma) {
69                 case 1: sql = "pragma application_id = ?;"; break;
70                 case 2: sql = "pragma user_version = ?;"; break;
71                 default: return -1; break;
72         }
73
74         rc = sqlite3_prepare_v2(db->db, sql, -1, &s, 0);
75
76         if (rc != SQLITE_OK) {
77                 SQLERROR(sqlite3_errmsg(db->db));
78                 return -1;
79         }
80
81         sqlite3_bind_int(s, 1, value);
82         if (rc != SQLITE_OK) {
83                 SQLERROR(sqlite3_errmsg(db->db));
84                 fprintf(stderr, "cant bind pragma value\n");
85                 sqlite3_finalize(s);
86                 return -1;
87         }
88         rc = sqlite3_step(s);
89         if (rc != SQLITE_DONE) {
90                 SQLERROR(sqlite3_errmsg(db->db));
91                 fprintf(stderr, "cant set pragma\n");
92                 sqlite3_finalize(s);
93                 return -1;
94         }
95
96         sqlite3_finalize(s);
97         return value;
98 }
99
100 int zpm_db_pragma(struct zpm *db, int pragma) {
101         int rc;
102         int value = -1;
103         char *sql = 0;
104         sqlite3_stmt *s;
105
106         switch (pragma) {
107                 case 1: sql = "pragma application_id;"; break;
108                 case 2: sql = "pragma user_version;"; break;
109                 default: return -1; break;
110         }
111
112         rc = sqlite3_prepare_v2(db->db, sql, -1, &s, 0);
113
114         if (rc != SQLITE_OK) {
115                 SQLERROR(sqlite3_errmsg(db->db));
116                 fprintf(stderr, "%s, errnum = %d\n", sqlite3_errmsg(db->db), rc);
117                 /* TODO just abort? */
118                 return -1;
119         }
120
121         rc = sqlite3_step(s);
122         if (rc == SQLITE_ROW) {
123                 value = sqlite3_column_int(s, 0);
124         }
125
126         sqlite3_finalize(s);
127         return value;
128 }
129
130 static
131 #include "newdb.c"
132
133 int zpm_db_initialize(struct zpm *pkg) {
134         char *error;
135         switch (sqlite3_exec(pkg->db, createdb, (int (*)(void *,int,char **,char **))0, NULL, &error)) {
136                 case SQLITE_OK: break;
137                 default:
138                         SQLERROR(sqlite3_errmsg(pkg->db));
139                         fprintf(stderr, "error: %s\n", error);
140                         sqlite3_free(error);
141                         zpm_rollback(pkg);
142                         return 0;
143                         break;
144         }
145         return 1;
146 }
147
148
149 /* assumes pkg->db is set */
150 static int setupdb(struct zpm *zpm) {
151         char *errstr = 0;
152         int appid, dbver;
153
154         zpm->error = 0;
155
156         appid = zpm_db_pragma(zpm, 1);
157         dbver = zpm_db_pragma(zpm, 2);
158
159         if (appid != 0x5a504442) {
160                 fprintf(stderr, "unknown database type\n");
161                 zpm->error = 1;
162                 return 0;
163         }
164
165         if (dbver > 1) {
166                 fprintf(stderr, "version %d zpm db detected, this program only works with version 1 databases\n", dbver);
167                 zpm->error = 1;
168                 return 0;
169         }
170
171         sqlite3_exec(zpm->db, "pragma foreign_keys = ON;", NULL, NULL, &errstr);
172
173         if (errstr) {
174                 free(zpm->errmsg);
175                 zpm->errmsg = strdup(errstr);
176                 fprintf(stderr, "sqlite foreign key error: %s\n", errstr);
177                 sqlite3_free(errstr);
178                 zpm->error = 1;
179                 return 0;
180         }
181
182         /* TODO add vercmp */
183
184         return 1;
185 }
186
187 struct zpm *zpm_clearmem(struct zpm *zpm) {
188         if (!zpm) {
189                 zpm = malloc(sizeof *zpm);
190         }
191
192         if (zpm) {
193                 *zpm = (struct zpm){0};
194         }
195
196         return zpm;
197 }
198
199 static void zpm_set_db_errmsg(struct zpm *zpm, const char *msg) {
200         if (zpm) {
201                 if (zpm->dberrmsg) {
202                         free(zpm->dberrmsg);
203                 }
204                 if (msg) {
205                         zpm->dberrmsg = strdup(msg);
206                         if (!zpm->dberrmsg) {
207                                 zpm->error = 1;
208                         }
209                 } else {
210                         zpm->dberrmsg = 0;
211                 }
212         }
213 }
214
215 int zpm_init(struct zpm *pkg, char *path) {
216         int rc;
217         sqlite3 *db = 0;
218         int appid;
219
220         if (!pkg) {
221                 return 0;
222         }
223
224         zpm_clearmem(pkg);
225
226         rc = sqlite3_open_v2(path, &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
227         if (rc) {
228                 pkg->error = 1;
229                 pkg->dbresult = rc;
230                 zpm_set_db_errmsg(pkg, sqlite3_errstr(rc));
231                 if (db) {
232                         sqlite3_close(db);
233                 }
234                 fprintf(stderr, "error (%d): %s: %s\n", rc,
235                                 pkg->dberrmsg ? pkg->dberrmsg : "null", path);
236
237                 return 0;
238         }
239         pkg->db   = db;
240         pkg->path = strdup(path);
241
242         appid = zpm_db_pragma(pkg, 1);
243
244         switch (appid) {
245                 case 0: if (!zpm_db_initialize(pkg)) {
246                                 sqlite3_close(db);
247                                 return 1;
248                         };
249                         break;
250                 case 0x5a504442:
251                         /* already initialized */
252                         break;
253                 default:
254                         fprintf(stderr, "unknown database type\n");
255                         sqlite3_close(db);
256                         return 0;
257                         break;
258         }
259
260         if (!setupdb(pkg)) {
261                 sqlite3_close(db);
262                 pkg->db = 0;
263                 return 0;
264         }
265         zpm_addvercmp(pkg);
266
267         return 1;
268 }
269
270 int zpm_open(struct zpm *zpm, char *path) {
271         int rc;
272         sqlite3 *db = 0;
273
274         zpm_clearmem(zpm);
275
276         if (!path) {
277                 path = getenv("ZPMDB");
278         }
279
280         if (!path) {
281                 path = "/var/lib/zpm/local.db";
282         }
283
284         rc = sqlite3_open_v2(path, &db, SQLITE_OPEN_READWRITE, NULL);
285         if (rc) {
286                 SQLERROR(sqlite3_errmsg(db));
287                 sqlite3_close(db);
288                 zpm->error = 1;
289                 fprintf(stderr, "path = %s\n", path);
290                 return 0;
291         }
292         zpm->db   = db;
293         zpm->path = strdup(path);
294
295         if (!setupdb(zpm)) {
296                 sqlite3_close(db);
297                 zpm->db = 0;
298                 zpm->error = 1;
299                 return 0;
300         }
301         zpm_addvercmp(zpm);
302
303         return 1;
304 }
305
306 int zpm_close(struct zpm *pkg) {
307         if (pkg) {
308                 sqlite3_close(pkg->db);
309                 free(pkg->path);
310         }
311         /* TODO free any malloced names and such */
312         return 1;
313 }
314
315 static int zpm_sqlite_vercmp(void *not_used, int lena, const void *a,
316                 int lenb, const void *b) {
317         int rv;
318         char *bufa, *bufb;
319
320         if (not_used != 0) fprintf(stderr, "sqlite vercmp not_used = %p\n",
321                         not_used);
322         if (lena == 0 && lenb > 0) return 1;
323         if (lenb == 0 && lena > 0) return -1;
324
325         bufa = sqlite3_malloc(lena+1);
326         bufb = sqlite3_malloc(lenb+1);
327
328         strncpy(bufa, a, lena);
329         strncpy(bufb, b, lenb);
330         bufa[lena] = 0;
331         bufb[lenb] = 0;
332
333         rv = zpm_vercmp(bufa, bufb);
334         sqlite3_free(bufa);
335         sqlite3_free(bufb);
336         return rv;
337 }
338
339 int zpm_addvercmp(struct zpm *pkg) {
340         return sqlite3_create_collation(
341                         pkg->db, "vercmp", SQLITE_UTF8, NULL,
342                         zpm_sqlite_vercmp
343                         );
344 }
345
346 int zpm_extract(struct zpm *pkg, char *hash, char *path, mode_t mode) {
347         int rc;
348         int blobsize;
349         void *xzdata;
350         int type;
351         int out;
352         sqlite3_stmt *ifile;
353         size_t len;
354         char *tmpfile = 0;
355         sqlite3 *db;
356
357         if (!pkg || !pkg->db) {
358                 return 0;
359         }
360         db = pkg->db;
361
362         if (!path) {
363                 path = "-";
364         }
365
366         rc = sqlite3_prepare(db, "select size, content from files where hash = ?", -1, &ifile,0);
367         if (rc != SQLITE_OK) {
368                 SQLERROR(sqlite3_errmsg(db));
369                 return 0;
370         }
371
372         /* hash, filename */
373
374         sqlite3_bind_text(ifile, 1, hash, 64, SQLITE_STATIC);
375
376         rc = sqlite3_step(ifile);
377
378         if (rc == SQLITE_DONE) {
379                 /* didn't find a row */
380                 sqlite3_finalize(ifile);
381                 sqlite3_close(db);
382                 fprintf(stderr, "no such hash: %s\n", hash);
383                 return 0;
384         }
385         /* either way we're done with this now */
386
387         if (rc != SQLITE_ROW) {
388                 SQLERROR(sqlite3_errmsg(db));
389                 sqlite3_finalize(ifile);
390                 sqlite3_close(db);
391                 return 0;
392         }
393
394         type = sqlite3_column_type(ifile, 0);
395         if (type == SQLITE_NULL) {
396                 fprintf(stderr, "no file size\n");
397                 sqlite3_finalize(ifile);
398                 sqlite3_close(db);
399                 return 0;
400         }
401         type = sqlite3_column_type(ifile, 1);
402         if (type == SQLITE_NULL) {
403                 fprintf(stderr, "no file data\n");
404                 sqlite3_finalize(ifile);
405                 sqlite3_close(db);
406                 return 0;
407         }
408         //size = sqlite3_column_int64(ifile, 0);
409         xzdata = (void *)sqlite3_column_blob(ifile, 1);
410         blobsize = sqlite3_column_bytes(ifile, 1);
411
412         if (strcmp(path, "-")) {
413                 len = strlen(path);
414                 tmpfile = malloc(len+8+1);
415                 if (!tmpfile) {
416                         fprintf(stderr, "malloc error\n");
417                         return 0;
418                 }
419                 sprintf(tmpfile, "%sXXXXXX", path);
420
421                 out = open(tmpfile, O_CREAT|O_WRONLY|O_TRUNC, 0600);
422                 if (out == -1) {
423                         fprintf(stderr, "can't open output file %s: %s\n",
424                                         tmpfile, strerror(errno));
425                         sqlite3_finalize(ifile);
426                         sqlite3_close(db);
427                         free(tmpfile);
428                         return 0;
429                 }
430         } else {
431                 out = 1;
432         }
433
434 #if 0
435         fprintf(stderr, "uncompressing %d bytes at %p, expect %lld\n", blobsize, xzdata, (long long int)size);
436 #endif
437         uncompresslzma(xzdata, blobsize, out);
438         close(out);
439         sqlite3_finalize(ifile);
440
441         rc = 1;
442         if (tmpfile) {
443                 if (chmod(tmpfile, mode) == -1) {
444                         fprintf(stderr, "can't chmod %s: %s\n", tmpfile,
445                                         strerror(errno));
446                         rc = 0;
447                 } else if (rename(tmpfile, path) == -1) {
448                         fprintf(stderr, "extract rename failed: %s\n",
449                                         strerror(errno));
450                         rc = 0;
451                 }
452         }
453
454         if (rc == 0 && tmpfile) {
455                 if (unlink(tmpfile) == -1) {
456                         fprintf(stderr, "unlink tmpfile %s fail: %s\n",
457                                         tmpfile,
458                                         strerror(errno));
459                 }
460         }
461
462         free(tmpfile);
463         return rc;
464 }
465
466 static int run_for_hash(sqlite3 *db, char *sql, char *hash) {
467         int rc;
468         sqlite3_stmt *ifile;
469
470         rc = sqlite3_prepare_v2(db, sql, -1, &ifile, 0);
471         if (rc != SQLITE_OK) {
472                 SQLERROR(sqlite3_errmsg(db));
473                 return 0;
474         }
475
476         /* hash, filename */
477
478         sqlite3_bind_text(ifile, 1, hash, 64, SQLITE_STATIC);
479         do {
480                 rc = sqlite3_step(ifile);
481         } while (rc == SQLITE_ROW);
482
483         sqlite3_finalize(ifile);
484
485         return rc == SQLITE_DONE;
486 }
487
488 #define SQLERP(db, msg) fprintf(stderr, "%s: %s\n", msg, sqlite3_errmsg(db))
489
490 static int set_elf_info(struct zpm *zpm, char *hash, char *content,
491                 size_t length) {
492
493         char *strtab;
494         Elf64_Dyn *dyn;
495         int i;
496         Elf64_Phdr *phdr;
497         Elf64_Ehdr *hdr;
498         sqlite3_stmt *ifile;
499         sqlite3 *db = zpm->db;
500
501         if (length < sizeof (Elf64_Ehdr) || !libelf_iself(content)) {
502                 /* not an elf file */
503                 return 1;
504         }
505
506         /* clear existing for this hash */
507
508         if (!run_for_hash(db, "delete from elflibraries where file = ?", hash)) {
509                 SQLERP(db, "error clearing elf library");
510                 return 0;
511         }
512
513         if (!run_for_hash(db, "delete from elfneeded where file = ?", hash)) {
514                 SQLERP(db, "error clearing elf needed");
515                 return 0;
516         }
517
518         hdr = libelf_header(content);
519         /* if lib, set soname */
520         if (libelf_type(content) == ET_DYN) {
521                 char *soname = libelf_soname(content);
522                 if (soname) {
523                         zpm_db_run(zpm, "insert into elflibraries (file,soname) values (%Q,%Q);", hash, soname);
524                         if (zpm->error) {
525                                 return 0;
526                         }
527                 }
528                 /* some dyn don't have an soname, so we don't
529                  * need to warn */
530         }
531
532         /* if exe, set neededs */
533         /* libraries can have neededs too */
534         if (libelf_type(content) == ET_EXEC || libelf_type(content) == ET_DYN) {
535                 Elf64_Shdr *dsect = 0;
536                 char *elf;
537
538                 elf = (char *)content;
539                 /* find program header table */
540                 for (i = 0; i < hdr->e_phnum; i++) {
541                         phdr = (Elf64_Phdr *)(elf + hdr->e_phoff + i * hdr->e_phentsize);
542                         if (phdr->p_type == PT_DYNAMIC) {
543                                 dsect = (Elf64_Shdr *)(elf + phdr->p_offset);
544                         }
545                 }
546                 if (!dsect) {
547                         /* no dynamic section found */
548                         return 1;
549                 }
550
551                 dyn = (Elf64_Dyn *)dsect;
552
553                 dsect = libelf_section(elf, SHT_DYNAMIC);
554                 Elf64_Shdr *strsect;
555
556                 strsect = libelf_section_n(elf, dsect->sh_link);
557                 strtab = elf + strsect->sh_offset;
558
559                 sqlite3_prepare_v2(db, "insert into elfneeded (file,needed) values (?,?)",-1, &ifile, 0);
560                 sqlite3_bind_text(ifile,1,hash,64,SQLITE_STATIC);
561                 while (dyn->d_tag != DT_NULL) {
562                         if (dyn->d_tag == DT_NEEDED) {
563                                 char *need;
564                                 int rc;
565
566                                 need = strtab + dyn->d_un.d_val;
567                                 if (strlen(need) == 0) continue;
568                                 sqlite3_bind_text(ifile,2,need,strlen(need),SQLITE_STATIC);
569 #if 0
570                                 fprintf(stderr, "%s needs %s\n", hash, need);
571 #endif
572                                 rc = sqlite3_step(ifile);
573                                 if (rc != SQLITE_DONE) {
574                                         SQLERP(db, "error setting needed library");
575                                         sqlite3_finalize(ifile);
576                                         return 0;
577                                 }
578                                 sqlite3_reset(ifile);
579                         }
580                         dyn++;
581                 }
582                 sqlite3_finalize(ifile);
583         }
584         return 1;
585 }
586
587 int zpm_import(struct zpm *zpm, char *path, uint32_t flags, char *hash) {
588         int fd;
589         void *content = 0;
590         struct stat sbuf;
591         sqlite3_stmt *ifile = 0;
592         int haverow = 0,havedata = 0;
593         int rc,type;
594         char hashbuf[65];
595
596         /* xz compress it */
597         size_t outlen = 0;
598         void *outbuf;
599
600         if (!zpm || !zpm->db || !path) {
601                 return 0;
602         }
603
604         /* use local if caller didn't pass in room */
605         if (!hash) {
606                 hash = hashbuf;
607         }
608
609         if (flags) {
610                 fprintf(stderr, "zpm_import unused flags = %d\n", flags);
611         }
612         /* mmap the file */
613         fd = open(path, O_RDONLY);
614         if (fd == -1) {
615                 zpm->error = errno;
616                 fprintf(stderr, "%s can't open %s: %s\n", __FUNCTION__, path,strerror(errno));
617                 return 0;
618         }
619         if (fstat(fd, &sbuf) == -1) {
620                 zpm->error = errno;
621                 fprintf(stderr, "%s can't fstat %s: %s\n", __FUNCTION__, path,strerror(errno));
622                 return 0;
623         }
624         /* not a regular file? */
625         if (!S_ISREG(sbuf.st_mode)) {
626                 char *ftype;
627                 switch (sbuf.st_mode & S_IFMT) {
628                         case S_IFSOCK: ftype = "socket"; break;
629                         case S_IFLNK : ftype = "symlink"; break;
630                         case S_IFBLK : ftype = "block device"; break;
631                         case S_IFDIR : ftype = "directory"; break;
632                         case S_IFCHR : ftype = "character device"; break;
633                         case S_IFIFO : ftype = "fifo"; break;
634                         default: ftype = "unknown file type"; break;
635                 }
636                 /* TODO this is ok, just stored differently */
637                 fprintf(stderr, "%s can't import %s file: %s\n", __FUNCTION__, ftype, path);
638                 zpm->error = EINVAL;
639                 return 0;
640         }
641
642         content = mmap(0, sbuf.st_size, PROT_READ,MAP_PRIVATE, fd, 0);
643         close(fd);
644         if (!content) {
645                 zpm->error = errno;
646                 fprintf(stderr, "%s can't mmap %s: %s\n", __FUNCTION__, path,strerror(errno));
647                 return 0;
648         }
649
650         zpm_hash_mem(content, sbuf.st_size, hash);
651         hash[64] = 0;
652
653         /* TODO check null */
654         sqlite3 *db = zpm->db;
655
656         /* prepare and bind */
657
658         rc = sqlite3_prepare_v2(db, "select size, content is not null from files where hash = ?", -1, &ifile,0);
659         if (rc != SQLITE_OK) {
660                 SQLERROR(sqlite3_errmsg(db));
661                 munmap(content, sbuf.st_size);
662                 return 0;
663         }
664
665         /* hash, filename */
666
667         sqlite3_bind_text(ifile, 1, hash, 64, SQLITE_STATIC);
668
669         rc = sqlite3_step(ifile);
670
671         if (rc != SQLITE_DONE) {
672                 if (rc != SQLITE_ROW) {
673                         /* didn't find a row */
674                         SQLERROR(sqlite3_errmsg(db));
675                 munmap(content, sbuf.st_size);
676                         return 0;
677                 }
678                 haverow = 1;
679                 type = sqlite3_column_type(ifile, 0);
680                 if (type == SQLITE_NULL) {
681                         /* TODO assert, this shouldn't be possible? */
682                         fprintf(stderr, "no file size\n");
683                         sqlite3_finalize(ifile);
684                 munmap(content, sbuf.st_size);
685                         return 0;
686                 }
687                 type = sqlite3_column_type(ifile, 1);
688                 if (type == SQLITE_NULL) {
689                         /* TODO assert, this shouldn't be possible? */
690                         fprintf(stderr, "no file data\n");
691                         sqlite3_finalize(ifile);
692                 munmap(content, sbuf.st_size);
693                         return 0;
694                         /* which is fine, just need to update the row then */
695                 }
696                 havedata = sqlite3_column_int(ifile, 1);
697         }
698
699         sqlite3_finalize(ifile);
700
701         if (!havedata) {
702                 /* compress */
703 //              fprintf(stderr, "compressing\n");
704                 outbuf = compresslzma(content, sbuf.st_size, &outlen);
705                 if (!outbuf) {
706                         fprintf(stderr, "compresslzma failed\n");
707                         munmap(content, sbuf.st_size);
708                         return 0;
709                 }
710 ////            fprintf(stderr, "compress finished\n");
711
712                 /* insert */
713                 if (haverow) {
714                         rc = sqlite3_prepare_v2(db, "update files set size = ?, content = ? where hash = ?", -1, &ifile,0);
715                 } else {
716                         rc = sqlite3_prepare_v2(db, "insert into files (size, content, hash) values (?,?,?)", -1, &ifile,0);
717                 }
718
719                 if (rc != SQLITE_OK) {
720                         SQLERROR(sqlite3_errmsg(db));
721                         fprintf(stderr, "cant prepare data\n");
722                         munmap(content, sbuf.st_size);
723                         return 0;
724                 }
725
726                 rc = sqlite3_bind_int64(ifile, 1, (sqlite3_int64)sbuf.st_size);
727                 if (rc != SQLITE_OK) {
728                         SQLERROR(sqlite3_errmsg(db));
729                         sqlite3_finalize(ifile);
730                         fprintf(stderr, "cant bind size\n");
731                         munmap(content, sbuf.st_size);
732                         return 0;
733                 }
734
735                 rc = sqlite3_bind_blob64(ifile, 2, outbuf,
736                                 (sqlite3_int64)outlen, SQLITE_STATIC);
737                 if (rc != SQLITE_OK) {
738                         SQLERROR(sqlite3_errmsg(db));
739                         sqlite3_finalize(ifile);
740                         fprintf(stderr, "cant bind content\n");
741                         munmap(content, sbuf.st_size);
742                         return 0;
743                 }
744
745                 rc = sqlite3_bind_text(ifile, 3, hash, 64, SQLITE_STATIC);
746                 if (rc != SQLITE_OK) {
747                         SQLERROR(sqlite3_errmsg(db));
748                         fprintf(stderr, "cant bind hash\n");
749                         sqlite3_finalize(ifile);
750                         munmap(content, sbuf.st_size);
751                         return 0;
752                 }
753
754                 rc = sqlite3_step(ifile);
755                 if (rc != SQLITE_DONE) {
756                         SQLERROR(sqlite3_errmsg(db));
757                         sqlite3_finalize(ifile);
758                         munmap(content, sbuf.st_size);
759                         return 0;
760                 }
761                 sqlite3_finalize(ifile);
762
763                 /* don't need the original file now */
764         }
765
766         if (!set_elf_info(zpm, hash, content, sbuf.st_size)) {
767                 fprintf(stderr, "setting elf info failed\n");
768                 munmap(content, sbuf.st_size);
769                 return 0;
770         }
771
772         munmap(content, sbuf.st_size);
773
774         /* return */
775         return 1;
776 }