This file is indexed.

/usr/share/perl5/Rex/Helper/Path.pm is in rex 1.4.1-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#
# (c) Jan Gehring <jan.gehring@gmail.com>
#
# vim: set ts=2 sw=2 tw=0:
# vim: set expandtab:

package Rex::Helper::Path;

use strict;
use warnings;

our $VERSION = '1.4.1'; # VERSION

use Rex::Helper::File::Spec;
use File::Basename qw(dirname);
require Exporter;

use base qw(Exporter);
use vars qw(@EXPORT);
use Cwd 'realpath';

require Rex;
use Rex::Commands;
require Rex::Config;

use Rex::Interface::Exec;

@EXPORT = qw(get_file_path get_tmp_file resolv_path parse_path);

set "path_map", {};

#
# CALL: get_file_path("foo.txt", caller());
# RETURNS: module file
#
sub get_file_path {
  my ( $file_name, $caller_package, $caller_file ) = @_;

  $file_name = resolv_path($file_name);

  my $ends_with_slash = 0;
  if ( $file_name =~ m/\/$/ ) {
    $ends_with_slash = 1;
  }

  my $fix_path = sub {
    my ($path) = @_;
    $path =~ s:^\./::;
    if ($ends_with_slash) {
      if ( $path !~ m/\/$/ ) {
        return "$path/";
      }
    }

    return $path;
  };

  if ( !$caller_package ) {
    ( $caller_package, $caller_file ) = caller();
  }

  # check if a file in $BASE overwrites the module file
  # first get the absolute path to the rexfile

  $::rexfile ||= $0;

  my @path_parts;
  if ( $^O =~ m/^MSWin/ && !Rex::is_ssh() ) {
    @path_parts = split( /\//, $::rexfile );
  }
  else {
    @path_parts = split( /\//, realpath($::rexfile) );
  }
  pop @path_parts;

  my $real_path = join( '/', @path_parts );

  my $map_setting = get("path_map");

  my %path_map = (
    map { ( ( substr( $_, -1 ) eq '/' ) ? $_ : "$_/" ) => $map_setting->{$_} }
      keys %$map_setting
  );

  foreach my $prefix (
    sort { length($b) <=> length($a) }
    grep { $file_name =~ m/^$_/ } keys %path_map
    )
  {
    foreach my $pattern ( @{ $path_map{$prefix} } ) {
      my $expansion =
        Rex::Helper::File::Spec->catfile( parse_path($pattern),
        substr( $file_name, length($prefix) ) );

      if ( -e $expansion ) {
        return $fix_path->($expansion);
      }

      $expansion = Rex::Helper::File::Spec->catfile( $real_path, $expansion );
      if ( -e $expansion ) {
        return $fix_path->($expansion);
      }
    }
  }

  if ( -e $file_name ) {
    return $fix_path->($file_name);
  }

  my $cat_file_name =
    Rex::Helper::File::Spec->catfile( $real_path, $file_name );
  if ( -e $cat_file_name ) {
    return $fix_path->($cat_file_name);
  }

  # walk down the wire to find the file...
  my ($old_caller_file) = $caller_file;
  my $i = 0;
  while ( $caller_package && $i <= 50 ) {
    ( $caller_package, $caller_file ) = caller($i);
    if ( !$caller_package ) {
      last;
    }

    my $module_path = Rex::get_module_path($caller_package);
    $cat_file_name =
      Rex::Helper::File::Spec->catfile( $module_path, $file_name );
    if ( -e $cat_file_name ) {
      return $fix_path->($cat_file_name);
    }

    $i++;
  }

  $file_name =
    Rex::Helper::File::Spec->catfile( dirname($old_caller_file), $file_name );

  return $fix_path->($file_name);
}

sub get_tmp_file {
  return Rex::Helper::File::Spec->join( Rex::Config->get_tmp_dir(),
    Rex::Commands::get_random( 12, 'a' .. 'z' ) . '.tmp' );
}

sub resolv_path {
  my ( $path, $local ) = @_;

  if ( $path !~ m/^~/ ) {

    # path starts not with ~ so we don't need to expand $HOME.
    # just return it.
    return $path;
  }

  my $home_path;
  require Rex::User;
  my $user_o = Rex::User->get;

  if ($local) {
    if ( $^O =~ m/^MSWin/ ) {

      # windows path:
      $home_path = $ENV{'USERPROFILE'};
    }
    else {
      if ( $path =~ m/^~([a-zA-Z0-9_][^\/]+)\// ) {
        my $user_name = $1;
        my %user_info = $user_o->get_user($user_name);
        $home_path = $user_info{home};
        $path =~ s/^~$user_name/$home_path/;
      }
      else {
        $home_path = $ENV{'HOME'};
        $path =~ s/^~/$home_path/;
      }
    }
  }
  else {
    if ( $path =~ m/^~([a-zA-Z0-9_][^\/]+)\// ) {
      my $user_name = $1;
      my %user_info = $user_o->get_user($user_name);
      $home_path = $user_info{home};
      $path =~ s/^~$user_name/$home_path/;
    }
    else {
      my $exec        = Rex::Interface::Exec->create;
      my $remote_home = $exec->exec("echo \$HOME");
      $remote_home =~ s/[\r\n]//gms;
      $home_path = $remote_home;
      $path =~ s/^~/$home_path/;
    }
  }

  return $path;
}

sub parse_path {
  my ($path) = @_;
  my %hw;

  require Rex::Commands::Gather;

  $hw{server}      = Rex::Commands::connection()->server;
  $hw{environment} = Rex::Commands::environment();

  $path =~ s/\{(server|environment)\}/$hw{$1}/gms;

  if ( $path =~ m/\{([^\}]+)\}/ ) {

    # if there are still some variables to replace, we need some information of
    # the system.
    %hw = Rex::Commands::Gather::get_system_information();
    $path =~ s/\{([^\}]+)\}/$hw{$1}/gms;
  }

  return $path;
}

1;