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