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);
193 int zpm_open(struct zpm *pkg, char *path) {
205 /* TODO some way to determine if the DB is newly created ? */
206 /* could check for tables, if there are any, then check version, etc */
207 rc = sqlite3_open(path, &db);
209 SQLERROR(sqlite3_errmsg(db));
213 sqlite3_exec(pkg->db, "pragma foreign_keys = ON;", NULL, NULL, &errstr);
215 fprintf(stderr, "sqlite foreign key error: %s\n", errstr);
216 sqlite3_free(errstr);
221 pkg->path = dupstr(path);
224 /* TODO if this is a new database, create structures */
226 /* get a package. what if more than one? what if none? */
230 int zpm_close(struct zpm *pkg) {
232 sqlite3_close(pkg->db);
235 /* TODO free any malloced names and such */
239 int zpm_extract(struct zpm *pkg, char *hash, char *path, int mode) {
249 /* TODO check null */
250 sqlite3 *db = pkg->db;
252 rc = sqlite3_prepare(db, "select size, content from files where hash = ?", -1, &ifile,0);
253 if (rc != SQLITE_OK) {
254 SQLERROR(sqlite3_errmsg(db));
260 sqlite3_bind_text(ifile, 1, hash, 64, SQLITE_STATIC);
262 rc = sqlite3_step(ifile);
264 if (rc == SQLITE_DONE) {
265 /* didn't find a row */
266 sqlite3_finalize(ifile);
268 fprintf(stderr, "no such hash\n");
271 /* either way we're done with this now */
273 if (rc != SQLITE_ROW) {
274 SQLERROR(sqlite3_errmsg(db));
275 sqlite3_finalize(ifile);
280 type = sqlite3_column_type(ifile, 0);
281 if (type == SQLITE_NULL) {
282 fprintf(stderr, "no file size\n");
283 sqlite3_finalize(ifile);
287 type = sqlite3_column_type(ifile, 1);
288 if (type == SQLITE_NULL) {
289 fprintf(stderr, "no file data\n");
290 sqlite3_finalize(ifile);
294 size = sqlite3_column_int64(ifile, 0);
295 xzdata = (void *)sqlite3_column_blob(ifile, 1);
296 blobsize = sqlite3_column_bytes(ifile, 1);
298 out = fopen(path, "w");
300 fprintf(stderr, "can't open output file %s\n", path);
301 sqlite3_finalize(ifile);
305 //fwrite(xzdata, blobsize, 1, stdout);
307 fprintf(stderr, "uncompressing %d bytes at %p, expect %lld\n", blobsize, xzdata, (long long int)size);
308 uncompresslzma(xzdata, blobsize, out);
311 sqlite3_finalize(ifile);
318 int zpm_import(struct zpm *pkg, char *path, uint32_t flags, char *hash) {
322 unsigned char tmp[32];
325 int haverow,havedata;
333 if (!pkg || !pkg->db || !path) {
337 /* use local if caller didn't pass in room */
343 fd = open(path, O_RDONLY);
347 if (fstat(fd, &sbuf) == -1) {
350 /* not a regular file? */
351 if (!S_ISREG(sbuf.st_mode)) {
352 /* TODO this is ok, just stored differently */
356 content = mmap(0, sbuf.st_size, PROT_READ,MAP_PRIVATE, fd, 0);
363 sha256_process(&md, content, sbuf.st_size);
364 sha256_done(&md, tmp);
366 sprintf(hash+j*2, "%02x", (unsigned)tmp[j]);
369 // fprintf(stderr, "file %s: %s\n", path, hash);
372 outbuf = compresslzma(content, sbuf.st_size, &outlen);
373 // fprintf(stderr, "compressed to %zu\n", outlen);
375 /* don't need the original file now */
376 munmap(content, sbuf.st_size);
379 /* prepare and bind */
380 /* TODO check null */
381 sqlite3 *db = pkg->db;
383 rc = sqlite3_prepare(db, "select size, content is not null from files where hash = ?", -1, &ifile,0);
384 if (rc != SQLITE_OK) {
385 SQLERROR(sqlite3_errmsg(db));
391 sqlite3_bind_text(ifile, 1, hash, 64, SQLITE_STATIC);
393 rc = sqlite3_step(ifile);
395 if (rc != SQLITE_DONE) {
396 if (rc != SQLITE_ROW) {
397 /* didn't find a row */
398 SQLERROR(sqlite3_errmsg(db));
403 // fprintf(stderr, "have row for hash\n");
404 type = sqlite3_column_type(ifile, 0);
405 if (type == SQLITE_NULL) {
406 /* TODO assert, this shouldn't be possible? */
407 fprintf(stderr, "no file size\n");
408 sqlite3_finalize(ifile);
411 type = sqlite3_column_type(ifile, 1);
412 if (type == SQLITE_NULL) {
413 /* TODO assert, this shouldn't be possible? */
414 fprintf(stderr, "no file data\n");
415 sqlite3_finalize(ifile);
417 /* which is fine, just need to update the row then */
419 havedata = sqlite3_column_int(ifile, 1);
422 sqlite3_finalize(ifile);
425 /* start a transaction */
426 // do that outside of here
431 rc = sqlite3_prepare(db, "update files set size = ?, content = ? where hash = ?", -1, &ifile,0);
433 // fprintf(stderr, "missing file data\n");
434 rc = sqlite3_prepare(db, "insert into files (size, content, hash) values (?,?,?)", -1, &ifile,0);
436 if (rc != SQLITE_OK) {
437 SQLERROR(sqlite3_errmsg(db));
438 fprintf(stderr, "cant prepare data\n");
443 sqlite3_bind_int64(ifile, 1, (sqlite3_int64)sbuf.st_size);
444 if (rc != SQLITE_OK) {
445 SQLERROR(sqlite3_errmsg(db));
446 fprintf(stderr, "cant bind size\n");
450 sqlite3_bind_blob64(ifile, 2, outbuf, (sqlite3_int64)outlen, SQLITE_STATIC);
451 if (rc != SQLITE_OK) {
452 SQLERROR(sqlite3_errmsg(db));
453 fprintf(stderr, "cant bind content\n");
457 sqlite3_bind_text(ifile, 3, hash, 64, SQLITE_STATIC);
458 if (rc != SQLITE_OK) {
459 SQLERROR(sqlite3_errmsg(db));
460 fprintf(stderr, "cant bind hash\n");
464 rc = sqlite3_step(ifile);
465 if (rc != SQLITE_DONE) {
466 SQLERROR(sqlite3_errmsg(db));
467 sqlite3_finalize(ifile);
471 sqlite3_finalize(ifile);
478 /* if package and not nopackage flag, add to package */
479 if (pkg->pkgname && (!ZPM_NOPACKAGE)) {
489 int main(int ac, char **av){
504 fprintf(stderr, "usage: db hash file\n");
508 rc = sqlite3_open(av[1], &db);
510 SQLERROR(sqlite3_errmsg(db));
519 Packages are sqlite databases
521 get application id and userver
523 Primitive operations:
526 associate path with package
527 associate blob with path?
529 * extract blob to a path
530 compare blob to filesystem path
531 create package with info
535 record elf information about blob
538 sign a package? What are we verifying?