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