2019-03-08 18:18:31 +01:00
|
|
|
/* SPDX-License-Identifier: ISC */
|
2019-01-19 17:26:41 +01:00
|
|
|
#include "pack.h"
|
|
|
|
|
|
|
|
static const struct option long_opts[] = {
|
2019-01-28 20:01:36 +01:00
|
|
|
{ "description", required_argument, NULL, 'd' },
|
2019-01-19 17:26:41 +01:00
|
|
|
{ "file-list", required_argument, NULL, 'l' },
|
2019-01-30 10:40:02 +01:00
|
|
|
{ "repo-dir", required_argument, NULL, 'r' },
|
2019-02-15 09:05:12 +01:00
|
|
|
{ "force", no_argument, NULL, 'f' },
|
2019-01-19 17:26:41 +01:00
|
|
|
{ NULL, 0, NULL, 0 },
|
|
|
|
};
|
|
|
|
|
2019-02-15 09:05:12 +01:00
|
|
|
static const char *short_opts = "l:r:d:f";
|
2019-01-19 17:26:41 +01:00
|
|
|
|
2019-02-15 09:05:12 +01:00
|
|
|
static pkg_writer_t *open_writer(pkg_desc_t *desc, const char *repodir,
|
|
|
|
bool force)
|
2019-01-30 10:40:02 +01:00
|
|
|
{
|
|
|
|
char *path;
|
|
|
|
|
|
|
|
if (mkdir_p(repodir))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
path = alloca(strlen(repodir) + strlen(desc->name) + 16);
|
|
|
|
sprintf(path, "%s/%s.pkg", repodir, desc->name);
|
|
|
|
|
2019-02-15 09:05:12 +01:00
|
|
|
return pkg_writer_open(path, force);
|
2019-01-30 10:40:02 +01:00
|
|
|
}
|
|
|
|
|
2019-01-19 17:26:41 +01:00
|
|
|
static int cmd_pack(int argc, char **argv)
|
|
|
|
{
|
2019-01-30 10:40:02 +01:00
|
|
|
const char *filelist = NULL, *repodir = NULL, *descfile = NULL;
|
2019-02-07 23:14:45 +01:00
|
|
|
image_entry_t *list = NULL;
|
2019-02-15 09:05:12 +01:00
|
|
|
bool force = false;
|
2019-01-19 17:26:41 +01:00
|
|
|
pkg_writer_t *wr;
|
2019-01-28 20:01:36 +01:00
|
|
|
pkg_desc_t desc;
|
2019-01-19 17:26:41 +01:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
i = getopt_long(argc, argv, short_opts, long_opts, NULL);
|
|
|
|
if (i == -1)
|
|
|
|
break;
|
|
|
|
|
|
|
|
switch (i) {
|
|
|
|
case 'l':
|
|
|
|
filelist = optarg;
|
|
|
|
break;
|
2019-01-28 20:01:36 +01:00
|
|
|
case 'd':
|
|
|
|
descfile = optarg;
|
|
|
|
break;
|
2019-01-30 10:40:02 +01:00
|
|
|
case 'r':
|
|
|
|
repodir = optarg;
|
|
|
|
break;
|
2019-02-15 09:05:12 +01:00
|
|
|
case 'f':
|
|
|
|
force = true;
|
|
|
|
break;
|
2019-01-19 17:26:41 +01:00
|
|
|
default:
|
|
|
|
tell_read_help(argv[0]);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-28 20:01:36 +01:00
|
|
|
if (descfile == NULL) {
|
|
|
|
fputs("missing argument: package description file\n", stderr);
|
|
|
|
tell_read_help(argv[0]);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2019-01-30 10:40:02 +01:00
|
|
|
if (repodir == NULL) {
|
|
|
|
fputs("missing argument: repository directory\n", stderr);
|
2019-01-19 17:26:41 +01:00
|
|
|
tell_read_help(argv[0]);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (optind < argc)
|
|
|
|
fputs("warning: ignoring extra arguments\n", stderr);
|
|
|
|
|
2019-01-28 20:01:36 +01:00
|
|
|
if (desc_read(descfile, &desc))
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
2019-02-07 23:14:45 +01:00
|
|
|
if (filelist != NULL && filelist_read(filelist, &list))
|
2019-01-28 20:01:36 +01:00
|
|
|
goto fail_desc;
|
2019-01-19 17:26:41 +01:00
|
|
|
|
2019-02-15 09:05:12 +01:00
|
|
|
wr = open_writer(&desc, repodir, force);
|
2019-01-19 17:26:41 +01:00
|
|
|
if (wr == NULL)
|
|
|
|
goto fail_fp;
|
|
|
|
|
2019-01-28 20:01:36 +01:00
|
|
|
if (write_header_data(wr, &desc))
|
|
|
|
goto fail;
|
|
|
|
|
2019-02-07 23:14:45 +01:00
|
|
|
if (list != NULL) {
|
2019-02-07 23:31:05 +01:00
|
|
|
if (write_toc(wr, list, desc.toccmp))
|
2019-02-07 23:14:45 +01:00
|
|
|
goto fail;
|
2019-01-19 17:26:41 +01:00
|
|
|
|
2019-02-07 23:31:05 +01:00
|
|
|
if (write_files(wr, list, desc.datacmp))
|
2019-02-07 23:14:45 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
2019-01-19 17:26:41 +01:00
|
|
|
|
|
|
|
pkg_writer_close(wr);
|
|
|
|
image_entry_free_list(list);
|
2019-01-28 20:01:36 +01:00
|
|
|
desc_free(&desc);
|
2019-01-19 17:26:41 +01:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
fail:
|
|
|
|
pkg_writer_close(wr);
|
|
|
|
fail_fp:
|
|
|
|
image_entry_free_list(list);
|
2019-01-28 20:01:36 +01:00
|
|
|
fail_desc:
|
|
|
|
desc_free(&desc);
|
2019-01-19 17:26:41 +01:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static command_t pack = {
|
|
|
|
.cmd = "pack",
|
|
|
|
.usage = "OPTIONS...",
|
|
|
|
.s_desc = "generate a package file",
|
|
|
|
.l_desc =
|
|
|
|
"Read a list of files from the given lists file and generate a package.\n"
|
|
|
|
"Possible options:\n"
|
2019-02-02 20:28:37 +01:00
|
|
|
" --file-list, -l <path> Specify a file containing a list of input files.\n"
|
|
|
|
" --repo-dir, -r <path> Specify the output repository path to store the\n"
|
|
|
|
" package in.\n"
|
|
|
|
" --description, -d <path> Specify a file containing a description of the\n"
|
|
|
|
" package, including information such as package\n"
|
|
|
|
" dependencies, the actual package name, etc.\n"
|
2019-02-15 09:05:12 +01:00
|
|
|
" --force, -f If a package with the same name already exists,\n"
|
|
|
|
" overwrite it.\n"
|
2019-02-07 23:31:05 +01:00
|
|
|
"\n",
|
2019-01-19 17:26:41 +01:00
|
|
|
.run_cmd = cmd_pack,
|
|
|
|
};
|
|
|
|
|
|
|
|
REGISTER_COMMAND(pack)
|