work in progress
[src/app-framework-main.git] / src / wgtpkg-info.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 _BSD_SOURCE /* see readdir */
18
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <dirent.h>
22 #include <unistd.h>
23 #include <limits.h>
24 #include <errno.h>
25 #include <syslog.h>
26 #include <getopt.h>
27
28 #include "verbose.h"
29 #include "wgtpkg.h"
30 #include "wgt.h"
31 #include "wgt-info.h"
32
33 static const char appname[] = "wgtpkg-info";
34
35 static void show(const char *wgtfile);
36
37 static void usage()
38 {
39         printf(
40                 "usage: %s [-f] [-q] [-v] wgtfile...\n"
41                 "\n"
42                 "   -q            quiet\n"
43                 "   -v            verbose\n"
44                 "\n",
45                 appname
46         );
47 }
48
49 static struct option options[] = {
50         { "help",        no_argument,       NULL, 'h' },
51         { "quiet",       no_argument,       NULL, 'q' },
52         { "verbose",     no_argument,       NULL, 'v' },
53         { NULL, 0, NULL, 0 }
54 };
55
56 /* info the widgets of the list */
57 int main(int ac, char **av)
58 {
59         int i;
60         char *wpath;
61
62         openlog(appname, LOG_PERROR, LOG_USER);
63
64         xmlsec_init();
65
66         for (;;) {
67                 i = getopt_long(ac, av, "hqv", options, NULL);
68                 if (i < 0)
69                         break;
70                 switch (i) {
71                 case 'h':
72                         usage();
73                         return 0;
74                 case 'q':
75                         if (verbosity)
76                                 verbosity--;
77                         break;
78                 case 'v':
79                         verbosity++;
80                         break;
81                 case ':':
82                         ERROR("missing argument value");
83                         return 1;
84                 default:
85                         ERROR("unrecognized option");
86                         return 1;
87                 }
88         }
89
90         /* canonic names for files */
91         av += optind;
92         for (i = 0 ; av[i] != NULL ; i++) {
93                 wpath = realpath(av[i], NULL);
94                 if (wpath == NULL) {
95                         ERROR("error while getting realpath of %dth widget: %s", i+1, av[i]);
96                         return 1;
97                 }
98                 av[i] = wpath;
99         }
100
101         /* info widgets */
102         for ( ; *av ; av++)
103                 show(*av);
104
105         return 0;
106 }
107
108 static int check_and_show()
109 {
110         struct wgt_info *ifo;
111
112         ifo = wgt_info_createat(workdirfd, NULL, 1, 1, 1);
113         if (!ifo)
114                 return -1;
115         wgt_info_dump(ifo, 1, "");
116         wgt_info_unref(ifo);
117         return 0;
118 }
119
120 /* install the widget of the file */
121 static void show(const char *wgtfile)
122 {
123         NOTICE("-- INFO for widget %s --", wgtfile);
124
125         /* workdir */
126         if (make_workdir_base("/tmp", "UNPACK", 0)) {
127                 ERROR("failed to create a working directory");
128                 return;
129         }
130
131         if (zread(wgtfile, 0))
132                 goto error2;
133
134         if (check_all_signatures())
135                 goto error2;
136
137         check_and_show();
138         
139 error2:
140         remove_workdir();
141         return;
142 }
143