Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep original file if -c or --stdout is given #3052

Merged
merged 1 commit into from
Mar 28, 2022

Conversation

dirkmueller
Copy link
Contributor

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.

@facebook-github-bot
Copy link

Hi @dirkmueller!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@fb.com. Thanks!

@facebook-github-bot
Copy link

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@Cyan4973
Copy link
Contributor

Cyan4973 commented Feb 7, 2022

Thanks @dirkmueller, I believe that's a good suggestion.

One missing aspect in this PR could be to add a test validating the new behavior, and ensuring it remains respected in future changes.
That's something we could also do ourselves if you don't feel comfortable to.

@dirkmueller dirkmueller force-pushed the gzip_keep branch 2 times, most recently from 70dd10d to ad8fc64 Compare February 7, 2022 22:30
@dirkmueller
Copy link
Contributor Author

@Cyan4973 Thank you for the quick review. I added a testcase to tests/cli-tests/compression/basic.sh. let me know if that works for you.

@@ -24,6 +24,12 @@ 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
ln -s "$(type -p zstd)" ./gzip
./gzip -c file | zstd -t ; test -f file
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor :
if you want to test that both sides of the test are completing fine, prefer && instead of ; separator.

> false ; echo this is successful
this is successful
> false ; true
> echo $?
0

> false && echo this is successful

> false && true
> echo $?
1

Copy link
Contributor Author

@dirkmueller dirkmueller Feb 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please test with set -e like these test scripts are using. In my experience the behavior of that with && is odd - && similar to if and while will temporarily turn off errexit mode and simply hide the error. try this example script:

$ cat x.sh 
set -e

false | false && true && echo it detected the failure
true && echo "did it really find the false?"
$ sh x.sh ; echo $?
did it really find the false?
0

The behavior we want that the first false return aborts the script (because if zstd -c fails for whatever reason, test -f will also remain trivially true)

@dirkmueller dirkmueller force-pushed the gzip_keep branch 2 times, most recently from d2ea3b5 to 15ff5ad Compare February 8, 2022 08:25
@dirkmueller
Copy link
Contributor Author

actually I realize after 169f8c1 I can remove the gzip symlinking logic and just call gzip directly.

@terrelln
Copy link
Contributor

terrelln commented Feb 8, 2022

@dirkmueller You still need the symlink logic. I believe that this is calling the system gzip.

I've avoided putting the zstd gzip symlink in cli-tools/bin so that it is still possible to call the system gzip.

@dirkmueller
Copy link
Contributor Author

@terrelln you're right. I've discovered $ZSTD_SYMLINK_DIR and changed it to use that. how about that?

Copy link
Contributor

@terrelln terrelln left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delay, and thanks for the ping!

@Cyan4973 are you happy if zstd --rm -c keeps the file, and zstd -c --rm removes the file?

If Yann is okay with that, can you also add a test to codify that behavior? E.g.

# Test -c overrides --rm
zstd --rm -c file | zstd -t; test -f file

# Test --rm overrides -c
cp file file-rm
zstd -c --rm file-rm | zstd -t; test ! -f file-rm

Then this PR looks good to me!

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.
@dirkmueller
Copy link
Contributor Author

@terrelln tests updated accordingly. I think this behavior is no different than using --rm and -k . I was a bit hesitant to hardcode this behavior in tests as afaik no user is relying on this behavior. we can still change the semantics.

However now the tests cover that case as you suggested.

@dirkmueller
Copy link
Contributor Author

hey y'all, I'd like to move this patch forward. anything I can do to get another round of review? @terrelln

Copy link
Contributor

@Cyan4973 Cyan4973 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me.
It's indeed proper behavior when combined with gzip symlink.

@dirkmueller
Copy link
Contributor Author

@terrelln are you good with the change as well?

@terrelln terrelln merged commit f229daa into facebook:dev Mar 28, 2022
@terrelln
Copy link
Contributor

Thanks for the PR @dirkmueller!

@Cyan4973 Cyan4973 mentioned this pull request Feb 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants