diff --git a/include/pkgwriter.h b/include/pkgwriter.h index 65f552e..cb485cc 100644 --- a/include/pkgwriter.h +++ b/include/pkgwriter.h @@ -1,12 +1,14 @@ #ifndef PKGWRITER_H #define PKGWRITER_H +#include + #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); diff --git a/main/cmd/pack/pack.c b/main/cmd/pack/pack.c index 1b57042..ea563e4 100644 --- a/main/cmd/pack/pack.c +++ b/main/cmd/pack/pack.c @@ -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 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, }; diff --git a/main/pkgwriter.c b/main/pkgwriter.c index 4831b46..bb37530 100644 --- a/main/pkgwriter.c +++ b/main/pkgwriter.c @@ -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;