Sha256: 37a733cb2a28018003e7859417fa8e8d2fbc597d15d5bc8ff15679419bcb300a

Contents?: true

Size: 636 Bytes

Versions: 2

Compression:

Stored size: 636 Bytes

Contents

module Spruz
  module SecureWrite
    # Write to a file atomically
    def secure_write(filename, content = nil, mode = 'w')
      temp = File.new(filename + ".tmp.#$$.#{Time.now.to_f}", mode)
      if content.nil? and block_given?
        yield temp
      elsif !content.nil?
        temp.write content
      else
        raise ArgumentError, "either content or block argument required"
      end
      temp.fsync
      size = temp.stat.size
      temp.close
      File.rename temp.path, filename
      size
    ensure
      temp and !temp.closed? and temp.close
      File.file?(temp.path) and File.unlink temp.path
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
spruz-0.2.13 lib/spruz/secure_write.rb
spruz-0.2.12 lib/spruz/secure_write.rb