class BFS::Bucket::FS

FS buckets are operating on the file system

Public Class Methods

new(root, **opts) click to toggle source
Calls superclass method BFS::Bucket::Abstract::new
# 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

Public Instance Methods

cp(src, dst, **_opts) click to toggle source

Copies a file.

@param [String] src The source path. @param [String] dst The destination path.

# File lib/bfs/bucket/fs.rb, line 86
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) click to toggle source

Creates a new file and opens it for writing

@param [String] path The creation path. @param [Hash] opts Additional options. @option opts [String] :encoding Custom encoding. @option opts [Integer] :perm Custom file permission, default: 0600.

# File lib/bfs/bucket/fs.rb, line 52
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
glob(pattern = '**/*', **_opts) click to toggle source

Iterates over the contents of a bucket using a glob pattern

# File lib/bfs/bucket/fs.rb, line 26
def glob(pattern = '**/*', **_opts)
  Enumerator.new do |acc|
    walk(pattern) do |path, stat|
      acc << file_info(path, stat)
    end
  end
end
info(path, **_opts) click to toggle source

Info returns the info for a single file

# File lib/bfs/bucket/fs.rb, line 35
def info(path, **_opts)
  norm = norm_path(path)
  pn   = @root.join(norm)
  stat = pn.stat
  raise BFS::FileNotFound, path unless stat.file?

  file_info(norm, stat)
rescue Errno::ENOENT
  raise BFS::FileNotFound, path
end
ls(pattern = '**/*', **_opts) click to toggle source

Lists the contents of a bucket using a glob pattern

# File lib/bfs/bucket/fs.rb, line 17
def ls(pattern = '**/*', **_opts)
  Enumerator.new do |acc|
    walk(pattern) do |path, _|
      acc << path
    end
  end
end
mv(src, dst, **_opts) click to toggle source

Moves a file.

@param [String] src The source path. @param [String] dst The destination path.

# File lib/bfs/bucket/fs.rb, line 99
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) click to toggle source

Opens an existing file for reading

@param [String] path The path to open. @param [Hash] opts Additional options. @option opts [String] :encoding Custom encoding.

# File lib/bfs/bucket/fs.rb, line 66
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) click to toggle source

Deletes a file.

@param [String] path The path to delete.

# File lib/bfs/bucket/fs.rb, line 77
def rm(path, **_opts)
  full = @root.join(norm_path(path))
  FileUtils.rm_f full.to_s
end

Private Instance Methods

file_info(path, stat) click to toggle source
# File lib/bfs/bucket/fs.rb, line 117
def file_info(path, stat)
  BFS::FileInfo.new(path: path, size: stat.size, mtime: stat.mtime, mode: BFS.norm_mode(stat.mode))
end
walk(pattern) { |trim_prefix(pn), stat| ... } click to toggle source
# File lib/bfs/bucket/fs.rb, line 110
def walk(pattern)
  Pathname.glob(@root.join(pattern)) do |pn|
    stat = pn.stat
    yield(trim_prefix(pn.to_s), stat) if stat.file?
  end
end