e7fa7526d71cec6c3ca444df0f90d4bd132f8a95
[src/app-framework-main.git] / src / wgtpkg-pack.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 _GNU_SOURCE
20
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <unistd.h>
24 #include <limits.h>
25 #include <errno.h>
26 #include <getopt.h>
27 #include <sys/stat.h>
28 #include <fcntl.h>
29
30 #include "verbose.h"
31 #include "wgtpkg.h"
32
33 #if !defined(MAXCERT)
34 #define MAXCERT 20
35 #endif
36 #if !defined(DEFAULT_KEY_FILE)
37 #define DEFAULT_KEY_FILE "key.pem"
38 #endif
39 #if !defined(DEFAULT_CERT_FILE)
40 #define DEFAULT_CERT_FILE "cert.pem"
41 #endif
42
43 const char appname[] = "wgtpkg-pack";
44
45 static void usage()
46 {
47         printf(
48                 "usage: %s [-f] [-o wgtfile] directory\n"
49                 "\n"
50                 "   -o wgtfile       the output widget file\n"
51                 "   -f               force overwriting\n"
52                 "   -q               quiet\n"
53                 "   -v               verbose\n"
54                 "\n",
55                 appname
56         );
57 }
58
59 static struct option options[] = {
60         { "output",      required_argument, NULL, 'o' },
61         { "force",       no_argument,       NULL, 'f' },
62         { "help",        no_argument,       NULL, 'h' },
63         { "quiet",       no_argument,       NULL, 'q' },
64         { "verbose",     no_argument,       NULL, 'v' },
65         { NULL, 0, NULL, 0 }
66 };
67
68 /* install the widgets of the list */
69 int main(int ac, char **av)
70 {
71         int i, force;
72         char *wgtfile, *directory, *x;
73         struct stat s;
74
75         LOGUSER(appname);
76
77         force = 0;
78         wgtfile = directory = NULL;
79         for (;;) {
80                 i = getopt_long(ac, av, "qvhfo:", options, NULL);
81                 if (i < 0)
82                         break;
83                 switch (i) {
84                 case 'o':
85                         wgtfile = optarg;
86                         break;
87                 case 'q':
88                         if (verbosity)
89                                 verbosity--;
90                         break;
91                 case 'v':
92                         verbosity++;
93                         break;
94                 case 'f':
95                         force = 1;
96                         break;
97                 case 'h':
98                         usage();
99                         return 0;
100                 case ':':
101                         ERROR("missing argument");
102                         return 1;
103                 default:
104                         ERROR("unrecognized option");
105                         return 1;
106                 }
107         }
108
109         /* remaining arguments and final checks */
110         if (optind >= ac) {
111                 ERROR("no directory set");
112                 return 1;
113         }
114         directory = av[optind++];
115         if (optind < ac) {
116                 ERROR("extra parameters found");
117                 return 1;
118         }
119
120         /* set default values */
121         if (wgtfile == NULL && 0 > asprintf(&wgtfile, "%s.wgt", directory)) {
122                 ERROR("asprintf failed");
123                 return 1;
124         }
125
126         /* check values */
127         if (stat(directory, &s)) {
128                 ERROR("can't find directory %s", directory);
129                 return 1;
130         }
131         if (!S_ISDIR(s.st_mode)) {
132                 ERROR("%s isn't a directory", directory);
133                 return 1;
134         }
135         if (access(wgtfile, F_OK) == 0 && force == 0) {
136                 ERROR("can't overwrite existing %s", wgtfile);
137                 return 1;
138         }
139
140         NOTICE("-- PACKING widget %s from directory %s", wgtfile, directory);
141
142         /* creates an existing widget (for realpath it must exist) */
143         i = open(wgtfile, O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK, 0644);
144         if (i < 0) {
145                 ERROR("can't write widget %s", wgtfile);
146                 return 1;
147         }
148         close(i);
149
150         /* compute absolutes paths */
151         x = realpath(wgtfile, NULL);
152         if (x == NULL) {
153                 ERROR("realpath failed for %s",wgtfile);
154                 return 1;
155         }
156         wgtfile = x;
157
158         /* set and enter the workdir */
159         if (set_workdir(directory, 0))
160                 return 1;
161
162
163         if (fill_files())
164                 return 1;
165
166         return !!zwrite(wgtfile);
167 }
168
169