Class: Rubytus::Storage

Inherits:
Object
  • Object
show all
Includes:
StorageHelper
Defined in:
lib/rubytus/storage.rb

Instance Method Summary collapse

Methods included from StorageHelper

#file_path, #info_path, #validates_data_dir

Constructor Details

#initialize(options) ⇒ Storage

Returns a new instance of Storage.



44
45
46
# File 'lib/rubytus/storage.rb', line 44

def initialize(options)
  @options = options
end

Instance Method Details

#create_file(uid, final_length) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# 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) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# 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) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
# 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) ⇒ Object



92
93
94
95
96
97
98
99
100
101
# 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) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
# 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