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