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
18
19
20
21
# 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?
  
  # attributes special for file system
  attrs[:updated_at] = stat.mtime
  
  attrs                  
rescue Net::SFTP::StatusException
  {}
end

#create_dir(path) ⇒ Object

Dir



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

def create_dir path
  sftp.mkdir! path
end

#delete_dir(path) ⇒ Object



82
83
84
# File 'lib/vos/drivers/ssh_vfs_storage.rb', line 82

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

#delete_file(remote_file_path) ⇒ Object



66
67
68
# File 'lib/vos/drivers/ssh_vfs_storage.rb', line 66

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

#each_entry(path, &block) ⇒ Object



86
87
88
89
90
91
92
93
94
95
# File 'lib/vos/drivers/ssh_vfs_storage.rb', line 86

def each_entry 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, override) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/vos/drivers/ssh_vfs_storage.rb', line 97

def efficient_dir_copy from, to, override
  return false if override # sftp doesn't support this behaviour
  
  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(from.path), to.path, recursive: true
        true
      else
        false
      end
    end
  end
end

#local?Boolean

Returns:

  • (Boolean)


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

def local?; false end

#read_file(path, &block) ⇒ Object

File



30
31
32
33
34
35
36
# File 'lib/vos/drivers/ssh_vfs_storage.rb', line 30

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



23
24
25
# File 'lib/vos/drivers/ssh_vfs_storage.rb', line 23

def set_attributes path, attrs      
  raise 'not supported'
end

#tmp(&block) ⇒ Object

Special



123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/vos/drivers/ssh_vfs_storage.rb', line 123

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



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

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