2 Copyright (C) 2015-2019 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";
41 " %s version="AFM_VERSION"\n"
43 " Copyright (C) 2015-2019 \"IoT.bzh\"\n"
44 " AFB comes with ABSOLUTELY NO WARRANTY.\n"
54 "usage: %s [-f] [-o wgtfile] directory\n"
56 " -o wgtfile the output widget file\n"
57 " -f force overwriting\n"
66 static struct option options[] = {
67 { "output", required_argument, NULL, 'o' },
68 { "force", no_argument, NULL, 'f' },
69 { "help", no_argument, NULL, 'h' },
70 { "quiet", no_argument, NULL, 'q' },
71 { "verbose", no_argument, NULL, 'v' },
72 { "version", no_argument, NULL, 'V' },
76 /* install the widgets of the list */
77 int main(int ac, char **av)
80 char *wgtfile, *directory, *x;
86 wgtfile = directory = NULL;
88 i = getopt_long(ac, av, "qvVhfo:", options, NULL);
112 ERROR("missing argument");
115 ERROR("unrecognized option");
120 /* remaining arguments and final checks */
122 ERROR("no directory set");
125 directory = av[optind++];
127 ERROR("extra parameters found");
131 /* set default values */
132 if (wgtfile == NULL && 0 > asprintf(&wgtfile, "%s.wgt", directory)) {
133 ERROR("asprintf failed");
138 if (stat(directory, &s)) {
139 ERROR("can't find directory %s", directory);
142 if (!S_ISDIR(s.st_mode)) {
143 ERROR("%s isn't a directory", directory);
146 if (access(wgtfile, F_OK) == 0 && force == 0) {
147 ERROR("can't overwrite existing %s", wgtfile);
151 NOTICE("-- PACKING widget %s from directory %s", wgtfile, directory);
153 /* creates an existing widget (for realpath it must exist) */
154 i = open(wgtfile, O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK, 0644);
156 ERROR("can't write widget %s", wgtfile);
161 /* compute absolutes paths */
162 x = realpath(wgtfile, NULL);
164 ERROR("realpath failed for %s", wgtfile);
169 /* set and enter the workdir */
170 if (chdir(directory)) {
171 ERROR("failed to enter directory %s", directory);
174 if (set_workdir(".", 0))
181 return !!zwrite(wgtfile);