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