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