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

#_create_root_dirObject



170
171
172
173
# File 'lib/vos/drivers/ssh_vfs_storage.rb', line 170

def _create_root_dir
  raise 'refuse to create / directory!' if root == '/'
  sftp.mkdir! root unless root.empty?
end

#_delete_root_dirObject



165
166
167
168
# File 'lib/vos/drivers/ssh_vfs_storage.rb', line 165

def _delete_root_dir
  raise 'refuse to delete / directory!' if root == '/'
  exec "rm -r #{@root}" unless root.empty?
end

#attributes(path) ⇒ Object

Attributes



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

def attributes path
  path = with_root path
  path = fix_path path

  stat = sftp.stat! path
  attrs = {}
  attrs[:file] = stat.file?
  attrs[:dir] = stat.directory?
  # stat.symlink?

  # attributes special for file system
  time = stat.mtime
  attrs[:updated_at] = time && Time.at(time)
  attrs[:size]       = stat.size if attrs[:file]

  attrs
rescue Net::SFTP::StatusException
  nil
end

#create_dir(path) ⇒ Object

Dir



99
100
101
102
103
# File 'lib/vos/drivers/ssh_vfs_storage.rb', line 99

def create_dir path
  path = with_root path
  path = fix_path path
  sftp.mkdir! path
end

#delete_dir(path) ⇒ Object



105
106
107
108
109
# File 'lib/vos/drivers/ssh_vfs_storage.rb', line 105

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

#delete_file(path) ⇒ Object



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

def delete_file path
  path = with_root path
  path = fix_path path
  sftp.remove! path
end

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



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/vos/drivers/ssh_vfs_storage.rb', line 111

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

#local?Boolean

Returns:

  • (Boolean)


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

def local?; false end

#read_file(path, &block) ⇒ Object

File



47
48
49
50
51
52
53
54
55
56
# File 'lib/vos/drivers/ssh_vfs_storage.rb', line 47

def read_file path, &block
  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

#set_attributes(path, attrs) ⇒ Object



37
38
39
40
41
42
# File 'lib/vos/drivers/ssh_vfs_storage.rb', line 37

def set_attributes path, attrs
  path = with_root path
  path = fix_path path

  raise 'not supported'
end

#tmp(&block) ⇒ Object

Special



148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/vos/drivers/ssh_vfs_storage.rb', line 148

def tmp &block
  tmp_dir = "/tmp_#{rand(10**6)}"
  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



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/vos/drivers/ssh_vfs_storage.rb', line 58

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[:file]
        os = ""
        read_file(path){|buff| os << buff}
        delete_file path
        os
    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
    path = with_root path
    path = fix_path path
    sftp.file.open path, 'w' do |out|
      block.call Writer.new(out)
    end
  end
end