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