From 7991c55181af49c5981ab7956d0189edd38e4274 Mon Sep 17 00:00:00 2001 From: senhuang42 Date: Wed, 26 Aug 2020 16:50:20 -0400 Subject: [PATCH] Move logic into new function FIO_removeMultiFilesWarning, add support for decompression --- programs/fileio.c | 54 +++++++++++++++++++++++++++++------------------ programs/util.c | 10 ++++----- programs/util.h | 2 +- 3 files changed, 40 insertions(+), 26 deletions(-) diff --git a/programs/fileio.c b/programs/fileio.c index 25b7d4d1..c982bda9 100644 --- a/programs/fileio.c +++ b/programs/fileio.c @@ -620,7 +620,7 @@ FIO_openDstFile(FIO_prefs_t* const prefs, return NULL; } DISPLAY("zstd: %s already exists; ", dstFileName); - if (UTIL_requireUserConfirmationToProceed("overwrite (y/n) ? ", "Not overwritten \n", "yY")) + if (UTIL_requireUserConfirmation("overwrite (y/n) ? ", "Not overwritten \n", "yY")) return NULL; } /* need to unlink */ @@ -789,6 +789,35 @@ static void FIO_adjustMemLimitForPatchFromMode(FIO_prefs_t* const prefs, FIO_setMemLimit(prefs, (unsigned)maxSize); } +/* FIO_removeMultiFilesWarning() : + * Returns 1 if the console should abort, 0 if console should proceed. + * Based on displayLevelCutoff (typically == 1) and the global setting g_display_prefs.displayLevel, this + * function may print a warning message, a user prompt, both, or none. + */ +static int FIO_removeMultiFilesWarning(const FIO_prefs_t* const prefs, int displayLevelCutoff, const char* outFileName) +{ + int error; + error = 0; + if (prefs->nbFiles > 1 && !prefs->overwrite) { + if (g_display_prefs.displayLevel <= displayLevelCutoff) { + if (prefs->removeSrcFile) { + DISPLAYLEVEL(1, "zstd: Aborting... not deleting files and processing into dst: %s", outFileName); + error = 1; + } + } else { + if (!strcmp(outFileName, stdoutmark)) { + DISPLAYLEVEL(2, "zstd: WARNING: all input files will be processed and concatenated into stdout. "); + } else { + DISPLAYLEVEL(2, "zstd: WARNING: all input files will be processed and concatenated into a single output file: %s ", outFileName); + } + if (prefs->removeSrcFile) + error = g_display_prefs.displayLevel > displayLevelCutoff && UTIL_requireUserConfirmation("Proceed? (y/n): ", "Aborting...", "yY"); + DISPLAY("\n"); + } + } + return error; +} + #ifndef ZSTD_NOCOMPRESS /* ********************************************************************** @@ -1693,25 +1722,8 @@ int FIO_compressMultipleFilenames(FIO_prefs_t* const prefs, /* init */ assert(outFileName != NULL || suffix != NULL); if (outFileName != NULL) { /* output into a single destination (stdout typically) */ - if (nbFiles > 1 && !prefs->overwrite) { - /* g_display_prefs.displayLevel <= 1 corresponds to -q flag */ - DISPLAY("%d\n", g_display_prefs.displayLevel); - if (g_display_prefs.displayLevel <= 1) { - if (prefs->removeSrcFile) { - DISPLAY("zstd: Aborting... not deleting files and processing into dst: %s", outFileName); - return 1; - } - } else { - if (!strcmp (outFileName, stdoutmark)) { - DISPLAY("zstd: WARNING: all input files will be processed and concatenated into stdout. "); - } else { - DISPLAY("zstd: WARNING: all input files will be processed and concatenated into a single output file: %s ", outFileName); - } - if (prefs->removeSrcFile) - error = g_display_prefs.displayLevel > 1 && UTIL_requireUserConfirmationToProceed("Proceed? (y/n): ", "Aborting...", "yY"); - DISPLAY("\n"); - } - } + if (FIO_removeMultiFilesWarning(prefs, 1 /* displayLevelCutoff */, outFileName)) + return 1; ress.dstFile = FIO_openDstFile(prefs, NULL, outFileName); if (ress.dstFile == NULL) { /* could not open outFileName */ error = 1; @@ -2606,6 +2618,8 @@ FIO_decompressMultipleFilenames(FIO_prefs_t* const prefs, dRess_t ress = FIO_createDResources(prefs, dictFileName); if (outFileName) { + if (FIO_removeMultiFilesWarning(prefs, 1 /* displayLevelCutoff */, outFileName)) + return 1; if (!prefs->testMode) { ress.dstFile = FIO_openDstFile(prefs, NULL, outFileName); if (ress.dstFile == 0) EXM_THROW(19, "cannot open %s", outFileName); diff --git a/programs/util.c b/programs/util.c index 6b220f39..d828dc42 100644 --- a/programs/util.c +++ b/programs/util.c @@ -87,20 +87,20 @@ UTIL_STATIC void* UTIL_realloc(void *ptr, size_t size) ******************************************/ int g_utilDisplayLevel; -int UTIL_requireUserConfirmationToProceed(const char* prompt, const char* abortMsg, +int UTIL_requireUserConfirmation(const char* prompt, const char* abortMsg, const char* acceptableLetters) { - int ch; + int ch, result; UTIL_DISPLAY("%s", prompt); ch = getchar(); + result = 0; if (strchr(acceptableLetters, ch) == NULL) { UTIL_DISPLAY("%s", abortMsg); - return 1; + result = 1; } /* flush the rest */ while ((ch!=EOF) && (ch!='\n')) ch = getchar(); - - return 0; + return result; } diff --git a/programs/util.h b/programs/util.h index aa48fd47..32649bf2 100644 --- a/programs/util.h +++ b/programs/util.h @@ -97,7 +97,7 @@ extern int g_utilDisplayLevel; * Displays a message prompt and returns success (0) if first character from stdin * matches any from acceptableLetters. Otherwise, returns failure (1) and displays abortMsg. */ -int UTIL_requireUserConfirmationToProceed(const char* const prompt, const char* const abortMsg, const char* const acceptableLetters); +int UTIL_requireUserConfirmation(const char* const prompt, const char* const abortMsg, const char* const acceptableLetters); /*-****************************************