]> git.donarmstrong.com Git - dsa-puppet.git/blob - 3rdparty/modules/stdlib/lib/puppet/parser/functions/validate_absolute_path.rb
upgrade to stdlib 4.6.1
[dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / validate_absolute_path.rb
1 module Puppet::Parser::Functions
2   newfunction(:validate_absolute_path, :doc => <<-'ENDHEREDOC') do |args|
3     Validate the string represents an absolute path in the filesystem.  This function works
4     for windows and unix style paths.
5
6     The following values will pass:
7
8         $my_path = 'C:/Program Files (x86)/Puppet Labs/Puppet'
9         validate_absolute_path($my_path)
10         $my_path2 = '/var/lib/puppet'
11         validate_absolute_path($my_path2)
12         $my_path3 = ['C:/Program Files (x86)/Puppet Labs/Puppet','C:/Program Files/Puppet Labs/Puppet']
13         validate_absolute_path($my_path3)
14         $my_path4 = ['/var/lib/puppet','/usr/share/puppet']
15         validate_absolute_path($my_path4)
16
17     The following values will fail, causing compilation to abort:
18
19         validate_absolute_path(true)
20         validate_absolute_path('../var/lib/puppet')
21         validate_absolute_path('var/lib/puppet')
22         validate_absolute_path([ 'var/lib/puppet', '/var/foo' ])
23         validate_absolute_path([ '/var/lib/puppet', 'var/foo' ])
24         $undefined = undef
25         validate_absolute_path($undefined)
26
27     ENDHEREDOC
28
29     require 'puppet/util'
30
31     unless args.length > 0 then
32       raise Puppet::ParseError, ("validate_absolute_path(): wrong number of arguments (#{args.length}; must be > 0)")
33     end
34
35     args.each do |arg|
36       # put arg to candidate var to be able to replace it
37       candidates = arg
38       # if arg is just a string with a path to test, convert it to an array
39       # to avoid test code duplication
40       unless arg.is_a?(Array) then
41         candidates = Array.new(1,arg)
42       end
43       # iterate over all pathes within the candidates array
44       candidates.each do |path|
45         # This logic was borrowed from
46         # [lib/puppet/file_serving/base.rb](https://github.com/puppetlabs/puppet/blob/master/lib/puppet/file_serving/base.rb)
47         # Puppet 2.7 and beyond will have Puppet::Util.absolute_path? Fall back to a back-ported implementation otherwise.
48         if Puppet::Util.respond_to?(:absolute_path?) then
49           unless Puppet::Util.absolute_path?(path, :posix) or Puppet::Util.absolute_path?(path, :windows)
50             raise Puppet::ParseError, ("#{path.inspect} is not an absolute path.")
51           end
52         else
53           # This code back-ported from 2.7.x's lib/puppet/util.rb Puppet::Util.absolute_path?
54           # Determine in a platform-specific way whether a path is absolute. This
55           # defaults to the local platform if none is specified.
56           # Escape once for the string literal, and once for the regex.
57           slash = '[\\\\/]'
58           name = '[^\\\\/]+'
59           regexes = {
60             :windows => %r!^(([A-Z]:#{slash})|(#{slash}#{slash}#{name}#{slash}#{name})|(#{slash}#{slash}\?#{slash}#{name}))!i,
61             :posix => %r!^/!,
62           }
63           rval = (!!(path =~ regexes[:posix])) || (!!(path =~ regexes[:windows]))
64           rval or raise Puppet::ParseError, ("#{path.inspect} is not an absolute path.")
65         end
66       end
67     end
68   end
69 end