Class: Configuration::S3SourceStoreBase::CacheRoot::CacheFile

Inherits:
Pathname
  • Object
show all
Defined in:
lib/httpimagestore/configuration/s3.rb

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ CacheFile

Returns a new instance of CacheFile.



89
90
91
92
# File 'lib/httpimagestore/configuration/s3.rb', line 89

def initialize(path)
  super
  @header = nil
end

Instance Method Details

#headerObject



94
95
96
97
98
99
100
101
# File 'lib/httpimagestore/configuration/s3.rb', line 94

def header
  begin
    read(0)
  rescue
    @header = {}
  end unless @header
  @header or fail 'no header data'
end

#read(max_bytes = nil) ⇒ Object



103
104
105
106
107
108
109
# File 'lib/httpimagestore/configuration/s3.rb', line 103

def read(max_bytes = nil)
  open('rb') do |io|
    io.flock(File::LOCK_SH)
    @header = read_header(io)
    return io.read(max_bytes)
  end
end

#write(data) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/httpimagestore/configuration/s3.rb', line 111

def write(data)
  dirname.directory? or dirname.mkpath
  open('ab') do |io|
    # opened but not truncated before lock can be obtained
    io.flock(File::LOCK_EX)

    # now get rid of the old content if any
    io.seek 0, IO::SEEK_SET
    io.truncate 0

    begin
      header = MessagePack.pack(@header)
      io.write [header.length].pack('L') # header length
      io.write header
      io.write data
    rescue => error
      unlink # remove broken cache file
      raise
    end
  end
end