Class: BFS::Bucket::FS

Inherits:
Abstract show all
Defined in:
lib/bfs/bucket/fs.rb

Overview

FS buckets are operating on the file system

Instance Method Summary collapse

Methods inherited from Abstract

#read, #write

Constructor Details

#initialize(root, _opts = {}) ⇒ FS

Returns a new instance of FS.



9
10
11
12
# File 'lib/bfs/bucket/fs.rb', line 9

def initialize(root, _opts={})
  @root = Pathname.new(root.to_s)
  @prefix = "#{@root}/"
end

Instance Method Details

#cp(src, dst, _opts = {}) ⇒ Object

Copies a file.



61
62
63
64
65
66
67
# File 'lib/bfs/bucket/fs.rb', line 61

def cp(src, dst, _opts={})
  src = norm_path(src)
  dst = norm_path(dst)
  FileUtils.cp @root.join(src).to_s, @root.join(dst).to_s
rescue Errno::ENOENT
  raise BFS::FileNotFound, src
end

#create(path, _opts = {}, &block) ⇒ Object

Creates a new file and opens it for writing



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/bfs/bucket/fs.rb', line 31

def create(path, _opts={}, &block)
  name = @root.join(norm_path(path))
  FileUtils.mkdir_p(name.dirname.to_s)

  temp = BFS::TempWriter.new(name) {|t| FileUtils.mv t, name.to_s }
  return temp unless block

  begin
    yield temp
  ensure
    temp.close
  end
end

#info(path, _opts = {}) ⇒ Object

Info returns the info for a single file



22
23
24
25
26
27
28
# File 'lib/bfs/bucket/fs.rb', line 22

def info(path, _opts={})
  name = @root.join(norm_path(path))
  path = name.to_s.sub(@prefix, '')
  BFS::FileInfo.new(path, name.size, name.mtime)
rescue Errno::ENOENT
  raise BFS::FileNotFound, path
end

#ls(pattern = '**/*', _opts = {}) ⇒ Object

Lists the contents of a bucket using a glob pattern



15
16
17
18
19
# File 'lib/bfs/bucket/fs.rb', line 15

def ls(pattern='**/*', _opts={})
  Pathname.glob(@root.join(pattern)).select(&:file?).map do |name|
    name.to_s.sub(@prefix, '')
  end
end

#mv(src, dst, _opts = {}) ⇒ Object

Moves a file.



70
71
72
73
74
75
76
# File 'lib/bfs/bucket/fs.rb', line 70

def mv(src, dst, _opts={})
  src = norm_path(src)
  dst = norm_path(dst)
  FileUtils.mv @root.join(src).to_s, @root.join(dst).to_s
rescue Errno::ENOENT
  raise BFS::FileNotFound, src
end

#open(path, opts = {}, &block) ⇒ Object

Opens an existing file for reading



46
47
48
49
50
51
52
# File 'lib/bfs/bucket/fs.rb', line 46

def open(path, opts={}, &block)
  path = norm_path(path)
  name = @root.join(path)
  name.open('r', opts, &block)
rescue Errno::ENOENT
  raise BFS::FileNotFound, path
end

#rm(path, _opts = {}) ⇒ Object

Deletes a file.



55
56
57
58
# File 'lib/bfs/bucket/fs.rb', line 55

def rm(path, _opts={})
  name = @root.join(norm_path(path))
  FileUtils.rm_f name.to_s
end