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