Module: Vos::Drivers::SshVfsStorage

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

Defined Under Namespace

Classes: Writer

Instance Method Summary collapse

Instance Method Details

#attributes(path) ⇒ Object

Attributes



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/vos/drivers/ssh_vfs_storage.rb', line 17

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



87
88
89
# File 'lib/vos/drivers/ssh_vfs_storage.rb', line 87

def create_dir path
  sftp.mkdir! path
end

#delete_dir(path) ⇒ Object



91
92
93
# File 'lib/vos/drivers/ssh_vfs_storage.rb', line 91

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

#delete_file(remote_file_path) ⇒ Object



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

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

#each_entry(path, query, &block) ⇒ Object



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

def each_entry path, query, &block
  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?
      block.call stat.name, :dir
    else
      block.call stat.name, :file
    end
  end
end

#local?Boolean

Returns:

  • (Boolean)


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

def local?; false end

#read_file(path, &block) ⇒ Object

File



40
41
42
43
44
45
46
# File 'lib/vos/drivers/ssh_vfs_storage.rb', line 40

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



33
34
35
# File 'lib/vos/drivers/ssh_vfs_storage.rb', line 33

def set_attributes path, attrs      
  raise 'not supported'
end

#tmp(&block) ⇒ Object

Special



129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/vos/drivers/ssh_vfs_storage.rb', line 129

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



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/vos/drivers/ssh_vfs_storage.rb', line 48

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.write data
      block.call writer
    end
  else
    sftp.file.open fix_path(path), 'w' do |out|
      block.call Writer.new(out)
    end
  end          
end