lib/perobs/BTreeBlob.rb in perobs-2.4.1 vs lib/perobs/BTreeBlob.rb in perobs-2.4.2
- old
+ new
@@ -298,10 +298,13 @@
entry_format = 'QQQCL'
restore_crc = false
if File.exist?(@index_file_name)
begin
File.open(@index_file_name, 'rb') do |f|
+ unless f.flock(File::LOCK_NB | File::LOCK_EX)
+ PEROBS.log.fatal 'BTreeDB Database is locked by another process'
+ end
# Since version 2.3.0, all index files start with a header.
# Earlier versions did not yet have this header. The header is 24
# bytes long. The 2nd set of 8 bytes must be 0 to distinguish the
# header from regular entries. The first 8 bytes are a magic
# number and the 3rd 8 bytes mark the schema version. We are
@@ -345,10 +348,11 @@
e[CRC] = Zlib.crc32(raw)
end
@entries << e
@entries_by_id[e[ID]] = e
end
+ f.flock(File::LOCK_UN)
end
rescue => e
PEROBS.log.fatal "BTreeBlob file #{@index_file_name} corrupted: " +
e.message
end
@@ -356,14 +360,18 @@
end
def write_index
begin
File.open(@index_file_name, 'wb') do |f|
+ unless f.flock(File::LOCK_NB | File::LOCK_EX)
+ PEROBS.log.fatal 'BTreeDB Database is locked by another process'
+ end
# See read_index for data format documentation.
f.write([ PEROBS_MAGIC, 0, 1].pack('QQQ'))
@entries.each do |entry|
f.write(entry.pack('QQQCL'))
end
+ f.flock(File::LOCK_UN)
end
rescue => e
PEROBS.log.fatal "Cannot write BTreeBlob index file " +
"#{@index_file_name}: " + e.message
end