]> pd.if.org Git - zpackage/blob - lib/zpm.c
add warnings to compile flags and fix
[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 av ? ac : 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         chmod(path, mode);
454
455         sqlite3_finalize(ifile);
456
457         return 1;
458 }
459
460 /* flags 0, close mmap, flags 1, return mmap fd */
461 int zpm_hash(char *path, char *hash, uint32_t flags) {
462         int fd;
463         void *content;
464         struct stat sbuf;
465         hash_state md;
466         int j;
467         unsigned char tmp[32];
468
469         /* mmap the file */
470         fd = open(path, O_RDONLY);
471         if (fd == -1) {
472                 fprintf(stderr, "%s can't open %s: %s\n", __FUNCTION__, path,strerror(errno));
473                 return 0;
474         }
475         if (fstat(fd, &sbuf) == -1) {
476                 fprintf(stderr, "%s can't fstat %s: %s\n", __FUNCTION__, path,strerror(errno));
477                 return 0;
478         }
479         /* not a regular file? */
480         if (!S_ISREG(sbuf.st_mode)) {
481                 /* TODO this is ok, just stored differently */
482                 fprintf(stderr, "%s non-regular files unsupported %s\n", __FUNCTION__, path);
483                 return 0;
484         }
485
486         content = mmap(0, sbuf.st_size, PROT_READ,MAP_PRIVATE, fd, 0);
487         close(fd);
488         if (!content) {
489                 fprintf(stderr, "%s can't mmap %s: %s\n", __FUNCTION__, path,strerror(errno));
490                 return 0;
491         }
492
493         /* get hash */
494         sha256_init(&md);
495         sha256_process(&md, content, sbuf.st_size);
496         sha256_done(&md, tmp);
497         for (j=0;j<32;j++) {
498                 sprintf(hash+j*2, "%02x", (unsigned)tmp[j]);
499         }
500         hash[64] = 0;
501         munmap(content, sbuf.st_size);
502         return flags ? fd : 1;
503 }
504
505 static sqlite3_stmt *run_for_hash(sqlite3 *db, char *sql, char *hash) {
506         int rc;
507         sqlite3_stmt *ifile;
508
509         rc = sqlite3_prepare_v2(db, sql, -1, &ifile, 0);
510         if (rc != SQLITE_OK) {
511                 SQLERROR(sqlite3_errmsg(db));
512                 return 0;
513         }
514
515         /* hash, filename */
516
517         sqlite3_bind_text(ifile, 1, hash, 64, SQLITE_STATIC);
518
519         return ifile;
520 }
521
522 static int set_elf_info(sqlite3 *db, char *hash, char *content, size_t length) {
523         if (length >= sizeof (Elf64_Ehdr) && libelf_iself(content)) {
524                 char *strtab;
525                 Elf64_Dyn *dyn;
526                 int i;
527                 Elf64_Phdr *phdr;
528                 Elf64_Ehdr *hdr;
529                 sqlite3_stmt *ifile;
530                 int rc;
531
532                 /* go ahead and set up elf information now */
533                 /* clear existing for this hash */
534                 ifile = run_for_hash(db, "delete from elfinfo where file = ?", hash);
535                 do {
536                         rc = sqlite3_step(ifile);
537 #if 0
538                         if (rc == SQLITE_ROW) {
539                                 int nc;
540                                 fprintf(stderr, "delete row has %d columns: ", sqlite3_column_count(ifile));
541                                 nc = sqlite3_column_count(ifile);
542                                 for (i = 0; i < nc; i++) {
543                                         char *r;
544                                         r = sqlite3_column_text(ifile, i);
545                                         fprintf(stderr, ", %s", r);
546                                 }
547                                 fprintf(stderr, "\n");
548                         }
549 #endif
550                 } while (rc == SQLITE_ROW);
551                 if (rc != SQLITE_DONE) {
552                         SQLERROR(sqlite3_errmsg(db));
553                         sqlite3_finalize(ifile);
554                         fprintf(stderr, "error clearing elf info: %d\n", rc);
555                         return 0;
556                 }
557                 sqlite3_finalize(ifile);
558                 ifile = run_for_hash(db, "delete from elflibraries where file = ?", hash);
559                 do {
560                         rc = sqlite3_step(ifile);
561                 } while (rc == SQLITE_ROW);
562                 if (rc != SQLITE_DONE) {
563                         SQLERROR(sqlite3_errmsg(db));
564                         sqlite3_finalize(ifile);
565                         fprintf(stderr, "error clearing elf library: %d\n", rc);
566                         return 0;
567                 }
568                 sqlite3_finalize(ifile);
569                 ifile = run_for_hash(db, "delete from elfneeded where file = ?", hash);
570                 do {
571                         rc = sqlite3_step(ifile);
572                 } while (rc == SQLITE_ROW);
573                 if (rc != SQLITE_DONE) {
574                         SQLERROR(sqlite3_errmsg(db));
575                         sqlite3_finalize(ifile);
576                         fprintf(stderr, "error clearing elf needed\n");
577                         return 0;
578                 }
579                 sqlite3_finalize(ifile);
580
581                 hdr = libelf_header(content);
582                 /* if lib, set soname */
583                 if (libelf_type(content) == ET_DYN) {
584                         char *elf;
585                         Elf64_Shdr *shdr, *dynsect, *dynstrtab = 0;
586
587                         elf = (char *)content;
588                         for (i = 0; i < hdr->e_shnum; i++) {
589                                 shdr = (Elf64_Shdr *)(elf + hdr->e_shoff + i * hdr->e_shentsize);
590                                 if (shdr->sh_type == SHT_DYNAMIC) {
591                                         dynsect = shdr;
592                                 } else if (shdr->sh_type == SHT_STRTAB && i == hdr->e_shstrndx) {
593                                         dynstrtab = shdr;
594                                 }
595                         }
596                         if (!dynstrtab) {
597                                 exit(8);
598                         }
599                         if (!dynsect) {
600                                 exit(9);
601                         }
602
603                         char *name;
604                         Elf64_Shdr *dyntab;
605                         name = elf + dynstrtab->sh_offset;
606                         for (i = 0; i < hdr->e_shnum; i++) {
607                                 shdr = (Elf64_Shdr *)(elf + hdr->e_shoff + i * hdr->e_shentsize);
608                                 if (shdr->sh_type == SHT_STRTAB && !strcmp(".dynstr", name+shdr->sh_name)) {
609                                         dyntab = shdr;
610                                 }
611                         }
612                         if (!dyntab) {
613                                 exit(10);
614                         }
615
616                         char *dynname;
617                         Elf64_Dyn *dent;
618                         dynname = elf + dyntab->sh_offset;
619
620                         for (dent = (Elf64_Dyn *)(elf + dynsect->sh_offset); dent->d_tag != DT_NULL; dent++) {
621                                 if (dent->d_tag == DT_SONAME) {
622                                         char *soname = dynname + dent->d_un.d_val;
623                                         sqlite3_prepare_v2(db, "insert into elflibraries (file,soname) values (?,?)",-1, &ifile, 0);
624                                         sqlite3_bind_text(ifile,1,hash,64,SQLITE_STATIC);
625                                         sqlite3_bind_text(ifile,2,soname,-1,SQLITE_STATIC);
626                                         rc = sqlite3_step(ifile);
627                                         if (rc != SQLITE_DONE) {
628                                                 SQLERROR(sqlite3_errmsg(db));
629                                                 sqlite3_finalize(ifile);
630                                                 fprintf(stderr, "error setting library soname\n");
631                                                 return 0;
632                                         }
633                                         sqlite3_finalize(ifile);
634                                 }
635                         }
636                 }
637
638                 /* if exe, set neededs */
639                 if (libelf_type(content) == ET_EXEC) {
640                         Elf64_Shdr *dsect;
641                         char *elf;
642
643                         elf = (char *)content;
644                         /* find program header table */
645                         for (i = 0; i < hdr->e_phnum; i++) {
646                                 phdr = (Elf64_Phdr *)(elf + hdr->e_phoff + i * hdr->e_phentsize);
647                                 if (phdr->p_type == PT_DYNAMIC) {
648                                         dsect = (Elf64_Shdr *)(elf + phdr->p_offset);
649                                 }
650                         }
651                         dyn = (Elf64_Dyn *)(elf + dsect->sh_offset);
652                         if (!dyn) {
653                                 exit(9);
654                         }
655                         dyn = (Elf64_Dyn *)dsect;
656
657                         dsect = libelf_section(elf, SHT_DYNAMIC);
658                         Elf64_Shdr *strsect;
659
660                         strsect = libelf_section_n(elf, dsect->sh_link);
661                         strtab = elf + strsect->sh_offset;
662
663                         sqlite3_prepare_v2(db, "insert into elfneeded (file,needed) values (?,?)",-1, &ifile, 0);
664                         sqlite3_bind_text(ifile,1,hash,64,SQLITE_STATIC);
665                         while (dyn->d_tag != DT_NULL) {
666                                 if (dyn->d_tag == DT_NEEDED) {
667                                         char *need;
668                                         int rc;
669
670                                         need = strtab + dyn->d_un.d_val;
671                                         if (strlen(need) == 0) continue;
672                                         sqlite3_bind_text(ifile,2,need,strlen(need),SQLITE_STATIC);
673                                         fprintf(stderr, "%s needs %s\n", hash, need);
674                                         rc = sqlite3_step(ifile);
675                                         if (rc != SQLITE_DONE) {
676                                                 SQLERROR(sqlite3_errmsg(db));
677                                                 sqlite3_finalize(ifile);
678                                                 fprintf(stderr, "error setting needed library\n");
679                                                 return 0;
680                                         }
681                                         sqlite3_reset(ifile);
682                                 }
683                                 dyn++;
684                         }
685                         sqlite3_finalize(ifile);
686                 }
687         }
688         return 1;
689 }
690
691 #if 1
692 int zpm_import(struct zpm *pkg, char *path, uint32_t flags, char *hash) {
693         int fd;
694         void *content;
695         struct stat sbuf;
696         unsigned char tmp[32];
697         hash_state md;
698         sqlite3_stmt *ifile;
699         int haverow = 0,havedata = 0;
700         int j,rc,type;
701         char hashbuf[65];
702
703         /* xz compress it */
704         size_t outlen = 0;
705         void *outbuf;
706
707         if (!pkg || !pkg->db || !path) {
708                 return 0;
709         }
710
711         /* use local if caller didn't pass in room */
712         if (!hash) {
713                 hash = hashbuf;
714         }
715
716         flags = 0; /* suppress warning, probably use to follow symlinks */
717         /* mmap the file */
718         fd = open(path, O_RDONLY);
719         if (fd == -1) {
720                 pkg->error = errno;
721                 fprintf(stderr, "%s can't open %s: %s\n", __FUNCTION__, path,strerror(errno));
722                 return 0;
723         }
724         if (fstat(fd, &sbuf) == -1) {
725                 pkg->error = errno;
726                 fprintf(stderr, "%s can't fstat %s: %s\n", __FUNCTION__, path,strerror(errno));
727                 return 0;
728         }
729         /* not a regular file? */
730         if (!S_ISREG(sbuf.st_mode)) {
731                 char *ftype;
732                 switch (sbuf.st_mode & S_IFMT) {
733                         case S_IFSOCK: ftype = "socket"; break;
734                         case S_IFLNK : ftype = "symlink"; break;
735                         case S_IFBLK : ftype = "block device"; break;
736                         case S_IFDIR : ftype = "directory"; break;
737                         case S_IFCHR : ftype = "character device"; break;
738                         case S_IFIFO : ftype = "fifo"; break;
739                         default: ftype = "unknown file type"; break;
740                 }
741                 /* TODO this is ok, just stored differently */
742                 fprintf(stderr, "%s can't import %s file: %s\n", __FUNCTION__, ftype, path);
743                 pkg->error = EINVAL;
744                 return 0;
745         }
746
747         content = mmap(0, sbuf.st_size, PROT_READ,MAP_PRIVATE, fd, 0);
748         close(fd);
749         if (!content) {
750                 pkg->error = errno;
751                 fprintf(stderr, "%s can't mmap %s: %s\n", __FUNCTION__, path,strerror(errno));
752                 return 0;
753         }
754
755         /* get hash */
756         sha256_init(&md);
757         sha256_process(&md, content, sbuf.st_size);
758         sha256_done(&md, tmp);
759         for (j=0;j<32;j++) {
760                 sprintf(hash+j*2, "%02x", (unsigned)tmp[j]);
761         }
762         hash[64] = 0;
763         //fprintf(stderr, "file %s: %s\n", path, hash);
764
765         /* TODO check null */
766         sqlite3 *db = pkg->db;
767
768         /* prepare and bind */
769
770         rc = sqlite3_prepare_v2(db, "select size, content is not null from files where hash = ?", -1, &ifile,0);
771         if (rc != SQLITE_OK) {
772                 SQLERROR(sqlite3_errmsg(db));
773                 munmap(content, sbuf.st_size);
774                 return 0;
775         }
776
777         /* hash, filename */
778
779         sqlite3_bind_text(ifile, 1, hash, 64, SQLITE_STATIC);
780
781         rc = sqlite3_step(ifile);
782
783         if (rc != SQLITE_DONE) {
784                 if (rc != SQLITE_ROW) {
785                         /* didn't find a row */
786                         SQLERROR(sqlite3_errmsg(db));
787                         zpm_rollback(pkg);
788                 munmap(content, sbuf.st_size);
789                         return 0;
790                 }
791                 haverow = 1;
792 //              fprintf(stderr, "have row for hash\n");
793                 type = sqlite3_column_type(ifile, 0);
794                 if (type == SQLITE_NULL) {
795                         /* TODO assert, this shouldn't be possible? */
796                         fprintf(stderr, "no file size\n");
797                         sqlite3_finalize(ifile);
798                 munmap(content, sbuf.st_size);
799                         return 0;
800                 }
801                 type = sqlite3_column_type(ifile, 1);
802                 if (type == SQLITE_NULL) {
803                         /* TODO assert, this shouldn't be possible? */
804                         fprintf(stderr, "no file data\n");
805                         sqlite3_finalize(ifile);
806                 munmap(content, sbuf.st_size);
807                         return 0;
808                         /* which is fine, just need to update the row then */
809                 }
810                 havedata = sqlite3_column_int(ifile, 1);
811         }
812
813         sqlite3_finalize(ifile);
814
815         if (!havedata) {
816                 /* compress */
817                 outbuf = compresslzma(content, sbuf.st_size, &outlen);
818                 if (!outbuf) {
819                         fprintf(stderr, "compresslzma failed\n");
820                 munmap(content, sbuf.st_size);
821                         return 0;
822                 }
823                 //fprintf(stderr, "compressed to %zu\n", outlen);
824
825                 /* start a transaction */
826                 // do that outside of here 
827                 //zpm_begin(pkg);
828
829                 /* insert */
830                 if (haverow) {
831                         //fprintf(stderr, "adding file data\n");
832                         rc = sqlite3_prepare(db, "update files set size = ?, content = ? where hash = ?", -1, &ifile,0);
833                 } else {
834                         //fprintf(stderr, "creating new data row\n");
835                         rc = sqlite3_prepare(db, "insert into files (size, content, hash) values (?,?,?)", -1, &ifile,0);
836                 }
837                 if (rc != SQLITE_OK) {
838                         SQLERROR(sqlite3_errmsg(db));
839                         fprintf(stderr, "cant prepare data\n");
840                         zpm_rollback(pkg);
841                 munmap(content, sbuf.st_size);
842                         return 0;
843                 }
844
845                 sqlite3_bind_int64(ifile, 1, (sqlite3_int64)sbuf.st_size);
846                 if (rc != SQLITE_OK) {
847                         SQLERROR(sqlite3_errmsg(db));
848                         fprintf(stderr, "cant bind size\n");
849                         zpm_rollback(pkg);
850                 munmap(content, sbuf.st_size);
851                         return 0;
852                 }
853                 sqlite3_bind_blob64(ifile, 2, outbuf, (sqlite3_int64)outlen, SQLITE_STATIC);
854                 if (rc != SQLITE_OK) {
855                         SQLERROR(sqlite3_errmsg(db));
856                         fprintf(stderr, "cant bind content\n");
857                         zpm_rollback(pkg);
858                 munmap(content, sbuf.st_size);
859                         return 0;
860                 }
861                 sqlite3_bind_text(ifile, 3, hash, 64, SQLITE_STATIC);
862                 if (rc != SQLITE_OK) {
863                         SQLERROR(sqlite3_errmsg(db));
864                         fprintf(stderr, "cant bind hash\n");
865                         zpm_rollback(pkg);
866                 munmap(content, sbuf.st_size);
867                         return 0;
868                 }
869                 rc = sqlite3_step(ifile);
870                 if (rc != SQLITE_DONE) {
871                         SQLERROR(sqlite3_errmsg(db));
872                         sqlite3_finalize(ifile);
873                         zpm_rollback(pkg);
874                 munmap(content, sbuf.st_size);
875                         return 0;
876                 }
877                 sqlite3_finalize(ifile);
878
879                 /* commit */
880                 //zpm_commit(pkg);
881
882                 /* don't need the original file now */
883
884         }
885
886         if (!set_elf_info(pkg->db, hash, content, sbuf.st_size)) {
887                 fprintf(stderr, "setting elf info failed\n");
888                 munmap(content, sbuf.st_size);
889                 return 0;
890         }
891
892         munmap(content, sbuf.st_size);
893
894         /* if package and not nopackage flag, add to package */
895         if (pkg->pkgname && (!ZPM_NOPACKAGE)) {
896                 /* TODO */
897         }
898
899         /* return */
900         return 1;
901 }
902 #endif
903
904 #if 0
905 int main(int ac, char **av){
906         sqlite3 *db = 0;
907         int rc;
908
909         int blobsize;
910         int64_t size;
911         void *xzdata;
912         int type;
913         FILE *out;
914         sqlite3_stmt *ifile;
915
916         char *hash;
917         char *filename;
918
919         if (ac < 3) {
920                 fprintf(stderr, "usage: db hash file\n");
921                 return 1;
922         }
923
924         rc = sqlite3_open(av[1], &db);
925         if (rc) {
926                 SQLERROR(sqlite3_errmsg(db));
927                 sqlite3_close(db);
928                 return 1;
929         }
930
931 }
932 #endif
933
934 #if 0
935 Packages are sqlite databases
936
937 get application id and userver
938
939 Primitive operations:
940
941 add path to repo
942 associate path with package
943 associate blob with path?
944 add blob to repo
945 * extract blob to a path
946 compare blob to filesystem path
947 create package with info
948
949 Extra primitives:
950
951 record elf information about blob
952 compress blob
953 uncompress blob
954 sign a package?  What are we verifying?
955 #endif