2 Copyright 2015, 2016, 2017 IoT.bzh
4 author: José Bollo <jose.bollo@iot.bzh>
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
10 http://www.apache.org/licenses/LICENSE-2.0
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.
31 #include "wgtpkg-files.h"
32 #include "wgtpkg-workdir.h"
33 #include "wgtpkg-zip.h"
35 const char appname[] = "wgtpkg-pack";
40 "usage: %s [-f] [-o wgtfile] directory\n"
42 " -o wgtfile the output widget file\n"
43 " -f force overwriting\n"
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' },
60 /* install the widgets of the list */
61 int main(int ac, char **av)
64 char *wgtfile, *directory, *x;
70 wgtfile = directory = NULL;
72 i = getopt_long(ac, av, "qvhfo:", options, NULL);
93 ERROR("missing argument");
96 ERROR("unrecognized option");
101 /* remaining arguments and final checks */
103 ERROR("no directory set");
106 directory = av[optind++];
108 ERROR("extra parameters found");
112 /* set default values */
113 if (wgtfile == NULL && 0 > asprintf(&wgtfile, "%s.wgt", directory)) {
114 ERROR("asprintf failed");
119 if (stat(directory, &s)) {
120 ERROR("can't find directory %s", directory);
123 if (!S_ISDIR(s.st_mode)) {
124 ERROR("%s isn't a directory", directory);
127 if (access(wgtfile, F_OK) == 0 && force == 0) {
128 ERROR("can't overwrite existing %s", wgtfile);
132 NOTICE("-- PACKING widget %s from directory %s", wgtfile, directory);
134 /* creates an existing widget (for realpath it must exist) */
135 i = open(wgtfile, O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK, 0644);
137 ERROR("can't write widget %s", wgtfile);
142 /* compute absolutes paths */
143 x = realpath(wgtfile, NULL);
145 ERROR("realpath failed for %s",wgtfile);
150 /* set and enter the workdir */
151 if (set_workdir(directory, 0))
158 return !!zwrite(wgtfile);