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