class Knife::Windows::PathHelper

Constants

BACKSLASH
WIN_MAX_PATH

Maximum characters in a standard Windows path (260 including drive letter and NUL)

Public Class Methods

all_homes(*args) { |p| ... } click to toggle source

See self.home. This method performs a similar operation except that it yields all the different possible values of 'HOME' that one could have on this platform. Hence, on windows, if HOMEDRIVEHOMEPATH and USERPROFILE are different, the provided block will be called twice. This method goes out and checks the existence of each location at the time of the call.

The return is a list of all the returned values from each block invocation or a list of paths if no block is provided.

# File lib/knife-winops/path_helper.rb, line 189
def self.all_homes(*args)
  paths = []
  if Chef::Platform.windows?
    # By default, Ruby uses the the following environment variables to determine Dir.home:
    # HOME
    # HOMEDRIVE HOMEPATH
    # USERPROFILE
    # Ruby only checks to see if the variable is specified - not if the directory actually exists.
    # On Windows, HOMEDRIVE HOMEPATH can point to a different location (such as an unavailable network mounted drive)
    # while USERPROFILE points to the location where the user application settings and profile are stored.  HOME
    # is not defined as an environment variable (usually).  If the home path actually uses UNC, then the prefix is
    # HOMESHARE instead of HOMEDRIVE.
    #
    # We instead walk down the following and only include paths that actually exist.
    # HOME
    # HOMEDRIVE HOMEPATH
    # HOMESHARE HOMEPATH
    # USERPROFILE

    paths << ENV['HOME']
    paths << ENV['HOMEDRIVE'] + ENV['HOMEPATH'] if ENV['HOMEDRIVE'] && ENV['HOMEPATH']
    paths << ENV['HOMESHARE'] + ENV['HOMEPATH'] if ENV['HOMESHARE'] && ENV['HOMEPATH']
    paths << ENV['USERPROFILE']
  end
  paths << Dir.home if ENV['HOME']

  # Depending on what environment variables we're using, the slashes can go in any which way.
  # Just change them all to / to keep things consistent.
  # Note: Maybe this is a bad idea on some unixy systems where \ might be a valid character depending on
  # the particular brand of kool-aid you consume.  This code assumes that \ and / are both
  # path separators on any system being used.
  paths = paths.map { |home_path| home_path.gsub(path_separator, ::File::SEPARATOR) if home_path }

  # Filter out duplicate paths and paths that don't exist.
  valid_paths = paths.select { |home_path| home_path && Dir.exists?(home_path) }
  valid_paths = valid_paths.uniq

  # Join all optional path elements at the end.
  # If a block is provided, invoke it - otherwise just return what we've got.
  joined_paths = valid_paths.map { |home_path| File.join(home_path, *args) }
  if block_given?
    joined_paths.each { |p| yield p }
  else
    joined_paths
  end
end
canonical_path(path, add_prefix=true) click to toggle source

Produces a comparable path.

# File lib/knife-winops/path_helper.rb, line 106
def self.canonical_path(path, add_prefix=true)
  # Rather than find an equivalent for File.absolute_path on 1.8.7, just bail out
  raise NotImplementedError, "This feature is not supported on Ruby versions < 1.9" if RUBY_VERSION.to_f < 1.9

  # First remove extra separators and resolve any relative paths
  abs_path = File.absolute_path(path)

  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, path_separator)
    if add_prefix && abs_path !~ /^\\\\?\\/
      abs_path.insert(0, "\\\\?\\")
    end

    abs_path.downcase!
  end

  abs_path
end
cleanpath(path) click to toggle source
# File lib/knife-winops/path_helper.rb, line 127
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
dirname(path) click to toggle source
# File lib/knife-winops/path_helper.rb, line 28
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
escape_glob(*parts) click to toggle source

Note: this method is deprecated. Please use escape_glob_dirs Paths which may contain glob-reserved characters need to be escaped before globbing can be done. stackoverflow.com/questions/14127343

