Sha256: 2fe56e7cfcbae5394b4585665333416e967cac83bcd8266dda178697e30113b3

Contents?: true

Size: 826 Bytes

Versions: 9

Compression:

Stored size: 826 Bytes

Contents

# frozen_string_literal: true

require 'image_optim/path'

class ImageOptim
  # ImageOptiom::Path with a non self destructing #replace method
  class CachePath < Path
    # Atomic replace dst with self
    def replace(dst)
      dst = self.class.convert(dst)
      tmpdir = [dirname, Path.new(Dir.tmpdir)].find do |dir|
        dir.same_dev?(dst.dirname)
      end
      if tmpdir
        begin
          replace_using_tmp_file(dst, tmpdir)
        rescue Errno::EXDEV
          replace_using_tmp_file(dst, dst.dirname)
        end
      else
        replace_using_tmp_file(dst, dst.dirname)
      end
    end

  private

    def replace_using_tmp_file(dst, tmpdir)
      dst.temp_path_with_tmp_ext(tmpdir) do |temp|
        copy(temp)
        dst.copy_metadata(temp)
        temp.rename(dst.to_s)
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
image_optim-0.31.4 lib/image_optim/cache_path.rb
image_optim-0.31.3 lib/image_optim/cache_path.rb
image_optim-0.31.2 lib/image_optim/cache_path.rb
image_optim-0.31.1 lib/image_optim/cache_path.rb
image_optim-0.31.0 lib/image_optim/cache_path.rb
image_optim-0.30.0 lib/image_optim/cache_path.rb
image_optim-0.29.0 lib/image_optim/cache_path.rb
image_optim-0.28.0 lib/image_optim/cache_path.rb
image_optim-0.27.1 lib/image_optim/cache_path.rb