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