Sha256: 6e5367d86652bc2b4256f8c907f10ac6c224be222c693f26e270fb53a41e253b

Contents?: true

Size: 779 Bytes

Versions: 1

Compression:

Stored size: 779 Bytes

Contents

# frozen_string_literal: true

require 'atomos/version'

module Atomos
  module_function

  def atomic_write(dest, contents = nil, tmpdir: Dir.tmpdir, &block)
    unless contents.nil? ^ block.nil?
      raise ArgumentError, 'must provide either contents or a block'
    end

    require 'tempfile'

    # Ensure the destination is on the same device as tmpdir
    if File.stat(tmpdir).dev != File.stat(File.dirname(dest)).dev
      # If not, use the directory of the destination as the tmpdir.
      tmpdir = File.dirname(dest)
    end

    Tempfile.open(".atomos.#{File.basename(dest)}", tmpdir) do |tmpfile|
      if contents
        tmpfile << contents
      else
        retval = yield tmpfile
      end

      File.rename(tmpfile.path, dest)

      retval
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
atomos-0.1.1 lib/atomos.rb