]> pd.if.org Git - zpackage/blobdiff - src/extract.c
move C source files into src
[zpackage] / src / extract.c
diff --git a/src/extract.c b/src/extract.c
new file mode 100644 (file)
index 0000000..f5a3a6e
--- /dev/null
@@ -0,0 +1,99 @@
+#define _POSIX_C_SOURCE 2
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <errno.h>
+#include <string.h>
+
+#include <sys/mman.h>
+
+#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;
+}