16 char *path; /* path to package file */
20 time_t installed; /* install time, 0 for not installed */
30 char *hash; /* could be fixed length */
32 struct zpm_file *next; /* so you can make a linked list */
36 /* associate with a package ? if only one? first? */
37 int zpm_open(struct zpm *pkg, char *path);
38 int zpm_pkgname(char *base, char *version, int release); /* construct a package file name */
40 /* flags for preserving mode, owner, etc */
41 /* puts hash of import in hash */
42 /* path can be a hash, with an "INTERNAL" flag, i.e. internally import */
46 #define ZPM_INTERNAL 0x8
47 #define ZPM_NOBLOB 0x10
48 /* don't run scripts on install */
49 #define ZPM_NOSCRIPTS 0x10
50 /* don't associate the file with a package, just do a raw insert */
51 /* otherwise, associate it with the current package */
52 #define ZPM_NOPACKAGE 0x20
54 int zpm_import(struct zpm *zp, char *path, uint32_t flags, uint8_t *hash);
56 /* link and unlink hashes to packages */
57 int zpm_link(struct zpm *pkg, char *path, char *hash, struct zpm_file *fileinfo);
58 int zpm_unlink(struct zpm *pkg, char *path);
60 /* tag a file. relative to "current package" */
61 int zpm_tag(struct zpm *zp, char *path, char *tags);
62 /* should this be broken up into separage functions ? */
63 int zpm_md(struct zpm *zp, char *path, int mode, char *owner, char *group, time_t mtime);
65 /* export hash to dest */
66 int zpm_extract(struct zpm *pkg, char *hash, char *path, int mode);
68 /* export path to dest */
69 int zpm_export(struct zpm *zp, char *path, uint32_t flags, char *dest);
71 int zpm_close(struct zpm *zp);
73 /* attach a signature to a package */
74 int zpm_sign(struct zpm *z, size_t s, void *signature);
76 /* set the package info to the nth package, -1 to return count? */
77 /* further import/exports and such will be relative to this package */
78 int zpm_package(struct zpm *zp, int n);
80 /* get file information */
81 int zpm_stat(struct zpm *z, struct zpm_file *f, int n);
83 /* will also set the package context to the new package */
84 int zpm_newpkg(struct zpm *z, char *base, char *version, int release);
87 int zpm_begin(struct zpm *z);
88 int zpm_commit(struct zpm *z);
89 int zpm_rollback(struct zpm *z);
91 /* higher level operations */
93 /* install or uninstall the package */
94 /* flag for keeping the blobs in local */
95 /* what about tag filtering */
96 int zpm_install(struct zpm *z, struct zpm *local, uint32_t flags);
97 int zpm_uninstall(struct zpm *local);
99 /* slurp up the actual blobs */
100 /* what about versioning them if they change */
101 int zpm_preserve(struct zpm *local);
103 /* check file integrity */
104 int zpm_checkinstall(struct zpm *local);
106 int zpm_merge(struct zpm *z, struct zpm *src, uint32_t flags);
108 void uncompresslzma(void *buf, size_t bufsize, FILE *out);
109 #define SQLERROR(x) fprintf(stderr, "%s %d: %s\n", __func__, __LINE__, (x))
112 static char *dupstr(char *s) {
125 int zpm_newpkg(struct zpm *z, char *base, char *version, int release) {
126 char *sql = "insert or ignore into packages (package,version,release) values (?,?,?)";
130 rc = sqlite3_prepare(db, sql, -1, &ifile,0);
131 if (rc != SQLITE_OK) {
132 SQLERROR(sqlite3_errmsg(db));
135 rc = sqlite3_bind_text(ifile, 1, base, strlen(base), SQLITE_STATIC);
136 if (rc != SQLITE_OK) {
137 SQLERROR(sqlite3_errmsg(db));
138 fprintf(stderr, "cant bind package name\n");
142 sqlite3_bind_text(ifile, 2, version, strlen(version), SQLITE_STATIC);
143 sqlite3_bind_int(ifile, 3, release)
145 rc = sqlite3_step(ifile);
147 if (rc != SQLITE_DONE) {
148 SQLERROR(sqlite3_errmsg(db));
149 sqlite3_finalize(ifile);
152 sqlite3_finalize(ifile);
153 z->pkg = dupstr(base);
154 z->version = dupstr(version);
155 z->release = release;
159 int zpm_begin(struct zpm *z) {
161 sqlite3_exec(z->db, "begin;", NULL, NULL, &errstr);
163 fprintf(stderr, "sqlite begin error: %s\n", errstr);
164 sqlite3_free(errstr);
170 int zpm_commit(struct zpm *z) {
172 sqlite3_exec(z->db, "commit;", NULL, NULL, &errstr);
174 fprintf(stderr, "sqlite commit error: %s\n", errstr);
175 sqlite3_free(errstr);
181 int zpm_rollback(struct zpm *z) {
183 sqlite3_exec(z->db, "rollback;", NULL, NULL, &errstr);
185 fprintf(stderr, "sqlite rollback error: %s\n", errstr);
186 sqlite3_free(errstr);
192 int zpm_db_set_pragma(struct zpm *db, int pragma, int value) {
198 case 1: sql = "pragma application_id = ?;"; break;
199 case 2: sql = "pragma user_version = ?;"; break;
200 default: return -1; break;
203 rc = sqlite3_prepare_v2(db->db, sql, -1, &s, 0);
205 if (rc != SQLITE_OK) {
206 SQLERROR(sqlite3_errmsg(db->db));
210 sqlite3_bind_int(s, 1, value);
211 if (rc != SQLITE_OK) {
212 SQLERROR(sqlite3_errmsg(db->db));
213 fprintf(stderr, "cant bind pragma value\n");
217 rc = sqlite3_step(s);
218 if (rc != SQLITE_DONE) {
219 SQLERROR(sqlite3_errmsg(db->db));
220 fprintf(stderr, "cant set pragma\n");
229 int zpm_db_pragma(struct zpm *db, int pragma) {
236 case 1: sql = "pragma application_id;"; break;
237 case 2: sql = "pragma user_version;"; break;
238 default: return -1; break;
241 rc = sqlite3_prepare_v2(db->db, sql, -1, &s, 0);
243 if (rc != SQLITE_OK) {
244 SQLERROR(sqlite3_errmsg(db->db));
245 fprintf(stderr, "%s, errnum = %d\n", sqlite3_errmsg(db->db), rc);
246 /* TODO just abort? */
250 rc = sqlite3_step(s);
251 if (rc == SQLITE_ROW) {
252 value = sqlite3_column_int(s, 0);
262 int zpm_db_initialize(struct zpm *pkg) {
263 fprintf(stderr, "initializing zpm database\n");
264 switch (sqlite3_exec(pkg->db, createdb, (int (*)(void *,int,char **,char **))0, NULL, NULL)) {
265 case SQLITE_OK: break;
267 SQLERROR(sqlite3_errmsg(pkg->db));
275 int zpm_open(struct zpm *pkg, char *path) {
288 rc = sqlite3_open(path, &db);
290 SQLERROR(sqlite3_errmsg(db));
295 pkg->path = dupstr(path);
297 appid = zpm_db_pragma(pkg, 1);
298 dbver = zpm_db_pragma(pkg, 2);
300 fprintf(stderr, "db appid = %x, dbver = %d\n", appid, dbver);
302 case 0: if (!zpm_db_initialize(pkg)) {
307 case 0x5a504442: break;
309 fprintf(stderr, "unknown database type\n");
315 fprintf(stderr, "version %d zpm db detected, this program only works with version 1 databases\n", dbver);
320 sqlite3_exec(pkg->db, "pragma foreign_keys = ON;", NULL, NULL, &errstr);
322 fprintf(stderr, "sqlite foreign key error: %s\n", errstr);
323 sqlite3_free(errstr);
329 /* TODO if this is a new database, create structures */
331 /* get a package. what if more than one? what if none? */
335 int zpm_close(struct zpm *pkg) {
337 sqlite3_close(pkg->db);
340 /* TODO free any malloced names and such */
344 int zpm_extract(struct zpm *pkg, char *hash, char *path, int mode) {
354 /* TODO check null */
355 sqlite3 *db = pkg->db;
357 rc = sqlite3_prepare(db, "select size, content from files where hash = ?", -1, &ifile,0);
358 if (rc != SQLITE_OK) {
359 SQLERROR(sqlite3_errmsg(db));
365 sqlite3_bind_text(ifile, 1, hash, 64, SQLITE_STATIC);
367 rc = sqlite3_step(ifile);
369 if (rc == SQLITE_DONE) {
370 /* didn't find a row */
371 sqlite3_finalize(ifile);
373 fprintf(stderr, "no such hash\n");
376 /* either way we're done with this now */
378 if (rc != SQLITE_ROW) {
379 SQLERROR(sqlite3_errmsg(db));
380 sqlite3_finalize(ifile);
385 type = sqlite3_column_type(ifile, 0);
386 if (type == SQLITE_NULL) {
387 fprintf(stderr, "no file size\n");
388 sqlite3_finalize(ifile);
392 type = sqlite3_column_type(ifile, 1);
393 if (type == SQLITE_NULL) {
394 fprintf(stderr, "no file data\n");
395 sqlite3_finalize(ifile);
399 size = sqlite3_column_int64(ifile, 0);
400 xzdata = (void *)sqlite3_column_blob(ifile, 1);
401 blobsize = sqlite3_column_bytes(ifile, 1);
403 out = fopen(path, "w");
405 fprintf(stderr, "can't open output file %s\n", path);
406 sqlite3_finalize(ifile);
410 //fwrite(xzdata, blobsize, 1, stdout);
412 fprintf(stderr, "uncompressing %d bytes at %p, expect %lld\n", blobsize, xzdata, (long long int)size);
413 uncompresslzma(xzdata, blobsize, out);
416 sqlite3_finalize(ifile);
423 int zpm_import(struct zpm *pkg, char *path, uint32_t flags, char *hash) {
427 unsigned char tmp[32];
430 int haverow = 0,havedata = 0;
438 if (!pkg || !pkg->db || !path) {
442 /* use local if caller didn't pass in room */
448 fd = open(path, O_RDONLY);
452 if (fstat(fd, &sbuf) == -1) {
455 /* not a regular file? */
456 if (!S_ISREG(sbuf.st_mode)) {
457 /* TODO this is ok, just stored differently */
461 content = mmap(0, sbuf.st_size, PROT_READ,MAP_PRIVATE, fd, 0);
468 sha256_process(&md, content, sbuf.st_size);
469 sha256_done(&md, tmp);
471 sprintf(hash+j*2, "%02x", (unsigned)tmp[j]);
474 fprintf(stderr, "file %s: %s\n", path, hash);
477 /* prepare and bind */
478 /* TODO check null */
479 sqlite3 *db = pkg->db;
481 rc = sqlite3_prepare_v2(db, "select size, content is not null from files where hash = ?", -1, &ifile,0);
482 if (rc != SQLITE_OK) {
483 SQLERROR(sqlite3_errmsg(db));
489 sqlite3_bind_text(ifile, 1, hash, 64, SQLITE_STATIC);
491 rc = sqlite3_step(ifile);
493 if (rc != SQLITE_DONE) {
494 if (rc != SQLITE_ROW) {
495 /* didn't find a row */
496 SQLERROR(sqlite3_errmsg(db));
501 // fprintf(stderr, "have row for hash\n");
502 type = sqlite3_column_type(ifile, 0);
503 if (type == SQLITE_NULL) {
504 /* TODO assert, this shouldn't be possible? */
505 fprintf(stderr, "no file size\n");
506 sqlite3_finalize(ifile);
509 type = sqlite3_column_type(ifile, 1);
510 if (type == SQLITE_NULL) {
511 /* TODO assert, this shouldn't be possible? */
512 fprintf(stderr, "no file data\n");
513 sqlite3_finalize(ifile);
515 /* which is fine, just need to update the row then */
517 havedata = sqlite3_column_int(ifile, 1);
520 sqlite3_finalize(ifile);
524 outbuf = compresslzma(content, sbuf.st_size, &outlen);
526 fprintf(stderr, "compresslzma failed\n");
529 fprintf(stderr, "compressed to %zu\n", outlen);
530 /* don't need the original file now */
531 munmap(content, sbuf.st_size);
534 /* start a transaction */
535 // do that outside of here
540 fprintf(stderr, "adding file data\n");
541 rc = sqlite3_prepare(db, "update files set size = ?, content = ? where hash = ?", -1, &ifile,0);
543 fprintf(stderr, "creating new data row\n");
544 rc = sqlite3_prepare(db, "insert into files (size, content, hash) values (?,?,?)", -1, &ifile,0);
546 if (rc != SQLITE_OK) {
547 SQLERROR(sqlite3_errmsg(db));
548 fprintf(stderr, "cant prepare data\n");
553 sqlite3_bind_int64(ifile, 1, (sqlite3_int64)sbuf.st_size);
554 if (rc != SQLITE_OK) {
555 SQLERROR(sqlite3_errmsg(db));
556 fprintf(stderr, "cant bind size\n");
560 sqlite3_bind_blob64(ifile, 2, outbuf, (sqlite3_int64)outlen, SQLITE_STATIC);
561 if (rc != SQLITE_OK) {
562 SQLERROR(sqlite3_errmsg(db));
563 fprintf(stderr, "cant bind content\n");
567 sqlite3_bind_text(ifile, 3, hash, 64, SQLITE_STATIC);
568 if (rc != SQLITE_OK) {
569 SQLERROR(sqlite3_errmsg(db));
570 fprintf(stderr, "cant bind hash\n");
574 rc = sqlite3_step(ifile);
575 if (rc != SQLITE_DONE) {
576 SQLERROR(sqlite3_errmsg(db));
577 sqlite3_finalize(ifile);
581 sqlite3_finalize(ifile);
587 /* don't need the original file now */
588 munmap(content, sbuf.st_size);
593 /* if package and not nopackage flag, add to package */
594 if (pkg->pkgname && (!ZPM_NOPACKAGE)) {
604 int main(int ac, char **av){
619 fprintf(stderr, "usage: db hash file\n");
623 rc = sqlite3_open(av[1], &db);
625 SQLERROR(sqlite3_errmsg(db));
634 Packages are sqlite databases
636 get application id and userver
638 Primitive operations:
641 associate path with package
642 associate blob with path?
644 * extract blob to a path
645 compare blob to filesystem path
646 create package with info
650 record elf information about blob
653 sign a package? What are we verifying?