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.



87
88
89
90
# File 'lib/httpimagestore/configuration/s3.rb', line 87

def initialize(path)
	super
	@header = nil
end

Instance Method Details

#headerObject



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

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

#read(max_bytes = nil) ⇒ Object



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

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



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

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