]> pd.if.org Git - zpackage/blob - lib/zpm.c
30ec7a669cae61ae2f8e50c2cb43f2c4f7b427b4
[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 static char *dupstr(char *s) {
118         size_t n;
119         char *d;
120
121         n = strlen(s);
122         d = malloc(n+1);
123         if (d) {
124                 d = strcpy(d, s);
125         }
126         return d;
127 }
128
129 #if 0
130 int zpm_newpkg(struct zpm *z, char *base, char *version, int release) {
131         char *sql = "insert or ignore into packages (package,version,release) values (?,?,?)";
132         int rc;
133         sqlite3_stmt *ifile;
134
135         rc = sqlite3_prepare(db, sql, -1, &ifile,0);
136         if (rc != SQLITE_OK) {
137                 SQLERROR(sqlite3_errmsg(db));
138                 return 0;
139         }
140         rc = sqlite3_bind_text(ifile, 1, base, strlen(base), SQLITE_STATIC);
141         if (rc != SQLITE_OK) {
142                 SQLERROR(sqlite3_errmsg(db));
143                 fprintf(stderr, "cant bind package name\n");
144                 zpm_rollback(pkg);
145                 return 0;
146         }
147         sqlite3_bind_text(ifile, 2, version, strlen(version), SQLITE_STATIC);
148         sqlite3_bind_int(ifile, 3, release)
149
150         rc = sqlite3_step(ifile);
151
152         if (rc != SQLITE_DONE) {
153                 SQLERROR(sqlite3_errmsg(db));
154                 sqlite3_finalize(ifile);
155                 return 0;
156         }
157         sqlite3_finalize(ifile);
158         z->pkg = dupstr(base);
159         z->version = dupstr(version);
160         z->release = release;
161 }
162 #endif
163
164 int zpm_begin(struct zpm *z) {
165         char *errstr = 0;
166         sqlite3_exec(z->db, "begin;", NULL, NULL, &errstr);
167         if (errstr) {
168                 fprintf(stderr, "sqlite begin error: %s\n", errstr);
169                 sqlite3_free(errstr);
170                 return 0;
171         }
172         return 1;
173 }
174
175 int zpm_commit(struct zpm *z) {
176         char *errstr = 0;
177         sqlite3_exec(z->db, "commit;", NULL, NULL, &errstr);
178         if (errstr) {
179                 fprintf(stderr, "sqlite commit error: %s\n", errstr);
180                 sqlite3_free(errstr);
181                 return 0;
182         }
183         return 1;
184 }
185
186 /* wrapper for sqlite3_exec */
187 int zpm_exec(struct zpm *z, const char *sql, int(*callback)(void *, int, char **, char**), void *arg, char **errmsg) {
188         return sqlite3_exec(z->db, sql, callback, arg, errmsg);
189 }
190
191 int zpm_rollback(struct zpm *z) {
192         char *errstr = 0;
193         sqlite3_exec(z->db, "rollback;", NULL, NULL, &errstr);
194         if (errstr) {
195                 fprintf(stderr, "sqlite rollback error: %s\n", errstr);
196                 sqlite3_free(errstr);
197                 return 0;
198         }
199         return 1;
200 }
201
202 int zpm_db_set_pragma(struct zpm *db, int pragma, int value) {
203         int rc;
204         char *sql;
205         sqlite3_stmt *s;
206
207         switch (pragma) {
208                 case 1: sql = "pragma application_id = ?;"; break;
209                 case 2: sql = "pragma user_version = ?;"; break;
210                 default: return -1; break;
211         }
212
213         rc = sqlite3_prepare_v2(db->db, sql, -1, &s, 0);
214
215         if (rc != SQLITE_OK) {
216                 SQLERROR(sqlite3_errmsg(db->db));
217                 return -1;
218         }
219
220         sqlite3_bind_int(s, 1, value);
221         if (rc != SQLITE_OK) {
222                 SQLERROR(sqlite3_errmsg(db->db));
223                 fprintf(stderr, "cant bind pragma value\n");
224                 sqlite3_finalize(s);
225                 return -1;
226         }
227         rc = sqlite3_step(s);
228         if (rc != SQLITE_DONE) {
229                 SQLERROR(sqlite3_errmsg(db->db));
230                 fprintf(stderr, "cant set pragma\n");
231                 sqlite3_finalize(s);
232                 return -1;
233         }
234
235         sqlite3_finalize(s);
236         return value;
237 }
238
239 int zpm_db_pragma(struct zpm *db, int pragma) {
240         int rc;
241         int value = -1;
242         char *sql = 0;
243         sqlite3_stmt *s;
244
245         switch (pragma) {
246                 case 1: sql = "pragma application_id;"; break;
247                 case 2: sql = "pragma user_version;"; break;
248                 default: return -1; break;
249         }
250
251         rc = sqlite3_prepare_v2(db->db, sql, -1, &s, 0);
252
253         if (rc != SQLITE_OK) {
254                 SQLERROR(sqlite3_errmsg(db->db));
255                 fprintf(stderr, "%s, errnum = %d\n", sqlite3_errmsg(db->db), rc);
256                 /* TODO just abort? */
257                 return -1;
258         }
259
260         rc = sqlite3_step(s);
261         if (rc == SQLITE_ROW) {
262                 value = sqlite3_column_int(s, 0);
263         }
264
265         sqlite3_finalize(s);
266         return value;
267 }
268
269 static
270 #include "newdb.c"
271
272 int zpm_db_initialize(struct zpm *pkg) {
273         //fprintf(stderr, "initializing zpm database\n");
274         char *error;
275         switch (sqlite3_exec(pkg->db, createdb, (int (*)(void *,int,char **,char **))0, NULL, &error)) {
276                 case SQLITE_OK: break;
277                 default:
278                         SQLERROR(sqlite3_errmsg(pkg->db));
279                         fprintf(stderr, "error: %s\n", error);
280                         sqlite3_free(error);
281                         return 0;
282                         break;
283         }
284         return 1;
285 }
286
287 /* NULL?  Create? */
288 int zpm_open(struct zpm *pkg, char *path) {
289         int rc;
290         char *errstr = 0;
291         sqlite3 *db = 0;
292         int appid, dbver;
293
294         pkg->db = 0;
295         pkg->path = 0;
296         pkg->version = 0;
297         pkg->release = 0;
298         pkg->pkgname = 0;
299         pkg->installed = 0;
300
301         rc = sqlite3_open(path, &db);
302         if (rc) {
303                 SQLERROR(sqlite3_errmsg(db));
304                 sqlite3_close(db);
305                 return 0;
306         }
307         pkg->db   = db;
308         pkg->path = dupstr(path);
309
310         appid = zpm_db_pragma(pkg, 1);
311         dbver = zpm_db_pragma(pkg, 2);
312
313         //fprintf(stderr, "db appid = %x, dbver = %d\n", appid, dbver);
314         switch (appid) {
315                 case 0: if (!zpm_db_initialize(pkg)) {
316                                 sqlite3_close(db);
317                                 return 1;
318                         };
319                         break;
320                 case 0x5a504442: break;
321                 default:
322                         fprintf(stderr, "unknown database type\n");
323                         sqlite3_close(db);
324                         return 0;
325                         break;
326         }
327         if (dbver > 1) {
328                 fprintf(stderr, "version %d zpm db detected, this program only works with version 1 databases\n", dbver);
329                         sqlite3_close(db);
330                         return 0;
331         }
332
333         sqlite3_exec(pkg->db, "pragma foreign_keys = ON;", NULL, NULL, &errstr);
334         if (errstr) {
335                 fprintf(stderr, "sqlite foreign key error: %s\n", errstr);
336                 sqlite3_free(errstr);
337                 sqlite3_close(db);
338                 return 0;
339         }
340
341
342         /* TODO if this is a new database, create structures */
343
344         /* get a package. what if more than one? what if none? */
345         return 1;
346 }
347
348 int zpm_close(struct zpm *pkg) {
349         if (pkg) {
350                 sqlite3_close(pkg->db);
351                 free(pkg->path);
352         }
353         /* TODO free any malloced names and such */
354         return 1;
355 }
356
357 static int zpm_sqlite_vercmp(void *not_used, int lena, const void *a,
358                 int lenb, const void *b) {
359         /* not sure what the ints are, possibly string lengths */
360         if (not_used != 0) fprintf(stderr, "sqlite vercmp not_used = %p\n",
361                         not_used);
362         if (lena == 0 && lenb > 0) return 1;
363         return zpm_vercmp(a, b);
364 }
365
366 int zpm_addvercmp(struct zpm *pkg) {
367         return sqlite3_create_collation(
368                         pkg->db, "vercmp", SQLITE_UTF8, NULL,
369                         zpm_sqlite_vercmp
370                         );
371 }
372
373 /* set package struct variables, database, environment, then command line */
374 int zpm_readopts(struct zpm *pkg, int ac, char **av) {
375         char *ev;
376
377         if (!pkg) {
378                 return -1;
379         }
380
381         ev = getenv("ZPMPACKAGE");
382         if (ev) {
383                 pkg->pkgname = dupstr(ev);
384         }
385         ev = getenv("ZPMPKGREL");
386         if (ev) {
387                 pkg->release = strtol(ev, 0, 0);
388         }
389         ev = getenv("ZPMPKGVER");
390         if (ev) {
391                 pkg->version = dupstr(ev);
392         }
393
394         /* now, parse the options, return optind so the caller can adjust if needed */
395
396         return av ? ac : 1;
397 }
398
399 int zpm_extract(struct zpm *pkg, char *hash, char *path, int mode) {
400         int rc;
401
402         int blobsize;
403         //int64_t size;
404         void *xzdata;
405         int type;
406         FILE *out;
407         sqlite3_stmt *ifile;
408
409         /* TODO check null */
410         sqlite3 *db = pkg->db;
411
412         rc = sqlite3_prepare(db, "select size, content from files where hash = ?", -1, &ifile,0);
413         if (rc != SQLITE_OK) {
414                 SQLERROR(sqlite3_errmsg(db));
415                 return 0;
416         }
417
418         /* hash, filename */
419
420         sqlite3_bind_text(ifile, 1, hash, 64, SQLITE_STATIC);
421
422         rc = sqlite3_step(ifile);
423
424         if (rc == SQLITE_DONE) {
425                 /* didn't find a row */
426                 sqlite3_finalize(ifile);
427                 sqlite3_close(db);
428                 fprintf(stderr, "no such hash: %s\n", hash);
429                 return 0;
430         }
431         /* either way we're done with this now */
432
433         if (rc != SQLITE_ROW) {
434                 SQLERROR(sqlite3_errmsg(db));
435                 sqlite3_finalize(ifile);
436                 sqlite3_close(db);
437                 return 0;
438         }
439
440         type = sqlite3_column_type(ifile, 0);
441         if (type == SQLITE_NULL) {
442                 fprintf(stderr, "no file size\n");
443                 sqlite3_finalize(ifile);
444                 sqlite3_close(db);
445                 return 0;
446         }
447         type = sqlite3_column_type(ifile, 1);
448         if (type == SQLITE_NULL) {
449                 fprintf(stderr, "no file data\n");
450                 sqlite3_finalize(ifile);
451                 sqlite3_close(db);
452                 return 0;
453         }
454         //size = sqlite3_column_int64(ifile, 0);
455         xzdata = (void *)sqlite3_column_blob(ifile, 1);
456         blobsize = sqlite3_column_bytes(ifile, 1);
457
458         if (strcmp(path, "-")) {
459                 out = fopen(path, "w");
460         } else {
461                 out = stdout;
462         }
463         if (!out) {
464                 fprintf(stderr, "can't open output file %s\n", path);
465                 sqlite3_finalize(ifile);
466                 sqlite3_close(db);
467                 return 0;
468         }
469         //fwrite(xzdata, blobsize, 1, stdout);
470
471         //fprintf(stderr, "uncompressing %d bytes at %p, expect %lld\n", blobsize, xzdata, (long long int)size);
472         uncompresslzma(xzdata, blobsize, out);
473         fclose(out);
474         chmod(path, mode);
475
476         sqlite3_finalize(ifile);
477
478         return 1;
479 }
480
481 /* flags 0, close mmap, flags 1, return mmap fd */
482 int zpm_hash(char *path, char *hash, uint32_t flags) {
483         int fd;
484         void *content;
485         struct stat sbuf;
486         struct sha256_state md;
487         int j;
488         unsigned char tmp[32];
489
490         /* mmap the file */
491         fd = open(path, O_RDONLY);
492         if (fd == -1) {
493                 fprintf(stderr, "%s can't open %s: %s\n", __FUNCTION__, path,strerror(errno));
494                 return 0;
495         }
496         if (fstat(fd, &sbuf) == -1) {
497                 fprintf(stderr, "%s can't fstat %s: %s\n", __FUNCTION__, path,strerror(errno));
498                 return 0;
499         }
500         /* not a regular file? */
501         if (!S_ISREG(sbuf.st_mode)) {
502                 /* TODO this is ok, just stored differently */
503                 fprintf(stderr, "%s non-regular files unsupported %s\n", __FUNCTION__, path);
504                 return 0;
505         }
506
507         content = mmap(0, sbuf.st_size, PROT_READ,MAP_PRIVATE, fd, 0);
508         close(fd);
509         if (!content) {
510                 fprintf(stderr, "%s can't mmap %s: %s\n", __FUNCTION__, path,strerror(errno));
511                 return 0;
512         }
513
514         /* get hash */
515         sha256_init(&md);
516         sha256_process(&md, content, sbuf.st_size);
517         sha256_done(&md, tmp);
518         for (j=0;j<32;j++) {
519                 sprintf(hash+j*2, "%02x", (unsigned)tmp[j]);
520         }
521         hash[64] = 0;
522         munmap(content, sbuf.st_size);
523         return flags ? fd : 1;
524 }
525
526 static sqlite3_stmt *run_for_hash(sqlite3 *db, char *sql, char *hash) {
527         int rc;
528         sqlite3_stmt *ifile;
529
530         rc = sqlite3_prepare_v2(db, sql, -1, &ifile, 0);
531         if (rc != SQLITE_OK) {
532                 SQLERROR(sqlite3_errmsg(db));
533                 return 0;
534         }
535
536         /* hash, filename */
537
538         sqlite3_bind_text(ifile, 1, hash, 64, SQLITE_STATIC);
539
540         return ifile;
541 }
542
543 static int set_elf_info(sqlite3 *db, char *hash, char *content, size_t length) {
544         if (length >= sizeof (Elf64_Ehdr) && libelf_iself(content)) {
545                 char *strtab;
546                 Elf64_Dyn *dyn;
547                 int i;
548                 Elf64_Phdr *phdr;
549                 Elf64_Ehdr *hdr;
550                 sqlite3_stmt *ifile;
551                 int rc;
552
553                 /* go ahead and set up elf information now */
554                 /* clear existing for this hash */
555                 ifile = run_for_hash(db, "delete from elfinfo where file = ?", hash);
556                 do {
557                         rc = sqlite3_step(ifile);
558 #if 0
559                         if (rc == SQLITE_ROW) {
560                                 int nc;
561                                 fprintf(stderr, "delete row has %d columns: ", sqlite3_column_count(ifile));
562                                 nc = sqlite3_column_count(ifile);
563                                 for (i = 0; i < nc; i++) {
564                                         char *r;
565                                         r = sqlite3_column_text(ifile, i);
566                                         fprintf(stderr, ", %s", r);
567                                 }
568                                 fprintf(stderr, "\n");
569                         }
570 #endif
571                 } while (rc == SQLITE_ROW);
572                 if (rc != SQLITE_DONE) {
573                         SQLERROR(sqlite3_errmsg(db));
574                         sqlite3_finalize(ifile);
575                         fprintf(stderr, "error clearing elf info: %d\n", rc);
576                         return 0;
577                 }
578                 sqlite3_finalize(ifile);
579                 ifile = run_for_hash(db, "delete from elflibraries where file = ?", hash);
580                 do {
581                         rc = sqlite3_step(ifile);
582                 } while (rc == SQLITE_ROW);
583                 if (rc != SQLITE_DONE) {
584                         SQLERROR(sqlite3_errmsg(db));
585                         sqlite3_finalize(ifile);
586                         fprintf(stderr, "error clearing elf library: %d\n", rc);
587                         return 0;
588                 }
589                 sqlite3_finalize(ifile);
590                 ifile = run_for_hash(db, "delete from elfneeded where file = ?", hash);
591                 do {
592                         rc = sqlite3_step(ifile);
593                 } while (rc == SQLITE_ROW);
594                 if (rc != SQLITE_DONE) {
595                         SQLERROR(sqlite3_errmsg(db));
596                         sqlite3_finalize(ifile);
597                         fprintf(stderr, "error clearing elf needed\n");
598                         return 0;
599                 }
600                 sqlite3_finalize(ifile);
601
602                 hdr = libelf_header(content);
603                 /* if lib, set soname */
604                 if (libelf_type(content) == ET_DYN) {
605                         char *soname = libelf_soname(content);
606                         if (soname) {
607
608                                 sqlite3_prepare_v2(db, "insert into elflibraries (file,soname) values (?,?)",-1, &ifile, 0);
609                                 sqlite3_bind_text(ifile,1,hash,64,SQLITE_STATIC);
610                                 sqlite3_bind_text(ifile,2,soname,-1,SQLITE_STATIC);
611                                 rc = sqlite3_step(ifile);
612                                 if (rc != SQLITE_DONE) {
613                                         SQLERROR(sqlite3_errmsg(db));
614                                         sqlite3_finalize(ifile);
615                                         fprintf(stderr, "error setting library soname\n");
616                                         return 0;
617                                 }
618                                 sqlite3_finalize(ifile);
619                         } else {
620                                 fprintf(stderr, "can't find soname\n");
621                         }
622                 }
623
624                 /* if exe, set neededs */
625                 if (libelf_type(content) == ET_EXEC) {
626                         Elf64_Shdr *dsect;
627                         char *elf;
628
629                         elf = (char *)content;
630                         /* find program header table */
631                         for (i = 0; i < hdr->e_phnum; i++) {
632                                 phdr = (Elf64_Phdr *)(elf + hdr->e_phoff + i * hdr->e_phentsize);
633                                 if (phdr->p_type == PT_DYNAMIC) {
634                                         dsect = (Elf64_Shdr *)(elf + phdr->p_offset);
635                                 }
636                         }
637                         dyn = (Elf64_Dyn *)(elf + dsect->sh_offset);
638                         if (!dyn) {
639                                 exit(9);
640                         }
641                         dyn = (Elf64_Dyn *)dsect;
642
643                         dsect = libelf_section(elf, SHT_DYNAMIC);
644                         Elf64_Shdr *strsect;
645
646                         strsect = libelf_section_n(elf, dsect->sh_link);
647                         strtab = elf + strsect->sh_offset;
648
649                         sqlite3_prepare_v2(db, "insert into elfneeded (file,needed) values (?,?)",-1, &ifile, 0);
650                         sqlite3_bind_text(ifile,1,hash,64,SQLITE_STATIC);
651                         while (dyn->d_tag != DT_NULL) {
652                                 if (dyn->d_tag == DT_NEEDED) {
653                                         char *need;
654                                         int rc;
655
656                                         need = strtab + dyn->d_un.d_val;
657                                         if (strlen(need) == 0) continue;
658                                         sqlite3_bind_text(ifile,2,need,strlen(need),SQLITE_STATIC);
659                                         fprintf(stderr, "%s needs %s\n", hash, need);
660                                         rc = sqlite3_step(ifile);
661                                         if (rc != SQLITE_DONE) {
662                                                 SQLERROR(sqlite3_errmsg(db));
663                                                 sqlite3_finalize(ifile);
664                                                 fprintf(stderr, "error setting needed library\n");
665                                                 return 0;
666                                         }
667                                         sqlite3_reset(ifile);
668                                 }
669                                 dyn++;
670                         }
671                         sqlite3_finalize(ifile);
672                 }
673         }
674         return 1;
675 }
676
677 #if 1
678 int zpm_import(struct zpm *pkg, char *path, uint32_t flags, char *hash) {
679         int fd;
680         void *content;
681         struct stat sbuf;
682         unsigned char tmp[32];
683         struct sha256_state md;
684         sqlite3_stmt *ifile;
685         int haverow = 0,havedata = 0;
686         int j,rc,type;
687         char hashbuf[65];
688
689         /* xz compress it */
690         size_t outlen = 0;
691         void *outbuf;
692
693         if (!pkg || !pkg->db || !path) {
694                 return 0;
695         }
696
697         /* use local if caller didn't pass in room */
698         if (!hash) {
699                 hash = hashbuf;
700         }
701
702         if (flags) {
703                 fprintf(stderr, "zpm_import unused flags = %d\n", flags);
704         }
705         /* mmap the file */
706         fd = open(path, O_RDONLY);
707         if (fd == -1) {
708                 pkg->error = errno;
709                 fprintf(stderr, "%s can't open %s: %s\n", __FUNCTION__, path,strerror(errno));
710                 return 0;
711         }
712         if (fstat(fd, &sbuf) == -1) {
713                 pkg->error = errno;
714                 fprintf(stderr, "%s can't fstat %s: %s\n", __FUNCTION__, path,strerror(errno));
715                 return 0;
716         }
717         /* not a regular file? */
718         if (!S_ISREG(sbuf.st_mode)) {
719                 char *ftype;
720                 switch (sbuf.st_mode & S_IFMT) {
721                         case S_IFSOCK: ftype = "socket"; break;
722                         case S_IFLNK : ftype = "symlink"; break;
723                         case S_IFBLK : ftype = "block device"; break;
724                         case S_IFDIR : ftype = "directory"; break;
725                         case S_IFCHR : ftype = "character device"; break;
726                         case S_IFIFO : ftype = "fifo"; break;
727                         default: ftype = "unknown file type"; break;
728                 }
729                 /* TODO this is ok, just stored differently */
730                 fprintf(stderr, "%s can't import %s file: %s\n", __FUNCTION__, ftype, path);
731                 pkg->error = EINVAL;
732                 return 0;
733         }
734
735         content = mmap(0, sbuf.st_size, PROT_READ,MAP_PRIVATE, fd, 0);
736         close(fd);
737         if (!content) {
738                 pkg->error = errno;
739                 fprintf(stderr, "%s can't mmap %s: %s\n", __FUNCTION__, path,strerror(errno));
740                 return 0;
741         }
742
743         /* get hash */
744         sha256_init(&md);
745         sha256_process(&md, content, sbuf.st_size);
746         sha256_done(&md, tmp);
747         for (j=0;j<32;j++) {
748                 sprintf(hash+j*2, "%02x", (unsigned)tmp[j]);
749         }
750         hash[64] = 0;
751         //fprintf(stderr, "file %s: %s\n", path, hash);
752
753         /* TODO check null */
754         sqlite3 *db = pkg->db;
755
756         /* prepare and bind */
757
758         rc = sqlite3_prepare_v2(db, "select size, content is not null from files where hash = ?", -1, &ifile,0);
759         if (rc != SQLITE_OK) {
760                 SQLERROR(sqlite3_errmsg(db));
761                 munmap(content, sbuf.st_size);
762                 return 0;
763         }
764
765         /* hash, filename */
766
767         sqlite3_bind_text(ifile, 1, hash, 64, SQLITE_STATIC);
768
769         rc = sqlite3_step(ifile);
770
771         if (rc != SQLITE_DONE) {
772                 if (rc != SQLITE_ROW) {
773                         /* didn't find a row */
774                         SQLERROR(sqlite3_errmsg(db));
775                         zpm_rollback(pkg);
776                 munmap(content, sbuf.st_size);
777                         return 0;
778                 }
779                 haverow = 1;
780 //              fprintf(stderr, "have row for hash\n");
781                 type = sqlite3_column_type(ifile, 0);
782                 if (type == SQLITE_NULL) {
783                         /* TODO assert, this shouldn't be possible? */
784                         fprintf(stderr, "no file size\n");
785                         sqlite3_finalize(ifile);
786                 munmap(content, sbuf.st_size);
787                         return 0;
788                 }
789                 type = sqlite3_column_type(ifile, 1);
790                 if (type == SQLITE_NULL) {
791                         /* TODO assert, this shouldn't be possible? */
792                         fprintf(stderr, "no file data\n");
793                         sqlite3_finalize(ifile);
794                 munmap(content, sbuf.st_size);
795                         return 0;
796                         /* which is fine, just need to update the row then */
797                 }
798                 havedata = sqlite3_column_int(ifile, 1);
799         }
800
801         sqlite3_finalize(ifile);
802
803         if (!havedata) {
804                 /* compress */
805                 outbuf = compresslzma(content, sbuf.st_size, &outlen);
806                 if (!outbuf) {
807                         fprintf(stderr, "compresslzma failed\n");
808                 munmap(content, sbuf.st_size);
809                         return 0;
810                 }
811                 //fprintf(stderr, "compressed to %zu\n", outlen);
812
813                 /* start a transaction */
814                 // do that outside of here 
815                 //zpm_begin(pkg);
816
817                 /* insert */
818                 if (haverow) {
819                         //fprintf(stderr, "adding file data\n");
820                         rc = sqlite3_prepare(db, "update files set size = ?, content = ? where hash = ?", -1, &ifile,0);
821                 } else {
822                         //fprintf(stderr, "creating new data row\n");
823                         rc = sqlite3_prepare(db, "insert into files (size, content, hash) values (?,?,?)", -1, &ifile,0);
824                 }
825                 if (rc != SQLITE_OK) {
826                         SQLERROR(sqlite3_errmsg(db));
827                         fprintf(stderr, "cant prepare data\n");
828                         zpm_rollback(pkg);
829                 munmap(content, sbuf.st_size);
830                         return 0;
831                 }
832
833                 sqlite3_bind_int64(ifile, 1, (sqlite3_int64)sbuf.st_size);
834                 if (rc != SQLITE_OK) {
835                         SQLERROR(sqlite3_errmsg(db));
836                         fprintf(stderr, "cant bind size\n");
837                         zpm_rollback(pkg);
838                 munmap(content, sbuf.st_size);
839                         return 0;
840                 }
841                 sqlite3_bind_blob64(ifile, 2, outbuf, (sqlite3_int64)outlen, SQLITE_STATIC);
842                 if (rc != SQLITE_OK) {
843                         SQLERROR(sqlite3_errmsg(db));
844                         fprintf(stderr, "cant bind content\n");
845                         zpm_rollback(pkg);
846                 munmap(content, sbuf.st_size);
847                         return 0;
848                 }
849                 sqlite3_bind_text(ifile, 3, hash, 64, SQLITE_STATIC);
850                 if (rc != SQLITE_OK) {
851                         SQLERROR(sqlite3_errmsg(db));
852                         fprintf(stderr, "cant bind hash\n");
853                         zpm_rollback(pkg);
854                 munmap(content, sbuf.st_size);
855                         return 0;
856                 }
857                 rc = sqlite3_step(ifile);
858                 if (rc != SQLITE_DONE) {
859                         SQLERROR(sqlite3_errmsg(db));
860                         sqlite3_finalize(ifile);
861                         zpm_rollback(pkg);
862                 munmap(content, sbuf.st_size);
863                         return 0;
864                 }
865                 sqlite3_finalize(ifile);
866
867                 /* commit */
868                 //zpm_commit(pkg);
869
870                 /* don't need the original file now */
871
872         }
873
874         if (!set_elf_info(pkg->db, hash, content, sbuf.st_size)) {
875                 fprintf(stderr, "setting elf info failed\n");
876                 munmap(content, sbuf.st_size);
877                 return 0;
878         }
879
880         munmap(content, sbuf.st_size);
881
882         /* if package and not nopackage flag, add to package */
883         if (pkg->pkgname && (!ZPM_NOPACKAGE)) {
884                 /* TODO */
885         }
886
887         /* return */
888         return 1;
889 }
890 #endif
891
892 #if 0
893 int main(int ac, char **av){
894         sqlite3 *db = 0;
895         int rc;
896
897         int blobsize;
898         int64_t size;
899         void *xzdata;
900         int type;
901         FILE *out;
902         sqlite3_stmt *ifile;
903
904         char *hash;
905         char *filename;
906
907         if (ac < 3) {
908                 fprintf(stderr, "usage: db hash file\n");
909                 return 1;
910         }
911
912         rc = sqlite3_open(av[1], &db);
913         if (rc) {
914                 SQLERROR(sqlite3_errmsg(db));
915                 sqlite3_close(db);
916                 return 1;
917         }
918
919 }
920 #endif
921
922 #if 0
923 Packages are sqlite databases
924
925 get application id and userver
926
927 Primitive operations:
928
929 add path to repo
930 associate path with package
931 associate blob with path?
932 add blob to repo
933 * extract blob to a path
934 compare blob to filesystem path
935 create package with info
936
937 Extra primitives:
938
939 record elf information about blob
940 compress blob
941 uncompress blob
942 sign a package?  What are we verifying?
943 #endif