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