17 char *path; /* path to package file */
21 time_t installed; /* install time, 0 for not installed */
31 char *hash; /* could be fixed length */
33 struct zpm_file *next; /* so you can make a linked list */
37 /* associate with a package ? if only one? first? */
38 int zpm_open(struct zpm *pkg, char *path);
39 int zpm_pkgname(char *base, char *version, int release); /* construct a package file name */
41 /* flags for preserving mode, owner, etc */
42 /* puts hash of import in hash */
43 /* path can be a hash, with an "INTERNAL" flag, i.e. internally import */
47 #define ZPM_INTERNAL 0x8
48 #define ZPM_NOBLOB 0x10
49 /* don't run scripts on install */
50 #define ZPM_NOSCRIPTS 0x10
51 /* don't associate the file with a package, just do a raw insert */
52 /* otherwise, associate it with the current package */
53 #define ZPM_NOPACKAGE 0x20
55 int zpm_import(struct zpm *zp, char *path, uint32_t flags, uint8_t *hash);
57 /* link and unlink hashes to packages */
58 int zpm_link(struct zpm *pkg, char *path, char *hash, struct zpm_file *fileinfo);
59 int zpm_unlink(struct zpm *pkg, char *path);
61 /* tag a file. relative to "current package" */
62 int zpm_tag(struct zpm *zp, char *path, char *tags);
63 /* should this be broken up into separage functions ? */
64 int zpm_md(struct zpm *zp, char *path, int mode, char *owner, char *group, time_t mtime);
66 /* export hash to dest */
67 int zpm_extract(struct zpm *pkg, char *hash, char *path, int mode);
69 /* export path to dest */
70 int zpm_export(struct zpm *zp, char *path, uint32_t flags, char *dest);
72 int zpm_close(struct zpm *zp);
74 /* attach a signature to a package */
75 int zpm_sign(struct zpm *z, size_t s, void *signature);
77 /* set the package info to the nth package, -1 to return count? */
78 /* further import/exports and such will be relative to this package */
79 int zpm_package(struct zpm *zp, int n);
81 /* get file information */
82 int zpm_stat(struct zpm *z, struct zpm_file *f, int n);
84 /* will also set the package context to the new package */
85 int zpm_newpkg(struct zpm *z, char *base, char *version, int release);
88 int zpm_begin(struct zpm *z);
89 int zpm_commit(struct zpm *z);
90 int zpm_rollback(struct zpm *z);
92 /* higher level operations */
94 /* install or uninstall the package */
95 /* flag for keeping the blobs in local */
96 /* what about tag filtering */
97 int zpm_install(struct zpm *z, struct zpm *local, uint32_t flags);
98 int zpm_uninstall(struct zpm *local);
100 /* slurp up the actual blobs */
101 /* what about versioning them if they change */
102 int zpm_preserve(struct zpm *local);
104 /* check file integrity */
105 int zpm_checkinstall(struct zpm *local);
107 int zpm_merge(struct zpm *z, struct zpm *src, uint32_t flags);
109 void uncompresslzma(void *buf, size_t bufsize, FILE *out);
110 #define SQLERROR(x) fprintf(stderr, "%s %d: %s\n", __func__, __LINE__, (x))
113 static char *dupstr(char *s) {
126 int zpm_newpkg(struct zpm *z, char *base, char *version, int release) {
127 char *sql = "insert or ignore into packages (package,version,release) values (?,?,?)";
131 rc = sqlite3_prepare(db, sql, -1, &ifile,0);
132 if (rc != SQLITE_OK) {
133 SQLERROR(sqlite3_errmsg(db));
136 rc = sqlite3_bind_text(ifile, 1, base, strlen(base), SQLITE_STATIC);
137 if (rc != SQLITE_OK) {
138 SQLERROR(sqlite3_errmsg(db));
139 fprintf(stderr, "cant bind package name\n");
143 sqlite3_bind_text(ifile, 2, version, strlen(version), SQLITE_STATIC);
144 sqlite3_bind_int(ifile, 3, release)
146 rc = sqlite3_step(ifile);
148 if (rc != SQLITE_DONE) {
149 SQLERROR(sqlite3_errmsg(db));
150 sqlite3_finalize(ifile);
153 sqlite3_finalize(ifile);
154 z->pkg = dupstr(base);
155 z->version = dupstr(version);
156 z->release = release;
160 int zpm_begin(struct zpm *z) {
162 sqlite3_exec(z->db, "begin;", NULL, NULL, &errstr);
164 fprintf(stderr, "sqlite begin error: %s\n", errstr);
165 sqlite3_free(errstr);
171 int zpm_commit(struct zpm *z) {
173 sqlite3_exec(z->db, "commit;", NULL, NULL, &errstr);
175 fprintf(stderr, "sqlite commit error: %s\n", errstr);
176 sqlite3_free(errstr);
182 int zpm_rollback(struct zpm *z) {
184 sqlite3_exec(z->db, "rollback;", NULL, NULL, &errstr);
186 fprintf(stderr, "sqlite rollback error: %s\n", errstr);
187 sqlite3_free(errstr);
193 int zpm_db_set_pragma(struct zpm *db, int pragma, int value) {
199 case 1: sql = "pragma application_id = ?;"; break;
200 case 2: sql = "pragma user_version = ?;"; break;
201 default: return -1; break;
204 rc = sqlite3_prepare_v2(db->db, sql, -1, &s, 0);
206 if (rc != SQLITE_OK) {
207 SQLERROR(sqlite3_errmsg(db->db));
211 sqlite3_bind_int(s, 1, value);
212 if (rc != SQLITE_OK) {
213 SQLERROR(sqlite3_errmsg(db->db));
214 fprintf(stderr, "cant bind pragma value\n");
218 rc = sqlite3_step(s);
219 if (rc != SQLITE_DONE) {
220 SQLERROR(sqlite3_errmsg(db->db));
221 fprintf(stderr, "cant set pragma\n");
230 int zpm_db_pragma(struct zpm *db, int pragma) {
237 case 1: sql = "pragma application_id;"; break;
238 case 2: sql = "pragma user_version;"; break;
239 default: return -1; break;
242 rc = sqlite3_prepare_v2(db->db, sql, -1, &s, 0);
244 if (rc != SQLITE_OK) {
245 SQLERROR(sqlite3_errmsg(db->db));
246 fprintf(stderr, "%s, errnum = %d\n", sqlite3_errmsg(db->db), rc);
247 /* TODO just abort? */
251 rc = sqlite3_step(s);
252 if (rc == SQLITE_ROW) {
253 value = sqlite3_column_int(s, 0);
263 int zpm_db_initialize(struct zpm *pkg) {
264 //fprintf(stderr, "initializing zpm database\n");
265 switch (sqlite3_exec(pkg->db, createdb, (int (*)(void *,int,char **,char **))0, NULL, NULL)) {
266 case SQLITE_OK: break;
268 SQLERROR(sqlite3_errmsg(pkg->db));
276 int zpm_open(struct zpm *pkg, char *path) {
289 rc = sqlite3_open(path, &db);
291 SQLERROR(sqlite3_errmsg(db));
296 pkg->path = dupstr(path);
298 appid = zpm_db_pragma(pkg, 1);
299 dbver = zpm_db_pragma(pkg, 2);
301 //fprintf(stderr, "db appid = %x, dbver = %d\n", appid, dbver);
303 case 0: if (!zpm_db_initialize(pkg)) {
308 case 0x5a504442: break;
310 fprintf(stderr, "unknown database type\n");
316 fprintf(stderr, "version %d zpm db detected, this program only works with version 1 databases\n", dbver);
321 sqlite3_exec(pkg->db, "pragma foreign_keys = ON;", NULL, NULL, &errstr);
323 fprintf(stderr, "sqlite foreign key error: %s\n", errstr);
324 sqlite3_free(errstr);
330 /* TODO if this is a new database, create structures */
332 /* get a package. what if more than one? what if none? */
336 int zpm_close(struct zpm *pkg) {
338 sqlite3_close(pkg->db);
341 /* TODO free any malloced names and such */
345 int zpm_extract(struct zpm *pkg, char *hash, char *path, int mode) {
355 /* TODO check null */
356 sqlite3 *db = pkg->db;
358 rc = sqlite3_prepare(db, "select size, content from files where hash = ?", -1, &ifile,0);
359 if (rc != SQLITE_OK) {
360 SQLERROR(sqlite3_errmsg(db));
366 sqlite3_bind_text(ifile, 1, hash, 64, SQLITE_STATIC);
368 rc = sqlite3_step(ifile);
370 if (rc == SQLITE_DONE) {
371 /* didn't find a row */
372 sqlite3_finalize(ifile);
374 fprintf(stderr, "no such hash\n");
377 /* either way we're done with this now */
379 if (rc != SQLITE_ROW) {
380 SQLERROR(sqlite3_errmsg(db));
381 sqlite3_finalize(ifile);
386 type = sqlite3_column_type(ifile, 0);
387 if (type == SQLITE_NULL) {
388 fprintf(stderr, "no file size\n");
389 sqlite3_finalize(ifile);
393 type = sqlite3_column_type(ifile, 1);
394 if (type == SQLITE_NULL) {
395 fprintf(stderr, "no file data\n");
396 sqlite3_finalize(ifile);
400 size = sqlite3_column_int64(ifile, 0);
401 xzdata = (void *)sqlite3_column_blob(ifile, 1);
402 blobsize = sqlite3_column_bytes(ifile, 1);
404 out = fopen(path, "w");
406 fprintf(stderr, "can't open output file %s\n", path);
407 sqlite3_finalize(ifile);
411 //fwrite(xzdata, blobsize, 1, stdout);
413 //fprintf(stderr, "uncompressing %d bytes at %p, expect %lld\n", blobsize, xzdata, (long long int)size);
414 uncompresslzma(xzdata, blobsize, out);
417 sqlite3_finalize(ifile);
424 int zpm_import(struct zpm *pkg, char *path, uint32_t flags, char *hash) {
428 unsigned char tmp[32];
431 int haverow = 0,havedata = 0;
439 if (!pkg || !pkg->db || !path) {
443 /* use local if caller didn't pass in room */
449 fd = open(path, O_RDONLY);
451 fprintf(stderr, "%s can't open %s: %s\n", __FUNCTION__, path,strerror(errno));
454 if (fstat(fd, &sbuf) == -1) {
455 fprintf(stderr, "%s can't fstat %s: %s\n", __FUNCTION__, path,strerror(errno));
458 /* not a regular file? */
459 if (!S_ISREG(sbuf.st_mode)) {
460 /* TODO this is ok, just stored differently */
461 fprintf(stderr, "%s non-regular files unsupported %s\n", __FUNCTION__, path);
465 content = mmap(0, sbuf.st_size, PROT_READ,MAP_PRIVATE, fd, 0);
467 fprintf(stderr, "%s can't mmap %s: %s\n", __FUNCTION__, path,strerror(errno));
473 sha256_process(&md, content, sbuf.st_size);
474 sha256_done(&md, tmp);
476 sprintf(hash+j*2, "%02x", (unsigned)tmp[j]);
479 fprintf(stderr, "file %s: %s\n", path, hash);
481 /* prepare and bind */
482 /* TODO check null */
483 sqlite3 *db = pkg->db;
485 rc = sqlite3_prepare_v2(db, "select size, content is not null from files where hash = ?", -1, &ifile,0);
486 if (rc != SQLITE_OK) {
487 SQLERROR(sqlite3_errmsg(db));
493 sqlite3_bind_text(ifile, 1, hash, 64, SQLITE_STATIC);
495 rc = sqlite3_step(ifile);
497 if (rc != SQLITE_DONE) {
498 if (rc != SQLITE_ROW) {
499 /* didn't find a row */
500 SQLERROR(sqlite3_errmsg(db));
505 // fprintf(stderr, "have row for hash\n");
506 type = sqlite3_column_type(ifile, 0);
507 if (type == SQLITE_NULL) {
508 /* TODO assert, this shouldn't be possible? */
509 fprintf(stderr, "no file size\n");
510 sqlite3_finalize(ifile);
513 type = sqlite3_column_type(ifile, 1);
514 if (type == SQLITE_NULL) {
515 /* TODO assert, this shouldn't be possible? */
516 fprintf(stderr, "no file data\n");
517 sqlite3_finalize(ifile);
519 /* which is fine, just need to update the row then */
521 havedata = sqlite3_column_int(ifile, 1);
524 sqlite3_finalize(ifile);
528 outbuf = compresslzma(content, sbuf.st_size, &outlen);
530 fprintf(stderr, "compresslzma failed\n");
533 fprintf(stderr, "compressed to %zu\n", outlen);
534 /* don't need the original file now */
535 munmap(content, sbuf.st_size);
538 /* start a transaction */
539 // do that outside of here
544 fprintf(stderr, "adding file data\n");
545 rc = sqlite3_prepare(db, "update files set size = ?, content = ? where hash = ?", -1, &ifile,0);
547 fprintf(stderr, "creating new data row\n");
548 rc = sqlite3_prepare(db, "insert into files (size, content, hash) values (?,?,?)", -1, &ifile,0);
550 if (rc != SQLITE_OK) {
551 SQLERROR(sqlite3_errmsg(db));
552 fprintf(stderr, "cant prepare data\n");
557 sqlite3_bind_int64(ifile, 1, (sqlite3_int64)sbuf.st_size);
558 if (rc != SQLITE_OK) {
559 SQLERROR(sqlite3_errmsg(db));
560 fprintf(stderr, "cant bind size\n");
564 sqlite3_bind_blob64(ifile, 2, outbuf, (sqlite3_int64)outlen, SQLITE_STATIC);
565 if (rc != SQLITE_OK) {
566 SQLERROR(sqlite3_errmsg(db));
567 fprintf(stderr, "cant bind content\n");
571 sqlite3_bind_text(ifile, 3, hash, 64, SQLITE_STATIC);
572 if (rc != SQLITE_OK) {
573 SQLERROR(sqlite3_errmsg(db));
574 fprintf(stderr, "cant bind hash\n");
578 rc = sqlite3_step(ifile);
579 if (rc != SQLITE_DONE) {
580 SQLERROR(sqlite3_errmsg(db));
581 sqlite3_finalize(ifile);
585 sqlite3_finalize(ifile);
591 /* don't need the original file now */
592 munmap(content, sbuf.st_size);
597 /* if package and not nopackage flag, add to package */
598 if (pkg->pkgname && (!ZPM_NOPACKAGE)) {
608 int main(int ac, char **av){
623 fprintf(stderr, "usage: db hash file\n");
627 rc = sqlite3_open(av[1], &db);
629 SQLERROR(sqlite3_errmsg(db));
638 Packages are sqlite databases
640 get application id and userver
642 Primitive operations:
645 associate path with package
646 associate blob with path?
648 * extract blob to a path
649 compare blob to filesystem path
650 create package with info
654 record elf information about blob
657 sign a package? What are we verifying?