<commit_before>#
# Author:: <NAME> (<<EMAIL>>)
# Copyright:: Copyright 2012-2016, Chef Software, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require "chef/chef_fs/file_system/base_fs_dir"
require "chef/chef_fs/file_system/repository/acls_dir"
require "chef/chef_fs/file_system/repository/clients_dir"
require "chef/chef_fs/file_system/repository/cookbooks_dir"
require "chef/chef_fs/file_system/repository/cookbook_artifacts_dir"
require "chef/chef_fs/file_system/repository/containers_dir"
require "chef/chef_fs/file_system/repository/data_bags_dir"
require "chef/chef_fs/file_system/repository/environments_dir"
require "chef/chef_fs/file_system/repository/groups_dir"
require "chef/chef_fs/file_system/repository/nodes_dir"
require "chef/chef_fs/file_system/repository/policy_groups_dir"
require "chef/chef_fs/file_system/repository/roles_dir"
require "chef/chef_fs/file_system/repository/users_dir"
require "chef/chef_fs/file_system/repository/client_keys_dir"
require "chef/chef_fs/file_system/repository/file_system_entry"
require "chef/chef_fs/file_system/repository/policies_dir"
require "chef/chef_fs/file_system/repository/versioned_cookbooks_dir"
require "chef/chef_fs/file_system/multiplexed_dir"
require "chef/chef_fs/data_handler/client_data_handler"
require "chef/chef_fs/data_handler/client_key_data_handler"
require "chef/chef_fs/data_handler/environment_data_handler"
require "chef/chef_fs/data_handler/node_data_handler"
require "chef/chef_fs/data_handler/policy_data_handler"
require "chef/chef_fs/data_handler/policy_group_data_handler"
require "chef/chef_fs/data_handler/role_data_handler"
require "chef/chef_fs/data_handler/user_data_handler"
require "chef/chef_fs/data_handler/group_data_handler"
require "chef/chef_fs/data_handler/container_data_handler"
require "chef/win32/security" if Chef::Platform.windows?

class Chef
  module ChefFS
    module FileSystem
      module Repository

        #
        # Represents the root of a local Chef repository, with directories for
        # nodes, cookbooks, roles, etc. under it.
        #
        class ChefRepositoryFileSystemRootDir < BaseFSDir
          #
          # Create a new Chef Repository File System root.
          #
          # == Parameters
          # [child_paths]
          #   A hash of child paths, e.g.:
          #     "nodes" => [ '/var/nodes', '/home/jkeiser/nodes' ],
          #     "roles" => [ '/var/roles' ],
          #     ...
          # [root_paths]
          #   An array of paths representing the top level, where
          #   +org.json+, +members.json+, and +invites.json+ will be stored.
          # [chef_config] - a hash of options that looks suspiciously like the ones
          #   stored in Chef::Config, containing at least these keys:
          #   :versioned_cookbooks:: whether to include versions in cookbook names
          def initialize(child_paths, root_paths = [], chef_config = Chef::Config)
            super("", nil)
            @child_paths = child_paths
            @root_paths = root_paths
            @versioned_cookbooks = chef_config[:versioned_cookbooks]
          end

          attr_accessor :write_pretty_json

          attr_reader :root_paths
          attr_reader :child_paths
          attr_reader :versioned_cookbooks

          CHILDREN = %w{org.json invitations.json members.json}.freeze

          def children
            @children ||= begin
                            result = child_paths.keys.sort.map { |name| make_child_entry(name) }
                            result += CHILDREN.map { |name| make_child_entry(name) }
                            result.select { |c| c && c.exists? }.sort_by { |c| c.name }
                          end
          end

          def can_have_child?(name, is_dir)
            if is_dir
              child_paths.key?(name)
            elsif root_dir
              CHILDREN.include?(name)
            else
              false
            end
          end

          def create_child(name, file_contents = nil)
            if file_contents
              child = root_dir.create_child(name, file_contents)
            else
              child_paths[name].each do |path|
                begin
                  Dir.mkdir(path, 0700)
                  if Chef::Platform.windows?
                    all_mask = Chef::ReservedNames::Win32::API::Security::GENERIC_ALL
                    administrators = Chef::ReservedNames::Win32::Security::SID.Administrators
                    owner = Chef::ReservedNames::Win32::Security::SID.default_security_object_owner
                    dacl = Chef::ReservedNames::Win32::Security::ACL.create([
                      Chef::ReservedNames::Win32::Security::ACE.access_allowed(owner, all_mask),
                      Chef::ReservedNames::Win32::Security::ACE.access_allowed(administrators, all_mask),
                    ])
                    so = Chef::ReservedNames::Win32::Security::SecurableObject.new(path)
                    so.owner = owner
                    so.set_dacl(dacl, false)
                  end
                rescue Errno::EEXIST
                end
              end
              child = make_child_entry(name)
            end<commit_msg>Fixes for extra cookbook_path chef-solo   - Create directory recursively instead to assume that parent dir should exist.<commit_after>#
