# class Chef::ChefFS::ChefFSDataStore

### Attributes

chef_fs[R]

### Public Class Methods

new(chef_fs) click to toggle source

    # File lib/chef/chef_fs/chef_fs_data_store.rb, line 30
    def initialize(chef_fs)
      @chef_fs = chef_fs
      @memory_store = ChefZero::DataStore::MemoryStore.new
    end

### Public Instance Methods

create(path, name, data, *options) click to toggle source

    # File lib/chef/chef_fs/chef_fs_data_store.rb, line 51
    def create(path, name, data, *options)
      if use_memory_store?(path)
        @memory_store.create(path, name, data, *options)

      elsif path[0] == 'cookbooks' && path.length == 2
        # Do nothing.  The entry gets created when the cookbook is created.

      else
        if !data.is_a?(String)
          raise "set only works with strings"
        end

        with_dir(path) do |parent|
          parent.create_child(chef_fs_filename(path + [name]), data)
        end
      end
    end

create_dir(path, name, *options) click to toggle source

    # File lib/chef/chef_fs/chef_fs_data_store.rb, line 41
    def create_dir(path, name, *options)
      if use_memory_store?(path)
        @memory_store.create_dir(path, name, *options)
      else
        with_dir(path) do |parent|
          parent.create_child(chef_fs_filename(path + [name]), nil)
        end
      end
    end

delete(path) click to toggle source

    # File lib/chef/chef_fs/chef_fs_data_store.rb, line 130
    def delete(path)
      if use_memory_store?(path)
        @memory_store.delete(path)
      else
        with_entry(path) do |entry|
          if path[0] == 'cookbooks' && path.length >= 3
            entry.delete(true)
          else
            entry.delete(false)
          end
        end
      end
    end

delete_dir(path, *options) click to toggle source

    # File lib/chef/chef_fs/chef_fs_data_store.rb, line 144
    def delete_dir(path, *options)
      if use_memory_store?(path)
        @memory_store.delete_dir(path, *options)
      else
        with_entry(path) do |entry|
          entry.delete(options.include?(:recursive))
        end
      end
    end

exists?(path) click to toggle source

    # File lib/chef/chef_fs/chef_fs_data_store.rb, line 201
    def exists?(path)
      if use_memory_store?(path)
        @memory_store.exists?(path)
      else
        path_always_exists?(path) || Chef::ChefFS::FileSystem.resolve_path(chef_fs, to_chef_fs_path(path)).exists?
      end
    end

exists_dir?(path) click to toggle source

    # File lib/chef/chef_fs/chef_fs_data_store.rb, line 209
    def exists_dir?(path)
      if use_memory_store?(path)
        @memory_store.exists_dir?(path)
      elsif path[0] == 'cookbooks' && path.length == 2
        list([ path[0] ]).include?(path[1])
      else
        Chef::ChefFS::FileSystem.resolve_path(chef_fs, to_chef_fs_path(path)).exists?
      end
    end

get(path, request=nil) click to toggle source

    # File lib/chef/chef_fs/chef_fs_data_store.rb, line 69
    def get(path, request=nil)
      if use_memory_store?(path)
        @memory_store.get(path)

      elsif path[0] == 'file_store' && path[1] == 'repo'
        entry = Chef::ChefFS::FileSystem.resolve_path(chef_fs, path[2..-1].join('/'))
        begin
          entry.read
        rescue Chef::ChefFS::FileSystem::NotFoundError => e
          raise ChefZero::DataStore::DataNotFoundError.new(to_zero_path(e.entry), e)
        end

      else
        with_entry(path) do |entry|
          if path[0] == 'cookbooks' && path.length == 3
            # get /cookbooks/NAME/version
            result = entry.chef_object.to_hash
            result.each_pair do |key, value|
              if value.is_a?(Array)
                value.each do |file|
                  if file.is_a?(Hash) && file.has_key?('checksum')
                    relative = ['file_store', 'repo', 'cookbooks']
                    if Chef::Config.versioned_cookbooks
                      relative << "#{path[1]}-#{path[2]}"
                    else
                      relative << path[1]
                    end
                    relative = relative + file[:path].split('/')
                    file['url'] = ChefZero::RestBase::build_uri(request.base_uri, relative)
                  end
                end
              end
            end
            JSON.pretty_generate(result)

          else
            entry.read
          end
        end
      end
    end

list(path) click to toggle source

    # File lib/chef/chef_fs/chef_fs_data_store.rb, line 154
    def list(path)
      if use_memory_store?(path)
        @memory_store.list(path)

      elsif path[0] == 'cookbooks' && path.length == 1
        with_entry(path) do |entry|
          begin
            if Chef::Config.versioned_cookbooks
              # /cookbooks/name-version -> /cookbooks/name
              entry.children.map { |child| split_name_version(child.name)[0] }.uniq
            else
              entry.children.map { |child| child.name }
            end
          rescue Chef::ChefFS::FileSystem::NotFoundError
            # If the cookbooks dir doesn't exist, we have no cookbooks (not 404)
            []
          end
        end

      elsif path[0] == 'cookbooks' && path.length == 2
        if Chef::Config.versioned_cookbooks
          # list /cookbooks/name = filter /cookbooks/name-version down to name
          entry.children.map { |child| split_name_version(child.name) }.
                         select { |name, version| name == path[1] }.
                         map { |name, version| version }.to_a
        else
          # list /cookbooks/name = <single version>
          version = get_single_cookbook_version(path)
          [version]
        end

      else
        with_entry(path) do |entry|
          begin
            entry.children.map { |c| zero_filename(c) }.sort
          rescue Chef::ChefFS::FileSystem::NotFoundError
            # /cookbooks, /data, etc. never return 404
            if path_always_exists?(path)
              []
            else
              raise
            end
          end
        end
      end
    end

publish_description() click to toggle source

    # File lib/chef/chef_fs/chef_fs_data_store.rb, line 35
    def publish_description
      "Reading and writing data to #{chef_fs.fs_description}"
    end

set(path, data, *options) click to toggle source

    # File lib/chef/chef_fs/chef_fs_data_store.rb, line 111
    def set(path, data, *options)
      if use_memory_store?(path)
        @memory_store.set(path, data, *options)
      else
        if !data.is_a?(String)
          raise "set only works with strings: #{path} = #{data.inspect}"
        end

        # Write out the files!
        if path[0] == 'cookbooks' && path.length == 3
          write_cookbook(path, data, *options)
        else
          with_dir(path[0..-2]) do |parent|
            parent.create_child(chef_fs_filename(path), data)
          end
        end
      end
    end