lib/vos/drivers/ssh_vfs_storage.rb in vos-0.3.13 vs lib/vos/drivers/ssh_vfs_storage.rb in vos-0.3.14

- old
+ new

@@ -13,69 +13,81 @@ # # Attributes # def attributes path + path = with_root path + path = fix_path path - stat = sftp.stat! fix_path(path) + stat = sftp.stat! path attrs = {} attrs[:file] = stat.file? attrs[:dir] = stat.directory? # stat.symlink? # attributes special for file system - attrs[:updated_at] = stat.mtime + time = stat.mtime + attrs[:updated_at] = time && Time.at(time) + attrs[:size] = stat.size if attrs[:file] attrs rescue Net::SFTP::StatusException - {} + nil end def set_attributes path, attrs + path = with_root path + path = fix_path path + raise 'not supported' end # # File # def read_file path, &block - sftp.file.open fix_path(path), 'r' do |is| + path = with_root path + path = fix_path path + + sftp.file.open path, 'r' do |is| while buff = is.gets block.call buff end end end def write_file path, append, &block # there's no support for :append in Net::SFTP, so we just mimic it if append attrs = attributes(path) - data = if attrs - if attrs[:file] + data = if attrs[:file] os = "" read_file(path){|buff| os << buff} delete_file path os - else - raise "can't append to dir!" - end + elsif attrs[:dir] + raise "can't append to dir!" else '' end write_file path, false do |writer| writer.write data block.call writer end else - sftp.file.open fix_path(path), 'w' do |out| + path = with_root path + path = fix_path path + sftp.file.open path, 'w' do |out| block.call Writer.new(out) end end end - def delete_file remote_file_path - sftp.remove! fix_path(remote_file_path) + def delete_file path + path = with_root path + path = fix_path path + sftp.remove! path end # def move_file path # raise 'not supported' # end @@ -83,18 +95,25 @@ # # Dir # def create_dir path + path = with_root path + path = fix_path path sftp.mkdir! path end def delete_dir path + path = with_root path + path = fix_path path exec "rm -r #{path}" end def each_entry path, query, &block + path = with_root path + path = fix_path path + raise "SshVfsStorage not support :each_entry with query!" if query sftp.dir.foreach path do |stat| next if stat.name == '.' or stat.name == '..' if stat.directory? @@ -125,11 +144,11 @@ # # Special # def tmp &block - tmp_dir = "/tmp/vfs_#{rand(10**3)}" + tmp_dir = "/tmp_#{rand(10**6)}" if block begin create_dir tmp_dir block.call tmp_dir ensure @@ -140,8 +159,27 @@ tmp_dir end end def local?; false end + + def _delete_root_dir + raise 'refuse to delete / directory!' if root == '/' + exec "rm -r #{@root}" unless root.empty? + end + + def _create_root_dir + raise 'refuse to create / directory!' if root == '/' + sftp.mkdir! root unless root.empty? + end + + protected + def root + @root || raise('root not defined!') + end + + def with_root path + path == '/' ? root : root + path + end end end end \ No newline at end of file