# File lib/knife-winops/path_helper.rb, line 144
def self.escape_glob(*parts)
  path = cleanpath(join(*parts))
  path.gsub(/[\\\{\}\[\]\*\?]/) { |x| "\\"+x }
end
escape_glob_dir(*parts) click to toggle source

This function does not switch to backslashes for windows This is because only forwardslashes should be used with dir (even for windows)

# File lib/knife-winops/path_helper.rb, line 151
def self.escape_glob_dir(*parts)
  path = Pathname.new(join(*parts)).cleanpath.to_s
  path.gsub(/[\\\{\}\[\]\*\?]/) { |x| "\\" + x }
end
home(*args) { |path) : path| ... } click to toggle source

Retrieves the “home directory” of the current user while trying to ascertain the existence of said directory. The path returned uses / for all separators (the ruby standard format). If the home directory doesn't exist or an error is otherwise encountered, nil is returned.

If a set of path elements is provided, they are appended as-is to the home path if the homepath exists.

If an optional block is provided, the joined path is passed to that block if the home path is valid and the result of the block is returned instead.

Home-path discovery is performed once. If a path is discovered, that value is memoized so that subsequent calls to home_dir don't bounce around.

See self.all_homes.

# File lib/knife-winops/path_helper.rb, line 174
def self.home(*args)
  @@home_dir ||= self.all_homes { |p| break p }
  if @@home_dir
    path = File.join(@@home_dir, *args)
    block_given? ? (yield path) : path
  end
end
join(*args) click to toggle source
# File lib/knife-winops/path_helper.rb, line 57
def self.join(*args)
  args.flatten.inject do |joined_path, component|
    # Joined path ends with /
    joined_path = joined_path.sub(/[#{Regexp.escape(File::SEPARATOR)}#{Regexp.escape(path_separator)}]+$/, '')
    component = component.sub(/^[#{Regexp.escape(File::SEPARATOR)}#{Regexp.escape(path_separator)}]+/, '')
    joined_path += "#{path_separator}#{component}"
  end
end
path_separator() click to toggle source
# File lib/knife-winops/path_helper.rb, line 49
def self.path_separator
  if Chef::Platform.windows?
    File::ALT_SEPARATOR || BACKSLASH
  else
    File::SEPARATOR
  end
end
paths_eql?(path1, path2) click to toggle source
# File lib/knife-winops/path_helper.rb, line 136
def self.paths_eql?(path1, path2)
  canonical_path(path1) == canonical_path(path2)
end
printable?(string) click to toggle source
# File lib/knife-winops/path_helper.rb, line 95
def self.printable?(string)
  # returns true if string is free of non-printable characters (escape sequences)
  # this returns false for whitespace escape sequences as well, e.g. \n\t
  if string =~ /[^[:print:]]/
    false
  else
    true
  end
end
relative_path_from(from, to) click to toggle source
# File lib/knife-winops/path_helper.rb, line 156
def self.relative_path_from(from, to)
  pathname = Pathname.new(Chef::Util::PathHelper.cleanpath(to)).relative_path_from(Pathname.new(Chef::Util::PathHelper.cleanpath(from)))
end
validate_path(path) click to toggle source
# File lib/knife-winops/path_helper.rb, line 66
def self.validate_path(path)
  if Chef::Platform.windows?
    unless printable?(path)
      msg = "Path '#{path}' contains non-printable characters. Check that backslashes are escaped with another backslash (e.g. C:\\\\Windows) in double-quoted strings."
      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, "\\\\?\\")
    end
  end

  path
end
windows_max_length_exceeded?(path) click to toggle source
# File lib/knife-winops/path_helper.rb, line 83
def self.windows_max_length_exceeded?(path)
  # Check to see if paths without the \\?\ prefix are over the maximum allowed length for the Windows API
  # http://msdn.microsoft.com/en-us/library/windows/desktop/aa365247%28v=vs.85%29.aspx
  unless path =~ /^\\\\?\\/
    if path.length > WIN_MAX_PATH
      return true
    end
  end

  false
end