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 Attribute Summary

Attributes inherited from Abstract

#encoding, #perm

Instance Method Summary collapse

Methods inherited from Abstract

#close, open, #read, #write

Constructor Details

#initialize(root, **opts) ⇒ FS

Returns a new instance of FS.



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

def initialize(root, **opts)
  super(**opts.dup)

  @root = Pathname.new(root.to_s)
  @prefix = "#{@root.to_s.chomp('/')}/"
end

Instance Method Details

#cp(src, dst, **_opts) ⇒ Object

Copies a file.

Parameters:

  • src (String)

    The source path.

  • dst (String)

    The destination path.



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

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, encoding: self.encoding, perm: self.perm, **_opts, &block) ⇒ Object

Creates a new file and opens it for writing

Parameters:

  • path (String)

    The creation path.

  • opts (Hash)

    Additional options.



41
42
43
44
45
46
47
48
# File 'lib/bfs/bucket/fs.rb', line 41

def create(path, encoding: self.encoding, perm: self.perm, **_opts, &block)
  full = @root.join(norm_path(path))
  FileUtils.mkdir_p(full.dirname.to_s)

  BFS::Writer.new(full, encoding: encoding, perm: perm) do |temp|
    FileUtils.mv temp, full.to_s
  end.perform(&block)
end

#info(path, **_opts) ⇒ Object

Info returns the info for a single file



26
27
28
29
30
31
32
33
# File 'lib/bfs/bucket/fs.rb', line 26

def info(path, **_opts)
  full = @root.join(norm_path(path))
  path = trim_prefix(full.to_s)
  stat = full.stat
  BFS::FileInfo.new(path: path, size: stat.size, mtime: stat.mtime, mode: BFS.norm_mode(stat.mode))
rescue Errno::ENOENT
  raise BFS::FileNotFound, path
end

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

Lists the contents of a bucket using a glob pattern



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

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.

Parameters:

  • src (String)

    The source path.

  • dst (String)

    The destination path.



88
89
90
91
92
93
94
95
# File 'lib/bfs/bucket/fs.rb', line 88

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

Parameters:

  • path (String)

    The path to open.

  • opts (Hash)

    Additional options.

Options Hash (**opts):

  • :encoding (String)

    Custom encoding.



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

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

#rm(path, **_opts) ⇒ Object

Deletes a file.

Parameters:

  • path (String)

    The path to delete.



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

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