864d3d369cbc74e51fc43721a1df0938b5bc6bbf
[src/app-framework-main.git] / src / wgtpkg-installer.c
1 /*
2  Copyright 2015, 2016 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 <libxml/tree.h>
31
32 #include "verbose.h"
33 #include "wgtpkg-permissions.h"
34 #include "wgtpkg-xmlsec.h"
35 #include "wgt-info.h"
36 #include "wgtpkg-install.h"
37
38 static const char appname[] = "wgtpkg-installer";
39 static const char *root;
40 static int force;
41
42 static void usage()
43 {
44         printf(
45                 "usage: %s [-f] [-q] [-v] [-p list] rootdir wgtfile...\n"
46                 "\n"
47                 "   rootdir       the root directory for installing\n"
48                 "   -p list       a list of comma separated permissions to allow\n"
49                 "   -f            force overwriting\n"
50                 "   -q            quiet\n"
51                 "   -v            verbose\n"
52                 "\n",
53                 appname
54         );
55 }
56
57 static struct option options[] = {
58         { "permissions", required_argument, NULL, 'p' },
59         { "force",       no_argument,       NULL, 'f' },
60         { "help",        no_argument,       NULL, 'h' },
61         { "quiet",       no_argument,       NULL, 'q' },
62         { "verbose",     no_argument,       NULL, 'v' },
63         { NULL, 0, NULL, 0 }
64 };
65
66 /* install the widgets of the list */
67 int main(int ac, char **av)
68 {
69         int i, rc;
70         struct wgt_info *ifo;
71
72         LOGAUTH(appname);
73
74         xmlsec_init();
75
76         force = 0;
77         for (;;) {
78                 i = getopt_long(ac, av, "hfqvp:", options, NULL);
79                 if (i < 0)
80                         break;
81                 switch (i) {
82                 case 'f':
83                         force = 1;
84                         break;
85                 case 'h':
86                         usage();
87                         return 0;
88                 case 'q':
89                         if (verbosity)
90                                 verbosity--;
91                         break;
92                 case 'v':
93                         verbosity++;
94                         break;
95                 case 'p':
96                         rc = grant_permission_list(optarg);
97                         if (rc < 0) {
98                                 ERROR("Can't set granted permission list");
99                                 exit(1);
100                         }
101                         break;
102                 case ':':
103                         ERROR("missing argument value");
104                         return 1;
105                 default:
106                         ERROR("unrecognized option");
107                         return 1;
108                 }
109         }
110
111         ac -= optind;
112         if (ac < 2) {
113                 ERROR("arguments are missing");
114                 return 1;
115         }
116
117         /* install widgets */
118         av += optind;
119         root = *av++;
120         for ( ; *av ; av++) {
121                 ifo = install_widget(*av, root, force);
122                 if (ifo)
123                         wgt_info_unref(ifo);
124         }
125
126         return 0;
127 }
128