summaryrefslogtreecommitdiff
path: root/lib/chef/chef_fs
diff options
context:
space:
mode:
authorThom May <thom@chef.io>2016-04-13 12:15:43 +0100
committerThom May <thom@chef.io>2016-04-13 18:52:09 +0100
commitbf121d46ad8e73b9927bfe3c9a241ecbe9d13c9f (patch)
tree097a0e96f2eee5a543b6c4024f1bd32cd05a686f /lib/chef/chef_fs
parent681610a324462594054b1a112b121d15525eae70 (diff)
downloadchef-bf121d46ad8e73b9927bfe3c9a241ecbe9d13c9f.tar.gz
ensure directory and file_system_entry are compatible
Diffstat (limited to 'lib/chef/chef_fs')
-rw-r--r--lib/chef/chef_fs/file_system/repository/chef_repository_file_system_entry.rb10
-rw-r--r--lib/chef/chef_fs/file_system/repository/directory.rb17
2 files changed, 25 insertions, 2 deletions
diff --git a/lib/chef/chef_fs/file_system/repository/chef_repository_file_system_entry.rb b/lib/chef/chef_fs/file_system/repository/chef_repository_file_system_entry.rb
index b9d681c116..e490daa584 100644
--- a/lib/chef/chef_fs/file_system/repository/chef_repository_file_system_entry.rb
+++ b/lib/chef/chef_fs/file_system/repository/chef_repository_file_system_entry.rb
@@ -57,12 +57,22 @@ class Chef
!is_dir && name[-5..-1] == ".json"
end
+ def name_valid?
+ !name.start_with?(".")
+ end
+
+ # basic implementation to support Repository::Directory API
+ def fs_entry_valid?
+ name_valid? && File.exist?(file_path)
+ end
+
def write(file_contents)
if file_contents && write_pretty_json && name[-5..-1] == ".json"
file_contents = minimize(file_contents, self)
end
super(file_contents)
end
+ alias :create :write
def minimize(file_contents, entry)
object = Chef::JSONCompat.parse(file_contents)
diff --git a/lib/chef/chef_fs/file_system/repository/directory.rb b/lib/chef/chef_fs/file_system/repository/directory.rb
index 782a134a58..35dd9f2540 100644
--- a/lib/chef/chef_fs/file_system/repository/directory.rb
+++ b/lib/chef/chef_fs/file_system/repository/directory.rb
@@ -66,13 +66,26 @@ class Chef
def children
dir_ls.sort.
map { |child_name| make_child_entry(child_name) }.
- select { |maybe_child| maybe_child.fs_entry_valid? }
+ select { |new_child| new_child.fs_entry_valid? && can_have_child?(new_child.name, new_child.dir?)}
rescue Errno::ENOENT => e
raise Chef::ChefFS::FileSystem::NotFoundError.new(self, e)
end
def create_child(child_name, file_contents = nil)
- make_child_entry(child_name).tap { |c| c.create(file_contents) }
+ child = make_child_entry(child_name)
+ if child.exists?
+ raise Chef::ChefFS::FileSystem::AlreadyExistsError.new(:create_child, child)
+ end
+ if file_contents
+ child.write(file_contents)
+ else
+ begin
+ Dir.mkdir(child.file_path)
+ rescue Errno::EEXIST
+ raise Chef::ChefFS::FileSystem::AlreadyExistsError.new(:create_child, child)
+ end
+ end
+ child
end
# An empty children array is an empty dir