1 #define _POSIX_C_SOURCE 200809L
21 char *path; /* path to package file */
25 time_t installed; /* install time, 0 for not installed */
35 char *hash; /* could be fixed length */
37 struct zpm_file *next; /* so you can make a linked list */
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 */
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 */
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
59 int zpm_import(struct zpm *zp, char *path, uint32_t flags, uint8_t *hash);
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);
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);
70 /* export hash to dest */
71 int zpm_extract(struct zpm *pkg, char *hash, char *path, int mode);
73 /* export path to dest */
74 int zpm_export(struct zpm *zp, char *path, uint32_t flags, char *dest);
76 int zpm_close(struct zpm *zp);
78 /* attach a signature to a package */
79 int zpm_sign(struct zpm *z, size_t s, void *signature);
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);
85 /* get file information */
86 int zpm_stat(struct zpm *z, struct zpm_file *f, int n);
88 /* will also set the package context to the new package */
89 int zpm_newpkg(struct zpm *z, char *base, char *version, int release);
92 int zpm_begin(struct zpm *z);
93 int zpm_commit(struct zpm *z);
94 int zpm_rollback(struct zpm *z);
96 /* higher level operations */
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);
104 /* slurp up the actual blobs */
105 /* what about versioning them if they change */
106 int zpm_preserve(struct zpm *local);
108 /* check file integrity */
109 int zpm_checkinstall(struct zpm *local);
111 int zpm_merge(struct zpm *z, struct zpm *src, uint32_t flags);
113 void uncompresslzma(void *buf, size_t bufsize, FILE *out);
114 #define SQLERROR(x) fprintf(stderr, "%s %d: %s\n", __func__, __LINE__, (x))
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 (?,?,?)";
123 rc = sqlite3_prepare(db, sql, -1, &ifile,0);
124 if (rc != SQLITE_OK) {
125 SQLERROR(sqlite3_errmsg(db));
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");
135 sqlite3_bind_text(ifile, 2, version, strlen(version), SQLITE_STATIC);
136 sqlite3_bind_int(ifile, 3, release)
138 rc = sqlite3_step(ifile);
140 if (rc != SQLITE_DONE) {
141 SQLERROR(sqlite3_errmsg(db));
142 sqlite3_finalize(ifile);
145 sqlite3_finalize(ifile);
146 z->pkg = dupstr(base);
147 z->version = dupstr(version);
148 z->release = release;
152 int zpm_begin(struct zpm *z) {
154 sqlite3_exec(z->db, "begin;", NULL, NULL, &errstr);
156 fprintf(stderr, "sqlite begin error: %s\n", errstr);
157 sqlite3_free(errstr);
163 int zpm_commit(struct zpm *z) {
165 sqlite3_exec(z->db, "commit;", NULL, NULL, &errstr);
167 fprintf(stderr, "sqlite commit error: %s\n", errstr);
168 sqlite3_free(errstr);
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);
179 int zpm_rollback(struct zpm *z) {
181 sqlite3_exec(z->db, "rollback;", NULL, NULL, &errstr);
183 fprintf(stderr, "sqlite rollback error: %s\n", errstr);
184 sqlite3_free(errstr);
190 int zpm_db_set_pragma(struct zpm *db, int pragma, int value) {
196 case 1: sql = "pragma application_id = ?;"; break;
197 case 2: sql = "pragma user_version = ?;"; break;
198 default: return -1; break;
201 rc = sqlite3_prepare_v2(db->db, sql, -1, &s, 0);
203 if (rc != SQLITE_OK) {
204 SQLERROR(sqlite3_errmsg(db->db));
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");
215 rc = sqlite3_step(s);
216 if (rc != SQLITE_DONE) {
217 SQLERROR(sqlite3_errmsg(db->db));
218 fprintf(stderr, "cant set pragma\n");
227 int zpm_db_pragma(struct zpm *db, int pragma) {
234 case 1: sql = "pragma application_id;"; break;
235 case 2: sql = "pragma user_version;"; break;
236 default: return -1; break;
239 rc = sqlite3_prepare_v2(db->db, sql, -1, &s, 0);
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? */
248 rc = sqlite3_step(s);
249 if (rc == SQLITE_ROW) {
250 value = sqlite3_column_int(s, 0);
260 int zpm_db_initialize(struct zpm *pkg) {
261 //fprintf(stderr, "initializing zpm database\n");
263 switch (sqlite3_exec(pkg->db, createdb, (int (*)(void *,int,char **,char **))0, NULL, &error)) {
264 case SQLITE_OK: break;
266 SQLERROR(sqlite3_errmsg(pkg->db));
267 fprintf(stderr, "error: %s\n", error);
276 /* assumes pkg->db is set */
277 static int setupdb(struct zpm *zpm) {
283 appid = zpm_db_pragma(zpm, 1);
284 dbver = zpm_db_pragma(zpm, 2);
286 if (appid != 0x5a504442) {
287 fprintf(stderr, "unknown database type\n");
293 fprintf(stderr, "version %d zpm db detected, this program only works with version 1 databases\n", dbver);
298 sqlite3_exec(zpm->db, "pragma foreign_keys = ON;", NULL, NULL, &errstr);
302 zpm->errmsg = strdup(errstr);
303 fprintf(stderr, "sqlite foreign key error: %s\n", errstr);
304 sqlite3_free(errstr);
309 /* TODO add vercmp */
314 struct zpm *zpm_clearmem(struct zpm *zpm) {
316 zpm = malloc(sizeof *zpm);
320 *zpm = (struct zpm){0};
326 int zpm_init(struct zpm *pkg, char *path) {
333 rc = sqlite3_open_v2(path, &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
335 SQLERROR(sqlite3_errmsg(db));
340 pkg->path = strdup(path);
342 appid = zpm_db_pragma(pkg, 1);
345 case 0: if (!zpm_db_initialize(pkg)) {
351 /* already initialized */
354 fprintf(stderr, "unknown database type\n");
369 int zpm_open(struct zpm *zpm, char *path) {
375 rc = sqlite3_open_v2(path, &db, SQLITE_OPEN_READWRITE, NULL);
377 SQLERROR(sqlite3_errmsg(db));
380 fprintf(stderr, "path = %s\n", path);
384 zpm->path = strdup(path);
396 int zpm_close(struct zpm *pkg) {
398 sqlite3_close(pkg->db);
401 /* TODO free any malloced names and such */
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",
410 if (lena == 0 && lenb > 0) return 1;
411 return zpm_vercmp(a, b);
414 int zpm_addvercmp(struct zpm *pkg) {
415 return sqlite3_create_collation(
416 pkg->db, "vercmp", SQLITE_UTF8, NULL,
421 int zpm_extract(struct zpm *pkg, char *hash, char *path, int mode) {
431 /* TODO check null */
432 sqlite3 *db = pkg->db;
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));
442 sqlite3_bind_text(ifile, 1, hash, 64, SQLITE_STATIC);
444 rc = sqlite3_step(ifile);
446 if (rc == SQLITE_DONE) {
447 /* didn't find a row */
448 sqlite3_finalize(ifile);
450 fprintf(stderr, "no such hash: %s\n", hash);
453 /* either way we're done with this now */
455 if (rc != SQLITE_ROW) {
456 SQLERROR(sqlite3_errmsg(db));
457 sqlite3_finalize(ifile);
462 type = sqlite3_column_type(ifile, 0);
463 if (type == SQLITE_NULL) {
464 fprintf(stderr, "no file size\n");
465 sqlite3_finalize(ifile);
469 type = sqlite3_column_type(ifile, 1);
470 if (type == SQLITE_NULL) {
471 fprintf(stderr, "no file data\n");
472 sqlite3_finalize(ifile);
476 //size = sqlite3_column_int64(ifile, 0);
477 xzdata = (void *)sqlite3_column_blob(ifile, 1);
478 blobsize = sqlite3_column_bytes(ifile, 1);
480 if (strcmp(path, "-")) {
481 out = fopen(path, "w");
486 fprintf(stderr, "can't open output file %s\n", path);
487 sqlite3_finalize(ifile);
491 //fwrite(xzdata, blobsize, 1, stdout);
493 //fprintf(stderr, "uncompressing %d bytes at %p, expect %lld\n", blobsize, xzdata, (long long int)size);
494 uncompresslzma(xzdata, blobsize, out);
498 sqlite3_finalize(ifile);
503 static sqlite3_stmt *run_for_hash(sqlite3 *db, char *sql, char *hash) {
507 rc = sqlite3_prepare_v2(db, sql, -1, &ifile, 0);
508 if (rc != SQLITE_OK) {
509 SQLERROR(sqlite3_errmsg(db));
515 sqlite3_bind_text(ifile, 1, hash, 64, SQLITE_STATIC);
520 static int set_elf_info(sqlite3 *db, char *hash, char *content, size_t length) {
521 if (length >= sizeof (Elf64_Ehdr) && libelf_iself(content)) {
530 /* go ahead and set up elf information now */
531 /* clear existing for this hash */
532 ifile = run_for_hash(db, "delete from elfinfo where file = ?", hash);
534 rc = sqlite3_step(ifile);
536 if (rc == SQLITE_ROW) {
538 fprintf(stderr, "delete row has %d columns: ", sqlite3_column_count(ifile));
539 nc = sqlite3_column_count(ifile);
540 for (i = 0; i < nc; i++) {
542 r = sqlite3_column_text(ifile, i);
543 fprintf(stderr, ", %s", r);
545 fprintf(stderr, "\n");
548 } while (rc == SQLITE_ROW);
549 if (rc != SQLITE_DONE) {
550 SQLERROR(sqlite3_errmsg(db));
551 sqlite3_finalize(ifile);
552 fprintf(stderr, "error clearing elf info: %d\n", rc);
555 sqlite3_finalize(ifile);
556 ifile = run_for_hash(db, "delete from elflibraries where file = ?", hash);
558 rc = sqlite3_step(ifile);
559 } while (rc == SQLITE_ROW);
560 if (rc != SQLITE_DONE) {
561 SQLERROR(sqlite3_errmsg(db));
562 sqlite3_finalize(ifile);
563 fprintf(stderr, "error clearing elf library: %d\n", rc);
566 sqlite3_finalize(ifile);
567 ifile = run_for_hash(db, "delete from elfneeded where file = ?", hash);
569 rc = sqlite3_step(ifile);
570 } while (rc == SQLITE_ROW);
571 if (rc != SQLITE_DONE) {
572 SQLERROR(sqlite3_errmsg(db));
573 sqlite3_finalize(ifile);
574 fprintf(stderr, "error clearing elf needed\n");
577 sqlite3_finalize(ifile);
579 hdr = libelf_header(content);
580 /* if lib, set soname */
581 if (libelf_type(content) == ET_DYN) {
582 char *soname = libelf_soname(content);
585 sqlite3_prepare_v2(db, "insert into elflibraries (file,soname) values (?,?)",-1, &ifile, 0);
586 sqlite3_bind_text(ifile,1,hash,64,SQLITE_STATIC);
587 sqlite3_bind_text(ifile,2,soname,-1,SQLITE_STATIC);
588 rc = sqlite3_step(ifile);
589 if (rc != SQLITE_DONE) {
590 SQLERROR(sqlite3_errmsg(db));
591 sqlite3_finalize(ifile);
592 fprintf(stderr, "error setting library soname\n");
595 sqlite3_finalize(ifile);
597 fprintf(stderr, "can't find soname\n");
601 /* if exe, set neededs */
602 if (libelf_type(content) == ET_EXEC) {
606 elf = (char *)content;
607 /* find program header table */
608 for (i = 0; i < hdr->e_phnum; i++) {
609 phdr = (Elf64_Phdr *)(elf + hdr->e_phoff + i * hdr->e_phentsize);
610 if (phdr->p_type == PT_DYNAMIC) {
611 dsect = (Elf64_Shdr *)(elf + phdr->p_offset);
614 dyn = (Elf64_Dyn *)(elf + dsect->sh_offset);
618 dyn = (Elf64_Dyn *)dsect;
620 dsect = libelf_section(elf, SHT_DYNAMIC);
623 strsect = libelf_section_n(elf, dsect->sh_link);
624 strtab = elf + strsect->sh_offset;
626 sqlite3_prepare_v2(db, "insert into elfneeded (file,needed) values (?,?)",-1, &ifile, 0);
627 sqlite3_bind_text(ifile,1,hash,64,SQLITE_STATIC);
628 while (dyn->d_tag != DT_NULL) {
629 if (dyn->d_tag == DT_NEEDED) {
633 need = strtab + dyn->d_un.d_val;
634 if (strlen(need) == 0) continue;
635 sqlite3_bind_text(ifile,2,need,strlen(need),SQLITE_STATIC);
637 fprintf(stderr, "%s needs %s\n", hash, need);
639 rc = sqlite3_step(ifile);
640 if (rc != SQLITE_DONE) {
641 SQLERROR(sqlite3_errmsg(db));
642 sqlite3_finalize(ifile);
643 fprintf(stderr, "error setting needed library\n");
646 sqlite3_reset(ifile);
650 sqlite3_finalize(ifile);
657 int zpm_import(struct zpm *zpm, char *path, uint32_t flags, char *hash) {
661 unsigned char tmp[32];
662 struct sha256_state md;
664 int haverow = 0,havedata = 0;
672 if (!zpm || !zpm->db || !path) {
676 /* use local if caller didn't pass in room */
682 fprintf(stderr, "zpm_import unused flags = %d\n", flags);
685 fd = open(path, O_RDONLY);
688 fprintf(stderr, "%s can't open %s: %s\n", __FUNCTION__, path,strerror(errno));
691 if (fstat(fd, &sbuf) == -1) {
693 fprintf(stderr, "%s can't fstat %s: %s\n", __FUNCTION__, path,strerror(errno));
696 /* not a regular file? */
697 if (!S_ISREG(sbuf.st_mode)) {
699 switch (sbuf.st_mode & S_IFMT) {
700 case S_IFSOCK: ftype = "socket"; break;
701 case S_IFLNK : ftype = "symlink"; break;
702 case S_IFBLK : ftype = "block device"; break;
703 case S_IFDIR : ftype = "directory"; break;
704 case S_IFCHR : ftype = "character device"; break;
705 case S_IFIFO : ftype = "fifo"; break;
706 default: ftype = "unknown file type"; break;
708 /* TODO this is ok, just stored differently */
709 fprintf(stderr, "%s can't import %s file: %s\n", __FUNCTION__, ftype, path);
714 content = mmap(0, sbuf.st_size, PROT_READ,MAP_PRIVATE, fd, 0);
718 fprintf(stderr, "%s can't mmap %s: %s\n", __FUNCTION__, path,strerror(errno));
724 sha256_process(&md, content, sbuf.st_size);
725 sha256_done(&md, tmp);
727 sprintf(hash+j*2, "%02x", (unsigned)tmp[j]);
730 //fprintf(stderr, "file %s: %s\n", path, hash);
732 /* TODO check null */
733 sqlite3 *db = zpm->db;
735 /* prepare and bind */
737 rc = sqlite3_prepare_v2(db, "select size, content is not null from files where hash = ?", -1, &ifile,0);
738 if (rc != SQLITE_OK) {
739 SQLERROR(sqlite3_errmsg(db));
740 munmap(content, sbuf.st_size);
746 sqlite3_bind_text(ifile, 1, hash, 64, SQLITE_STATIC);
748 rc = sqlite3_step(ifile);
750 if (rc != SQLITE_DONE) {
751 if (rc != SQLITE_ROW) {
752 /* didn't find a row */
753 SQLERROR(sqlite3_errmsg(db));
755 munmap(content, sbuf.st_size);
759 // fprintf(stderr, "have row for hash\n");
760 type = sqlite3_column_type(ifile, 0);
761 if (type == SQLITE_NULL) {
762 /* TODO assert, this shouldn't be possible? */
763 fprintf(stderr, "no file size\n");
764 sqlite3_finalize(ifile);
765 munmap(content, sbuf.st_size);
768 type = sqlite3_column_type(ifile, 1);
769 if (type == SQLITE_NULL) {
770 /* TODO assert, this shouldn't be possible? */
771 fprintf(stderr, "no file data\n");
772 sqlite3_finalize(ifile);
773 munmap(content, sbuf.st_size);
775 /* which is fine, just need to update the row then */
777 havedata = sqlite3_column_int(ifile, 1);
780 sqlite3_finalize(ifile);
784 outbuf = compresslzma(content, sbuf.st_size, &outlen);
786 fprintf(stderr, "compresslzma failed\n");
787 munmap(content, sbuf.st_size);
790 //fprintf(stderr, "compressed to %zu\n", outlen);
792 /* start a transaction */
793 // do that outside of here
798 //fprintf(stderr, "adding file data\n");
799 rc = sqlite3_prepare(db, "update files set size = ?, content = ? where hash = ?", -1, &ifile,0);
801 //fprintf(stderr, "creating new data row\n");
802 rc = sqlite3_prepare(db, "insert into files (size, content, hash) values (?,?,?)", -1, &ifile,0);
804 if (rc != SQLITE_OK) {
805 SQLERROR(sqlite3_errmsg(db));
806 fprintf(stderr, "cant prepare data\n");
808 munmap(content, sbuf.st_size);
812 sqlite3_bind_int64(ifile, 1, (sqlite3_int64)sbuf.st_size);
813 if (rc != SQLITE_OK) {
814 SQLERROR(sqlite3_errmsg(db));
815 fprintf(stderr, "cant bind size\n");
817 munmap(content, sbuf.st_size);
820 sqlite3_bind_blob64(ifile, 2, outbuf, (sqlite3_int64)outlen, SQLITE_STATIC);
821 if (rc != SQLITE_OK) {
822 SQLERROR(sqlite3_errmsg(db));
823 fprintf(stderr, "cant bind content\n");
825 munmap(content, sbuf.st_size);
828 sqlite3_bind_text(ifile, 3, hash, 64, SQLITE_STATIC);
829 if (rc != SQLITE_OK) {
830 SQLERROR(sqlite3_errmsg(db));
831 fprintf(stderr, "cant bind hash\n");
833 munmap(content, sbuf.st_size);
836 rc = sqlite3_step(ifile);
837 if (rc != SQLITE_DONE) {
838 SQLERROR(sqlite3_errmsg(db));
839 sqlite3_finalize(ifile);
841 munmap(content, sbuf.st_size);
844 sqlite3_finalize(ifile);
849 /* don't need the original file now */
853 if (!set_elf_info(zpm->db, hash, content, sbuf.st_size)) {
854 fprintf(stderr, "setting elf info failed\n");
855 munmap(content, sbuf.st_size);
859 munmap(content, sbuf.st_size);
861 /* if package and not nopackage flag, add to package */
862 if (zpm->current_package->name && (!ZPM_NOPACKAGE)) {
872 int main(int ac, char **av){
887 fprintf(stderr, "usage: db hash file\n");
891 rc = sqlite3_open(av[1], &db);
893 SQLERROR(sqlite3_errmsg(db));
902 Packages are sqlite databases
904 get application id and userver
906 Primitive operations:
909 associate path with package
910 associate blob with path?
912 * extract blob to a path
913 compare blob to filesystem path
914 create package with info
918 record elf information about blob
921 sign a package? What are we verifying?