Module: Vos::Drivers::SshVfsStorage

Included in:
Ssh
Defined in:
lib/vos/drivers/ssh_vfs_storage.rb

Instance Method Summary collapse

Instance Method Details

#attributes(path) ⇒ Object

Attributes



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/vos/drivers/ssh_vfs_storage.rb', line 7

def attributes path

  stat = sftp.stat! fix_path(path)
  attrs = {}
  attrs[:file] = stat.file?
  attrs[:dir] = stat.directory?
  # stat.symlink?
  attrs                  
rescue Net::SFTP::StatusException
  {}
end

#create_dir(path) ⇒ Object

Dir



74
75
76
# File 'lib/vos/drivers/ssh_vfs_storage.rb', line 74

def create_dir path
  sftp.mkdir! path
end

#delete_dir(path) ⇒ Object



78
79
80
# File 'lib/vos/drivers/ssh_vfs_storage.rb', line 78

def delete_dir path
  exec "rm -r #{path}"
end

#delete_file(remote_file_path) ⇒ Object



62
63
64
# File 'lib/vos/drivers/ssh_vfs_storage.rb', line 62

def delete_file remote_file_path
  sftp.remove! fix_path(remote_file_path)
end

#each(path, &block) ⇒ Object



82
83
84
85
86
87
88
89
90
91
# File 'lib/vos/drivers/ssh_vfs_storage.rb', line 82

def each path, &block
  sftp.dir.foreach path do |stat|
    next if stat.name == '.' or stat.name == '..'
    if stat.directory?
      block.call stat.name, :dir
    else
      block.call stat.name, :file
    end
  end
end

#efficient_dir_copy(from, to) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/vos/drivers/ssh_vfs_storage.rb', line 93

def efficient_dir_copy from, to
  from.storage.open_fs do |from_fs|          
    to.storage.open_fs do |to_fs|
      if from_fs.local? 
        sftp.upload! from.path, fix_path(to.path)
        true
      elsif to_fs.local?
        sftp.download! fix_path(to.path), from.path, :recursive => true
        true
      else
        false
      end
    end
  end
end

#local?Boolean

Returns:

  • (Boolean)


132
# File 'lib/vos/drivers/ssh_vfs_storage.rb', line 132

def local?; false end

#read_file(path, &block) ⇒ Object

File



26
27
28
29
30
31
32
# File 'lib/vos/drivers/ssh_vfs_storage.rb', line 26

def read_file path, &block
  sftp.file.open fix_path(path), 'r' do |is|
    while buff = is.gets
      block.call buff
    end
  end
end

#set_attributes(path, attrs) ⇒ Object



19
20
21
# File 'lib/vos/drivers/ssh_vfs_storage.rb', line 19

def set_attributes path, attrs      
  raise 'not supported'
end

#tmp(&block) ⇒ Object

Special



117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/vos/drivers/ssh_vfs_storage.rb', line 117

def tmp &block
  tmp_dir = "/tmp/vfs_#{rand(10**3)}"        
  if block
    begin
      create_dir tmp_dir
      block.call tmp_dir
    ensure
      delete_dir tmp_dir
    end
  else
    create_dir tmp_dir
    tmp_dir
  end
end

#write_file(path, append, &block) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/vos/drivers/ssh_vfs_storage.rb', line 34

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]
        os = ""
        read_file(path){|buff| os << buff}
        delete_file path                
        os
      else
        raise "can't append to dir!"
      end
    else
      ''
    end
    write_file path, false do |writer|
      writer.call data
      block.call writer
    end
  else
    sftp.file.open fix_path(path), 'w' do |os|
      writer = -> buff {os.write buff}
      block.call writer
    end
  end          
end