]> pd.if.org Git - zpackage/blob - src/extract.c
remove stray debug fprintf
[zpackage] / src / extract.c
1 #define _POSIX_C_SOURCE 2
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <fcntl.h>
7 #include <unistd.h>
8 #include <errno.h>
9 #include <string.h>
10
11 #include <sys/mman.h>
12
13 #include "zpm.h"
14
15 /* more usage:
16  * -t : use a temp file, then move into place, possible reverse the sense
17  *  -u : userid
18  *  -g : groupid
19  *  -m : mode (i.e. final mode)
20         int mode = 0600;
21  *  -l : log all actions
22  *  -d : logging database file, if different
23  *
24  *  check if file exists, if it does, and has the same hash, do
25  *  nothing, unless -f is given
26  */ 
27
28 void usage(void) {
29         fprintf(stderr, "usage: zpm-extract [-d dbfile] [-t tmpfile] hash [output]\n");
30 }
31
32 int main(int ac, char **av){
33         struct zpm pkg;
34         int rv;
35
36         char *tmpfile = 0, *output = 0;
37         int opt;
38         mode_t mode;
39         uid_t uid = 0;
40         gid_t gid = 0;
41         char *dest = 0, *hashmatch = 0;
42         char hash[ZPM_HASH_STRLEN+1];
43         char *dbfile = getenv("ZPMDB");
44         if (!dbfile) {
45                 dbfile = "/var/lib/zpm/local.db";
46         }
47
48         while ((opt = getopt(ac, av, "f:")) != -1) {
49                 switch (opt) {
50                         case 'f': dbfile = optarg; break;
51                         case 't': tmpfile = optarg; break;
52                         case 'u': uid = atoi(optarg); break;
53                         case 'g': gid = atoi(optarg); break;
54                         case 'o': dest = optarg; break;
55                         case 'm': mode = atoi(optarg); break;
56                         default:
57                                   usage();
58                                   exit(EXIT_FAILURE);
59                                   break;
60                 }
61         }
62
63         if (ac < optind) {
64                 usage();
65                 exit(EXIT_FAILURE);
66         }
67
68         hashmatch = av[optind];
69
70         if (ac > optind) {
71                 output = av[optind+1];
72         }
73
74         zpm_open(&pkg, dbfile);
75
76         rv = zpm_findhash(&pkg, hashmatch, hash);
77         if (rv == 0) {
78                 fprintf(stderr, "no such hash %s\n", hashmatch);
79                 exit(EXIT_FAILURE);
80         } else if (rv > 1) {
81                 fprintf(stderr, "%d matching hashes for %s\n", rv, hashmatch);
82                 exit(EXIT_FAILURE);
83         }
84
85         rv = zpm_extract(&pkg, hash, tmpfile ? tmpfile : output, 0600);
86         zpm_close(&pkg);
87
88         if (!rv) {
89                 exit(EXIT_FAILURE);
90         }
91
92         /* set mode and such */
93
94         if (dest != tmpfile) {
95                 rename(tmpfile, dest);
96         }
97
98         return rv ? 0 : 1;
99 }