b5da59fee5ab431fa20b5911c22179f662f67d9b
[src/app-framework-main.git] / src / wgtpkg-installer.c
1 /*
2  Copyright 2015 IoT.bzh
3
4  author: José Bollo <jose.bollo@iot.bzh>
5
6  Licensed under the Apache License, Version 2.0 (the "License");
7  you may not use this file except in compliance with the License.
8  You may obtain a copy of the License at
9
10      http://www.apache.org/licenses/LICENSE-2.0
11
12  Unless required by applicable law or agreed to in writing, software
13  distributed under the License is distributed on an "AS IS" BASIS,
14  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  See the License for the specific language governing permissions and
16  limitations under the License.
17 */
18
19 #define _GNU_SOURCE
20
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <dirent.h>
25 #include <unistd.h>
26 #include <limits.h>
27 #include <errno.h>
28 #include <getopt.h>
29
30 #include "verbose.h"
31 #include "wgtpkg.h"
32 #include "wgt-info.h"
33 #include "wgtpkg-install.h"
34
35 static const char appname[] = "wgtpkg-installer";
36 static const char *root;
37 static int force;
38
39 static void usage()
40 {
41         printf(
42                 "usage: %s [-f] [-q] [-v] [-p list] rootdir wgtfile...\n"
43                 "\n"
44                 "   rootdir       the root directory for installing\n"
45                 "   -p list       a list of comma separated permissions to allow\n"
46                 "   -f            force overwriting\n"
47                 "   -q            quiet\n"
48                 "   -v            verbose\n"
49                 "\n",
50                 appname
51         );
52 }
53
54 static struct option options[] = {
55         { "permissions", required_argument, NULL, 'p' },
56         { "force",       no_argument,       NULL, 'f' },
57         { "help",        no_argument,       NULL, 'h' },
58         { "quiet",       no_argument,       NULL, 'q' },
59         { "verbose",     no_argument,       NULL, 'v' },
60         { NULL, 0, NULL, 0 }
61 };
62
63 /* install the widgets of the list */
64 int main(int ac, char **av)
65 {
66         int i;
67         struct wgt_info *ifo;
68
69         LOGAUTH(appname);
70
71         xmlsec_init();
72
73         force = 0;
74         for (;;) {
75                 i = getopt_long(ac, av, "hfqvp:", options, NULL);
76                 if (i < 0)
77                         break;
78                 switch (i) {
79                 case 'f':
80                         force = 1;
81                         break;
82                 case 'h':
83                         usage();
84                         return 0;
85                 case 'q':
86                         if (verbosity)
87                                 verbosity--;
88                         break;
89                 case 'v':
90                         verbosity++;
91                         break;
92                 case 'p':
93                         grant_permission_list(optarg);
94                         break;
95                 case ':':
96                         ERROR("missing argument value");
97                         return 1;
98                 default:
99                         ERROR("unrecognized option");
100                         return 1;
101                 }
102         }
103
104         ac -= optind;
105         if (ac < 2) {
106                 ERROR("arguments are missing");
107                 return 1;
108         }
109
110         /* install widgets */
111         av += optind;
112         root = *av++;
113         for ( ; *av ; av++) {
114                 ifo = install_widget(*av, root, force);
115                 if (ifo)
116                         wgt_info_unref(ifo);
117         }
118
119         return 0;
120 }
121