X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;f=src%2Fextract.c;fp=src%2Fextract.c;h=f5a3a6eabfc7c7ecd89abe95449b32dbf7f4af51;hb=5dd3c3e64a9574112dda77a5afc167f5daa53fd8;hp=0000000000000000000000000000000000000000;hpb=bd21f0a1265b43ad5f05353a39db31c16826f05c;p=zpackage diff --git a/src/extract.c b/src/extract.c new file mode 100644 index 0000000..f5a3a6e --- /dev/null +++ b/src/extract.c @@ -0,0 +1,99 @@ +#define _POSIX_C_SOURCE 2 +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "zpm.h" + +/* more usage: + * -t : use a temp file, then move into place, possible reverse the sense + * -u : userid + * -g : groupid + * -m : mode (i.e. final mode) + int mode = 0600; + * -l : log all actions + * -d : logging database file, if different + * + * check if file exists, if it does, and has the same hash, do + * nothing, unless -f is given + */ + +void usage(void) { + fprintf(stderr, "usage: zpm-extract [-d dbfile] [-t tmpfile] hash [output]\n"); +} + +int main(int ac, char **av){ + struct zpm pkg; + int rv; + + char *tmpfile = 0, *output = 0; + int opt; + mode_t mode; + uid_t uid = 0; + gid_t gid = 0; + char *dest = 0, *hashmatch = 0; + char hash[ZPM_HASH_STRLEN+1]; + char *dbfile = getenv("ZPMDB"); + if (!dbfile) { + dbfile = "/var/lib/zpm/local.db"; + } + + while ((opt = getopt(ac, av, "f:")) != -1) { + switch (opt) { + case 'f': dbfile = optarg; break; + case 't': tmpfile = optarg; break; + case 'u': uid = atoi(optarg); break; + case 'g': gid = atoi(optarg); break; + case 'o': dest = optarg; break; + case 'm': mode = atoi(optarg); break; + default: + usage(); + exit(EXIT_FAILURE); + break; + } + } + + if (ac < optind) { + usage(); + exit(EXIT_FAILURE); + } + + hashmatch = av[optind]; + + if (ac > optind) { + output = av[optind+1]; + } + + zpm_open(&pkg, dbfile); + + rv = zpm_findhash(&pkg, hashmatch, hash); + if (rv == 0) { + fprintf(stderr, "no such hash %s\n", hashmatch); + exit(EXIT_FAILURE); + } else if (rv > 1) { + fprintf(stderr, "%d matching hashes for %s\n", rv, hashmatch); + exit(EXIT_FAILURE); + } + + rv = zpm_extract(&pkg, hash, tmpfile ? tmpfile : output, 0600); + zpm_close(&pkg); + + if (!rv) { + exit(EXIT_FAILURE); + } + + /* set mode and such */ + + if (dest != tmpfile) { + rename(tmpfile, dest); + } + + return rv ? 0 : 1; +}