]> git.ipfire.org Git - thirdparty/zstd.git/commitdiff
Move logic into new function FIO_removeMultiFilesWarning, add support for decompression
authorsenhuang42 <senhuang96@fb.com>
Wed, 26 Aug 2020 20:50:20 +0000 (16:50 -0400)
committersenhuang42 <senhuang96@fb.com>
Wed, 26 Aug 2020 20:50:20 +0000 (16:50 -0400)
programs/fileio.c
programs/util.c
programs/util.h

index 25b7d4d11894baacbedffa7b2788ae91c8b78acd..c982bda94b74014c4a91cfcce33d6d92fef6c32f 100644 (file)
@@ -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);
index 6b220f39ae8c7e9374768c0ec7d95017663917c1..d828dc42831daca732f96c5b010c1b80c3609a20 100644 (file)
@@ -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;
 }
 
 
index aa48fd47ae362e093174b9a987d854964e01bdc6..32649bf2505bd58bf8b0475f8c9acb29935f9c13 100644 (file)
@@ -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);
 
 
 /*-****************************************