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