# File lib/rfilter/deliver.rb, line 42def deliver_mbox(filename, message)
return filename if filename == '/dev/null'
File.open(filename,
File::RDWR|File::CREAT|SYNC_IF_NO_FSYNC,
0600) { |f|
max = 5
max.times { |i|
breakif f.flock(File::LOCK_EX | File::LOCK_NB)
raise LockingError, "Timeout locking mailbox."if i == max - 1
sleep(1)
}
st = f.lstat
unless st.file?
raise NotAFile,
"Can not deliver to #{filename}, not a regular file."
endunless is_an_mbox(f, st)
raise NotAMailbox,
"Can not deliver to #{filename}, file is not in mbox format."
endbegin# Ignore SIGXFSZ, since we want to get the Errno::EFBIG# exception when the file is too big.
old_handler = trap('XFSZ', 'IGNORE') || 'DEFAULT'
write_to_mbox(f, message)
begin
f.fsync
rescue NameError
# NameError happens with older versions of Ruby that have# no File#fsync
f.flush
endrescue Exception => e
beginbegin
f.flush
rescue Exception
end
f.truncate(st.size)
ensure
raise e
endensureif old_handler
trap('XFSZ', old_handler)
endend
f.flock(File::LOCK_UN)
}
filename
end