@@ -612,7 +612,7 @@ FIO_openDstFile(FIO_ctx_t* fCtx, FIO_prefs_t* const prefs,
612612 if (!prefs -> overwrite ) {
613613 if (g_display_prefs .displayLevel <= 1 ) {
614614 /* No interaction possible */
615- DISPLAY ( "zstd: %s already exists; not overwritten \n" ,
615+ DISPLAYLEVEL ( 1 , "zstd: %s already exists; not overwritten \n" ,
616616 dstFileName );
617617 return NULL ;
618618 }
@@ -723,7 +723,7 @@ int FIO_checkFilenameCollisions(const char** filenameTable, unsigned nbFiles) {
723723
724724 filenameTableSorted = (const char * * ) malloc (sizeof (char * ) * nbFiles );
725725 if (!filenameTableSorted ) {
726- DISPLAY ( "Unable to malloc new str array, not checking for name collisions \n" );
726+ DISPLAYLEVEL ( 1 , "Allocation error during filename collision checking \n" );
727727 return 1 ;
728728 }
729729
@@ -740,7 +740,7 @@ int FIO_checkFilenameCollisions(const char** filenameTable, unsigned nbFiles) {
740740 prevElem = filenameTableSorted [0 ];
741741 for (u = 1 ; u < nbFiles ; ++ u ) {
742742 if (strcmp (prevElem , filenameTableSorted [u ]) == 0 ) {
743- DISPLAY ( "WARNING: Two files have same filename: %s\n" , prevElem );
743+ DISPLAYLEVEL ( 2 , "WARNING: Two files have same filename: %s\n" , prevElem );
744744 }
745745 prevElem = filenameTableSorted [u ];
746746 }
@@ -823,41 +823,59 @@ static void FIO_adjustMemLimitForPatchFromMode(FIO_prefs_t* const prefs,
823823 FIO_setMemLimit (prefs , (unsigned )maxSize );
824824}
825825
826- /* FIO_removeMultiFilesWarning() :
826+ /* FIO_multiFilesConcatWarning() :
827+ * This function handles logic when processing multiple files with -o or -c, displaying the appropriate warnings/prompts.
827828 * Returns 1 if the console should abort, 0 if console should proceed.
828- * This function handles logic when processing multiple files with -o, displaying the appropriate warnings/prompts.
829829 *
830- * If -f is specified, or there is just 1 file, zstd will always proceed as usual.
831- * If --rm is specified, there will be a prompt asking for user confirmation.
832- * If -f is specified with --rm, zstd will proceed as usual
833- * If -q is specified with --rm, zstd will abort pre-emptively
834- * If neither flag is specified, zstd will prompt the user for confirmation to proceed.
835- * If --rm is not specified, then zstd will print a warning to the user (which can be silenced with -q).
836- * Note : --rm in combination with stdout is not allowed.
830+ * If output is stdout or test mode is active, check that `--rm` disabled.
831+ *
832+ * If there is just 1 file to process, zstd will proceed as usual.
833+ * If each file get processed into its own separate destination file, proceed as usual.
834+ *
835+ * When multiple files are processed into a single output,
836+ * display a warning message, then disable --rm if it's set.
837+ *
838+ * If -f is specified or if output is stdout, just proceed.
839+ * If output is set with -o, prompt for confirmation.
837840 */
838- static int FIO_removeMultiFilesWarning ( FIO_ctx_t * const fCtx , const FIO_prefs_t * const prefs , const char * outFileName , int displayLevelCutoff )
841+ static int FIO_multiFilesConcatWarning ( const FIO_ctx_t * fCtx , FIO_prefs_t * prefs , const char * outFileName , int displayLevelCutoff )
839842{
840- int error = 0 ;
841- if (fCtx -> nbFilesTotal > 1 && !prefs -> overwrite ) {
842- if (g_display_prefs .displayLevel <= displayLevelCutoff ) {
843- if (prefs -> removeSrcFile ) {
844- DISPLAYLEVEL (1 , "zstd: Aborting... not deleting files and processing into dst: %s\n" , outFileName );
845- error = 1 ;
846- }
847- } else {
848- if (!strcmp (outFileName , stdoutmark )) {
849- DISPLAYLEVEL (2 , "zstd: WARNING: all input files will be processed and concatenated into stdout. \n" );
850- } else {
851- DISPLAYLEVEL (2 , "zstd: WARNING: all input files will be processed and concatenated into a single output file: %s \n" , outFileName );
852- }
853- DISPLAYLEVEL (2 , "The concatenated output CANNOT regenerate original file names nor directory structure. \n" )
854- if (prefs -> removeSrcFile ) {
855- assert (fCtx -> hasStdoutOutput == 0 ); /* not possible : never erase source files when output == stdout */
856- error = (g_display_prefs .displayLevel > displayLevelCutoff ) && UTIL_requireUserConfirmation ("This is a destructive operation. Proceed? (y/n): " , "Aborting..." , "yY" , fCtx -> hasStdinInput );
857- }
858- }
843+ if (fCtx -> hasStdoutOutput ) assert (prefs -> removeSrcFile == 0 );
844+ if (prefs -> testMode ) {
845+ assert (prefs -> removeSrcFile == 0 );
846+ return 0 ;
859847 }
860- return error ;
848+
849+ if (fCtx -> nbFilesTotal == 1 ) return 0 ;
850+ assert (fCtx -> nbFilesTotal > 1 );
851+
852+ if (!outFileName ) return 0 ;
853+
854+ if (fCtx -> hasStdoutOutput ) {
855+ DISPLAYLEVEL (2 , "zstd: WARNING: all input files will be processed and concatenated into stdout. \n" );
856+ } else {
857+ DISPLAYLEVEL (2 , "zstd: WARNING: all input files will be processed and concatenated into a single output file: %s \n" , outFileName );
858+ }
859+ DISPLAYLEVEL (2 , "The concatenated output CANNOT regenerate original file names nor directory structure. \n" )
860+
861+ /* multi-input into single output : --rm is not allowed */
862+ if (prefs -> removeSrcFile ) {
863+ DISPLAYLEVEL (2 , "Since it's a destructive operation, input files will not be removed. \n" );
864+ prefs -> removeSrcFile = 0 ;
865+ }
866+
867+ if (fCtx -> hasStdoutOutput ) return 0 ;
868+ if (prefs -> overwrite ) return 0 ;
869+
870+ /* multiple files concatenated into single destination file using -o without -f */
871+ if (g_display_prefs .displayLevel <= displayLevelCutoff ) {
872+ /* quiet mode => no prompt => fail automatically */
873+ DISPLAYLEVEL (1 , "Concatenating multiple processed inputs into a single output loses file metadata. \n" );
874+ DISPLAYLEVEL (1 , "Aborting. \n" );
875+ return 1 ;
876+ }
877+ /* normal mode => prompt */
878+ return UTIL_requireUserConfirmation ("Proceed? (y/n): " , "Aborting..." , "yY" , fCtx -> hasStdinInput );
861879}
862880
863881static ZSTD_inBuffer setInBuffer (const void * buf , size_t s , size_t pos )
@@ -1767,9 +1785,9 @@ FIO_compressFilename_srcFile(FIO_ctx_t* const fCtx,
17671785 & srcFileStat , compressionLevel );
17681786 AIO_ReadPool_closeFile (ress .readCtx );
17691787
1770- if ( prefs -> removeSrcFile /* --rm */
1771- && result == 0 /* success */
1772- && strcmp (srcFileName , stdinmark ) /* exception : don't erase stdin */
1788+ if ( prefs -> removeSrcFile /* --rm */
1789+ && result == 0 /* success */
1790+ && strcmp (srcFileName , stdinmark ) /* exception : don't erase stdin */
17731791 ) {
17741792 /* We must clear the handler, since after this point calling it would
17751793 * delete both the source and destination files.
@@ -1791,7 +1809,8 @@ checked_index(const char* options[], size_t length, size_t index) {
17911809
17921810#define INDEX (options , index ) checked_index((options), sizeof(options) / sizeof(char*), (size_t)(index))
17931811
1794- void FIO_displayCompressionParameters (const FIO_prefs_t * prefs ) {
1812+ void FIO_displayCompressionParameters (const FIO_prefs_t * prefs )
1813+ {
17951814 static const char * formatOptions [5 ] = {ZSTD_EXTENSION , GZ_EXTENSION , XZ_EXTENSION ,
17961815 LZMA_EXTENSION , LZ4_EXTENSION };
17971816 static const char * sparseOptions [3 ] = {" --no-sparse" , "" , " --sparse" };
@@ -1920,7 +1939,7 @@ int FIO_compressMultipleFilenames(FIO_ctx_t* const fCtx,
19201939 assert (outFileName != NULL || suffix != NULL );
19211940 if (outFileName != NULL ) { /* output into a single destination (stdout typically) */
19221941 FILE * dstFile ;
1923- if (FIO_removeMultiFilesWarning (fCtx , prefs , outFileName , 1 /* displayLevelCutoff */ )) {
1942+ if (FIO_multiFilesConcatWarning (fCtx , prefs , outFileName , 1 /* displayLevelCutoff */ )) {
19241943 FIO_freeCResources (& ress );
19251944 return 1 ;
19261945 }
@@ -2742,7 +2761,7 @@ FIO_decompressMultipleFilenames(FIO_ctx_t* const fCtx,
27422761 dRess_t ress = FIO_createDResources (prefs , dictFileName );
27432762
27442763 if (outFileName ) {
2745- if (FIO_removeMultiFilesWarning (fCtx , prefs , outFileName , 1 /* displayLevelCutoff */ )) {
2764+ if (FIO_multiFilesConcatWarning (fCtx , prefs , outFileName , 1 /* displayLevelCutoff */ )) {
27462765 FIO_freeDResources (ress );
27472766 return 1 ;
27482767 }
0 commit comments