summaryrefslogtreecommitdiff
path: root/lib/chef/util
diff options
context:
space:
mode:
authorJohn Keiser <jkeiser@opscode.com>2014-08-27 21:06:59 -0700
committerAdam Edwards <adamed@opscode.com>2014-08-30 12:31:09 -0700
commitbc3d6871086721d2049bd2e28e8801d0eed176ff (patch)
tree48b608e6bd893e3697350433c7efd1aacb9cfca8 /lib/chef/util
parent482f8bb073ddd322017b9366fb8aac2301e56e3a (diff)
downloadchef-bc3d6871086721d2049bd2e28e8801d0eed176ff.tar.gz
Move Chef::Config path functions to Chef::Util::PathHelper
Diffstat (limited to 'lib/chef/util')
-rw-r--r--lib/chef/util/path_helper.rb60
1 files changed, 54 insertions, 6 deletions
diff --git a/lib/chef/util/path_helper.rb b/lib/chef/util/path_helper.rb
index 534a9087ae..1877667b9a 100644
--- a/lib/chef/util/path_helper.rb
+++ b/lib/chef/util/path_helper.rb
@@ -16,15 +16,50 @@
# limitations under the License.
#
-require 'chef/platform'
-require 'chef/exceptions'
-
class Chef
class Util
class PathHelper
# Maximum characters in a standard Windows path (260 including drive letter and NUL)
WIN_MAX_PATH = 259
+ def self.dirname(path)
+ if Chef::Platform.windows?
+ # Find the first slash, not counting trailing slashes
+ end_slash = path.size
+ while true
+ slash = path.rindex(/[#{Regexp.escape(File::SEPARATOR)}#{Regexp.escape(path_separator)}]/, end_slash - 1)
+ if !slash
+ return end_slash == path.size ? '.' : path_separator
+ elsif slash == end_slash - 1
+ end_slash = slash
+ else
+ return path[0..slash-1]
+ end
+ end
+ else
+ ::File.dirname(path)
+ end
+ end
+
+ BACKSLASH = '\\'.freeze
+
+ def self.path_separator
+ if Chef::Platform.windows?
+ File::ALT_SEPARATOR || BACKSLASH
+ else
+ File::SEPARATOR
+ end
+ end
+
+ def self.join(*args)
+ args.flatten.inject do |joined_path, component|
+ unless [ File::SEPARATOR, path_separator ].include?(joined_path[-1])
+ joined_path += path_separator
+ end
+ joined_path += component
+ end
+ end
+
def self.validate_path(path)
if Chef::Platform.windows?
unless printable?(path)
@@ -32,7 +67,7 @@ class Chef
Chef::Log.error(msg)
raise Chef::Exceptions::ValidationFailed, msg
end
-
+
if windows_max_length_exceeded?(path)
Chef::Log.debug("Path '#{path}' is longer than #{WIN_MAX_PATH}, prefixing with'\\\\?\\'")
path.insert(0, "\\\\?\\")
@@ -50,7 +85,7 @@ class Chef
return true
end
end
-
+
false
end
@@ -75,7 +110,7 @@ class Chef
if Chef::Platform.windows?
# Add the \\?\ API prefix on Windows unless add_prefix is false
# Downcase on Windows where paths are still case-insensitive
- abs_path.gsub!(::File::SEPARATOR, ::File::ALT_SEPARATOR)
+ abs_path.gsub!(::File::SEPARATOR, path_separator)
if add_prefix && abs_path !~ /^\\\\?\\/
abs_path.insert(0, "\\\\?\\")
end
@@ -86,9 +121,22 @@ class Chef
abs_path
end
+ def self.cleanpath(path)
+ path = Pathname.new(path).cleanpath.to_s
+ # ensure all forward slashes are backslashes
+ if Chef::Platform.windows?
+ path = path.gsub(File::SEPARATOR, path_separator)
+ end
+ path
+ end
+
def self.paths_eql?(path1, path2)
canonical_path(path1) == canonical_path(path2)
end
end
end
end
+
+# Break a require loop when require chef/util/path_helper
+require 'chef/platform'
+require 'chef/exceptions'