Skip to content

Commit ad8fc64

Browse files
committed
Keep original file if -c or --stdout is given
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.
1 parent b848c16 commit ad8fc64

File tree

4 files changed

+12
-8
lines changed

4 files changed

+12
-8
lines changed

programs/zstd.1

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
.
2-
.TH "ZSTD" "1" "January 2022" "zstd 1.5.2" "User Commands"
2+
.TH "ZSTD" "1" "February 2022" "zstd 1.5.2" "User Commands"
33
.
44
.SH "NAME"
55
\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
165165
\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\.
166166
.
167167
.IP "\(bu" 4
168-
\fB\-c\fR, \fB\-\-stdout\fR: write to standard output (even if it is the console)
168+
\fB\-c\fR, \fB\-\-stdout\fR: write to standard output (even if it is the console); keep original files unchanged\.
169169
.
170170
.IP "\(bu" 4
171171
\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\.

programs/zstd.1.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ the last one takes effect.
212212
disable input and output checks. Allows overwriting existing files, input
213213
from console, output to stdout, operating on links, block devices, etc.
214214
* `-c`, `--stdout`:
215-
write to standard output (even if it is the console)
215+
write to standard output (even if it is the console); keep original files unchanged.
216216
* `--[no-]sparse`:
217217
enable / disable sparse FS support,
218218
to make files with many zeroes smaller on disk.

programs/zstdcli.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ static void usage_advanced(const char* programName)
164164
DISPLAYOUT( "Advanced arguments : \n");
165165
DISPLAYOUT( " -V : display Version number and exit \n");
166166

167-
DISPLAYOUT( " -c : write to standard output (even if it is the console) \n");
167+
DISPLAYOUT( " -c : write to standard output (even if it is the console), keep original file \n");
168168

169169
DISPLAYOUT( " -v : verbose mode; specify multiple times to increase verbosity \n");
170170
DISPLAYOUT( " -q : suppress warnings; specify twice to suppress errors too \n");
@@ -911,7 +911,7 @@ int main(int argCount, const char* argv[])
911911
if (!strcmp(argument, "--help")) { usage_advanced(programName); CLEAN_RETURN(0); }
912912
if (!strcmp(argument, "--verbose")) { g_displayLevel++; continue; }
913913
if (!strcmp(argument, "--quiet")) { g_displayLevel--; continue; }
914-
if (!strcmp(argument, "--stdout")) { forceStdout=1; outFileName=stdoutmark; g_displayLevel-=(g_displayLevel==2); continue; }
914+
if (!strcmp(argument, "--stdout")) { forceStdout=1; outFileName=stdoutmark; FIO_setRemoveSrcFile(prefs, 0); g_displayLevel-=(g_displayLevel==2); continue; }
915915
if (!strcmp(argument, "--ultra")) { ultra=1; continue; }
916916
if (!strcmp(argument, "--check")) { FIO_setChecksumFlag(prefs, 2); continue; }
917917
if (!strcmp(argument, "--no-check")) { FIO_setChecksumFlag(prefs, 0); continue; }
@@ -1096,7 +1096,7 @@ int main(int argCount, const char* argv[])
10961096
operation=zom_decompress; argument++; break;
10971097

10981098
/* Force stdout, even if stdout==console */
1099-
case 'c': forceStdout=1; outFileName=stdoutmark; argument++; break;
1099+
case 'c': forceStdout=1; outFileName=stdoutmark; FIO_setRemoveSrcFile(prefs, 0); argument++; break;
11001100

11011101
/* Use file content as dictionary */
11021102
case 'D': argument++; NEXT_FIELD(dictFileName); break;
@@ -1258,7 +1258,7 @@ int main(int argCount, const char* argv[])
12581258
}
12591259

12601260
nbInputFileNames = filenames->tableSize; /* saving number of input files */
1261-
1261+
12621262
if (recursive) { /* at this stage, filenameTable is a list of paths, which can contain both files and directories */
12631263
UTIL_expandFNT(&filenames, followLinks);
12641264
}
@@ -1371,7 +1371,7 @@ int main(int argCount, const char* argv[])
13711371
}
13721372
UTIL_refFilename(filenames, stdinmark);
13731373
}
1374-
1374+
13751375
if (!strcmp(filenames->fileNames[0], stdinmark) && !outFileName)
13761376
outFileName = stdoutmark; /* when input is stdin, default output is stdout */
13771377

tests/cli-tests/compression/basic.sh

+4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ zstd -c file | zstd -t
2424
zstd --stdout file | zstd -t
2525
println bob | zstd | zstd -t
2626

27+
# Test preservation in gzip mode
28+
(exec -a gzip zstd -c file) | zstd -t
29+
(exec -a gzip zstd --stdout file) | zstd -t
30+
2731
# Test --rm
2832
cp file file-rm
2933
zstd --rm file-rm; zstd -t file-rm.zst

0 commit comments

Comments
 (0)