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