1 #define _POSIX_C_SOURCE 200809L
20 char *path; /* path to package file */
24 time_t installed; /* install time, 0 for not installed */
34 char *hash; /* could be fixed length */
36 struct zpm_file *next; /* so you can make a linked list */
40 /* associate with a package ? if only one? first? */
41 int zpm_open(struct zpm *pkg, char *path);
42 int zpm_pkgname(char *base, char *version, int release); /* construct a package file name */
44 /* flags for preserving mode, owner, etc */
45 /* puts hash of import in hash */
46 /* path can be a hash, with an "INTERNAL" flag, i.e. internally import */
50 #define ZPM_INTERNAL 0x8
51 #define ZPM_NOBLOB 0x10
52 /* don't run scripts on install */
53 #define ZPM_NOSCRIPTS 0x10
54 /* don't associate the file with a package, just do a raw insert */
55 /* otherwise, associate it with the current package */
56 #define ZPM_NOPACKAGE 0x20
58 int zpm_import(struct zpm *zp, char *path, uint32_t flags, uint8_t *hash);
60 /* link and unlink hashes to packages */
61 int zpm_link(struct zpm *pkg, char *path, char *hash, struct zpm_file *fileinfo);
62 int zpm_unlink(struct zpm *pkg, char *path);
64 /* tag a file. relative to "current package" */
65 int zpm_tag(struct zpm *zp, char *path, char *tags);
66 /* should this be broken up into separage functions ? */
67 int zpm_md(struct zpm *zp, char *path, int mode, char *owner, char *group, time_t mtime);
69 /* export hash to dest */
70 int zpm_extract(struct zpm *pkg, char *hash, char *path, int mode);
72 /* export path to dest */
73 int zpm_export(struct zpm *zp, char *path, uint32_t flags, char *dest);
75 int zpm_close(struct zpm *zp);
77 /* attach a signature to a package */
78 int zpm_sign(struct zpm *z, size_t s, void *signature);
80 /* set the package info to the nth package, -1 to return count? */
81 /* further import/exports and such will be relative to this package */
82 int zpm_package(struct zpm *zp, int n);
84 /* get file information */
85 int zpm_stat(struct zpm *z, struct zpm_file *f, int n);
87 /* will also set the package context to the new package */
88 int zpm_newpkg(struct zpm *z, char *base, char *version, int release);
91 int zpm_begin(struct zpm *z);
92 int zpm_commit(struct zpm *z);
93 int zpm_rollback(struct zpm *z);
95 /* higher level operations */
97 /* install or uninstall the package */
98 /* flag for keeping the blobs in local */
99 /* what about tag filtering */
100 int zpm_install(struct zpm *z, struct zpm *local, uint32_t flags);
101 int zpm_uninstall(struct zpm *local);
103 /* slurp up the actual blobs */
104 /* what about versioning them if they change */
105 int zpm_preserve(struct zpm *local);
107 /* check file integrity */
108 int zpm_checkinstall(struct zpm *local);
110 int zpm_merge(struct zpm *z, struct zpm *src, uint32_t flags);
112 void uncompresslzma(void *buf, size_t bufsize, FILE *out);
113 #define SQLERROR(x) fprintf(stderr, "%s %d: %s\n", __func__, __LINE__, (x))
116 static char *dupstr(char *s) {
129 int zpm_newpkg(struct zpm *z, char *base, char *version, int release) {
130 char *sql = "insert or ignore into packages (package,version,release) values (?,?,?)";
134 rc = sqlite3_prepare(db, sql, -1, &ifile,0);
135 if (rc != SQLITE_OK) {
136 SQLERROR(sqlite3_errmsg(db));
139 rc = sqlite3_bind_text(ifile, 1, base, strlen(base), SQLITE_STATIC);
140 if (rc != SQLITE_OK) {
141 SQLERROR(sqlite3_errmsg(db));
142 fprintf(stderr, "cant bind package name\n");
146 sqlite3_bind_text(ifile, 2, version, strlen(version), SQLITE_STATIC);
147 sqlite3_bind_int(ifile, 3, release)
149 rc = sqlite3_step(ifile);
151 if (rc != SQLITE_DONE) {
152 SQLERROR(sqlite3_errmsg(db));
153 sqlite3_finalize(ifile);
156 sqlite3_finalize(ifile);
157 z->pkg = dupstr(base);
158 z->version = dupstr(version);
159 z->release = release;
163 int zpm_begin(struct zpm *z) {
165 sqlite3_exec(z->db, "begin;", NULL, NULL, &errstr);
167 fprintf(stderr, "sqlite begin error: %s\n", errstr);
168 sqlite3_free(errstr);
174 int zpm_commit(struct zpm *z) {
176 sqlite3_exec(z->db, "commit;", NULL, NULL, &errstr);
178 fprintf(stderr, "sqlite commit error: %s\n", errstr);
179 sqlite3_free(errstr);
185 int zpm_rollback(struct zpm *z) {
187 sqlite3_exec(z->db, "rollback;", NULL, NULL, &errstr);
189 fprintf(stderr, "sqlite rollback error: %s\n", errstr);
190 sqlite3_free(errstr);
196 int zpm_db_set_pragma(struct zpm *db, int pragma, int value) {
202 case 1: sql = "pragma application_id = ?;"; break;
203 case 2: sql = "pragma user_version = ?;"; break;
204 default: return -1; break;
207 rc = sqlite3_prepare_v2(db->db, sql, -1, &s, 0);
209 if (rc != SQLITE_OK) {
210 SQLERROR(sqlite3_errmsg(db->db));
214 sqlite3_bind_int(s, 1, value);
215 if (rc != SQLITE_OK) {
216 SQLERROR(sqlite3_errmsg(db->db));
217 fprintf(stderr, "cant bind pragma value\n");
221 rc = sqlite3_step(s);
222 if (rc != SQLITE_DONE) {
223 SQLERROR(sqlite3_errmsg(db->db));
224 fprintf(stderr, "cant set pragma\n");
233 int zpm_db_pragma(struct zpm *db, int pragma) {
240 case 1: sql = "pragma application_id;"; break;
241 case 2: sql = "pragma user_version;"; break;
242 default: return -1; break;
245 rc = sqlite3_prepare_v2(db->db, sql, -1, &s, 0);
247 if (rc != SQLITE_OK) {
248 SQLERROR(sqlite3_errmsg(db->db));
249 fprintf(stderr, "%s, errnum = %d\n", sqlite3_errmsg(db->db), rc);
250 /* TODO just abort? */
254 rc = sqlite3_step(s);
255 if (rc == SQLITE_ROW) {
256 value = sqlite3_column_int(s, 0);
266 int zpm_db_initialize(struct zpm *pkg) {
267 //fprintf(stderr, "initializing zpm database\n");
268 switch (sqlite3_exec(pkg->db, createdb, (int (*)(void *,int,char **,char **))0, NULL, NULL)) {
269 case SQLITE_OK: break;
271 SQLERROR(sqlite3_errmsg(pkg->db));
279 int zpm_open(struct zpm *pkg, char *path) {
292 rc = sqlite3_open(path, &db);
294 SQLERROR(sqlite3_errmsg(db));
299 pkg->path = dupstr(path);
301 appid = zpm_db_pragma(pkg, 1);
302 dbver = zpm_db_pragma(pkg, 2);
304 //fprintf(stderr, "db appid = %x, dbver = %d\n", appid, dbver);
306 case 0: if (!zpm_db_initialize(pkg)) {
311 case 0x5a504442: break;
313 fprintf(stderr, "unknown database type\n");
319 fprintf(stderr, "version %d zpm db detected, this program only works with version 1 databases\n", dbver);
324 sqlite3_exec(pkg->db, "pragma foreign_keys = ON;", NULL, NULL, &errstr);
326 fprintf(stderr, "sqlite foreign key error: %s\n", errstr);
327 sqlite3_free(errstr);
333 /* TODO if this is a new database, create structures */
335 /* get a package. what if more than one? what if none? */
339 int zpm_close(struct zpm *pkg) {
341 sqlite3_close(pkg->db);
344 /* TODO free any malloced names and such */
348 /* set package struct variables, database, environment, then command line */
349 int zpm_readopts(struct zpm *pkg, int ac, char **av) {
356 ev = getenv("ZPMPACKAGE");
358 pkg->pkgname = dupstr(ev);
360 ev = getenv("ZPMPKGREL");
362 pkg->release = strtol(ev, 0, 0);
364 ev = getenv("ZPMPKGVER");
366 pkg->version = dupstr(ev);
369 /* now, parse the options, return optind so the caller can adjust if needed */
374 int zpm_extract(struct zpm *pkg, char *hash, char *path, int mode) {
384 /* TODO check null */
385 sqlite3 *db = pkg->db;
387 rc = sqlite3_prepare(db, "select size, content from files where hash = ?", -1, &ifile,0);
388 if (rc != SQLITE_OK) {
389 SQLERROR(sqlite3_errmsg(db));
395 sqlite3_bind_text(ifile, 1, hash, 64, SQLITE_STATIC);
397 rc = sqlite3_step(ifile);
399 if (rc == SQLITE_DONE) {
400 /* didn't find a row */
401 sqlite3_finalize(ifile);
403 fprintf(stderr, "no such hash\n");
406 /* either way we're done with this now */
408 if (rc != SQLITE_ROW) {
409 SQLERROR(sqlite3_errmsg(db));
410 sqlite3_finalize(ifile);
415 type = sqlite3_column_type(ifile, 0);
416 if (type == SQLITE_NULL) {
417 fprintf(stderr, "no file size\n");
418 sqlite3_finalize(ifile);
422 type = sqlite3_column_type(ifile, 1);
423 if (type == SQLITE_NULL) {
424 fprintf(stderr, "no file data\n");
425 sqlite3_finalize(ifile);
429 //size = sqlite3_column_int64(ifile, 0);
430 xzdata = (void *)sqlite3_column_blob(ifile, 1);
431 blobsize = sqlite3_column_bytes(ifile, 1);
433 out = fopen(path, "w");
435 fprintf(stderr, "can't open output file %s\n", path);
436 sqlite3_finalize(ifile);
440 //fwrite(xzdata, blobsize, 1, stdout);
442 //fprintf(stderr, "uncompressing %d bytes at %p, expect %lld\n", blobsize, xzdata, (long long int)size);
443 uncompresslzma(xzdata, blobsize, out);
446 sqlite3_finalize(ifile);
452 /* flags 0, close mmap, flags 1, return mmap fd */
453 int zpm_hash(char *path, char *hash, uint32_t flags) {
459 unsigned char tmp[32];
462 fd = open(path, O_RDONLY);
464 fprintf(stderr, "%s can't open %s: %s\n", __FUNCTION__, path,strerror(errno));
467 if (fstat(fd, &sbuf) == -1) {
468 fprintf(stderr, "%s can't fstat %s: %s\n", __FUNCTION__, path,strerror(errno));
471 /* not a regular file? */
472 if (!S_ISREG(sbuf.st_mode)) {
473 /* TODO this is ok, just stored differently */
474 fprintf(stderr, "%s non-regular files unsupported %s\n", __FUNCTION__, path);
478 content = mmap(0, sbuf.st_size, PROT_READ,MAP_PRIVATE, fd, 0);
481 fprintf(stderr, "%s can't mmap %s: %s\n", __FUNCTION__, path,strerror(errno));
487 sha256_process(&md, content, sbuf.st_size);
488 sha256_done(&md, tmp);
490 sprintf(hash+j*2, "%02x", (unsigned)tmp[j]);
493 munmap(content, sbuf.st_size);
498 int zpm_import(struct zpm *pkg, char *path, uint32_t flags, char *hash) {
502 unsigned char tmp[32];
505 int haverow = 0,havedata = 0;
513 if (!pkg || !pkg->db || !path) {
517 /* use local if caller didn't pass in room */
523 fd = open(path, O_RDONLY);
526 fprintf(stderr, "%s can't open %s: %s\n", __FUNCTION__, path,strerror(errno));
529 if (fstat(fd, &sbuf) == -1) {
531 fprintf(stderr, "%s can't fstat %s: %s\n", __FUNCTION__, path,strerror(errno));
534 /* not a regular file? */
535 if (!S_ISREG(sbuf.st_mode)) {
537 switch (sbuf.st_mode & S_IFMT) {
538 case S_IFSOCK: ftype = "socket"; break;
539 case S_IFLNK : ftype = "symlink"; break;
540 case S_IFBLK : ftype = "block device"; break;
541 case S_IFDIR : ftype = "directory"; break;
542 case S_IFCHR : ftype = "character device"; break;
543 case S_IFIFO : ftype = "fifo"; break;
544 default: ftype = "unknown file type"; break;
546 /* TODO this is ok, just stored differently */
547 fprintf(stderr, "%s can't import %s file: %s\n", __FUNCTION__, ftype, path);
552 content = mmap(0, sbuf.st_size, PROT_READ,MAP_PRIVATE, fd, 0);
555 fprintf(stderr, "%s can't mmap %s: %s\n", __FUNCTION__, path,strerror(errno));
561 sha256_process(&md, content, sbuf.st_size);
562 sha256_done(&md, tmp);
564 sprintf(hash+j*2, "%02x", (unsigned)tmp[j]);
567 //fprintf(stderr, "file %s: %s\n", path, hash);
569 /* prepare and bind */
570 /* TODO check null */
571 sqlite3 *db = pkg->db;
573 rc = sqlite3_prepare_v2(db, "select size, content is not null from files where hash = ?", -1, &ifile,0);
574 if (rc != SQLITE_OK) {
575 SQLERROR(sqlite3_errmsg(db));
581 sqlite3_bind_text(ifile, 1, hash, 64, SQLITE_STATIC);
583 rc = sqlite3_step(ifile);
585 if (rc != SQLITE_DONE) {
586 if (rc != SQLITE_ROW) {
587 /* didn't find a row */
588 SQLERROR(sqlite3_errmsg(db));
593 // fprintf(stderr, "have row for hash\n");
594 type = sqlite3_column_type(ifile, 0);
595 if (type == SQLITE_NULL) {
596 /* TODO assert, this shouldn't be possible? */
597 fprintf(stderr, "no file size\n");
598 sqlite3_finalize(ifile);
601 type = sqlite3_column_type(ifile, 1);
602 if (type == SQLITE_NULL) {
603 /* TODO assert, this shouldn't be possible? */
604 fprintf(stderr, "no file data\n");
605 sqlite3_finalize(ifile);
607 /* which is fine, just need to update the row then */
609 havedata = sqlite3_column_int(ifile, 1);
612 sqlite3_finalize(ifile);
616 outbuf = compresslzma(content, sbuf.st_size, &outlen);
618 fprintf(stderr, "compresslzma failed\n");
621 //fprintf(stderr, "compressed to %zu\n", outlen);
622 /* don't need the original file now */
623 munmap(content, sbuf.st_size);
626 /* start a transaction */
627 // do that outside of here
632 //fprintf(stderr, "adding file data\n");
633 rc = sqlite3_prepare(db, "update files set size = ?, content = ? where hash = ?", -1, &ifile,0);
635 //fprintf(stderr, "creating new data row\n");
636 rc = sqlite3_prepare(db, "insert into files (size, content, hash) values (?,?,?)", -1, &ifile,0);
638 if (rc != SQLITE_OK) {
639 SQLERROR(sqlite3_errmsg(db));
640 fprintf(stderr, "cant prepare data\n");
645 sqlite3_bind_int64(ifile, 1, (sqlite3_int64)sbuf.st_size);
646 if (rc != SQLITE_OK) {
647 SQLERROR(sqlite3_errmsg(db));
648 fprintf(stderr, "cant bind size\n");
652 sqlite3_bind_blob64(ifile, 2, outbuf, (sqlite3_int64)outlen, SQLITE_STATIC);
653 if (rc != SQLITE_OK) {
654 SQLERROR(sqlite3_errmsg(db));
655 fprintf(stderr, "cant bind content\n");
659 sqlite3_bind_text(ifile, 3, hash, 64, SQLITE_STATIC);
660 if (rc != SQLITE_OK) {
661 SQLERROR(sqlite3_errmsg(db));
662 fprintf(stderr, "cant bind hash\n");
666 rc = sqlite3_step(ifile);
667 if (rc != SQLITE_DONE) {
668 SQLERROR(sqlite3_errmsg(db));
669 sqlite3_finalize(ifile);
673 sqlite3_finalize(ifile);
679 /* don't need the original file now */
680 munmap(content, sbuf.st_size);
685 /* if package and not nopackage flag, add to package */
686 if (pkg->pkgname && (!ZPM_NOPACKAGE)) {
696 int main(int ac, char **av){
711 fprintf(stderr, "usage: db hash file\n");
715 rc = sqlite3_open(av[1], &db);
717 SQLERROR(sqlite3_errmsg(db));
726 Packages are sqlite databases
728 get application id and userver
730 Primitive operations:
733 associate path with package
734 associate blob with path?
736 * extract blob to a path
737 compare blob to filesystem path
738 create package with info
742 record elf information about blob
745 sign a package? What are we verifying?