class Rubytus::Storage

Public Class Methods

new(options) click to toggle source
# File lib/rubytus/storage.rb, line 44
def initialize(options)
  @options = options
end

Public Instance Methods

create_file(uid, final_length) click to toggle source
# File lib/rubytus/storage.rb, line 48
def create_file(uid, final_length)
  fpath = file_path(uid)
  ipath = info_path(uid)
  info  = Rubytus::Info.new
  info.final_length = final_length

  begin
    File.open(fpath, 'w') {}
    File.open(ipath, 'w') do |f|
      f.write(info.to_json)
    end
  rescue SystemCallError => e
    raise(PermissionError, e.message) if e.class.name.start_with?('Errno::')
  end
end
patch_file(uid, data, offset = nil) click to toggle source
# File lib/rubytus/storage.rb, line 77
def patch_file(uid, data, offset = nil)
  fpath = file_path(uid)
  begin
    f = File.open(fpath, 'r+b')
    f.sync = true
    f.seek(offset) unless offset.nil?
    f.write(data)
    size = f.size
    f.close
    update_info(uid, size)
  rescue SystemCallError => e
    raise(PermissionError, e.message) if e.class.name.start_with?('Errno::')
  end
end
read_file(uid) click to toggle source
# File lib/rubytus/storage.rb, line 64
def read_file(uid)
  fpath = file_path(uid)

  begin
    f = File.open(fpath, 'rb')
    f.read
  rescue SystemCallError => e
    raise(PermissionError, e.message) if e.class.name.start_with?('Errno::')
  ensure
    f.close unless f.nil?
  end
end
read_info(uid) click to toggle source
# File lib/rubytus/storage.rb, line 92
def read_info(uid)
  ipath = info_path(uid)

  begin
    data = File.open(ipath, 'r') { |f| f.read }
    JSON.parse(data, :object_class => Rubytus::Info)
  rescue SystemCallError => e
    raise(PermissionError, e.message) if e.class.name.start_with?('Errno::')
  end
end
update_info(uid, offset) click to toggle source
# File lib/rubytus/storage.rb, line 103
def update_info(uid, offset)
  ipath = info_path(uid)
  info = read_info(uid)
  info.offset = offset

  begin
    File.open(ipath, 'w') do |f|
      f.write(info.to_json)
    end
  rescue SystemCallError => e
    raise(PermissionError, e.message) if e.class.name.start_with?('Errno::')
  end
end