Fix Naming/MethodParameterName cop in the library code.

This commit is contained in:
Robert Haines 2020-02-19 07:28:46 +00:00
parent b09f05d8d3
commit aa6ea05d45
10 changed files with 228 additions and 230 deletions

View File

@ -38,13 +38,6 @@ Naming/AccessorMethodName:
- 'lib/zip/streamable_stream.rb' - 'lib/zip/streamable_stream.rb'
- 'test/file_permissions_test.rb' - 'test/file_permissions_test.rb'
# Offense count: 140
# Configuration parameters: MinNameLength, AllowNamesEndingInNumbers, AllowedNames, ForbiddenNames.
# AllowedNames: io, id, to, by, on, in, at, ip, db, os
Naming/MethodParameterName:
Exclude:
- 'lib/**/*.rb'
# Offense count: 721 # Offense count: 721
# Configuration parameters: EnforcedStyle. # Configuration parameters: EnforcedStyle.
# SupportedStyles: snake_case, camelCase # SupportedStyles: snake_case, camelCase

View File

@ -181,8 +181,8 @@ module Zip
end end
# For iterating over the entries. # For iterating over the entries.
def each(&proc) def each(&a_proc)
@entry_set.each(&proc) @entry_set.each(&a_proc)
end end
# Returns the number of entries in the central directory (and # Returns the number of entries in the central directory (and

View File

@ -24,8 +24,8 @@ module Zip
end end
end end
def update_keys(n) def update_keys(num)
@key0 = ~Zlib.crc32(n, ~@key0) @key0 = ~Zlib.crc32(num, ~@key0)
@key1 = ((@key1 + (@key0 & 0xff)) * 134_775_813 + 1) & 0xffffffff @key1 = ((@key1 + (@key0 & 0xff)) * 134_775_813 + 1) & 0xffffffff
@key2 = ~Zlib.crc32((@key1 >> 24).chr, ~@key2) @key2 = ~Zlib.crc32((@key1 >> 24).chr, ~@key2)
end end
@ -63,10 +63,10 @@ module Zip
private private
def encode(n) def encode(num)
t = decrypt_byte t = decrypt_byte
update_keys(n.chr) update_keys(num.chr)
t ^ n t ^ num
end end
end end
@ -86,10 +86,10 @@ module Zip
private private
def decode(n) def decode(num)
n ^= decrypt_byte num ^= decrypt_byte
update_keys(n.chr) update_keys(num.chr)
n num
end end
end end
end end

View File

@ -34,13 +34,13 @@ module Zip
local(time.year, time.month, time.day, time.hour, time.min, time.sec) local(time.year, time.month, time.day, time.hour, time.min, time.sec)
end end
def self.parse_binary_dos_format(binaryDosDate, binaryDosTime) def self.parse_binary_dos_format(bin_dos_date, bin_dos_time)
second = 2 * (0b11111 & binaryDosTime) second = 2 * (0b11111 & bin_dos_time)
minute = (0b11111100000 & binaryDosTime) >> 5 minute = (0b11111100000 & bin_dos_time) >> 5
hour = (0b1111100000000000 & binaryDosTime) >> 11 hour = (0b1111100000000000 & bin_dos_time) >> 11
day = (0b11111 & binaryDosDate) day = (0b11111 & bin_dos_date)
month = (0b111100000 & binaryDosDate) >> 5 month = (0b111100000 & bin_dos_date) >> 5
year = ((0b1111111000000000 & binaryDosDate) >> 9) + 1980 year = ((0b1111111000000000 & bin_dos_date) >> 9) + 1980
begin begin
local(year, month, day, hour, minute, second) local(year, month, day, hour, minute, second)
end end

View File

