# File lib/rfilter/deliver.rb, line 42 def 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| break if 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." end unless is_an_mbox(f, st) raise NotAMailbox, "Can not deliver to #{filename}, file is not in mbox format." end begin # 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 end rescue Exception => e begin begin f.flush rescue Exception end f.truncate(st.size) ensure raise e end ensure if old_handler trap('XFSZ', old_handler) end end f.flock(File::LOCK_UN) } filename end