# Author:: <NAME> (<<EMAIL>>)
# Copyright:: Copyright 2012-2016, Chef Software, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require "chef/chef_fs/file_system/base_fs_dir"
require "chef/chef_fs/file_system/repository/acls_dir"
require "chef/chef_fs/file_system/repository/clients_dir"
require "chef/chef_fs/file_system/repository/cookbooks_dir"
require "chef/chef_fs/file_system/repository/cookbook_artifacts_dir"
require "chef/chef_fs/file_system/repository/containers_dir"
require "chef/chef_fs/file_system/repository/data_bags_dir"
require "chef/chef_fs/file_system/repository/environments_dir"
require "chef/chef_fs/file_system/repository/groups_dir"
require "chef/chef_fs/file_system/repository/nodes_dir"
require "chef/chef_fs/file_system/repository/policy_groups_dir"
require "chef/chef_fs/file_system/repository/roles_dir"
require "chef/chef_fs/file_system/repository/users_dir"
require "chef/chef_fs/file_system/repository/client_keys_dir"
require "chef/chef_fs/file_system/repository/file_system_entry"
require "chef/chef_fs/file_system/repository/policies_dir"
require "chef/chef_fs/file_system/repository/versioned_cookbooks_dir"
require "chef/chef_fs/file_system/multiplexed_dir"
require "chef/chef_fs/data_handler/client_data_handler"
require "chef/chef_fs/data_handler/client_key_data_handler"
require "chef/chef_fs/data_handler/environment_data_handler"
require "chef/chef_fs/data_handler/node_data_handler"
require "chef/chef_fs/data_handler/policy_data_handler"
require "chef/chef_fs/data_handler/policy_group_data_handler"
require "chef/chef_fs/data_handler/role_data_handler"
require "chef/chef_fs/data_handler/user_data_handler"
require "chef/chef_fs/data_handler/group_data_handler"
require "chef/chef_fs/data_handler/container_data_handler"
require "chef/win32/security" if Chef::Platform.windows?

class Chef
  module ChefFS
    module FileSystem
      module Repository

        #
        # Represents the root of a local Chef repository, with directories for
        # nodes, cookbooks, roles, etc. under it.
        #
        class ChefRepositoryFileSystemRootDir < BaseFSDir
          #
          # Create a new Chef Repository File System root.
          #
          # == Parameters
          # [child_paths]
          #   A hash of child paths, e.g.:
          #     "nodes" => [ '/var/nodes', '/home/jkeiser/nodes' ],
          #     "roles" => [ '/var/roles' ],
          #     ...
          # [root_paths]
          #   An array of paths representing the top level, where
          #   +org.json+, +members.json+, and +invites.json+ will be stored.
          # [chef_config] - a hash of options that looks suspiciously like the ones
          #   stored in Chef::Config, containing at least these keys:
          #   :versioned_cookbooks:: whether to include versions in cookbook names
          def initialize(child_paths, root_paths = [], chef_config = Chef::Config)
            super("", nil)
            @child_paths = child_paths
            @root_paths = root_paths
            @versioned_cookbooks = chef_config[:versioned_cookbooks]
          end

          attr_accessor :write_pretty_json

          attr_reader :root_paths
          attr_reader :child_paths
          attr_reader :versioned_cookbooks

          CHILDREN = %w{org.json invitations.json members.json}.freeze

          def children
            @children ||= begin
                            result = child_paths.keys.sort.map { |name| make_child_entry(name) }
                            result += CHILDREN.map { |name| make_child_entry(name) }
                            result.select { |c| c && c.exists? }.sort_by { |c| c.name }
                          end
          end

          def can_have_child?(name, is_dir)
            if is_dir
              child_paths.key?(name)
            elsif root_dir
              CHILDREN.include?(name)
            else
              false
            end
          end

          def create_child(name, file_contents = nil)
            if file_contents
              child = root_dir.create_child(name, file_contents)
            else
              child_paths[name].each do |path|
                begin
                  ::FileUtils.mkdir_p(path)
                  ::FileUtils.chmod(0700, path)
                  if Chef::Platform.windows?
                    all_mask = Chef::ReservedNames::Win32::API::Security::GENERIC_ALL
                    administrators = Chef::ReservedNames::Win32::Security::SID.Administrators
                    owner = Chef::ReservedNames::Win32::Security::SID.default_security_object_owner
                    dacl = Chef::ReservedNames::Win32::Security::ACL.create([
                      Chef::ReservedNames::Win32::Security::ACE.access_allowed(owner, all_mask),
                      Chef::ReservedNames::Win32::Security::ACE.access_allowed(administrators, all_mask),
                    ])
                    so = Chef::ReservedNames::Win32::Security::SecurableObject.new(path)
                    so.owner = owner
                    so.set_dacl(dacl, false)
                  end
                rescue Errno::EEXIST
                end
              end
              child = make_child_entry(name)
            end
{'fraction_non_alphanumeric': 0.09049462365591397, 'fraction_numerical': 0.005849462365591398, 'mean_word_length': 2.5838471023427867, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '536745', 'n_tokens_mistral': 3519, 'n_tokens_neox': 3396, 'n_words': 715}