Merge pull request #237 from hainesr/create-file-perms
Do something more expected with new file permissions.
This commit is contained in:
commit
228cd4a74f
10
README.md
10
README.md
|
@ -141,6 +141,16 @@ fruit/orange
|
|||
|
||||
After this, entries in the zip archive will be saved in ordered state.
|
||||
|
||||
### Default permissions of zip archives
|
||||
|
||||
On Posix file systems the default file permissions applied to a new archive
|
||||
are (0666 - umask), which mimics the behavior of standard tools such as `touch`.
|
||||
|
||||
On Windows the default file permissions are set to 0644 as suggested by the
|
||||
[Ruby File documentation](http://ruby-doc.org/core-2.2.2/File.html).
|
||||
|
||||
When modifying a zip archive the file permissions of the archive are preserved.
|
||||
|
||||
### Reading a Zip file
|
||||
|
||||
```ruby
|
||||
|
|
|
@ -71,11 +71,12 @@ module Zip
|
|||
case
|
||||
when !buffer && ::File.size?(file_name)
|
||||
@create = nil
|
||||
@exist_file_perms = ::File.stat(file_name).mode
|
||||
@file_permissions = ::File.stat(file_name).mode
|
||||
::File.open(name, 'rb') do |f|
|
||||
read_from_stream(f)
|
||||
end
|
||||
when create
|
||||
@file_permissions = create_file_permissions
|
||||
@entry_set = EntrySet.new
|
||||
else
|
||||
raise Error, "File #{file_name} not found"
|
||||
|
@ -405,7 +406,7 @@ module Zip
|
|||
tmpfile.close
|
||||
if yield tmp_filename
|
||||
::File.rename(tmp_filename, name)
|
||||
::File.chmod(@exist_file_perms, name) if defined?(@exist_file_perms)
|
||||
::File.chmod(@file_permissions, name) if defined?(@file_permissions)
|
||||
end
|
||||
ensure
|
||||
tmpfile.unlink if tmpfile
|
||||
|
@ -416,6 +417,10 @@ module Zip
|
|||
temp_file.binmode
|
||||
temp_file
|
||||
end
|
||||
|
||||
def create_file_permissions
|
||||
::Zip::RUNNING_ON_WINDOWS ? 0644 : 0666 - ::File.umask
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -0,0 +1,69 @@
|
|||
require 'test_helper'
|
||||
|
||||
class FilePermissionsTest < MiniTest::Test
|
||||
|
||||
FILENAME = File.join(File.dirname(__FILE__), "umask.zip")
|
||||
|
||||
def teardown
|
||||
::File.unlink(FILENAME)
|
||||
end
|
||||
|
||||
if ::Zip::RUNNING_ON_WINDOWS
|
||||
# Windows tests
|
||||
|
||||
DEFAULT_PERMS = 0644
|
||||
|
||||
def test_windows_perms
|
||||
create_file
|
||||
|
||||
assert_equal DEFAULT_PERMS, ::File.stat(FILENAME).mode
|
||||
end
|
||||
|
||||
else
|
||||
# Unix tests
|
||||
|
||||
DEFAULT_PERMS = 0100666
|
||||
|
||||
def test_current_umask
|
||||
umask = DEFAULT_PERMS - ::File.umask
|
||||
create_file
|
||||
|
||||
assert_equal umask, ::File.stat(FILENAME).mode
|
||||
end
|
||||
|
||||
def test_umask_000
|
||||
set_umask(0000) do
|
||||
create_file
|
||||
end
|
||||
|
||||
assert_equal DEFAULT_PERMS, ::File.stat(FILENAME).mode
|
||||
end
|
||||
|
||||
def test_umask_066
|
||||
umask = 0066
|
||||
set_umask(umask) do
|
||||
create_file
|
||||
end
|
||||
|
||||
assert_equal((DEFAULT_PERMS - umask), ::File.stat(FILENAME).mode)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
def create_file
|
||||
::Zip::File.open(FILENAME, ::Zip::File::CREATE) do |zip|
|
||||
zip.comment = "test"
|
||||
end
|
||||
end
|
||||
|
||||
# If anything goes wrong, make sure the umask is restored.
|
||||
def set_umask(umask, &block)
|
||||
begin
|
||||
saved_umask = ::File.umask(umask)
|
||||
yield
|
||||
ensure
|
||||
::File.umask(saved_umask)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
Loading…
Reference in New Issue