# module Chef::ChefFS::FileSystem

### Public Class Methods

child_pairs(a, b) click to toggle source

Get entries for children of either a or b, with matching pairs matched up.

#### Returns¶ ↑

An array of child pairs.

    [ [ a_child, b_child ], ... ]

If a child is only in a or only in b, the other child entry will be retrieved by name (and will most likely be a “nonexistent child”).

#### Example¶ ↑

    Chef::ChefFS::FileSystem.child_pairs(a, b).length

    # File lib/chef/chef_fs/file_system.rb, line 222
    def self.child_pairs(a, b)
      # If both are directories, recurse into them and diff the children instead of returning ourselves.
      result = []
      a_children_names = Set.new
      a.children.each do |a_child|
        a_children_names << a_child.name
        result << [ a_child, b.child(a_child.name) ]
      end

      # Check b for children that aren't in a
      b.children.each do |b_child|
        if !a_children_names.include?(b_child.name)
          result << [ a.child(b_child.name), b_child ]
        end
      end
      result
    end

compare(a, b) click to toggle source

    # File lib/chef/chef_fs/file_system.rb, line 240
    def self.compare(a, b)
      are_same, a_value, b_value = a.compare_to(b)
      if are_same.nil?
        are_same, b_value, a_value = b.compare_to(a)
      end
      if are_same.nil?
        # TODO these reads can be parallelized
        begin
          a_value = a.read if a_value.nil?
        rescue Chef::ChefFS::FileSystem::NotFoundError
          a_value = :none
        end
        begin
          b_value = b.read if b_value.nil?
        rescue Chef::ChefFS::FileSystem::NotFoundError
          b_value = :none
        end
        are_same = (a_value == b_value)
      end
      [ are_same, a_value, b_value ]
    end

copy_to(pattern, src_root, dest_root, recurse_depth, options, ui = nil, format_path = nil) click to toggle source

Copy everything matching the given pattern from src to dest.

After this method completes, everything in dest matching the given pattern will look identical to src.

#### Attributes¶ ↑

- `pattern` - Chef::ChefFS::FilePattern to match children under

- `src_root` - the root from which things will be copied

- `dest_root` - the root to which things will be copied

- `recurse_depth` - the maximum depth to copy things. `nil` means infinite depth. 0 means no recursion.

- `options` - hash of options:

  - `purge` - if `true`, items in `dest` that are not in `src`

  will be deleted from `dest`. If `false`, these items will be left alone.

  - `force` - if `true`, matching files are always copied from `src` to `dest`. If `false`, they will only be copied if actually different (which will take time to determine).

  - `dry_run` - if `true`, action will not actually be taken; things will be printed out instead.

#### Examples¶ ↑

    Chef::ChefFS::FileSystem.copy_to(FilePattern.new('/cookbooks'),
      chef_fs, local_fs, nil, true) do |message|
      puts message
    end

    # File lib/chef/chef_fs/file_system.rb, line 139
    def self.copy_to(pattern, src_root, dest_root, recurse_depth, options, ui = nil, format_path = nil)
      found_result = false
      error = false
      parallel_do(list_pairs(pattern, src_root, dest_root)) do |src, dest|
        found_result = true
        new_dest_parent = get_or_create_parent(dest, options, ui, format_path)
        child_error = copy_entries(src, dest, new_dest_parent, recurse_depth, options, ui, format_path)
        error ||= child_error
      end
      if !found_result && pattern.exact_path
        ui.error "#{pattern}: No such file or directory on remote or local" if ui
        error = true
      end
      error
    end

list(root, pattern) click to toggle source

Returns a list of all things under (and including) this entry that match the given pattern.

#### Attributes¶ ↑

- `root` - Entry to start listing under

- `pattern` - Chef::ChefFS::FilePattern to match children under

    # File lib/chef/chef_fs/file_system.rb, line 36
    def self.list(root, pattern)
      Lister.new(root, pattern)
    end

list_pairs(pattern, a_root, b_root) click to toggle source

Yield entries for children that are in either `a_root` or `b_root`, with matching pairs matched up.

#### Yields¶ ↑

Yields matching entries in pairs:

    [ a_entry, b_entry ]

#### Example¶ ↑

    Chef::ChefFS::FileSystem.list_pairs(FilePattern.new('**x.txt', a_root, b_root)).each do |a, b|
      ...
    end

    # File lib/chef/chef_fs/file_system.rb, line 170
    def self.list_pairs(pattern, a_root, b_root)
      PairLister.new(pattern, a_root, b_root)
    end

resolve_path(entry, path) click to toggle source

Resolve the given path against the entry, returning the entry at the end of the path.

#### Attributes¶ ↑

- `entry` - the entry to start looking under. Relative paths will be resolved from here.

- `path` - the path to resolve. If it starts with `/`, the path will be resolved starting from `entry.root`.

#### Examples¶ ↑

    Chef::ChefFS::FileSystem.resolve_path(root_path, 'cookbooks/java/recipes/default.rb')

    # File lib/chef/chef_fs/file_system.rb, line 96
    def self.resolve_path(entry, path)
      return entry if path.length == 0
      return resolve_path(entry.root, path) if path[0,1] == "/" && entry.root != entry
      if path[0,1] == "/"
        path = path[1,path.length-1]
      end

      result = entry
      Chef::ChefFS::PathUtils::split(path).each do |part|
        result = result.child(part)
      end
      result
    end