-
Notifications
You must be signed in to change notification settings - Fork 321
Fix permissions on new zip files (#294) #300
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
Conversation
...instead of comparing against in-ruby subtraction math.
This test fails because in *NIX when the umask is 0027, files are created with 0640 permissions. The current implementation of Zip::File.create_file_permissions does a simple subtraction of 0666 - 0027 == 0637
Before this change: 1) Failure: FilePermissionsTest#test_umask_027 [/Users/marcoswk/workspace/rubyzip/test/file_permissions_test.rb:52]: Expected: 33184 Actual: 33183 After this change: 1) Failure: FilePermissionsTest#test_umask_027 [/Users/marcoswk/workspace/rubyzip/test/file_permissions_test.rb:52]: --- expected +++ actual @@ -1,2 +1,2 @@ # encoding: US-ASCII -"100640" +"100637"
This fixes rubyzip#294 in what I hope is a more sensible way than trying to mess with umasks, etc, directly. Temporary files were being created with 0600 permissions and then being set to different permissions, based on umask, etc, afterwards. I don't know what the rationale for this was, but there were errors in the umask calculations when moving from the temporary file to the intended end result.
Now we don't differentiate between Windows and Linux in the library code for this, we don't need separate tests.
lib/zip/file.rb
Outdated
| if yield tmp_filename | ||
| ::File.rename(tmp_filename, name) | ||
| ::File.chmod(@file_permissions, name) if defined?(@file_permissions) | ||
| ::File.chmod(@file_permissions, name) if @create.nil? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why change this to if @create.nil? instead of defined?(@file_permissions)
With your removal of the @file_permissions setter in the when create branch should mean that that variable is only-ever set for the non-create path.
I also lean towards avoiding a .nil? check on the @create variable, because the desired values for @create are a bit fuzzy to me, as a developer who's a bit unfamiliar with the code. The code comment for the initialize method suggests that @create should be either nil or true, but the constant is CREATE = 1 😃
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The main reasons I stopped using the @file_permissions variable here are:
- The logic for whether we need to change the permissions here is based upon whether the file is created or not, not whether there exists some file permissions. So,
- If someone decided to use the
@file_permissionsvariable for something else, we might get an odd bug here.
I agree with your concerns about the @create variable though. I'll look at whether I can make its use consistent.
|
@hainesr, looks sensible to me! There's one note suggesting not using a |
|
Thanks for your notes @metavida! I'll see if I can fix the usage of the |
|
Maybe something as simple as switching to a private method like this would do the trick: Then updating the code comment for |
|
I've been playing with this today. I added a couple of debug There were many cases where f = File.open(tmpname, opts)This, of course, calls the f = ::File.open(tmpname, opts)still passes all the tests and I think will make it easier to fix I'll add a commit to this pull request soon. |
|
Hmm, actually, as that file is simply opened and then immediately closed, maybe the only reason that code is there was to set the permissions and raise an error if the file already existed? Given we don't want the permissions to be set any more, and the file will not exist as we're generating a new random filename, then maybe it could be refactored out completely. I'll have a go and see what happens. |
Combine the creation of the temporary filename with the writing to it.
Make the internal @create varible more consistent and actually match the documentation. Zip::File::CREATE is now true, rather than 1. A new test is added to check if passing 1 in still works to ensure backwards compatibility.
|
Right, this pull request has ended up fixing a few other internal bits which were broken/inconsistent. Comments appreciated. |
4 similar comments
|
Awesome! Best case scenario for a bug fix: less application code to maintain and more test coverage! Thanks again for the time you're putting in to maintain this gem. It's been quite handy for us! |
|
Thanks @metavida. Unfortunately I can't merge this myself as I'm not a member of the rubyzip project, but hopefully someone will be along soon who can! |
4 similar comments
|
Ping? |
This is an attempt to fix #294 in a more sensible way than was attempted, by me :-), previously.
I used the improved tests by @metavida.
To fix it I stopped the original temporary files from being created with 0600 permissions. Is this sensible? Have I missed something?