Permalink
Browse files

Merge branch 'perms'

  • Loading branch information...
alor committed Nov 17, 2011
2 parents a2a4558 + 05cb90b commit 4b3fb4fb688fe3805244e127e33cacf2e07523a4
View
@@ -83,11 +83,11 @@ def gets(aSepString=$/)
bufferIndex=0
while ((matchIndex = @outputBuffer.index(aSepString, bufferIndex)) == nil)
bufferIndex=@outputBuffer.length
if input_finished?
return @outputBuffer.empty? ? nil : flush
end
@outputBuffer << produce_input
bufferIndex=@outputBuffer.length
if input_finished?
return @outputBuffer.empty? ? nil : flush
end
@outputBuffer << produce_input
end
sepIndex=matchIndex + aSepString.length
return @outputBuffer.slice!(0...sepIndex)
@@ -107,7 +107,7 @@ def readline(aSepString = $/)
def each_line(aSepString = $/)
while true
yield readline(aSepString)
yield readline(aSepString)
end
rescue EOFError
end
@@ -147,10 +147,10 @@ def putc(anObject)
def puts(*params)
params << "\n" if params.empty?
params.flatten.each {
|element|
val = element.to_s
self << val
self << "\n" unless val[-1,1] == "\n"
|element|
val = element.to_s
self << val
self << "\n" unless val[-1,1] == "\n"
}
end
View
@@ -56,6 +56,8 @@ class ZipEntry
attr_accessor :restore_times, :restore_permissions, :restore_ownership
attr_accessor :unix_uid, :unix_gid, :unix_perms
attr_accessor :dirty
attr_reader :ftype, :filepath # :nodoc:
# Returns the character encoding used for name and comment
@@ -118,6 +120,7 @@ def initialize(zipfile = "", name = "", comment = "", extra = "",
@unix_uid = nil
@unix_gid = nil
@unix_perms = nil
#puts "--> ZipEntry.new #{@zipfile} #{@name}"
# @posix_acl = nil
# @ntfs_acl = nil
@@ -130,6 +133,8 @@ def initialize(zipfile = "", name = "", comment = "", extra = "",
unless ZipExtraField === @extra
@extra = ZipExtraField.new(@extra.to_s)
end
@dirty = false
end
def time
@@ -407,6 +412,7 @@ def set_extra_attributes_on_path(destPath) # :nodoc:
end
def write_c_dir_entry(io) #:nodoc:all
#puts "--> write_c_dir_entry [%o]" % (@unix_perms.nil? ? 0 : @unix_perms)
case @fstype
when FSTYPE_UNIX
ft = nil
@@ -433,15 +439,15 @@ def write_c_dir_entry(io) #:nodoc:all
@fstype , # filesystem type
VERSION_NEEDED_TO_EXTRACT , # @versionNeededToExtract ,
0 , # @gp_flags ,
@compression_method ,
@time.to_binary_dos_time , # @lastModTime ,
@time.to_binary_dos_date , # @lastModDate ,
@compression_method ,
@time.to_binary_dos_time , # @lastModTime ,
@time.to_binary_dos_date , # @lastModDate ,
@crc ,
@compressed_size ,
@compressed_size ,
@size ,
@name ? @name.length : 0 ,
@extra ? @extra.c_dir_length : 0 ,
@comment ? @comment.length : 0 ,
@comment ? @comment.length : 0 ,
0 , # disk number start
@internalFileAttributes , # file type (binary=0, text=1)
@externalFileAttributes , # native filesystem attributes
View
@@ -50,8 +50,8 @@ def parent(entry)
def glob(pattern, flags = File::FNM_PATHNAME|File::FNM_DOTMATCH)
entries.select {
|entry|
File.fnmatch(pattern, entry.name.chomp('/'), flags)
|entry|
File.fnmatch(pattern, entry.name.chomp('/'), flags)
}
end
@@ -173,7 +173,7 @@ def create(name)
end
}
if ! field_class
raise ZipError, "Unknown extra field '#{name}'"
raise ZipError, "Unknown extra field '#{name}'"
end
self[name] = field_class.new()
end
View
@@ -87,7 +87,7 @@ def ZipFile.open(fileName, create = nil)
ensure
zf.close
end
else
else
zf
end
end
@@ -129,12 +129,14 @@ def get_input_stream(entry, &aProc)
# Returns an output stream to the specified entry. If a block is passed
# the stream object is passed to the block and the stream is automatically
# closed afterwards just as with ruby's builtin File.open method.
def get_output_stream(entry, &aProc)
def get_output_stream(entry, permissionInt = nil, &aProc)
newEntry = entry.kind_of?(ZipEntry) ? entry : ZipEntry.new(@name, entry.to_s)
if newEntry.directory?
raise ArgumentError,
"cannot open stream to directory entry - '#{newEntry}'"
raise ArgumentError,
"cannot open stream to directory entry - '#{newEntry}'"
end
#puts "--> [%o]" % permissionInt
newEntry.unix_perms = permissionInt
zipStreamableEntry = ZipStreamableStream.new(newEntry)
@entrySet << zipStreamableEntry
zipStreamableEntry.get_output_stream(&aProc)
@@ -190,13 +192,19 @@ def extract(entry, destPath, &onExistsProc)
# Commits changes that has been made since the previous commit to
# the zip archive.
def commit
return if ! commit_required?
#puts "--> commit"
return if ! commit_required?
#puts "--> commit real"
on_success_replace(name) {
|tmpFile|
ZipOutputStream.open(tmpFile) {
|zos|
@entrySet.each { |e| e.write_to_zip_output_stream(zos) }
@entrySet.each { |e|
#puts "--> commit [#{e.name}] perm [%o]" % e.unix_perms
e.write_to_zip_output_stream(zos)
e.dirty = false
}
zos.comment = comment
}
true
@@ -221,6 +229,12 @@ def close
# Returns true if any changes has been made to this archive since
# the previous commit
def commit_required?
#puts "--> cr? entryset: [#{@entrySet != @storedEntries}]"
#puts "--> cr? [#{@entrySet.inspect}]"
#puts "--> cr? [#{@storedEntries.inspect}]"
@entrySet.each do |e|
return true if e.dirty
end
return @entrySet != @storedEntries || @create == ZipFile::CREATE
end
@@ -77,11 +77,12 @@ def close_buffer
return @outputStream
end
# Closes the current entry and opens a new for writing.
# Closes the current entry and opens a new for writing.
# +entry+ can be a ZipEntry object or a string.
def put_next_entry(entryname, comment = nil, extra = nil, compression_method = ZipEntry::DEFLATED, level = Zlib::DEFAULT_COMPRESSION)
raise ZipError, "zip stream is closed" if @closed
new_entry = ZipEntry.new(@fileName, entryname.to_s)
new_entry.unix_perms = entryname.unix_perms if entryname.respond_to? :unix_perms
new_entry.comment = comment if !comment.nil?
if (!extra.nil?)
new_entry.extra = ZipExtraField === extra ? extra : ZipExtraField.new(extra.to_s)
@@ -101,8 +102,8 @@ def copy_raw_entry(entry)
entry.write_local_entry(@outputStream)
@compressor = NullCompressor.instance
entry.get_raw_input_stream {
|is|
is.seek(src_pos, IO::SEEK_SET)
|is|
is.seek(src_pos, IO::SEEK_SET)
IOExtras.copy_stream_n(@outputStream, is, entry.compressed_size)
}
@compressor = NullCompressor.instance
@@ -114,7 +115,7 @@ def finalize_current_entry
return unless @currentEntry
finish
@currentEntry.compressed_size = @outputStream.tell - @currentEntry.localHeaderOffset -
@currentEntry.calculate_local_header_size
@currentEntry.calculate_local_header_size
@currentEntry.size = @compressor.size
@currentEntry.crc = @compressor.crc
@currentEntry = nil
@@ -130,19 +131,19 @@ def init_next_entry(entry, level = Zlib::DEFAULT_COMPRESSION)
def get_compressor(entry, level)
case entry.compression_method
when ZipEntry::DEFLATED then Deflater.new(@outputStream, level)
when ZipEntry::STORED then PassThruCompressor.new(@outputStream)
when ZipEntry::DEFLATED then Deflater.new(@outputStream, level)
when ZipEntry::STORED then PassThruCompressor.new(@outputStream)
else raise ZipCompressionMethodError,
"Invalid compression method: '#{entry.compression_method}'"
"Invalid compression method: '#{entry.compression_method}'"
end
end
def update_local_headers
pos = @outputStream.tell
@entrySet.each {
|entry|
@outputStream.pos = entry.localHeaderOffset
entry.write_local_entry(@outputStream)
|entry|
@outputStream.pos = entry.localHeaderOffset
entry.write_local_entry(@outputStream)
}
@outputStream.pos = pos
end
View
@@ -224,15 +224,16 @@ def directory?(fileName)
expand_path(fileName) == "/" || (entry != nil && entry.directory?)
end
def open(fileName, openMode = "r", &block)
def open(fileName, openMode = "r", permissionInt = 0644, &block)
#puts "--> open #{fileName}"
openMode.gsub!("b", "") # ignore b option
case openMode
when "r"
@mappedZip.get_input_stream(fileName, &block)
when "w"
@mappedZip.get_output_stream(fileName, &block)
else
raise StandardError, "openmode '#{openMode} not supported" unless openMode == "r"
when "r"
@mappedZip.get_input_stream(fileName, &block)
when "w"
@mappedZip.get_output_stream(fileName, permissionInt, &block)
else
raise StandardError, "openmode '#{openMode} not supported" unless openMode == "r"
end
end
@@ -266,7 +267,10 @@ def chmod (modeInt, *filenames)
filenames.each { |fileName|
e = get_entry(fileName)
e.fstype = 3 # force convertion filesystem type to unix
e.unix_perms = modeInt
e.externalFileAttributes = modeInt << 16
e.dirty = true
#puts "--> chmod [%o] [%o]" % [e.unix_perms, e.externalFileAttributes]
}
filenames.size
end
@@ -555,8 +559,8 @@ def get_input_stream(fileName, &aProc)
@zipFile.get_input_stream(expand_to_entry(fileName), &aProc)
end
def get_output_stream(fileName, &aProc)
@zipFile.get_output_stream(expand_to_entry(fileName), &aProc)
def get_output_stream(fileName, permissionInt = nil, &aProc)
@zipFile.get_output_stream(expand_to_entry(fileName), permissionInt, &aProc)
end
def read(fileName)
View
@@ -8,6 +8,8 @@
require 'test/unit'
require 'fileutils'
require 'digest/sha1'
module ExtraAssertions
def assert_forwarded(anObject, method, retVal, *expectedArgs)
@@ -33,11 +35,13 @@ def #{method}(*args)
class ZipFsFileNonmutatingTest < Test::Unit::TestCase
def setup
@zipsha = Digest::SHA1.file("data/zipWithDirs.zip")
@zipFile = ZipFile.new("data/zipWithDirs.zip")
end
def teardown
@zipFile.close if @zipFile
assert_equal(@zipsha, Digest::SHA1.file("data/zipWithDirs.zip"))
end
def test_umask
@@ -421,14 +425,6 @@ def test_lstat
assert(@zipFile.file.lstat("file1").file?)
end
def test_chmod
assert_raise(Errno::ENOENT, "No such file or directory - noSuchFile") {
@zipFile.file.chmod(0644, "file1", "NoSuchFile")
}
assert_equal(2, @zipFile.file.chmod(0644, "file1", "dir1"))
end
def test_pipe
assert_raise(NotImplementedError) {
@zipFile.file.pipe
@@ -582,7 +578,6 @@ def test_open_write
zf.file.open("test_open_write_entry", "w") {
|f|
blockCalled = true
f.write "This is what I'm writing"
}
assert_equal("This is what I'm writing",
@@ -591,7 +586,6 @@ def test_open_write
# Test with existing entry
zf.file.open("file1", "wb") { #also check that 'b' option is ignored
|f|
blockCalled = true
f.write "This is what I'm writing too"
}
assert_equal("This is what I'm writing too",
@@ -780,8 +774,8 @@ def test_open_new
d.close
zf.dir.open("dir1") {
|d|
assert_equal(["dir11", "file11", "file12"].sort, d.entries.sort)
|dir|
assert_equal(["dir11", "file11", "file12"].sort, dir.entries.sort)
}
}
end

0 comments on commit 4b3fb4f

Please sign in to comment.