added info retrieval
[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                         syslog(LOG_ERR, "missing argument value");
83                         return 1;
84                 default:
85                         syslog(LOG_ERR, "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                         syslog(LOG_ERR, "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 struct wgt *wgt_at_workdir()
109 {
110         int rc, wfd;
111         struct wgt *wgt;
112
113         wfd = workdirfd();
114         if (wfd < 0)
115                 return NULL;
116
117         wgt = wgt_create();
118         if (!wgt) {
119                 syslog(LOG_ERR, "failed to allocate wgt");
120                 close(wfd);
121                 return NULL;
122         }
123
124         rc = wgt_connectat(wgt, wfd, NULL);
125         if (rc) {
126                 syslog(LOG_ERR, "failed to connect wgt to workdir");
127                 close(wfd);
128                 wgt_unref(wgt);
129                 return NULL;
130         }
131
132         return wgt;
133 }
134
135
136 static int check_and_show()
137 {
138         struct wgt *wgt;
139         struct wgt_info *ifo;
140
141         wgt = wgt_at_workdir();
142         if (!wgt)
143                 return -1;
144
145         ifo = wgt_info_get(wgt, 1, 1, 1);
146         if (!ifo) {
147                 wgt_unref(wgt);
148                 return -1;
149         }
150         wgt_info_dump(ifo, 1, "");
151         wgt_info_unref(ifo);
152         wgt_unref(wgt);
153         return 0;
154 }
155
156 /* install the widget of the file */
157 static void show(const char *wgtfile)
158 {
159         notice("-- INFO for widget %s --", wgtfile);
160
161         /* workdir */
162         if (make_workdir_base("/tmp", "UNPACK", 0)) {
163                 syslog(LOG_ERR, "failed to create a working directory");
164                 return;
165         }
166
167         if (enter_workdir(0))
168                 goto error2;
169
170         if (zread(wgtfile, 0))
171                 goto error2;
172
173         if (check_all_signatures())
174                 goto error2;
175
176         check_and_show();
177         
178 error2:
179         remove_workdir();
180         return;
181 }
182