1
0
Fork 0
mirror of https://github.com/pygos/pkg-utils.git synced 2024-11-22 12:59:46 +01:00

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 #ifndef PKGWRITER_H
#define PKGWRITER_H #define PKGWRITER_H
#include <stdbool.h>
#include "pkgformat.h" #include "pkgformat.h"
#include "compressor.h" #include "compressor.h"
typedef struct pkg_writer_t pkg_writer_t; 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); 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' }, { "description", required_argument, NULL, 'd' },
{ "file-list", required_argument, NULL, 'l' }, { "file-list", required_argument, NULL, 'l' },
{ "repo-dir", required_argument, NULL, 'r' }, { "repo-dir", required_argument, NULL, 'r' },
{ "force", no_argument, NULL, 'f' },
{ NULL, 0, NULL, 0 }, { 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; 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); path = alloca(strlen(repodir) + strlen(desc->name) + 16);
sprintf(path, "%s/%s.pkg", repodir, desc->name); 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) static int cmd_pack(int argc, char **argv)
{ {
const char *filelist = NULL, *repodir = NULL, *descfile = NULL; const char *filelist = NULL, *repodir = NULL, *descfile = NULL;
image_entry_t *list = NULL; image_entry_t *list = NULL;
bool force = false;
pkg_writer_t *wr; pkg_writer_t *wr;
pkg_desc_t desc; pkg_desc_t desc;
int i; int i;
@ -45,6 +48,9 @@ static int cmd_pack(int argc, char **argv)
case 'r': case 'r':
repodir = optarg; repodir = optarg;
break; break;
case 'f':
force = true;
break;
default: default:
tell_read_help(argv[0]); tell_read_help(argv[0]);
return EXIT_FAILURE; return EXIT_FAILURE;
@ -72,7 +78,7 @@ static int cmd_pack(int argc, char **argv)
if (filelist != NULL && filelist_read(filelist, &list)) if (filelist != NULL && filelist_read(filelist, &list))
goto fail_desc; goto fail_desc;
wr = open_writer(&desc, repodir); wr = open_writer(&desc, repodir, force);
if (wr == NULL) if (wr == NULL)
goto fail_fp; goto fail_fp;
@ -113,6 +119,8 @@ static command_t pack = {
" --description, -d <path> Specify a file containing a description of the\n" " --description, -d <path> Specify a file containing a description of the\n"
" package, including information such as package\n" " package, including information such as package\n"
" dependencies, the actual package name, etc.\n" " dependencies, the actual package name, etc.\n"
" --force, -f If a package with the same name already exists,\n"
" overwrite it.\n"
"\n", "\n",
.run_cmd = cmd_pack, .run_cmd = cmd_pack,
}; };

View file

@ -78,10 +78,11 @@ static int flush_to_file(pkg_writer_t *wr)
return 0; 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)); pkg_writer_t *wr = calloc(1, sizeof(*wr));
compressor_t *cmp; compressor_t *cmp;
int flags;
cmp = compressor_by_id(PKG_COMPRESSION_NONE); cmp = compressor_by_id(PKG_COMPRESSION_NONE);
if (cmp == NULL) { if (cmp == NULL) {
@ -101,7 +102,15 @@ pkg_writer_t *pkg_writer_open(const char *path)
goto fail; 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) { if (wr->fd == -1) {
perror(path); perror(path);
goto fail_stream; goto fail_stream;