@ -6,23 +6,23 @@ module Zip
merge(binstr) if binstr merge(binstr) if binstr
end end
def extra_field_type_exist(binstr, id, len, i) def extra_field_type_exist(binstr, id, len, index)
field_name = ID_MAP[id].name field_name = ID_MAP[id].name
if member?(field_name) if member?(field_name)
self[field_name].merge(binstr[i, len + 4]) self[field_name].merge(binstr[index, len + 4])
else else
field_obj = ID_MAP[id].new(binstr[i, len + 4]) field_obj = ID_MAP[id].new(binstr[index, len + 4])
self[field_name] = field_obj self[field_name] = field_obj
end end
end end
def extra_field_type_unknown(binstr, len, i) def extra_field_type_unknown(binstr, len, index)
create_unknown_item unless self['Unknown'] create_unknown_item unless self['Unknown']
if !len || len + 4 > binstr[i..-1].bytesize if !len || len + 4 > binstr[index..-1].bytesize
self['Unknown'] << binstr[i..-1] self['Unknown'] << binstr[index..-1]
return return
end end
self['Unknown'] << binstr[i, len + 4] self['Unknown'] << binstr[index, len + 4]
end end
def create_unknown_item def create_unknown_item

View File

@ -168,8 +168,8 @@ module Zip
# whereas ZipInputStream jumps through the entire archive accessing the # whereas ZipInputStream jumps through the entire archive accessing the
# local entry headers (which contain the same information as the # local entry headers (which contain the same information as the
# central directory). # central directory).
def foreach(aZipFileName, &block) def foreach(zip_file_name, &block)
::Zip::File.open(aZipFileName) do |zip_file| ::Zip::File.open(zip_file_name) do |zip_file|
zip_file.each(&block) zip_file.each(&block)
end end
end end
@ -255,8 +255,8 @@ module Zip
# Returns an input stream to the specified entry. If a block is passed # Returns an input stream to the specified entry. If a block is passed
# the stream object is passed to the block and the stream is automatically # the stream object is passed to the block and the stream is automatically
# closed afterwards just as with ruby's builtin File.open method. # closed afterwards just as with ruby's builtin File.open method.
def get_input_stream(entry, &aProc) def get_input_stream(entry, &a_proc)
get_entry(entry).get_input_stream(&aProc) get_entry(entry).get_input_stream(&a_proc)
end end
# Returns an output stream to the specified entry. If entry is not an instance # Returns an output stream to the specified entry. If entry is not an instance
@ -267,7 +267,7 @@ module Zip
def get_output_stream(entry, permission_int = nil, comment = nil, def get_output_stream(entry, permission_int = nil, comment = nil,
extra = nil, compressed_size = nil, crc = nil, extra = nil, compressed_size = nil, crc = nil,
compression_method = nil, size = nil, time = nil, compression_method = nil, size = nil, time = nil,
&aProc) &a_proc)
new_entry = new_entry =
if entry.kind_of?(Entry) if entry.kind_of?(Entry)
@ -282,7 +282,7 @@ module Zip
new_entry.unix_perms = permission_int new_entry.unix_perms = permission_int
zip_streamable_entry = StreamableStream.new(new_entry) zip_streamable_entry = StreamableStream.new(new_entry)
@entry_set << zip_streamable_entry @entry_set << zip_streamable_entry
zip_streamable_entry.get_output_stream(&aProc) zip_streamable_entry.get_output_stream(&a_proc)
end end
# Returns the name of the zip archive # Returns the name of the zip archive
@ -326,12 +326,12 @@ module Zip
@entry_set << foundEntry @entry_set << foundEntry
end end
# Replaces the specified entry with the contents of srcPath (from # Replaces the specified entry with the contents of src_path (from
# the file system). # the file system).
def replace(entry, srcPath) def replace(entry, src_path)
check_file(srcPath) check_file(src_path)
remove(entry) remove(entry)
add(entry, srcPath) add(entry, src_path)
end end
# Extracts entry to file dest_path. # Extracts entry to file dest_path.
@ -409,37 +409,37 @@ module Zip
end end
# Creates a directory # Creates a directory
def mkdir(entryName, permissionInt = 0o755) def mkdir(entry_name, permission = 0o755)
raise Errno::EEXIST, "File exists - #{entryName}" if find_entry(entryName) raise Errno::EEXIST, "File exists - #{entry_name}" if find_entry(entry_name)
entryName = entryName.dup.to_s entry_name = entry_name.dup.to_s
entryName << '/' unless entryName.end_with?('/') entry_name << '/' unless entry_name.end_with?('/')
@entry_set << ::Zip::StreamableDirectory.new(@name, entryName, nil, permissionInt) @entry_set << ::Zip::StreamableDirectory.new(@name, entry_name, nil, permission)
end end
private private
def directory?(newEntry, srcPath) def directory?(new_entry, src_path)
srcPathIsDirectory = ::File.directory?(srcPath) srcPathIsDirectory = ::File.directory?(src_path)
if newEntry.directory? && !srcPathIsDirectory if new_entry.directory? && !srcPathIsDirectory
raise ArgumentError, raise ArgumentError,
"entry name '#{newEntry}' indicates directory entry, but " \ "entry name '#{new_entry}' indicates directory entry, but " \
"'#{srcPath}' is not a directory" "'#{src_path}' is not a directory"
elsif !newEntry.directory? && srcPathIsDirectory elsif !new_entry.directory? && srcPathIsDirectory
newEntry.name += '/' new_entry.name += '/'
end end
newEntry.directory? && srcPathIsDirectory new_entry.directory? && srcPathIsDirectory
end end
def check_entry_exists(entryName, continue_on_exists_proc, procedureName) def check_entry_exists(entry_name, continue_on_exists_proc, proc_name)
continue_on_exists_proc ||= proc { Zip.continue_on_exists_proc } continue_on_exists_proc ||= proc { Zip.continue_on_exists_proc }
return unless @entry_set.include?(entryName) return unless @entry_set.include?(entry_name)
if continue_on_exists_proc.call if continue_on_exists_proc.call
remove get_entry(entryName) remove get_entry(entry_name)
else else
raise ::Zip::EntryExistsError, raise ::Zip::EntryExistsError,
procedureName + " failed. Entry #{entryName} already exists" proc_name + " failed. Entry #{entry_name} already exists"
end end
end end

