Module: Vfs::Storages::Local::LocalVfsHelper

Included in:
Vfs::Storages::Local
Defined in:
lib/vfs/storages/local.rb

Constant Summary collapse

DEFAULT_BUFFER =
1000 * 1024

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#bufferObject



16
17
18
# File 'lib/vfs/storages/local.rb', line 16

def buffer
  @buffer || DEFAULT_BUFFER
end

Instance Method Details

#attributes(path) ⇒ Object

Attributes



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/vfs/storages/local.rb', line 23

def attributes path
  path = with_root path

  stat = ::File.stat path
  attrs = {}
  attrs[:file] = !!stat.file?
  attrs[:dir]  = !!stat.directory?

  # attributes special for file system
  attrs[:created_at] = stat.ctime
  attrs[:updated_at] = stat.mtime
  attrs[:size]       = stat.size if attrs[:file]
  attrs
rescue Errno::ENOENT
  nil
end

#create_dir(path) ⇒ Object

Dir



83
84
85
86
# File 'lib/vfs/storages/local.rb', line 83

def create_dir path
  path = with_root path
  ::Dir.mkdir path
end

#delete_dir(original_path) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/vfs/storages/local.rb', line 88

def delete_dir original_path
  path = with_root original_path

  # TODO2 Performance lost, extra call to check file existence
  raise "can't delete file (#{original_path})!" if ::File.file?(path)

  ::FileUtils.rm_r path
end

#delete_file(path) ⇒ Object



70
71
72
73
# File 'lib/vfs/storages/local.rb', line 70

def delete_file path
  path = with_root path
  ::File.delete path
end

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



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/vfs/storages/local.rb', line 97

def each_entry path, query, &block
  path = with_root path

  if query
    path_with_trailing_slash = path == '/' ? path : "#{path}/"
    ::Dir["#{path_with_trailing_slash}#{query}"].each do |absolute_path|
      relative_path = absolute_path.sub path_with_trailing_slash, ''
      # TODO2 Performance loss
      if ::File.directory? absolute_path
        block.call relative_path, :dir
      else
        block.call relative_path, :file
      end
    end
  else
    ::Dir.foreach path do |relative_name|
      next if relative_name == '.' or relative_name == '..'
      if ::File.directory? "#{path}/#{relative_name}"
        block.call relative_name, :dir
      else
        block.call relative_name, :file
      end
    end
  end
end

#local?Boolean

Other

Returns:

  • (Boolean)


141
# File 'lib/vfs/storages/local.rb', line 141

def local?; true end

#read_file(path, &block) ⇒ Object

File



49
50
51
52
53
54
55
56
# File 'lib/vfs/storages/local.rb', line 49

def read_file path, &block
  path = with_root path
  ::File.open path, 'r' do |is|
    while buff = is.gets(self.buffer || DEFAULT_BUFFER)
      block.call buff
    end
  end
end

#set_attributes(path, attrs) ⇒ Object



40
41
42
43
# File 'lib/vfs/storages/local.rb', line 40

def set_attributes path, attrs
  # TODO2 set attributes
  not_implemented
end

#tmp(&block) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/vfs/storages/local.rb', line 143

def tmp &block
  path = "/tmp/#{rand(10**6)}"
  # tmp_dir = "#{::Dir.tmpdir}/#{rand(10**6)}"
  if block
    begin
      ::FileUtils.mkdir_p with_root(path)
      block.call path
    ensure
      ::FileUtils.rm_r with_root(path) if ::File.exist? with_root(path)
    end
  else
    ::FileUtils.mkdir_p with_root(path)
    path
  end
end

#to_sObject



159
# File 'lib/vfs/storages/local.rb', line 159

def to_s; '' end

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



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/vfs/storages/local.rb', line 58

def write_file original_path, append, &block
  path = with_root original_path

  # TODO2 Performance lost, extra call to check file existence
  raise "can't write, entry #{original_path} already exist!" if !append and ::File.exist?(path)

  option = append ? 'a' : 'w'
  ::File.open path, option do |out|
    block.call Writer.new(out)
  end
end