-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpathhelp.pm
185 lines (161 loc) · 5.87 KB
/
pathhelp.pm
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
###########################################################################
# _ _ ____ _
# Project ___| | | | _ \| |
# / __| | | | |_) | |
# | (__| |_| | _ <| |___
# \___|\___/|_| \_\_____|
#
# Copyright (C) Evgeny Grin (Karlson2k), <[email protected]>.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
# are also available at https://curl.se/docs/copyright.html.
#
# You may opt to use, copy, modify, merge, publish, distribute and/or sell
# copies of the Software, and permit persons to whom the Software is
# furnished to do so, under the terms of the COPYING file.
#
# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
# KIND, either express or implied.
#
# SPDX-License-Identifier: curl
#
###########################################################################
# This Perl package helps with path transforming when running curl tests on
# native Windows and MSYS/Cygwin.
# Following input formats are supported (via built-in Perl functions):
# (1) /some/path - absolute path in POSIX-style
# (2) D:/some/path - absolute path in Windows-style
# (3) some/path - relative path
# (4) D:some/path - path relative to current directory on Windows drive
# (paths like 'D:' are treated as 'D:./') (*)
# (5) \some/path - path from root directory on current Windows drive (*)
# All forward '/' and back '\' slashes are treated identically except leading
# slash in forms (1) and (5).
# Forward slashes are simpler processed in Perl, do not require extra escaping
# for shell (unlike back slashes) and accepted by Windows native programs, so
# all functions return paths with only forward slashes.
# All returned paths don't contain any duplicated slashes, only single slashes
# are used as directory separators on output.
# On non-Windows platforms functions acts as transparent wrappers for similar
# Perl's functions or return unmodified string (depending on functionality),
# so all functions can be unconditionally used on all platforms.
#
# (*) CAUTION! Forms (4) and (5) are not recommended to use as they can be
# interpreted incorrectly in Perl and MSYS/Cygwin environment have low
# control on Windows current drive and Windows current path on specific
# drive.
package pathhelp;
use strict;
use warnings;
use Cwd 'abs_path';
BEGIN {
use base qw(Exporter);
our @EXPORT_OK = qw(
os_is_win
exe_ext
sys_native_abs_path
sys_native_current_path
build_sys_abs_path
);
}
#######################################################################
# Block for cached static variables
#
{
# Cached static variable, Perl 5.0-compatible.
my $is_win = $^O eq 'MSWin32'
|| $^O eq 'cygwin'
|| $^O eq 'msys';
# Returns boolean true if OS is any form of Windows.
sub os_is_win {
return $is_win;
}
# Cached static variable, Perl 5.0-compatible.
my $cygdrive_present;
# Returns boolean true if Windows drives mounted with '/cygdrive/' prefix.
sub drives_mounted_on_cygdrive {
return $cygdrive_present if defined $cygdrive_present;
$cygdrive_present = ((-e '/cygdrive/') && (-d '/cygdrive/')) ? 1 : 0;
return $cygdrive_present;
}
}
#######################################################################
# Returns current working directory in Windows format on Windows.
#
sub sys_native_current_path {
return Cwd::getcwd() if !os_is_win();
my $cur_dir;
if($^O eq 'MSWin32') {
$cur_dir = Cwd::getcwd();
}
else {
$cur_dir = Cygwin::posix_to_win_path(Cwd::getcwd());
}
$cur_dir =~ s{[/\\]+}{/}g;
return $cur_dir;
}
#######################################################################
# Converts given path to system native absolute path, i.e. to Windows
# absolute format on Windows platform. Both relative and absolute
# formats are supported for input.
#
sub sys_native_abs_path {
my ($path) = @_;
# Return untouched on non-Windows platforms.
return Cwd::abs_path($path) if !os_is_win();
# Do not process empty path.
return $path if ($path eq '');
my $res;
if($^O eq 'msys' || $^O eq 'cygwin') {
$res = Cygwin::posix_to_win_path(Cwd::abs_path($path));
}
elsif($path =~ m{^/(cygdrive/)?([a-z])/(.*)}) {
$res = uc($2) . ":/" . $3;
}
else {
$res = Cwd::abs_path($path);
}
$res =~ s{[/\\]+}{/}g;
return $res;
}
#######################################################################
# Converts given path to build system format absolute path, i.e. to
# MSYS/Cygwin POSIX-style absolute format on Windows platform. Both
# relative and absolute formats are supported for input.
#
sub build_sys_abs_path {
my ($path) = @_;
# Return untouched on non-Windows platforms.
return Cwd::abs_path($path) if !os_is_win();
my $res;
if($^O eq 'msys' || $^O eq 'cygwin') {
$res = Cygwin::win_to_posix_path($path, 1);
}
else {
$res = Cwd::abs_path($path);
if($res =~ m{^([A-Za-z]):(.*)}) {
$res = "/" . lc($1) . $2;
$res = '/cygdrive' . $res if(drives_mounted_on_cygdrive());
}
}
return $res;
}
#***************************************************************************
# Return file extension for executable files on this operating system
#
sub exe_ext {
my ($component, @arr) = @_;
if ($ENV{'CURL_TEST_EXE_EXT'}) {
return $ENV{'CURL_TEST_EXE_EXT'};
}
if ($ENV{'CURL_TEST_EXE_EXT_'.$component}) {
return $ENV{'CURL_TEST_EXE_EXT_'.$component};
}
if ($^O eq 'MSWin32' || $^O eq 'cygwin' || $^O eq 'msys' ||
$^O eq 'dos' || $^O eq 'os2') {
return '.exe';
}
return '';
}
1; # End of module