in progress
[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-installer";
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
65         openlog(appname, LOG_PERROR, LOG_AUTH);
66
67         xmlsec_init();
68
69         force = 0;
70         for (;;) {
71                 i = getopt_long(ac, av, "hfqvp:", options, NULL);
72                 if (i < 0)
73                         break;
74                 switch (i) {
75                 case 'f':
76                         force = 1;
77                         break;
78                 case 'h':
79                         usage();
80                         return 0;
81                 case 'q':
82                         if (verbosity)
83                                 verbosity--;
84                         break;
85                 case 'v':
86                         verbosity++;
87                         break;
88                 case 'p':
89                         grant_permission_list(optarg);
90                         break;
91                 case ':':
92                         ERROR("missing argument value");
93                         return 1;
94                 default:
95                         ERROR("unrecognized option");
96                         return 1;
97                 }
98         }
99
100         ac -= optind;
101         if (ac < 2) {
102                 ERROR("arguments are missing");
103                 return 1;
104         }
105
106         /* install widgets */
107         av += optind;
108         root = *av++;
109         for ( ; *av ; av++)
110                 install_widget(*av, root, force);
111
112         return 0;
113 }
114