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.to_s.chomp('/')}/"
end

Instance Method Details

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

Copies a file.



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

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

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

Creates a new file and opens it for writing



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

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

  temp = BFS::TempWriter.new(full) {|t| FileUtils.mv t, full.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



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

def info(path, _opts={})
  full = @root.join(norm_path(path))
  path = trim_prefix(full.to_s)
  BFS::FileInfo.new(path, full.size, full.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
20
21
# File 'lib/bfs/bucket/fs.rb', line 15

def ls(pattern='**/*', _opts={})
  Enumerator.new do |y|
    Pathname.glob(@root.join(pattern)) do |pname|
      y << trim_prefix(pname.to_s) if pname.file?
    end
  end
end

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

Moves a file.



73
74
75
76
77
78
79
80
# File 'lib/bfs/bucket/fs.rb', line 73

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

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

Opens an existing file for reading



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

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

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

Deletes a file.



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

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