6738192c0ec3563f05a553c7c420c72687504c96
[src/app-framework-main.git] / src / wgtpkg-installer.c
1 /*
2  Copyright 2015, 2016, 2017 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 version()
43 {
44         printf(
45                 "\n"
46                 "  %s  version="AFM_VERSION"\n"
47                 "\n"
48                 "  Copyright (C) 2015, 2016, 2017 \"IoT.bzh\"\n"
49                 "  AFB comes with ABSOLUTELY NO WARRANTY.\n"
50                 "  Licence Apache 2\n"
51                 "\n",
52                 appname
53         );
54 }
55
56 static void usage()
57 {
58         printf(
59                 "usage: %s [-f] [-q] [-v] [-p list] rootdir wgtfile...\n"
60                 "\n"
61                 "   rootdir       the root directory for installing\n"
62                 "   -p list       a list of comma separated permissions to allow\n"
63                 "   -f            force overwriting\n"
64                 "   -q            quiet\n"
65                 "   -v            verbose\n"
66                 "   -V            version\n"
67                 "\n",
68                 appname
69         );
70 }
71
72 static struct option options[] = {
73         { "permissions", required_argument, NULL, 'p' },
74         { "force",       no_argument,       NULL, 'f' },
75         { "help",        no_argument,       NULL, 'h' },
76         { "quiet",       no_argument,       NULL, 'q' },
77         { "verbose",     no_argument,       NULL, 'v' },
78         { "version",     no_argument,       NULL, 'V' },
79         { NULL, 0, NULL, 0 }
80 };
81
82 /* install the widgets of the list */
83 int main(int ac, char **av)
84 {
85         int i, rc;
86         struct wgt_info *ifo;
87
88         LOGAUTH(appname);
89
90         xmlsec_init();
91
92         force = 0;
93         for (;;) {
94                 i = getopt_long(ac, av, "hfqvVp:", options, NULL);
95                 if (i < 0)
96                         break;
97                 switch (i) {
98                 case 'f':
99                         force = 1;
100                         break;
101                 case 'h':
102                         usage();
103                         return 0;
104                 case 'q':
105                         if (verbosity)
106                                 verbosity--;
107                         break;
108                 case 'v':
109                         verbosity++;
110                         break;
111                 case 'V':
112                         version();
113                         return 0;
114                 case 'p':
115                         rc = grant_permission_list(optarg);
116                         if (rc < 0) {
117                                 ERROR("Can't set granted permission list");
118                                 exit(1);
119                         }
120                         break;
121                 case ':':
122                         ERROR("missing argument value");
123                         return 1;
124                 default:
125                         ERROR("unrecognized option");
126                         return 1;
127                 }
128         }
129
130         ac -= optind;
131         if (ac < 2) {
132                 ERROR("arguments are missing");
133                 return 1;
134         }
135
136         /* install widgets */
137         av += optind;
138         root = *av++;
139         for ( ; *av ; av++) {
140                 ifo = install_widget(*av, root, force);
141                 if (ifo)
142                         wgt_info_unref(ifo);
143         }
144
145         return 0;
146 }
147