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))
117 static char *dupstr(char *s) {
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 (?,?,?)";
135 rc = sqlite3_prepare(db, sql, -1, &ifile,0);
136 if (rc != SQLITE_OK) {
137 SQLERROR(sqlite3_errmsg(db));
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");
147 sqlite3_bind_text(ifile, 2, version, strlen(version), SQLITE_STATIC);
148 sqlite3_bind_int(ifile, 3, release)
150 rc = sqlite3_step(ifile);
152 if (rc != SQLITE_DONE) {
153 SQLERROR(sqlite3_errmsg(db));
154 sqlite3_finalize(ifile);
157 sqlite3_finalize(ifile);
158 z->pkg = dupstr(base);
159 z->version = dupstr(version);
160 z->release = release;
164 int zpm_begin(struct zpm *z) {
166 sqlite3_exec(z->db, "begin;", NULL, NULL, &errstr);
168 fprintf(stderr, "sqlite begin error: %s\n", errstr);
169 sqlite3_free(errstr);
175 int zpm_commit(struct zpm *z) {
177 sqlite3_exec(z->db, "commit;", NULL, NULL, &errstr);
179 fprintf(stderr, "sqlite commit error: %s\n", errstr);
180 sqlite3_free(errstr);
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);
191 int zpm_rollback(struct zpm *z) {
193 sqlite3_exec(z->db, "rollback;", NULL, NULL, &errstr);
195 fprintf(stderr, "sqlite rollback error: %s\n", errstr);
196 sqlite3_free(errstr);
202 int zpm_db_set_pragma(struct zpm *db, int pragma, int value) {
208 case 1: sql = "pragma application_id = ?;"; break;
209 case 2: sql = "pragma user_version = ?;"; break;
210 default: return -1; break;
213 rc = sqlite3_prepare_v2(db->db, sql, -1, &s, 0);
215 if (rc != SQLITE_OK) {
216 SQLERROR(sqlite3_errmsg(db->db));
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");
227 rc = sqlite3_step(s);
228 if (rc != SQLITE_DONE) {
229 SQLERROR(sqlite3_errmsg(db->db));
230 fprintf(stderr, "cant set pragma\n");
239 int zpm_db_pragma(struct zpm *db, int pragma) {
246 case 1: sql = "pragma application_id;"; break;
247 case 2: sql = "pragma user_version;"; break;
248 default: return -1; break;
251 rc = sqlite3_prepare_v2(db->db, sql, -1, &s, 0);
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? */
260 rc = sqlite3_step(s);
261 if (rc == SQLITE_ROW) {
262 value = sqlite3_column_int(s, 0);
272 int zpm_db_initialize(struct zpm *pkg) {
273 //fprintf(stderr, "initializing zpm database\n");
275 switch (sqlite3_exec(pkg->db, createdb, (int (*)(void *,int,char **,char **))0, NULL, &error)) {
276 case SQLITE_OK: break;
278 SQLERROR(sqlite3_errmsg(pkg->db));
279 fprintf(stderr, "error: %s\n", error);
288 int zpm_open(struct zpm *pkg, char *path) {
301 rc = sqlite3_open(path, &db);
303 SQLERROR(sqlite3_errmsg(db));
308 pkg->path = dupstr(path);
310 appid = zpm_db_pragma(pkg, 1);
311 dbver = zpm_db_pragma(pkg, 2);
313 //fprintf(stderr, "db appid = %x, dbver = %d\n", appid, dbver);
315 case 0: if (!zpm_db_initialize(pkg)) {
320 case 0x5a504442: break;
322 fprintf(stderr, "unknown database type\n");
328 fprintf(stderr, "version %d zpm db detected, this program only works with version 1 databases\n", dbver);
333 sqlite3_exec(pkg->db, "pragma foreign_keys = ON;", NULL, NULL, &errstr);
335 fprintf(stderr, "sqlite foreign key error: %s\n", errstr);
336 sqlite3_free(errstr);
342 /* TODO if this is a new database, create structures */
344 /* get a package. what if more than one? what if none? */
348 int zpm_close(struct zpm *pkg) {
350 sqlite3_close(pkg->db);
353 /* TODO free any malloced names and such */
357 static int zpm_sqlite_vercmp(void *not_used, int lena, const void *a,
358 int lenb, const void *b) {
359 /* not sure what the ints are, possibly string lengths */
360 if (not_used != 0) fprintf(stderr, "sqlite vercmp not_used = %p\n",
362 if (lena == 0 && lenb > 0) return 1;
363 return zpm_vercmp(a, b);
366 int zpm_addvercmp(struct zpm *pkg) {
367 return sqlite3_create_collation(
368 pkg->db, "vercmp", SQLITE_UTF8, NULL,
373 /* set package struct variables, database, environment, then command line */
374 int zpm_readopts(struct zpm *pkg, int ac, char **av) {
381 ev = getenv("ZPMPACKAGE");
383 pkg->pkgname = dupstr(ev);
385 ev = getenv("ZPMPKGREL");
387 pkg->release = strtol(ev, 0, 0);
389 ev = getenv("ZPMPKGVER");
391 pkg->version = dupstr(ev);
394 /* now, parse the options, return optind so the caller can adjust if needed */
399 int zpm_extract(struct zpm *pkg, char *hash, char *path, int mode) {
409 /* TODO check null */
410 sqlite3 *db = pkg->db;
412 rc = sqlite3_prepare(db, "select size, content from files where hash = ?", -1, &ifile,0);
413 if (rc != SQLITE_OK) {
414 SQLERROR(sqlite3_errmsg(db));
420 sqlite3_bind_text(ifile, 1, hash, 64, SQLITE_STATIC);
422 rc = sqlite3_step(ifile);
424 if (rc == SQLITE_DONE) {
425 /* didn't find a row */
426 sqlite3_finalize(ifile);
428 fprintf(stderr, "no such hash: %s\n", hash);
431 /* either way we're done with this now */
433 if (rc != SQLITE_ROW) {
434 SQLERROR(sqlite3_errmsg(db));
435 sqlite3_finalize(ifile);
440 type = sqlite3_column_type(ifile, 0);
441 if (type == SQLITE_NULL) {
442 fprintf(stderr, "no file size\n");
443 sqlite3_finalize(ifile);
447 type = sqlite3_column_type(ifile, 1);
448 if (type == SQLITE_NULL) {
449 fprintf(stderr, "no file data\n");
450 sqlite3_finalize(ifile);
454 //size = sqlite3_column_int64(ifile, 0);
455 xzdata = (void *)sqlite3_column_blob(ifile, 1);
456 blobsize = sqlite3_column_bytes(ifile, 1);
458 if (strcmp(path, "-")) {
459 out = fopen(path, "w");
464 fprintf(stderr, "can't open output file %s\n", path);
465 sqlite3_finalize(ifile);
469 //fwrite(xzdata, blobsize, 1, stdout);
471 //fprintf(stderr, "uncompressing %d bytes at %p, expect %lld\n", blobsize, xzdata, (long long int)size);
472 uncompresslzma(xzdata, blobsize, out);
476 sqlite3_finalize(ifile);
481 static sqlite3_stmt *run_for_hash(sqlite3 *db, char *sql, char *hash) {
485 rc = sqlite3_prepare_v2(db, sql, -1, &ifile, 0);
486 if (rc != SQLITE_OK) {
487 SQLERROR(sqlite3_errmsg(db));
493 sqlite3_bind_text(ifile, 1, hash, 64, SQLITE_STATIC);
498 static int set_elf_info(sqlite3 *db, char *hash, char *content, size_t length) {
499 if (length >= sizeof (Elf64_Ehdr) && libelf_iself(content)) {
508 /* go ahead and set up elf information now */
509 /* clear existing for this hash */
510 ifile = run_for_hash(db, "delete from elfinfo where file = ?", hash);
512 rc = sqlite3_step(ifile);
514 if (rc == SQLITE_ROW) {
516 fprintf(stderr, "delete row has %d columns: ", sqlite3_column_count(ifile));
517 nc = sqlite3_column_count(ifile);
518 for (i = 0; i < nc; i++) {
520 r = sqlite3_column_text(ifile, i);
521 fprintf(stderr, ", %s", r);
523 fprintf(stderr, "\n");
526 } while (rc == SQLITE_ROW);
527 if (rc != SQLITE_DONE) {
528 SQLERROR(sqlite3_errmsg(db));
529 sqlite3_finalize(ifile);
530 fprintf(stderr, "error clearing elf info: %d\n", rc);
533 sqlite3_finalize(ifile);
534 ifile = run_for_hash(db, "delete from elflibraries where file = ?", hash);
536 rc = sqlite3_step(ifile);
537 } while (rc == SQLITE_ROW);
538 if (rc != SQLITE_DONE) {
539 SQLERROR(sqlite3_errmsg(db));
540 sqlite3_finalize(ifile);
541 fprintf(stderr, "error clearing elf library: %d\n", rc);
544 sqlite3_finalize(ifile);
545 ifile = run_for_hash(db, "delete from elfneeded where file = ?", hash);
547 rc = sqlite3_step(ifile);
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 needed\n");
555 sqlite3_finalize(ifile);
557 hdr = libelf_header(content);
558 /* if lib, set soname */
559 if (libelf_type(content) == ET_DYN) {
560 char *soname = libelf_soname(content);
563 sqlite3_prepare_v2(db, "insert into elflibraries (file,soname) values (?,?)",-1, &ifile, 0);
564 sqlite3_bind_text(ifile,1,hash,64,SQLITE_STATIC);
565 sqlite3_bind_text(ifile,2,soname,-1,SQLITE_STATIC);
566 rc = sqlite3_step(ifile);
567 if (rc != SQLITE_DONE) {
568 SQLERROR(sqlite3_errmsg(db));
569 sqlite3_finalize(ifile);
570 fprintf(stderr, "error setting library soname\n");
573 sqlite3_finalize(ifile);
575 fprintf(stderr, "can't find soname\n");
579 /* if exe, set neededs */
580 if (libelf_type(content) == ET_EXEC) {
584 elf = (char *)content;
585 /* find program header table */
586 for (i = 0; i < hdr->e_phnum; i++) {
587 phdr = (Elf64_Phdr *)(elf + hdr->e_phoff + i * hdr->e_phentsize);
588 if (phdr->p_type == PT_DYNAMIC) {
589 dsect = (Elf64_Shdr *)(elf + phdr->p_offset);
592 dyn = (Elf64_Dyn *)(elf + dsect->sh_offset);
596 dyn = (Elf64_Dyn *)dsect;
598 dsect = libelf_section(elf, SHT_DYNAMIC);
601 strsect = libelf_section_n(elf, dsect->sh_link);
602 strtab = elf + strsect->sh_offset;
604 sqlite3_prepare_v2(db, "insert into elfneeded (file,needed) values (?,?)",-1, &ifile, 0);
605 sqlite3_bind_text(ifile,1,hash,64,SQLITE_STATIC);
606 while (dyn->d_tag != DT_NULL) {
607 if (dyn->d_tag == DT_NEEDED) {
611 need = strtab + dyn->d_un.d_val;
612 if (strlen(need) == 0) continue;
613 sqlite3_bind_text(ifile,2,need,strlen(need),SQLITE_STATIC);
615 fprintf(stderr, "%s needs %s\n", hash, need);
617 rc = sqlite3_step(ifile);
618 if (rc != SQLITE_DONE) {
619 SQLERROR(sqlite3_errmsg(db));
620 sqlite3_finalize(ifile);
621 fprintf(stderr, "error setting needed library\n");
624 sqlite3_reset(ifile);
628 sqlite3_finalize(ifile);
635 int zpm_import(struct zpm *pkg, char *path, uint32_t flags, char *hash) {
639 unsigned char tmp[32];
640 struct sha256_state md;
642 int haverow = 0,havedata = 0;
650 if (!pkg || !pkg->db || !path) {
654 /* use local if caller didn't pass in room */
660 fprintf(stderr, "zpm_import unused flags = %d\n", flags);
663 fd = open(path, O_RDONLY);
666 fprintf(stderr, "%s can't open %s: %s\n", __FUNCTION__, path,strerror(errno));
669 if (fstat(fd, &sbuf) == -1) {
671 fprintf(stderr, "%s can't fstat %s: %s\n", __FUNCTION__, path,strerror(errno));
674 /* not a regular file? */
675 if (!S_ISREG(sbuf.st_mode)) {
677 switch (sbuf.st_mode & S_IFMT) {
678 case S_IFSOCK: ftype = "socket"; break;
679 case S_IFLNK : ftype = "symlink"; break;
680 case S_IFBLK : ftype = "block device"; break;
681 case S_IFDIR : ftype = "directory"; break;
682 case S_IFCHR : ftype = "character device"; break;
683 case S_IFIFO : ftype = "fifo"; break;
684 default: ftype = "unknown file type"; break;
686 /* TODO this is ok, just stored differently */
687 fprintf(stderr, "%s can't import %s file: %s\n", __FUNCTION__, ftype, path);
692 content = mmap(0, sbuf.st_size, PROT_READ,MAP_PRIVATE, fd, 0);
696 fprintf(stderr, "%s can't mmap %s: %s\n", __FUNCTION__, path,strerror(errno));
702 sha256_process(&md, content, sbuf.st_size);
703 sha256_done(&md, tmp);
705 sprintf(hash+j*2, "%02x", (unsigned)tmp[j]);
708 //fprintf(stderr, "file %s: %s\n", path, hash);
710 /* TODO check null */
711 sqlite3 *db = pkg->db;
713 /* prepare and bind */
715 rc = sqlite3_prepare_v2(db, "select size, content is not null from files where hash = ?", -1, &ifile,0);
716 if (rc != SQLITE_OK) {
717 SQLERROR(sqlite3_errmsg(db));
718 munmap(content, sbuf.st_size);
724 sqlite3_bind_text(ifile, 1, hash, 64, SQLITE_STATIC);
726 rc = sqlite3_step(ifile);
728 if (rc != SQLITE_DONE) {
729 if (rc != SQLITE_ROW) {
730 /* didn't find a row */
731 SQLERROR(sqlite3_errmsg(db));
733 munmap(content, sbuf.st_size);
737 // fprintf(stderr, "have row for hash\n");
738 type = sqlite3_column_type(ifile, 0);
739 if (type == SQLITE_NULL) {
740 /* TODO assert, this shouldn't be possible? */
741 fprintf(stderr, "no file size\n");
742 sqlite3_finalize(ifile);
743 munmap(content, sbuf.st_size);
746 type = sqlite3_column_type(ifile, 1);
747 if (type == SQLITE_NULL) {
748 /* TODO assert, this shouldn't be possible? */
749 fprintf(stderr, "no file data\n");
750 sqlite3_finalize(ifile);
751 munmap(content, sbuf.st_size);
753 /* which is fine, just need to update the row then */
755 havedata = sqlite3_column_int(ifile, 1);
758 sqlite3_finalize(ifile);
762 outbuf = compresslzma(content, sbuf.st_size, &outlen);
764 fprintf(stderr, "compresslzma failed\n");
765 munmap(content, sbuf.st_size);
768 //fprintf(stderr, "compressed to %zu\n", outlen);
770 /* start a transaction */
771 // do that outside of here
776 //fprintf(stderr, "adding file data\n");
777 rc = sqlite3_prepare(db, "update files set size = ?, content = ? where hash = ?", -1, &ifile,0);
779 //fprintf(stderr, "creating new data row\n");
780 rc = sqlite3_prepare(db, "insert into files (size, content, hash) values (?,?,?)", -1, &ifile,0);
782 if (rc != SQLITE_OK) {
783 SQLERROR(sqlite3_errmsg(db));
784 fprintf(stderr, "cant prepare data\n");
786 munmap(content, sbuf.st_size);
790 sqlite3_bind_int64(ifile, 1, (sqlite3_int64)sbuf.st_size);
791 if (rc != SQLITE_OK) {
792 SQLERROR(sqlite3_errmsg(db));
793 fprintf(stderr, "cant bind size\n");
795 munmap(content, sbuf.st_size);
798 sqlite3_bind_blob64(ifile, 2, outbuf, (sqlite3_int64)outlen, SQLITE_STATIC);
799 if (rc != SQLITE_OK) {
800 SQLERROR(sqlite3_errmsg(db));
801 fprintf(stderr, "cant bind content\n");
803 munmap(content, sbuf.st_size);
806 sqlite3_bind_text(ifile, 3, hash, 64, SQLITE_STATIC);
807 if (rc != SQLITE_OK) {
808 SQLERROR(sqlite3_errmsg(db));
809 fprintf(stderr, "cant bind hash\n");
811 munmap(content, sbuf.st_size);
814 rc = sqlite3_step(ifile);
815 if (rc != SQLITE_DONE) {
816 SQLERROR(sqlite3_errmsg(db));
817 sqlite3_finalize(ifile);
819 munmap(content, sbuf.st_size);
822 sqlite3_finalize(ifile);
827 /* don't need the original file now */
831 if (!set_elf_info(pkg->db, hash, content, sbuf.st_size)) {
832 fprintf(stderr, "setting elf info failed\n");
833 munmap(content, sbuf.st_size);
837 munmap(content, sbuf.st_size);
839 /* if package and not nopackage flag, add to package */
840 if (pkg->pkgname && (!ZPM_NOPACKAGE)) {
850 int main(int ac, char **av){
865 fprintf(stderr, "usage: db hash file\n");
869 rc = sqlite3_open(av[1], &db);
871 SQLERROR(sqlite3_errmsg(db));
880 Packages are sqlite databases
882 get application id and userver
884 Primitive operations:
887 associate path with package
888 associate blob with path?
890 * extract blob to a path
891 compare blob to filesystem path
892 create package with info
896 record elf information about blob
899 sign a package? What are we verifying?