lib/vfs/storages/local.rb in vfs-0.3.14 vs lib/vfs/storages/local.rb in vfs-0.3.15
- old
+ new
@@ -19,51 +19,58 @@
#
# Attributes
#
def attributes path
+ path = with_root path
+
stat = ::File.stat path
attrs = {}
- attrs[:file] = stat.file?
- attrs[:dir] = stat.directory?
+ attrs[:file] = !!stat.file?
+ attrs[:dir] = !!stat.directory?
# attributes special for file system
attrs[:created_at] = stat.ctime
attrs[:updated_at] = stat.mtime
- attrs[:size] = stat.size if stat.file?
+ attrs[:size] = stat.size if attrs[:file]
attrs
rescue Errno::ENOENT
- {}
+ nil
end
def set_attributes path, attrs
- raise 'not supported'
+ # TODO2 set attributes
+ not_implemented
end
#
# File
#
def read_file path, &block
+ path = with_root path
::File.open path, 'r' do |is|
while buff = is.gets(self.buffer || DEFAULT_BUFFER)
block.call buff
end
end
end
- def write_file path, append, &block
+ def write_file original_path, append, &block
+ path = with_root original_path
+
# TODO2 Performance lost, extra call to check file existence
- raise "can't write, entry #{path} already exist!" if !append and ::File.exist?(path)
+ raise "can't write, entry #{original_path} already exist!" if !append and ::File.exist?(path)
option = append ? 'a' : 'w'
::File.open path, option do |out|
block.call Writer.new(out)
end
end
def delete_file path
+ path = with_root path
::File.delete path
end
# def move_file from, to
# FileUtils.mv from, to
@@ -72,25 +79,31 @@
#
# Dir
#
def create_dir path
+ path = with_root path
::Dir.mkdir path
end
- def delete_dir path
+ def delete_dir original_path
+ path = with_root original_path
+
# TODO2 Performance lost, extra call to check file existence
- raise "can't delete file (#{path})!" if ::File.file?(path)
+ raise "can't delete file (#{original_path})!" if ::File.file?(path)
- FileUtils.rm_r path
+ ::FileUtils.rm_r path
end
def each_entry path, query, &block
+ path = with_root path
+
if query
path_with_trailing_slash = path == '/' ? path : "#{path}/"
::Dir["#{path_with_trailing_slash}#{query}"].each do |absolute_path|
relative_path = absolute_path.sub path_with_trailing_slash, ''
+ # TODO2 Performance loss
if ::File.directory? absolute_path
block.call relative_path, :dir
else
block.call relative_path, :file
end
@@ -126,30 +139,45 @@
# Other
#
def local?; true end
def tmp &block
- tmp_dir = "#{::Dir.tmpdir}/#{rand(10**6)}"
+ path = "/tmp/#{rand(10**6)}"
+ # tmp_dir = "#{::Dir.tmpdir}/#{rand(10**6)}"
if block
begin
- create_dir tmp_dir
- block.call tmp_dir
+ ::FileUtils.mkdir_p with_root(path)
+ block.call path
ensure
- delete_dir tmp_dir
+ ::FileUtils.rm_r with_root(path) if ::File.exist? with_root(path)
end
else
- create_dir tmp_dir
- tmp_dir
+ ::FileUtils.mkdir_p with_root(path)
+ path
end
end
def to_s; '' end
+
+ protected
+ def root
+ @root || raise('root not defined!')
+ end
+
+ def with_root path
+ path == '/' ? root : root + path
+ end
end
include LocalVfsHelper
- def open_fs &block
- block.call self
+ def initialize root = ''
+ @root = root
end
+
+ def open &block
+ block.call self if block
+ end
+ def close; end
end
end
end
\ No newline at end of file