Sha256: e754ef1603328ab69ee62356ccfc04b0a31d14edbd16e1bd4185e4c68bf22974

Contents?: true

Size: 1.75 KB

Versions: 63

Compression:

Stored size: 1.75 KB

Contents

module Puppet::Util::Autoload::FileCache
  @found_files = {}
  @missing_files = {}
  class << self
    attr_reader :found_files, :missing_files
  end

  # Only used for testing.
  def self.clear
    @found_files.clear
    @missing_files.clear
  end

  def found_files
    Puppet::Util::Autoload::FileCache.found_files
  end

  def missing_files
    Puppet::Util::Autoload::FileCache.missing_files
  end

  def directory_exist?(path)
    cache = cached_data?(path, :directory?)
    return cache unless cache.nil?

    protect(path) do
      stat = File.lstat(path)
      if stat.directory?
        found_file(path, stat)
        return true
      else
        missing_file(path)
        return false
      end
    end
  end

  def file_exist?(path)
    cache = cached_data?(path)
    return cache unless cache.nil?

    protect(path) do
      stat = File.lstat(path)
      found_file(path, stat)
      return true
    end
  end

  def found_file?(path, type = nil)
    if data = found_files[path] and ! data_expired?(data[:time])
      return(type and ! data[:stat].send(type)) ? false : true
    else
      return false
    end
  end

  def found_file(path, stat)
    found_files[path] = {:stat => stat, :time => Time.now}
  end

  def missing_file?(path)
    !!(time = missing_files[path] and ! data_expired?(time))
  end

  def missing_file(path)
    missing_files[path] = Time.now
  end

  private

  def cached_data?(path, type = nil)
    if found_file?(path, type)
      return true
    elsif missing_file?(path)
      return false
    else
      return nil
    end
  end

  def data_expired?(time)
    Time.now - time > 15
  end

  def protect(path)
      yield
  rescue => detail
      raise unless detail.class.to_s.include?("Errno")
      missing_file(path)
      return false
  end
end

Version data entries

63 entries across 63 versions & 4 rubygems

Version Path
puppet-parse-0.1.4 lib/vendor/puppet/util/autoload/file_cache.rb
puppet-parse-0.1.3 lib/vendor/puppet/util/autoload/file_cache.rb
puppet-parse-0.1.2 lib/vendor/puppet/util/autoload/file_cache.rb
puppet-parse-0.1.1 lib/vendor/puppet/util/autoload/file_cache.rb
puppet-2.7.26 lib/puppet/util/autoload/file_cache.rb
puppet-2.7.25 lib/puppet/util/autoload/file_cache.rb
puppet-2.7.24 lib/puppet/util/autoload/file_cache.rb
puppet-2.7.23 lib/puppet/util/autoload/file_cache.rb
puppet-2.7.22 lib/puppet/util/autoload/file_cache.rb
puppet-parse-0.1.0 lib/vendor/puppet/util/autoload/file_cache.rb
puppet-parse-0.0.6 lib/vendor/puppet/util/autoload/file_cache.rb
puppet-2.7.21 lib/puppet/util/autoload/file_cache.rb
puppet-2.6.18 lib/puppet/util/autoload/file_cache.rb
puppet-parse-0.0.5 lib/vendor/puppet/util/autoload/file_cache.rb
puppet-parse-0.0.4 lib/vendor/puppet/util/autoload/file_cache.rb
puppet-parse-0.0.2 lib/vendor/puppet/util/autoload/file_cache.rb
puppet-2.7.20 lib/puppet/util/autoload/file_cache.rb
puppet-2.7.20.rc1 lib/puppet/util/autoload/file_cache.rb
librarian-puppet-0.9.4 vendor/gems/ruby/1.8/gems/puppet-2.7.18/lib/puppet/util/autoload/file_cache.rb
puppet-2.7.19 lib/puppet/util/autoload/file_cache.rb