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