From 6c1d146bdca9e77faa995419d4541f8b490246e6 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Thu, 27 Feb 2025 10:31:58 +0900 Subject: [PATCH] Add "--force-tmpfile" "--force-tmpfile" forces the use of "tmpfile()" instead of a file created in the default directory for temporary files (which is currently the same directory as the currently created archive) when using compression. Note that this option is mutually exclusive with "--temp-files-dir". --- Changelog.md | 4 ++++ README.md | 3 ++- src/helpers.c | 4 ++++ src/main.c | 9 +++++++++ src/parser.c | 9 ++++++++- src/parser.h | 1 + 6 files changed, 28 insertions(+), 2 deletions(-) diff --git a/Changelog.md b/Changelog.md index 5ca71fe..2758b1e 100644 --- a/Changelog.md +++ b/Changelog.md @@ -10,6 +10,10 @@ Some minor fixes. Changed behavior of permissions for temp-file (created during compression) to be more strict. This may not be important to most end-users. +Added `--force-tmpfile`, which acts like `--temp-files-dir ` but forces the +use of `tmpdir()` instead of forcing the temporary file's directory. +Note that these two flags are mutually exclusive. + ## Version 1.17 Fix `--whitelist-begins-with ` and `--whitelist-ends-with `. diff --git a/README.md b/README.md index a8bc6a9..0e7c2a1 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,8 @@ API calls. --no-abs-symlink : do not store absolute paths for symlinks --preserve-symlinks : preserve the symlink's path on archive creation instead of deriving abs/relative paths, ignores "--no-abs-symlink" (It is not recommended to use this option, as absolute-path-symlinks may be clobbered on extraction) --no-safe-links : keep symlinks that link to outside archive contents - --temp-files-dir : where to store temporary files created when compressing (defaults to same directory as output file) + --temp-files-dir : where to store temporary files created when compressing (defaults to same directory as output file) (this is mutually exclusive with "--force-tmpfile") + --force-tmpfile : Force the use of "tmpfile()" during compression (this is mutually exclusive with "--temp-files-dir") --write-version : Force write version file format (default 4) --chunk-min-size : minimum chunk size (default 4194304 or 4MiB) when using chunks (file formats v. 1 and up) --no-pre-sort-files : do NOT pre-sort files by size (by default enabled so that the first file is the largest) diff --git a/src/helpers.c b/src/helpers.c index e4b5921..66cccf1 100644 --- a/src/helpers.c +++ b/src/helpers.c @@ -852,6 +852,10 @@ uint_fast8_t simple_archiver_helper_string_allowed_lists( FILE *simple_archiver_helper_temp_dir(const SDArchiverParsed *parsed, char **out_temp_filename) { + if (parsed->flags & 0x40000) { + return tmpfile(); + } + __attribute__((cleanup(simple_archiver_helper_cleanup_c_string))) char *real_path_to_filename = parsed->temp_dir ? strdup(parsed->temp_dir) diff --git a/src/main.c b/src/main.c index 5f3c137..d68ca0e 100644 --- a/src/main.c +++ b/src/main.c @@ -59,6 +59,15 @@ int main(int argc, const char **argv) { return 6; } + if (parsed.temp_dir && (parsed.flags & 0x40000)) { + fprintf(stderr, + "ERROR: \"--temp-files-dir\" and \"--force-tmpfile\" is mutually " + "exclusive!\n"); + sleep(2); + simple_archiver_print_usage(); + return 9; + } + if ((parsed.flags & 0x3) == 0 && (parsed.flags & 0x2000) != 0) { fprintf(stderr, "WARNING: --force-dir-permissions specified, but has no effect " diff --git a/src/parser.c b/src/parser.c index f4fcae4..7cc60c6 100644 --- a/src/parser.c +++ b/src/parser.c @@ -186,7 +186,12 @@ void simple_archiver_print_usage(void) { "contents\n"); fprintf(stderr, "--temp-files-dir : where to store temporary files created " - "when compressing (defaults to same directory as output file)\n"); + "when compressing (defaults to same directory as output file) " + "(this is mutually exclusive with \"--force-tmpfile\")\n"); + fprintf(stderr, + "--force-tmpfile : Force the use of \"tmpfile()\" during " + "compression (this is mutually exclusive with \"--temp-files-dir\")" + "\n"); fprintf(stderr, "--write-version : Force write version file format " "(default 4)\n"); @@ -479,6 +484,8 @@ int simple_archiver_parse_args(int argc, const char **argv, out->temp_dir = simple_archiver_helper_real_path_to_name(argv[1]); --argc; ++argv; + } else if (strcmp(argv[0], "--force-tmpfile") == 0) { + out->flags |= 0x40000; } else if (strcmp(argv[0], "--write-version") == 0) { if (argc < 2) { fprintf(stderr, diff --git a/src/parser.h b/src/parser.h index 490c701..29be7f3 100644 --- a/src/parser.h +++ b/src/parser.h @@ -61,6 +61,7 @@ typedef struct SDArchiverParsed { /// 0b xxxx xxx1 xxxx xxxx xxxx xxxx - Force set empty directory permissions. /// 0b xxxx xx1x xxxx xxxx xxxx xxxx - white/black-list checking is /// case-insensitive. + /// 0b xxxx x1xx xxxx xxxx xxxx xxxx - Force use tmpfile. uint32_t flags; /// Null-terminated string. char *filename; -- 2.49.0