Merge pull request #1 from Tarnyko/master
[src/app-framework-main.git] / 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 "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                 "\n",
51                 appname
52         );
53 }
54
55 static struct option options[] = {
56         { "output",      required_argument, NULL, 'o' },
57         { "force",       no_argument,       NULL, 'f' },
58         { "help",        no_argument,       NULL, 'h' },
59         { NULL, 0, NULL, 0 }
60 };
61
62 /* install the widgets of the list */
63 int main(int ac, char **av)
64 {
65         int i, force;
66         char *wgtfile, *directory, *x;
67         struct stat s;
68
69         openlog(appname, LOG_PERROR, LOG_USER);
70
71         force = 0;
72         wgtfile = directory = NULL;
73         for (;;) {
74                 i = getopt_long(ac, av, "hfo:", options, NULL);
75                 if (i < 0)
76                         break;
77                 switch (i) {
78                 case 'o':
79                         wgtfile = optarg;
80                         break;
81                 case 'f':
82                         force = 1;
83                         break;
84                 case 'h':
85                         usage();
86                         return 0;
87                 case ':':
88                         syslog(LOG_ERR, "missing argument");
89                         return 1;
90                 default:
91                         syslog(LOG_ERR, "unrecognized option");
92                         return 1;
93                 }
94         }
95
96         /* remaining arguments and final checks */
97         if (optind >= ac) {
98                 syslog(LOG_ERR, "no directory set");
99                 return 1;
100         }
101         directory = av[optind++];
102         if (optind < ac) {
103                 syslog(LOG_ERR, "extra parameters found");
104                 return 1;
105         }
106
107         /* set default values */
108         if (wgtfile == NULL && 0 > asprintf(&wgtfile, "%s.wgt", directory)) {
109                 syslog(LOG_ERR, "asprintf failed");
110                 return 1;
111         }
112
113         /* check values */
114         if (stat(directory, &s)) {
115                 syslog(LOG_ERR, "can't find directory %s", directory);
116                 return 1;
117         }
118         if (!S_ISDIR(s.st_mode)) {
119                 syslog(LOG_ERR, "%s isn't a directory", directory);
120                 return 1;
121         }
122         if (access(wgtfile, F_OK) == 0 && force == 0) {
123                 syslog(LOG_ERR, "can't overwrite existing %s", wgtfile);
124                 return 1;
125         }
126
127 printf("\n\nPACKING widget %s from directory %s\n", wgtfile, directory);
128
129         /* creates an existing widget (for realpath it must exist) */
130         i = open(wgtfile, O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK, 0666);
131         if (i < 0) {
132                 syslog(LOG_ERR, "can't write widget %s", wgtfile);
133                 return 1;
134         }
135         close(i);
136
137         /* compute absolutes paths */
138         x = realpath(wgtfile, NULL);
139         if (x == NULL) {
140                 syslog(LOG_ERR, "realpath failed for %s",wgtfile);
141                 return 1;
142         }
143         wgtfile = x;
144
145         /* set and enter the workdir */
146         if (set_workdir(directory, 0) || enter_workdir(0))
147                 return 1;
148
149
150         if (fill_files())
151                 return 1;
152
153         return !!zwrite(wgtfile);
154 }
155
156