# File /home/matt/rubymail/mail/deliver.rb, line 94 def deliver_maildir(dir, message) require 'socket' # First, make the required directories new = File.join(dir, 'new') tmp = File.join(dir, 'tmp') [ dir, new, tmp, File.join(dir, 'cur') ].each { |d| begin Dir.mkdir(d, 0700) rescue Errno::EEXIST raise unless FileTest::directory?(d) end } sequence = @@mail_deliver_maildir_count @@mail_deliver_maildir_count = @@mail_deliver_maildir_count.next try_count = 1 tmp_name = nil new_name = nil begin # Try to open the file in the 'tmp' directory up to 5 times f = begin name = sprintf("%d.%d_%d.%s", Time::now.to_i, Process::pid, sequence, Socket::gethostname) tmp_name = File.join(tmp, name) new_name = File.join(new, name) File.open(tmp_name, File::CREAT|File::EXCL|File::WRONLY|File::SYNC, 0600) rescue Errno::EEXIST raise if try_count >= 5 sleep(2) try_count = try_count.next retry end begin # Write the message to the file first = true message.each { |line| if first first = false next if line =~ /From / end f << line f << "\n" unless line[-1] == ?\n } f.close f = nil # Link the tmp file to the new file File.link(tmp_name, new_name) ensure # Try to delete the tmp file begin File.delete(tmp_name) unless tmp_name.nil? rescue Errno::ENOENT end end ensure f.close unless f.nil? end new_name end