X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;f=lib%2Fzpm.c;h=8df7fa82f2048baee280c67cdb918009c28c8674;hb=4238c6794cfe19c0d8fd62da35eb966f1868f9da;hp=87ffbbb3ebde53cf464a48fe5a9972883b8a1203;hpb=34c41d71027a3e94c1f613baeea2e74e038e0108;p=zpackage diff --git a/lib/zpm.c b/lib/zpm.c index 87ffbbb..8df7fa8 100644 --- a/lib/zpm.c +++ b/lib/zpm.c @@ -15,140 +15,6 @@ #include "sha256.h" -#if 0 -struct zpm { - sqlite3 *db; - char *path; /* path to package file */ - char *version; - int release; - char *pkgname; - time_t installed; /* install time, 0 for not installed */ -}; - -struct zpm_file { - char *path; - int mode; - uint32_t filetype; - char *tags; - char *owner; - char *group; - char *hash; /* could be fixed length */ - time_t mtime; - struct zpm_file *next; /* so you can make a linked list */ -}; - -/* NULL? Create? */ -/* associate with a package ? if only one? first? */ -int zpm_open(struct zpm *pkg, char *path); -int zpm_pkgname(char *base, char *version, int release); /* construct a package file name */ - -/* flags for preserving mode, owner, etc */ -/* puts hash of import in hash */ -/* path can be a hash, with an "INTERNAL" flag, i.e. internally import */ -#define ZPM_MODE 0x1 -#define ZPM_OWNER 0x2 -#define ZPM_MTIME 0x4 -#define ZPM_INTERNAL 0x8 -#define ZPM_NOBLOB 0x10 -/* don't run scripts on install */ -#define ZPM_NOSCRIPTS 0x10 -/* don't associate the file with a package, just do a raw insert */ -/* otherwise, associate it with the current package */ -#define ZPM_NOPACKAGE 0x20 - -int zpm_import(struct zpm *zp, char *path, uint32_t flags, uint8_t *hash); - -/* link and unlink hashes to packages */ -int zpm_link(struct zpm *pkg, char *path, char *hash, struct zpm_file *fileinfo); -int zpm_unlink(struct zpm *pkg, char *path); - -/* tag a file. relative to "current package" */ -int zpm_tag(struct zpm *zp, char *path, char *tags); -/* should this be broken up into separage functions ? */ -int zpm_md(struct zpm *zp, char *path, int mode, char *owner, char *group, time_t mtime); - -/* export hash to dest */ -int zpm_extract(struct zpm *pkg, char *hash, char *path, int mode); - -/* export path to dest */ -int zpm_export(struct zpm *zp, char *path, uint32_t flags, char *dest); - -int zpm_close(struct zpm *zp); - -/* attach a signature to a package */ -int zpm_sign(struct zpm *z, size_t s, void *signature); - -/* set the package info to the nth package, -1 to return count? */ -/* further import/exports and such will be relative to this package */ -int zpm_package(struct zpm *zp, int n); - -/* get file information */ -int zpm_stat(struct zpm *z, struct zpm_file *f, int n); - -/* will also set the package context to the new package */ -int zpm_newpkg(struct zpm *z, char *base, char *version, int release); - -/* transactions */ -int zpm_begin(struct zpm *z); -int zpm_commit(struct zpm *z); -int zpm_rollback(struct zpm *z); - -/* higher level operations */ - -/* install or uninstall the package */ -/* flag for keeping the blobs in local */ -/* what about tag filtering */ -int zpm_install(struct zpm *z, struct zpm *local, uint32_t flags); -int zpm_uninstall(struct zpm *local); - -/* slurp up the actual blobs */ -/* what about versioning them if they change */ -int zpm_preserve(struct zpm *local); - -/* check file integrity */ -int zpm_checkinstall(struct zpm *local); - -int zpm_merge(struct zpm *z, struct zpm *src, uint32_t flags); - -void uncompresslzma(void *buf, size_t bufsize, FILE *out); -#define SQLERROR(x) fprintf(stderr, "%s %d: %s\n", __func__, __LINE__, (x)) -#endif - -#if 0 -int zpm_newpkg(struct zpm *z, char *base, char *version, int release) { - char *sql = "insert or ignore into packages (package,version,release) values (?,?,?)"; - int rc; - sqlite3_stmt *ifile; - - rc = sqlite3_prepare(db, sql, -1, &ifile,0); - if (rc != SQLITE_OK) { - SQLERROR(sqlite3_errmsg(db)); - return 0; - } - rc = sqlite3_bind_text(ifile, 1, base, strlen(base), SQLITE_STATIC); - if (rc != SQLITE_OK) { - SQLERROR(sqlite3_errmsg(db)); - fprintf(stderr, "cant bind package name\n"); - zpm_rollback(pkg); - return 0; - } - sqlite3_bind_text(ifile, 2, version, strlen(version), SQLITE_STATIC); - sqlite3_bind_int(ifile, 3, release) - - rc = sqlite3_step(ifile); - - if (rc != SQLITE_DONE) { - SQLERROR(sqlite3_errmsg(db)); - sqlite3_finalize(ifile); - return 0; - } - sqlite3_finalize(ifile); - z->pkg = dupstr(base); - z->version = dupstr(version); - z->release = release; -} -#endif - int zpm_begin(struct zpm *z) { char *errstr = 0; sqlite3_exec(z->db, "begin;", NULL, NULL, &errstr); @@ -447,18 +313,21 @@ int zpm_addvercmp(struct zpm *pkg) { ); } -int zpm_extract(struct zpm *pkg, char *hash, char *path, int mode) { +int zpm_extract(struct zpm *pkg, char *hash, char *path, mode_t mode) { int rc; - int blobsize; - //int64_t size; void *xzdata; int type; - FILE *out; + int out; sqlite3_stmt *ifile; + size_t len; + char *tmpfile = 0; + sqlite3 *db; - /* TODO check null */ - sqlite3 *db = pkg->db; + if (!pkg || !pkg->db) { + return 0; + } + db = pkg->db; rc = sqlite3_prepare(db, "select size, content from files where hash = ?", -1, &ifile,0); if (rc != SQLITE_OK) { @@ -507,26 +376,55 @@ int zpm_extract(struct zpm *pkg, char *hash, char *path, int mode) { blobsize = sqlite3_column_bytes(ifile, 1); if (strcmp(path, "-")) { - out = fopen(path, "w"); + len = strlen(path); + tmpfile = malloc(len+8+1); + if (!tmpfile) { + fprintf(stderr, "malloc error\n"); + return 0; + } + sprintf(tmpfile, "%sXXXXXX", path); + + out = open(tmpfile, O_CREAT|O_WRONLY|O_TRUNC, 0600); + if (out == -1) { + fprintf(stderr, "can't open output file %s: %s\n", + tmpfile, strerror(errno)); + sqlite3_finalize(ifile); + sqlite3_close(db); + return 0; + } } else { - out = stdout; - } - if (!out) { - fprintf(stderr, "can't open output file %s\n", path); - sqlite3_finalize(ifile); - sqlite3_close(db); - return 0; + out = 1; } - //fwrite(xzdata, blobsize, 1, stdout); - //fprintf(stderr, "uncompressing %d bytes at %p, expect %lld\n", blobsize, xzdata, (long long int)size); +#if 0 + fprintf(stderr, "uncompressing %d bytes at %p, expect %lld\n", blobsize, xzdata, (long long int)size); +#endif uncompresslzma(xzdata, blobsize, out); - fclose(out); - chmod(path, mode); - + close(out); sqlite3_finalize(ifile); - return 1; + rc = 1; + if (tmpfile) { + if (chmod(tmpfile, mode) == -1) { + fprintf(stderr, "can't chmod %s: %s\n", tmpfile, + strerror(errno)); + rc = 0; + } else if (rename(tmpfile, path) == -1) { + fprintf(stderr, "extract rename failed: %s\n", + strerror(errno)); + rc = 0; + } + } + + if (rc == 0 && tmpfile) { + if (unlink(tmpfile) == -1) { + fprintf(stderr, "unlink tmpfile %s fail: %s\n", + tmpfile, + strerror(errno)); + } + } + + return rc; } static int run_for_hash(sqlite3 *db, char *sql, char *hash) {