View File

@ -35,25 +35,25 @@ module Zip
module FileSystem module FileSystem
def initialize # :nodoc: def initialize # :nodoc:
mappedZip = ZipFileNameMapper.new(self) mapped_zip = ZipFileNameMapper.new(self)
@zipFsDir = ZipFsDir.new(mappedZip) @zip_fs_dir = ZipFsDir.new(mapped_zip)
@zipFsFile = ZipFsFile.new(mappedZip) @zip_fs_file = ZipFsFile.new(mapped_zip)
@zipFsDir.file = @zipFsFile @zip_fs_dir.file = @zip_fs_file
@zipFsFile.dir = @zipFsDir @zip_fs_file.dir = @zip_fs_dir
end end
# Returns a ZipFsDir which is much like ruby's builtin Dir (class) # Returns a ZipFsDir which is much like ruby's builtin Dir (class)
# object, except it works on the Zip::File on which this method is # object, except it works on the Zip::File on which this method is
# invoked # invoked
def dir def dir
@zipFsDir @zip_fs_dir
end end
# Returns a ZipFsFile which is much like ruby's builtin File (class) # Returns a ZipFsFile which is much like ruby's builtin File (class)
# object, except it works on the Zip::File on which this method is # object, except it works on the Zip::File on which this method is
# invoked # invoked
def file def file
@zipFsFile @zip_fs_file
end end
# Instances of this class are normally accessed via the accessor # Instances of this class are normally accessed via the accessor
@ -72,20 +72,20 @@ module Zip
methods.each do |method| methods.each do |method|
class_eval <<-END_EVAL, __FILE__, __LINE__ + 1 class_eval <<-END_EVAL, __FILE__, __LINE__ + 1
def #{method} # def file? def #{method} # def file?
@zipFsFile.#{method}(@entryName) # @zipFsFile.file?(@entryName) @zip_fs_file.#{method}(@entry_name) # @zip_fs_file.file?(@entry_name)
end # end end # end
END_EVAL END_EVAL
end end
end end
end end
def initialize(zipFsFile, entryName) def initialize(zip_fs_file, entry_name)
@zipFsFile = zipFsFile @zip_fs_file = zip_fs_file
@entryName = entryName @entry_name = entry_name
end end
def kind_of?(t) def kind_of?(type)
super || t == ::File::Stat super || type == ::File::Stat
end end
delegate_to_fs_file :file?, :directory?, :pipe?, :chardev?, :symlink?, delegate_to_fs_file :file?, :directory?, :pipe?, :chardev?, :symlink?,
@ -98,7 +98,7 @@ module Zip
end end
def get_entry def get_entry
@zipFsFile.__send__(:get_entry, @entryName) @zip_fs_file.__send__(:get_entry, @entry_name)
end end
private :get_entry private :get_entry
@ -168,29 +168,29 @@ module Zip
end end
end end
def initialize(mappedZip) def initialize(mapped_zip)
@mappedZip = mappedZip @mapped_zip = mapped_zip
end end
def get_entry(fileName) def get_entry(filename)
unless exists?(fileName) unless exists?(filename)
raise Errno::ENOENT, "No such file or directory - #{fileName}" raise Errno::ENOENT, "No such file or directory - #{filename}"
end end
@mappedZip.find_entry(fileName) @mapped_zip.find_entry(filename)
end end
private :get_entry private :get_entry
def unix_mode_cmp(fileName, mode) def unix_mode_cmp(filename, mode)
e = get_entry(fileName) e = get_entry(filename)
e.fstype == 3 && ((e.external_file_attributes >> 16) & mode) != 0 e.fstype == 3 && ((e.external_file_attributes >> 16) & mode) != 0
rescue Errno::ENOENT rescue Errno::ENOENT
false false
end end
private :unix_mode_cmp private :unix_mode_cmp
def exists?(fileName) def exists?(filename)
expand_path(fileName) == '/' || !@mappedZip.find_entry(fileName).nil? expand_path(filename) == '/' || !@mapped_zip.find_entry(filename).nil?
end end
alias exist? exists? alias exist? exists?
@ -198,133 +198,133 @@ module Zip
alias owned? exists? alias owned? exists?
alias grpowned? exists? alias grpowned? exists?
def readable?(fileName) def readable?(filename)
unix_mode_cmp(fileName, 0o444) unix_mode_cmp(filename, 0o444)
end end
alias readable_real? readable? alias readable_real? readable?
def writable?(fileName) def writable?(filename)
unix_mode_cmp(fileName, 0o222) unix_mode_cmp(filename, 0o222)
end end
alias writable_real? writable? alias writable_real? writable?
def executable?(fileName) def executable?(filename)
unix_mode_cmp(fileName, 0o111) unix_mode_cmp(filename, 0o111)
end end
alias executable_real? executable? alias executable_real? executable?
def setuid?(fileName) def setuid?(filename)
unix_mode_cmp(fileName, 0o4000) unix_mode_cmp(filename, 0o4000)
end end
def setgid?(fileName) def setgid?(filename)
unix_mode_cmp(fileName, 0o2000) unix_mode_cmp(filename, 0o2000)
end end
def sticky?(fileName) def sticky?(filename)
unix_mode_cmp(fileName, 0o1000) unix_mode_cmp(filename, 0o1000)
end end
def umask(*args) def umask(*args)
::File.umask(*args) ::File.umask(*args)
end end
def truncate(_fileName, _len) def truncate(_filename, _len)
raise StandardError, 'truncate not supported' raise StandardError, 'truncate not supported'
end end
def directory?(fileName) def directory?(filename)
entry = @mappedZip.find_entry(fileName) entry = @mapped_zip.find_entry(filename)
expand_path(fileName) == '/' || (!entry.nil? && entry.directory?) expand_path(filename) == '/' || (!entry.nil? && entry.directory?)
end end
def open(fileName, openMode = 'r', permissionInt = 0o644, &block) def open(filename, mode = 'r', permissions = 0o644, &block)
openMode.delete!('b') # ignore b option mode.delete!('b') # ignore b option
case openMode case mode
when 'r' when 'r'
@mappedZip.get_input_stream(fileName, &block) @mapped_zip.get_input_stream(filename, &block)
when 'w' when 'w'
@mappedZip.get_output_stream(fileName, permissionInt, &block) @mapped_zip.get_output_stream(filename, permissions, &block)
else else
raise StandardError, "openmode '#{openMode} not supported" unless openMode == 'r' raise StandardError, "openmode '#{mode} not supported" unless mode == 'r'
end end
end end
def new(fileName, openMode = 'r') def new(filename, mode = 'r')
self.open(fileName, openMode) self.open(filename, mode)
end end
def size(fileName) def size(filename)
@mappedZip.get_entry(fileName).size @mapped_zip.get_entry(filename).size
end end
# Returns nil for not found and nil for directories # Returns nil for not found and nil for directories
def size?(fileName) def size?(filename)
entry = @mappedZip.find_entry(fileName) entry = @mapped_zip.find_entry(filename)
entry.nil? || entry.directory? ? nil : entry.size entry.nil? || entry.directory? ? nil : entry.size
end end
def chown(ownerInt, groupInt, *filenames) def chown(owner, group, *filenames)
filenames.each do |filename| filenames.each do |filename|
e = get_entry(filename) e = get_entry(filename)
e.extra.create('IUnix') unless e.extra.member?('IUnix') e.extra.create('IUnix') unless e.extra.member?('IUnix')
e.extra['IUnix'].uid = ownerInt e.extra['IUnix'].uid = owner
e.extra['IUnix'].gid = groupInt e.extra['IUnix'].gid = group
end end
filenames.size filenames.size
end end
def chmod(modeInt, *filenames) def chmod(mode, *filenames)
filenames.each do |filename| filenames.each do |filename|
e = get_entry(filename) e = get_entry(filename)
e.fstype = 3 # force convertion filesystem type to unix e.fstype = 3 # force convertion filesystem type to unix
e.unix_perms = modeInt e.unix_perms = mode
e.external_file_attributes = modeInt << 16 e.external_file_attributes = mode << 16
e.dirty = true e.dirty = true
end end
filenames.size filenames.size
end end
def zero?(fileName) def zero?(filename)
sz = size(fileName) sz = size(filename)
sz.nil? || sz == 0 sz.nil? || sz == 0
rescue Errno::ENOENT rescue Errno::ENOENT
false false
end end
def file?(fileName) def file?(filename)
entry = @mappedZip.find_entry(fileName) entry = @mapped_zip.find_entry(filename)
!entry.nil? && entry.file? !entry.nil? && entry.file?
end end
def dirname(fileName) def dirname(filename)
::File.dirname(fileName) ::File.dirname(filename)
end end
def basename(fileName) def basename(filename)
::File.basename(fileName) ::File.basename(filename)
end end
def split(fileName) def split(filename)
::File.split(fileName) ::File.split(filename)
end end
def join(*fragments) def join(*fragments)
::File.join(*fragments) ::File.join(*fragments)
end end
def utime(modifiedTime, *fileNames) def utime(modified_time, *filenames)
fileNames.each do |filename| filenames.each do |filename|
get_entry(filename).time = modifiedTime get_entry(filename).time = modified_time
end end
end end
def mtime(fileName) def mtime(filename)
@mappedZip.get_entry(fileName).mtime @mapped_zip.get_entry(filename).mtime
end end
def atime(fileName) def atime(filename)
e = get_entry(fileName) e = get_entry(filename)
if e.extra.member? 'UniversalTime' if e.extra.member? 'UniversalTime'
e.extra['UniversalTime'].atime e.extra['UniversalTime'].atime
elsif e.extra.member? 'NTFS' elsif e.extra.member? 'NTFS'
@ -332,8 +332,8 @@ module Zip
end end
end end
def ctime(fileName) def ctime(filename)
e = get_entry(fileName) e = get_entry(filename)
if e.extra.member? 'UniversalTime' if e.extra.member? 'UniversalTime'
e.extra['UniversalTime'].ctime e.extra['UniversalTime'].ctime
elsif e.extra.member? 'NTFS' elsif e.extra.member? 'NTFS'
@ -353,27 +353,27 @@ module Zip
false false
end end
def symlink?(_fileName) def symlink?(_filename)
false false
end end
def socket?(_fileName) def socket?(_filename)
false false
end end
def ftype(fileName) def ftype(filename)
@mappedZip.get_entry(fileName).directory? ? 'directory' : 'file' @mapped_zip.get_entry(filename).directory? ? 'directory' : 'file'
end end
def readlink(_fileName) def readlink(_filename)
raise NotImplementedError, 'The readlink() function is not implemented' raise NotImplementedError, 'The readlink() function is not implemented'
end end
def symlink(_fileName, _symlinkName) def symlink(_filename, _symlink_name)
raise NotImplementedError, 'The symlink() function is not implemented' raise NotImplementedError, 'The symlink() function is not implemented'
end end
def link(_fileName, _symlinkName) def link(_filename, _symlink_name)
raise NotImplementedError, 'The link() function is not implemented' raise NotImplementedError, 'The link() function is not implemented'
end end
@ -381,28 +381,28 @@ module Zip
raise NotImplementedError, 'The pipe() function is not implemented' raise NotImplementedError, 'The pipe() function is not implemented'
end end
def stat(fileName) def stat(filename)
raise Errno::ENOENT, fileName unless exists?(fileName) raise Errno::ENOENT, filename unless exists?(filename)
ZipFsStat.new(self, fileName) ZipFsStat.new(self, filename)
end end
alias lstat stat alias lstat stat
def readlines(fileName) def readlines(filename)
self.open(fileName, &:readlines) self.open(filename, &:readlines)
end end
def read(fileName) def read(filename)
@mappedZip.read(fileName) @mapped_zip.read(filename)
end end
def popen(*args, &aProc) def popen(*args, &a_proc)
::File.popen(*args, &aProc) ::File.popen(*args, &a_proc)
end end
def foreach(fileName, aSep = $INPUT_RECORD_SEPARATOR, &aProc) def foreach(filename, sep = $INPUT_RECORD_SEPARATOR, &a_proc)
self.open(fileName) { |is| is.each_line(aSep, &aProc) } self.open(filename) { |is| is.each_line(sep, &a_proc) }
end end
def delete(*args) def delete(*args)
@ -411,18 +411,18 @@ module Zip
raise Errno::EISDIR, "Is a directory - \"#{filename}\"" raise Errno::EISDIR, "Is a directory - \"#{filename}\""
end end
@mappedZip.remove(filename) @mapped_zip.remove(filename)
end end
end end
def rename(fileToRename, newName) def rename(file_to_rename, new_name)
@mappedZip.rename(fileToRename, newName) { true } @mapped_zip.rename(file_to_rename, new_name) { true }
end end
alias unlink delete alias unlink delete
def expand_path(aPath) def expand_path(path)
@mappedZip.expand_path(aPath) @mapped_zip.expand_path(path)
end end
end end
@ -433,18 +433,18 @@ module Zip
# The individual methods are not documented due to their # The individual methods are not documented due to their
# similarity with the methods in Dir # similarity with the methods in Dir
class ZipFsDir class ZipFsDir
def initialize(mappedZip) def initialize(mapped_zip)
@mappedZip = mappedZip @mapped_zip = mapped_zip
end end
attr_writer :file attr_writer :file
def new(aDirectoryName) def new(directory_name)
ZipFsDirIterator.new(entries(aDirectoryName)) ZipFsDirIterator.new(entries(directory_name))
end end
def open(aDirectoryName) def open(directory_name)
dirIt = new(aDirectoryName) dirIt = new(directory_name)
if block_given? if block_given?
begin begin
yield(dirIt) yield(dirIt)
@ -457,55 +457,55 @@ module Zip
end end
def pwd def pwd
@mappedZip.pwd @mapped_zip.pwd
end end
alias getwd pwd alias getwd pwd
def chdir(aDirectoryName) def chdir(directory_name)
unless @file.stat(aDirectoryName).directory? unless @file.stat(directory_name).directory?
raise Errno::EINVAL, "Invalid argument - #{aDirectoryName}" raise Errno::EINVAL, "Invalid argument - #{directory_name}"
end end
@mappedZip.pwd = @file.expand_path(aDirectoryName) @mapped_zip.pwd = @file.expand_path(directory_name)
end end
def entries(aDirectoryName) def entries(directory_name)
entries = [] entries = []
foreach(aDirectoryName) { |e| entries << e } foreach(directory_name) { |e| entries << e }
entries entries
end end
def glob(*args, &block) def glob(*args, &block)
@mappedZip.glob(*args, &block) @mapped_zip.glob(*args, &block)
end end
def foreach(aDirectoryName) def foreach(directory_name)
unless @file.stat(aDirectoryName).directory? unless @file.stat(directory_name).directory?
raise Errno::ENOTDIR, aDirectoryName raise Errno::ENOTDIR, directory_name
end end
path = @file.expand_path(aDirectoryName) path = @file.expand_path(directory_name)
path << '/' unless path.end_with?('/') path << '/' unless path.end_with?('/')
path = Regexp.escape(path) path = Regexp.escape(path)
subDirEntriesRegex = Regexp.new("^#{path}([^/]+)$") subDirEntriesRegex = Regexp.new("^#{path}([^/]+)$")
@mappedZip.each do |filename| @mapped_zip.each do |filename|
match = subDirEntriesRegex.match(filename) match = subDirEntriesRegex.match(filename)
yield(match[1]) unless match.nil? yield(match[1]) unless match.nil?
end end
end end
def delete(entryName) def delete(entry_name)
unless @file.stat(entryName).directory? unless @file.stat(entry_name).directory?
raise Errno::EINVAL, "Invalid argument - #{entryName}" raise Errno::EINVAL, "Invalid argument - #{entry_name}"
end end
@mappedZip.remove(entryName) @mapped_zip.remove(entry_name)
end end
alias rmdir delete alias rmdir delete
alias unlink delete alias unlink delete
def mkdir(entryName, permissionInt = 0o755) def mkdir(entry_name, permissions = 0o755)
@mappedZip.mkdir(entryName, permissionInt) @mapped_zip.mkdir(entry_name, permissions)
end end
def chroot(*_args) def chroot(*_args)
@ -516,41 +516,41 @@ module Zip
class ZipFsDirIterator # :nodoc:all class ZipFsDirIterator # :nodoc:all
include Enumerable include Enumerable
def initialize(arrayOfFileNames) def initialize(filenames)
@fileNames = arrayOfFileNames @filenames = filenames
@index = 0 @index = 0
end end
def close def close
@fileNames = nil @filenames = nil
end end
def each(&aProc) def each(&a_proc)
raise IOError, 'closed directory' if @fileNames.nil? raise IOError, 'closed directory' if @filenames.nil?
@fileNames.each(&aProc) @filenames.each(&a_proc)
end end
def read def read
raise IOError, 'closed directory' if @fileNames.nil? raise IOError, 'closed directory' if @filenames.nil?
@fileNames[(@index += 1) - 1] @filenames[(@index += 1) - 1]
end end
def rewind def rewind
raise IOError, 'closed directory' if @fileNames.nil? raise IOError, 'closed directory' if @filenames.nil?
@index = 0 @index = 0
end end
def seek(anIntegerPosition) def seek(position)
raise IOError, 'closed directory' if @fileNames.nil? raise IOError, 'closed directory' if @filenames.nil?
@index = anIntegerPosition @index = position
end end
def tell def tell
raise IOError, 'closed directory' if @fileNames.nil? raise IOError, 'closed directory' if @filenames.nil?
@index @index
end end
@ -561,60 +561,65 @@ module Zip
class ZipFileNameMapper # :nodoc:all class ZipFileNameMapper # :nodoc:all
include Enumerable include Enumerable
def initialize(zipFile) def initialize(zip_file)
@zipFile = zipFile @zip_file = zip_file
@pwd = '/' @pwd = '/'
end end
attr_accessor :pwd attr_accessor :pwd
def find_entry(fileName) def find_entry(filename)
@zipFile.find_entry(expand_to_entry(fileName)) @zip_file.find_entry(expand_to_entry(filename))
end end
def get_entry(fileName) def get_entry(filename)
@zipFile.get_entry(expand_to_entry(fileName)) @zip_file.get_entry(expand_to_entry(filename))
end end
def get_input_stream(fileName, &aProc) def get_input_stream(filename, &a_proc)
@zipFile.get_input_stream(expand_to_entry(fileName), &aProc) @zip_file.get_input_stream(expand_to_entry(filename), &a_proc)
end end
def get_output_stream(fileName, permissionInt = nil, &aProc) def get_output_stream(filename, permissions = nil, &a_proc)
@zipFile.get_output_stream(expand_to_entry(fileName), permissionInt, &aProc) @zip_file.get_output_stream(
expand_to_entry(filename), permissions, &a_proc
)
end end
def glob(pattern, *flags, &block) def glob(pattern, *flags, &block)
@zipFile.glob(expand_to_entry(pattern), *flags, &block) @zip_file.glob(expand_to_entry(pattern), *flags, &block)
end end
def read(fileName) def read(filename)
@zipFile.read(expand_to_entry(fileName)) @zip_file.read(expand_to_entry(filename))
end end
def remove(fileName) def remove(filename)
@zipFile.remove(expand_to_entry(fileName)) @zip_file.remove(expand_to_entry(filename))
end end
def rename(fileName, newName, &continueOnExistsProc) def rename(filename, new_name, &continue_on_exists_proc)
@zipFile.rename(expand_to_entry(fileName), expand_to_entry(newName), @zip_file.rename(
&continueOnExistsProc) expand_to_entry(filename),
expand_to_entry(new_name),
&continue_on_exists_proc
)
end end
def mkdir(fileName, permissionInt = 0o755) def mkdir(filename, permissions = 0o755)
@zipFile.mkdir(expand_to_entry(fileName), permissionInt) @zip_file.mkdir(expand_to_entry(filename), permissions)
end end
# Turns entries into strings and adds leading / # Turns entries into strings and adds leading /
# and removes trailing slash on directories # and removes trailing slash on directories
def each def each
@zipFile.each do |e| @zip_file.each do |e|
yield('/' + e.to_s.chomp('/')) yield('/' + e.to_s.chomp('/'))
end end
end end
def expand_path(aPath) def expand_path(path)
expanded = aPath.start_with?('/') ? aPath : ::File.join(@pwd, aPath) expanded = path.start_with?('/') ? path : ::File.join(@pwd, path)
expanded.gsub!(/\/\.(\/|$)/, '') expanded.gsub!(/\/\.(\/|$)/, '')
expanded.gsub!(/[^\/]+\/\.\.(\/|$)/, '') expanded.gsub!(/[^\/]+\/\.\.(\/|$)/, '')
expanded.empty? ? '/' : expanded expanded.empty? ? '/' : expanded
@ -622,8 +627,8 @@ module Zip
private private
def expand_to_entry(aPath) def expand_to_entry(path)
expand_path(aPath)[1..-1] expand_path(path)[1..-1]
end end
end end
end end

