Add option to forcefully overwrite packages in pack command

Signed-off-by: David Oberhollenzer <goliath@infraroot.at>
This commit is contained in:
David Oberhollenzer 2019-02-15 09:05:12 +01:00
parent 4141e666b6
commit f2627b8933
3 changed files with 26 additions and 7 deletions

View File

@ -1,12 +1,14 @@
#ifndef PKGWRITER_H
#define PKGWRITER_H
#include <stdbool.h>
#include "pkgformat.h"
#include "compressor.h"
typedef struct pkg_writer_t pkg_writer_t;
pkg_writer_t *pkg_writer_open(const char *path);
pkg_writer_t *pkg_writer_open(const char *path, bool force);
void pkg_writer_close(pkg_writer_t *writer);

View File

@ -4,12 +4,14 @@ static const struct option long_opts[] = {
{ "description", required_argument, NULL, 'd' },
{ "file-list", required_argument, NULL, 'l' },
{ "repo-dir", required_argument, NULL, 'r' },
{ "force", no_argument, NULL, 'f' },
{ NULL, 0, NULL, 0 },
};
static const char *short_opts = "l:r:d:";
static const char *short_opts = "l:r:d:f";
static pkg_writer_t *open_writer(pkg_desc_t *desc, const char *repodir)
static pkg_writer_t *open_writer(pkg_desc_t *desc, const char *repodir,
bool force)
{
char *path;
@ -19,13 +21,14 @@ static pkg_writer_t *open_writer(pkg_desc_t *desc, const char *repodir)
path = alloca(strlen(repodir) + strlen(desc->name) + 16);
sprintf(path, "%s/%s.pkg", repodir, desc->name);
return pkg_writer_open(path);
return pkg_writer_open(path, force);
}
static int cmd_pack(int argc, char **argv)
{
const char *filelist = NULL, *repodir = NULL, *descfile = NULL;
image_entry_t *list = NULL;
bool force = false;
pkg_writer_t *wr;
pkg_desc_t desc;
int i;
@ -45,6 +48,9 @@ static int cmd_pack(int argc, char **argv)
case 'r':
repodir = optarg;
break;
case 'f':
force = true;
break;
default:
tell_read_help(argv[0]);
return EXIT_FAILURE;
@ -72,7 +78,7 @@ static int cmd_pack(int argc, char **argv)
if (filelist != NULL && filelist_read(filelist, &list))
goto fail_desc;
wr = open_writer(&desc, repodir);
wr = open_writer(&desc, repodir, force);
if (wr == NULL)
goto fail_fp;
@ -113,6 +119,8 @@ static command_t pack = {
" --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"
" --force, -f If a package with the same name already exists,\n"
" overwrite it.\n"
"\n",
.run_cmd = cmd_pack,
};

View File

@ -78,10 +78,11 @@ static int flush_to_file(pkg_writer_t *wr)
return 0;
}
pkg_writer_t *pkg_writer_open(const char *path)
pkg_writer_t *pkg_writer_open(const char *path, bool force)
{
pkg_writer_t *wr = calloc(1, sizeof(*wr));
compressor_t *cmp;
int flags;
cmp = compressor_by_id(PKG_COMPRESSION_NONE);
if (cmp == NULL) {
@ -101,7 +102,15 @@ pkg_writer_t *pkg_writer_open(const char *path)
goto fail;
}
wr->fd = open(path, O_WRONLY | O_CREAT | O_EXCL, 0644);
flags = O_WRONLY | O_CREAT;
if (force) {
flags |= O_TRUNC;
} else {
flags |= O_EXCL;
}
wr->fd = open(path, flags, 0644);
if (wr->fd == -1) {
perror(path);
goto fail_stream;