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