]> git.ipfire.org Git - thirdparty/zstd.git/commitdiff
Keep original file if -c or --stdout is given 3052/head
authorDirk Müller <dirk@dmllr.de>
Mon, 7 Feb 2022 19:39:15 +0000 (20:39 +0100)
committerDirk Müller <dirk@dmllr.de>
Wed, 9 Mar 2022 22:38:59 +0000 (23:38 +0100)
Set removeSrcFile back to false when -c or --stdout is used to improve
compatibility with gzip(1) behavior.

gzip(1) is removing the original file on compression unless --stdout or
/-c is used. zstd is defaulting to keep the file unless --rm is used or
when it is called via a gzip symlink, in which it is removing by
default. Specifying -c/--stdout turns this behavior off.

programs/zstd.1
programs/zstd.1.md
programs/zstdcli.c
tests/cli-tests/compression/basic.sh

index 0e6e016e950423cf97d6e6a545b3768cb327e4ea..346b08b181b7689529af1b42bcf84b88ac417a6b 100644 (file)
@@ -1,5 +1,5 @@
 .
-.TH "ZSTD" "1" "January 2022" "zstd 1.5.2" "User Commands"
+.TH "ZSTD" "1" "February 2022" "zstd 1.5.2" "User Commands"
 .
 .SH "NAME"
 \fBzstd\fR \- zstd, zstdmt, unzstd, zstdcat \- Compress or decompress \.zst files
@@ -165,7 +165,7 @@ Additionally, this can be used to limit memory for dictionary training\. This pa
 \fB\-f\fR, \fB\-\-force\fR: disable input and output checks\. Allows overwriting existing files, input from console, output to stdout, operating on links, block devices, etc\.
 .
 .IP "\(bu" 4
-\fB\-c\fR, \fB\-\-stdout\fR: write to standard output (even if it is the console)
+\fB\-c\fR, \fB\-\-stdout\fR: write to standard output (even if it is the console); keep original files unchanged\.
 .
 .IP "\(bu" 4
 \fB\-\-[no\-]sparse\fR: enable / disable sparse FS support, to make files with many zeroes smaller on disk\. Creating sparse files may save disk space and speed up decompression by reducing the amount of disk I/O\. default: enabled when output is into a file, and disabled when output is stdout\. This setting overrides default and can force sparse mode over stdout\.
index 569ca1aa0db4b1b576c076da70f20a4b0a115933..c78fd4abf066ae1bf8759ca8de6fee06adb53923 100644 (file)
@@ -212,7 +212,7 @@ the last one takes effect.
     disable input and output checks. Allows overwriting existing files, input
     from console, output to stdout, operating on links, block devices, etc.
 * `-c`, `--stdout`:
-    write to standard output (even if it is the console)
+    write to standard output (even if it is the console); keep original files unchanged.
 * `--[no-]sparse`:
     enable / disable sparse FS support,
     to make files with many zeroes smaller on disk.
index 0b9b82a9859990a66d5a58d328019c1b4b5e41e7..6c200bc98f6073f30c282740b4e4ffe8d368be48 100644 (file)
@@ -178,7 +178,7 @@ static void usage_advanced(const char* programName)
     DISPLAYOUT( "Advanced arguments : \n");
     DISPLAYOUT( " -V     : display Version number and exit \n");
 
-    DISPLAYOUT( " -c     : write to standard output (even if it is the console) \n");
+    DISPLAYOUT( " -c     : write to standard output (even if it is the console), keep original file \n");
 
     DISPLAYOUT( " -v     : verbose mode; specify multiple times to increase verbosity \n");
     DISPLAYOUT( " -q     : suppress warnings; specify twice to suppress errors too \n");
@@ -925,7 +925,7 @@ int main(int argCount, const char* argv[])
                 if (!strcmp(argument, "--help")) { usage_advanced(programName); CLEAN_RETURN(0); }
                 if (!strcmp(argument, "--verbose")) { g_displayLevel++; continue; }
                 if (!strcmp(argument, "--quiet")) { g_displayLevel--; continue; }
-                if (!strcmp(argument, "--stdout")) { forceStdout=1; outFileName=stdoutmark; g_displayLevel-=(g_displayLevel==2); continue; }
+                if (!strcmp(argument, "--stdout")) { forceStdout=1; outFileName=stdoutmark; FIO_setRemoveSrcFile(prefs, 0); g_displayLevel-=(g_displayLevel==2); continue; }
                 if (!strcmp(argument, "--ultra")) { ultra=1; continue; }
                 if (!strcmp(argument, "--check")) { FIO_setChecksumFlag(prefs, 2); continue; }
                 if (!strcmp(argument, "--no-check")) { FIO_setChecksumFlag(prefs, 0); continue; }
@@ -1114,7 +1114,7 @@ int main(int argCount, const char* argv[])
                         operation=zom_decompress; argument++; break;
 
                     /* Force stdout, even if stdout==console */
-                case 'c': forceStdout=1; outFileName=stdoutmark; argument++; break;
+                case 'c': forceStdout=1; outFileName=stdoutmark; FIO_setRemoveSrcFile(prefs, 0); argument++; break;
 
                     /* do not store filename - gzip compatibility - nothing to do */
                 case 'n': argument++; break;
@@ -1279,7 +1279,7 @@ int main(int argCount, const char* argv[])
     }
 
     nbInputFileNames = filenames->tableSize; /* saving number of input files */
-    
+
     if (recursive) {  /* at this stage, filenameTable is a list of paths, which can contain both files and directories */
         UTIL_expandFNT(&filenames, followLinks);
     }
@@ -1392,7 +1392,7 @@ int main(int argCount, const char* argv[])
        }
        UTIL_refFilename(filenames, stdinmark);
     }
-    
+
     if (!strcmp(filenames->fileNames[0], stdinmark) && !outFileName)
         outFileName = stdoutmark;  /* when input is stdin, default output is stdout */
 
index 8b63e40760cc174807a79d248f2eb932f66dcd36..b6e2aa0456a354423ae1b53aa292ed981e4222ae 100755 (executable)
@@ -24,6 +24,10 @@ zstd -c file       | zstd -t
 zstd --stdout file | zstd -t
 println bob | zstd | zstd -t
 
+# Test keeping input file when compressing to stdout in gzip mode
+$ZSTD_SYMLINK_DIR/gzip -c file       | zstd -t ; test -f file
+$ZSTD_SYMLINK_DIR/gzip --stdout file | zstd -t ; test -f file
+
 # Test --rm
 cp file file-rm
 zstd --rm file-rm; zstd -t file-rm.zst