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.



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

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

Instance Method Details

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

Copies a file.



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

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



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

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



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

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



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

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.



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

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



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

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.



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

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