Sha256: 26ff16abbd3d5e0a30d9d5a5b6c2f1498088a81806d7472dd526494ad61eb4a3

Contents?: true

Size: 1.12 KB

Versions: 22

Compression:

Stored size: 1.12 KB

Contents

module Vitrine
  # Stolen from Rails
  def self.atomic_write(file_name, temp_dir = Dir.tmpdir)
    require 'tempfile' unless defined?(Tempfile)
    require 'fileutils' unless defined?(FileUtils)
  
    temp_file = Tempfile.new(File.basename(file_name), temp_dir)
    temp_file.binmode
    yield temp_file
    temp_file.close
  
    begin
      # Get original file permissions
      old_stat = File.stat(file_name)
    rescue Errno::ENOENT
      # No old permissions, write a temp file to determine the defaults
      check_name = File.join(File.dirname(file_name), ".permissions_check.#{Thread.current.object_id}.#{Process.pid}.#{rand(1000000)}")
      open(check_name, "w") { }
      old_stat = File.stat(check_name)
      File.unlink(check_name)
    end

    # Overwrite original file with temp file
    FileUtils.mv(temp_file.path, file_name)

    # Set correct permissions on new file
    begin
      File.chown(old_stat.uid, old_stat.gid, file_name)
      # This operation will affect filesystem ACL's
      File.chmod(old_stat.mode, file_name)
    rescue Errno::EPERM
      # Changing file ownership failed, moving on.
    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
vitrine-0.0.31 lib/atomic_write.rb
vitrine-0.0.30 lib/atomic_write.rb
vitrine-0.0.29 lib/atomic_write.rb
vitrine-0.0.28 lib/atomic_write.rb
vitrine-0.0.27 lib/atomic_write.rb
vitrine-0.0.26 lib/atomic_write.rb
vitrine-0.0.25 lib/atomic_write.rb
vitrine-0.0.24 lib/atomic_write.rb
vitrine-0.0.23 lib/atomic_write.rb
vitrine-0.0.22 lib/atomic_write.rb
vitrine-0.0.21 lib/atomic_write.rb
vitrine-0.0.19 lib/atomic_write.rb
vitrine-0.0.18 lib/atomic_write.rb
vitrine-0.0.17 lib/atomic_write.rb
vitrine-0.0.16 lib/atomic_write.rb
vitrine-0.0.15 lib/atomic_write.rb
vitrine-0.0.14 lib/atomic_write.rb
vitrine-0.0.13 lib/atomic_write.rb
vitrine-0.0.12 lib/atomic_write.rb
vitrine-0.0.11 lib/atomic_write.rb