ZipFsDir was in the wrong module. ZipFileSystem now has a ctor that creates ZipFsDir and ZipFsFile instances, instead of creating them lazily. It then passes the dir instance to the file instance and vice versa

This commit is contained in:
thomas 2003-08-20 14:10:20 +00:00
parent 2f4123429c
commit a47b107314
1 changed files with 70 additions and 58 deletions

View File

@ -5,16 +5,26 @@ require 'zip'
module Zip module Zip
module ZipFileSystem module ZipFileSystem
def initialize
@zipFsDir = ZipFsDir.new(self)
@zipFsFile = ZipFsFile.new(self)
@zipFsDir.file = @zipFsFile
@zipFsFile.dir = @zipFsDir
end
def dir def dir
@zipFsDir ||= ZipFsDir.new(self) @zipFsDir
end end
def file def file
@zipFsFile ||= ZipFsFile.new(self) @zipFsFile
end end
class ZipFsFile class ZipFsFile
attr_writer :dir
# protected :dir
class ZipFsStat class ZipFsStat
def initialize(zipFsFile, entryName) def initialize(zipFsFile, entryName)
@zipFsFile = zipFsFile @zipFsFile = zipFsFile
@ -78,7 +88,7 @@ module Zip
end end
def exists?(fileName) def exists?(fileName)
expand_path(fileName) == "/" || @zipFile.find_entry(@zipFile.dir.expand_to_entry(fileName)) != nil expand_path(fileName) == "/" || @zipFile.find_entry(@dir.expand_to_entry(fileName)) != nil
end end
alias :exist? :exists? alias :exist? :exists?
@ -113,12 +123,12 @@ module Zip
end end
def directory?(fileName) def directory?(fileName)
entry = @zipFile.find_entry(@zipFile.dir.expand_to_entry(fileName)) entry = @zipFile.find_entry(@dir.expand_to_entry(fileName))
expand_path(fileName) == "/" || (entry != nil && entry.directory?) expand_path(fileName) == "/" || (entry != nil && entry.directory?)
end end
def open(fileName, openMode = "r", &block) def open(fileName, openMode = "r", &block)
entryName = @zipFile.dir.expand_to_entry(fileName) entryName = @dir.expand_to_entry(fileName)
case openMode case openMode
when "r" when "r"
@zipFile.get_input_stream(entryName, &block) @zipFile.get_input_stream(entryName, &block)
@ -285,64 +295,66 @@ module Zip
alias :unlink :delete alias :unlink :delete
def expand_path(aPath) def expand_path(aPath)
@zipFile.dir.expand_path(aPath) @dir.expand_path(aPath)
end end
end end
end
class ZipFsDir class ZipFsDir
def initialize(zipFile) def initialize(zipFile)
@zipFile = zipFile @zipFile = zipFile
@file = @zipFile.file @pwd = "/"
@pwd = "/"
end
attr_reader :pwd
alias getwd pwd
def chdir(aDirectoryName)
unless @file.stat(aDirectoryName).directory?
raise Errno::EINVAL, "Invalid argument - #{aDirectoryName}"
end end
@pwd = expand_path(aDirectoryName)
end
def entries(aDirectoryName) attr_writer :file
unless @file.stat(aDirectoryName).directory? # protected :file
raise Errno::ENOTDIR, aDirectoryName
attr_reader :pwd
alias getwd pwd
def chdir(aDirectoryName)
unless @file.stat(aDirectoryName).directory?
raise Errno::EINVAL, "Invalid argument - #{aDirectoryName}"
end
@pwd = expand_path(aDirectoryName)
end end
path = expand_path(aDirectoryName).lchop.ensure_end("/")
@zipFile.entries.select { def entries(aDirectoryName)
|e| unless @file.stat(aDirectoryName).directory?
parent = e.parent_as_string raise Errno::ENOTDIR, aDirectoryName
parent == path || (path == "/" && parent == nil ) end
}.map { |e| @file.basename(e.to_s.chomp("/")) } path = expand_path(aDirectoryName).lchop.ensure_end("/")
end
def delete(entryName) @zipFile.entries.select {
unless @zipFile.file.stat(entryName).directory? |e|
raise Errno::EINVAL, "Invalid argument - #{entryName}" parent = e.parent_as_string
parent == path || (path == "/" && parent == nil )
}.map { |e| @file.basename(e.to_s.chomp("/")) }
end end
@zipFile.remove(entryName)
end
alias rmdir delete
alias unlink delete
def mkdir(entryName, permissionInt = 0) def delete(entryName)
@zipFile.mkdir(entryName, permissionInt) unless @file.stat(entryName).directory?
end raise Errno::EINVAL, "Invalid argument - #{entryName}"
end
@zipFile.remove(entryName)
end
alias rmdir delete
alias unlink delete
def expand_path(aPath) def mkdir(entryName, permissionInt = 0)
expanded = aPath.starts_with("/") ? aPath : @pwd.ensure_end("/") + aPath @zipFile.mkdir(entryName, permissionInt)
expanded.gsub!(/\/\.(\/|$)/, "") end
expanded.gsub!(/[^\/]+\/\.\.(\/|$)/, "")
expanded.empty? ? "/" : expanded
end
def expand_to_entry(aPath) def expand_path(aPath)
expand_path(aPath).lchop expanded = aPath.starts_with("/") ? aPath : @pwd.ensure_end("/") + aPath
expanded.gsub!(/\/\.(\/|$)/, "")
expanded.gsub!(/[^\/]+\/\.\.(\/|$)/, "")
expanded.empty? ? "/" : expanded
end
def expand_to_entry(aPath)
expand_path(aPath).lchop
end
end end
end end