1 | module Puppet::Parser::Functions
|
---|
2 | newfunction(:is_absolute_path, :type => :rvalue, :arity => 1, :doc => <<-'ENDHEREDOC') do |args|
|
---|
3 | Returns boolean true if 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 return true:
|
---|
7 |
|
---|
8 | $my_path = 'C:/Program Files (x86)/Puppet Labs/Puppet'
|
---|
9 | is_absolute_path($my_path)
|
---|
10 | $my_path2 = '/var/lib/puppet'
|
---|
11 | is_absolute_path($my_path2)
|
---|
12 | $my_path3 = ['C:/Program Files (x86)/Puppet Labs/Puppet']
|
---|
13 | is_absolute_path($my_path3)
|
---|
14 | $my_path4 = ['/var/lib/puppet']
|
---|
15 | is_absolute_path($my_path4)
|
---|
16 |
|
---|
17 | The following values will return false:
|
---|
18 |
|
---|
19 | is_absolute_path(true)
|
---|
20 | is_absolute_path('../var/lib/puppet')
|
---|
21 | is_absolute_path('var/lib/puppet')
|
---|
22 | $undefined = undef
|
---|
23 | is_absolute_path($undefined)
|
---|
24 |
|
---|
25 | ENDHEREDOC
|
---|
26 | function_deprecation([:is_absolute_path, 'This method is deprecated, please use the stdlib validate_legacy function, with Stdlib::Compat::Absolute_path. There is further documentation for validate_legacy function in the README.'])
|
---|
27 | require 'puppet/util'
|
---|
28 |
|
---|
29 | path = args[0]
|
---|
30 | # This logic was borrowed from
|
---|
31 | # [lib/puppet/file_serving/base.rb](https://github.com/puppetlabs/puppet/blob/master/lib/puppet/file_serving/base.rb)
|
---|
32 | # Puppet 2.7 and beyond will have Puppet::Util.absolute_path? Fall back to a back-ported implementation otherwise.
|
---|
33 | if Puppet::Util.respond_to?(:absolute_path?) then
|
---|
34 | value = (Puppet::Util.absolute_path?(path, :posix) or Puppet::Util.absolute_path?(path, :windows))
|
---|
35 | else
|
---|
36 | # This code back-ported from 2.7.x's lib/puppet/util.rb Puppet::Util.absolute_path?
|
---|
37 | # Determine in a platform-specific way whether a path is absolute. This
|
---|
38 | # defaults to the local platform if none is specified.
|
---|
39 | # Escape once for the string literal, and once for the regex.
|
---|
40 | slash = '[\\\\/]'
|
---|
41 | name = '[^\\\\/]+'
|
---|
42 | regexes = {
|
---|
43 | :windows => %r!^(([A-Z]:#{slash})|(#{slash}#{slash}#{name}#{slash}#{name})|(#{slash}#{slash}\?#{slash}#{name}))!i,
|
---|
44 | :posix => %r!^/!
|
---|
45 | }
|
---|
46 | value = (!!(path =~ regexes[:posix])) || (!!(path =~ regexes[:windows]))
|
---|
47 | end
|
---|
48 | value
|
---|
49 | end
|
---|
50 | end
|
---|