View File

@ -1,8 +1,8 @@
module Zip module Zip
class PassThruCompressor < Compressor #:nodoc:all class PassThruCompressor < Compressor #:nodoc:all
def initialize(outputStream) def initialize(output_stream)
super() super()
@output_stream = outputStream @output_stream = output_stream
@crc = Zlib.crc32 @crc = Zlib.crc32
@size = 0 @size = 0
end end

View File

@ -1,11 +1,11 @@
module Zip module Zip
class StreamableDirectory < Entry class StreamableDirectory < Entry
def initialize(zipfile, entry, srcPath = nil, permissionInt = nil) def initialize(zipfile, entry, src_path = nil, permission = nil)
super(zipfile, entry) super(zipfile, entry)
@ftype = :directory @ftype = :directory
entry.get_extra_attributes_from_path(srcPath) if srcPath entry.get_extra_attributes_from_path(src_path) if src_path
@unix_perms = permissionInt if permissionInt @unix_perms = permission if permission
end end
end end
end end

View File

@ -36,9 +36,9 @@ module Zip
end end
end end
def write_to_zip_output_stream(aZipOutputStream) def write_to_zip_output_stream(output_stream)
aZipOutputStream.put_next_entry(self) output_stream.put_next_entry(self)
get_input_stream { |is| ::Zip::IOExtras.copy_stream(aZipOutputStream, is) } get_input_stream { |is| ::Zip::IOExtras.copy_stream(output_stream, is) }
end end
def clean_up def clean_up