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