Skip to content

Commit

Permalink
Merge pull request #3942 from facebook/fix3719
Browse files Browse the repository at this point in the history
Fix #3719 : mixing -c, -o and --rm
  • Loading branch information
Cyan4973 committed Mar 12, 2024
2 parents 4eea5af + 1362699 commit 8688c6e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
8 changes: 5 additions & 3 deletions programs/zstd.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,15 +225,17 @@ the last one takes effect.
This parameter defines a loose target: compressed blocks will target this size "on average", but individual blocks can still be larger or smaller.
Enabling this feature can decrease compression speed by up to ~10% at level 1.
Higher levels will see smaller relative speed regression, becoming invisible at higher settings.
* `-o FILE`:
save result into `FILE`.
* `-f`, `--force`:
disable input and output checks. Allows overwriting existing files, input
from console, output to stdout, operating on links, block devices, etc.
During decompression and when the output destination is stdout, pass-through
unrecognized formats as-is.
* `-c`, `--stdout`:
write to standard output (even if it is the console); keep original files unchanged.
write to standard output (even if it is the console); keep original files (disable `--rm`).
* `-o FILE`:
save result into `FILE`.
Note that this operation is in conflict with `-c`.
If both operations are present on the command line, the last expressed one wins.
* `--[no-]sparse`:
enable / disable sparse FS support,
to make files with many zeroes smaller on disk.
Expand Down
10 changes: 5 additions & 5 deletions programs/zstdcli.c
Original file line number Diff line number Diff line change
Expand Up @@ -962,7 +962,7 @@ int main(int argCount, const char* argv[])
if (!strcmp(argument, "--help")) { usageAdvanced(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; removeSrcFile=0; continue; }
if (!strcmp(argument, "--stdout")) { forceStdout=1; outFileName=stdoutmark; 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; }
Expand Down Expand Up @@ -1176,7 +1176,10 @@ int main(int argCount, const char* argv[])
operation=zom_decompress; argument++; break;

/* Force stdout, even if stdout==console */
case 'c': forceStdout=1; outFileName=stdoutmark; removeSrcFile=0; argument++; break;
case 'c': forceStdout=1; outFileName=stdoutmark; argument++; break;

/* destination file name */
case 'o': argument++; NEXT_FIELD(outFileName); break;

/* do not store filename - gzip compatibility - nothing to do */
case 'n': argument++; break;
Expand All @@ -1202,9 +1205,6 @@ int main(int argCount, const char* argv[])
/* test compressed file */
case 't': operation=zom_test; argument++; break;

/* destination file name */
case 'o': argument++; NEXT_FIELD(outFileName); break;

/* limit memory */
case 'M':
argument++;
Expand Down
32 changes: 30 additions & 2 deletions tests/playTests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -234,12 +234,23 @@ unset ZSTD_CLEVEL
println "test : compress to stdout"
zstd tmp -c > tmpCompressed
zstd tmp --stdout > tmpCompressed # long command format
println "test : compress to named file"

println "test : compress to named file (-o)"
rm -f tmpCompressed
zstd tmp -o tmpCompressed
test -f tmpCompressed # file must be created

println "test : force write, correct order"
zstd tmp -fo tmpCompressed

println "test : -c + -o : last one wins"
rm -f tmpOut
zstd tmp -c > tmpCompressed -o tmpOut
test -f tmpOut # file must be created
rm -f tmpCompressed
zstd tmp -o tmpOut -c > tmpCompressed
test -f tmpCompressed # file must be created

println "test : forgotten argument"
cp tmp tmp2
zstd tmp2 -fo && die "-o must be followed by filename "
Expand Down Expand Up @@ -439,6 +450,8 @@ println "test: --rm is disabled when output is stdout"
test -f tmp
zstd --rm tmp -c > $INTOVOID
test -f tmp # tmp shall still be there
zstd --rm tmp --stdout > $INTOVOID
test -f tmp # tmp shall still be there
zstd -f --rm tmp -c > $INTOVOID
test -f tmp # tmp shall still be there
zstd -f tmp -c > $INTOVOID --rm
Expand All @@ -456,7 +469,22 @@ zstd -f tmp tmp2 -o tmp3.zst --rm # just warns, no prompt
test -f tmp
test -f tmp2
zstd -q tmp tmp2 -o tmp3.zst --rm && die "should refuse to concatenate"

println "test: --rm is active with -o when single input"
rm -f tmp2.zst
zstd --rm tmp2 -o tmp2.zst
test -f tmp2.zst
test ! -f tmp2
println "test: -c followed by -o => -o wins, so --rm remains active" # (#3719)
rm tmp2.zst
cp tmp tmp2
zstd --rm tmp2 -c > $INTOVOID -o tmp2.zst
test ! -f tmp2
println "test: -o followed by -c => -c wins, so --rm is disabled" # (#3719)
rm tmp3.zst
cp tmp tmp2
zstd -v --rm tmp2 -o tmp2.zst -c > tmp3.zst
test -f tmp2
test -f tmp3.zst
println "test : should quietly not remove non-regular file"
println hello > tmp
zstd tmp -f -o "$DEVDEVICE" 2>tmplog > "$INTOVOID"
Expand Down

0 comments on commit 8688c6e

Please sign in to comment.