2
0

pathhelp.pm 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  1. ###########################################################################
  2. # _ _ ____ _
  3. # Project ___| | | | _ \| |
  4. # / __| | | | |_) | |
  5. # | (__| |_| | _ <| |___
  6. # \___|\___/|_| \_\_____|
  7. #
  8. # Copyright (C) 2016 - 2021, Evgeny Grin (Karlson2k), <k2k@narod.ru>.
  9. #
  10. # This software is licensed as described in the file COPYING, which
  11. # you should have received as part of this distribution. The terms
  12. # are also available at https://curl.se/docs/copyright.html.
  13. #
  14. # You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. # copies of the Software, and permit persons to whom the Software is
  16. # furnished to do so, under the terms of the COPYING file.
  17. #
  18. # This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. # KIND, either express or implied.
  20. #
  21. ###########################################################################
  22. # This Perl package helps with path transforming when running curl tests on
  23. # Win32 platform with Msys or Cygwin.
  24. # Three main functions 'sys_native_abs_path', 'sys_native_path' and
  25. # 'build_sys_abs_path' autodetect format of given pathnames. Following formats
  26. # are supported:
  27. # (1) /some/path - absolute path in Unix-style
  28. # (2) D:/some/path - absolute path in Win32-style
  29. # (3) some/path - relative path
  30. # (4) D:some/path - path relative to current directory on Win32 drive (paths
  31. # like 'D:' are treated as 'D:./') (*)
  32. # (5) \some/path - path from root directory on current Win32 drive (*)
  33. # All forward '/' and back '\' slashes are treated identically except leading
  34. # slash in forms (1) and (5).
  35. # Forward slashes are simpler processed in Perl, do not require extra escaping
  36. # for shell (unlike back slashes) and accepted by Win32 native programs, so
  37. # all functions return paths with only forward slashes except
  38. # 'sys_native_path' which returns paths with first forward slash for form (5).
  39. # All returned paths don't contain any duplicated slashes, only single slashes
  40. # are used as directory separators on output.
  41. # On non-Windows platforms functions acts as transparent wrappers for similar
  42. # Perl's functions or return unmodified string (depending on functionality),
  43. # so all functions can be unconditionally used on all platforms.
  44. #
  45. # (*) CAUTION! Forms (4) and (5) are not recommended to use as they can be
  46. # interpreted incorrectly in Perl and Msys/Cygwin environment have low
  47. # control on Win32 current drive and Win32 current path on specific drive.
  48. package pathhelp;
  49. use strict;
  50. use warnings;
  51. use Cwd 'abs_path';
  52. BEGIN {
  53. require Exporter;
  54. our @ISA = qw(Exporter);
  55. our @EXPORT = qw(
  56. sys_native_abs_path
  57. sys_native_path
  58. );
  59. our @EXPORT_OK = qw(
  60. build_sys_abs_path
  61. sys_native_current_path
  62. normalize_path
  63. os_is_win
  64. $use_cygpath
  65. should_use_cygpath
  66. drives_mounted_on_cygdrive
  67. );
  68. }
  69. #######################################################################
  70. # Block for cached static variables
  71. #
  72. {
  73. # Cached static variable, Perl 5.0-compatible.
  74. my $is_win = $^O eq 'MSWin32'
  75. || $^O eq 'cygwin'
  76. || $^O eq 'msys';
  77. # Returns boolean true if OS is any form of Windows.
  78. sub os_is_win {
  79. return $is_win;
  80. }
  81. # Cached static variable, Perl 5.0-compatible.
  82. my $cygdrive_present;
  83. # Returns boolean true if Win32 drives mounted with '/cygdrive/' prefix.
  84. sub drives_mounted_on_cygdrive {
  85. return $cygdrive_present if defined $cygdrive_present;
  86. $cygdrive_present = ((-e '/cygdrive/') && (-d '/cygdrive/')) ? 1 : 0;
  87. return $cygdrive_present;
  88. }
  89. }
  90. our $use_cygpath; # Only for Win32:
  91. # undef - autodetect
  92. # 1 - use cygpath
  93. # 0 - do not use cygpath
  94. # Returns boolean true if 'cygpath' utility should be used for path conversion.
  95. sub should_use_cygpath {
  96. unless (os_is_win()) {
  97. $use_cygpath = 0;
  98. return 0;
  99. }
  100. return $use_cygpath if defined $use_cygpath;
  101. $use_cygpath = (qx{cygpath -u '.\\' 2>/dev/null} eq "./\n" && $? == 0);
  102. return $use_cygpath;
  103. }
  104. #######################################################################
  105. # Performs path "normalization": all slashes converted to forward
  106. # slashes (except leading slash), all duplicated slashes are replaced
  107. # with single slashes, all relative directories ('./' and '../') are
  108. # resolved if possible.
  109. # Path processed as string, directories are not checked for presence so
  110. # path for not yet existing directory can be "normalized".
  111. #
  112. sub normalize_path;
  113. #######################################################################
  114. # Returns current working directory in Win32 format on Windows.
  115. #
  116. sub sys_native_current_path {
  117. return Cwd::getcwd() unless os_is_win();
  118. my $cur_dir;
  119. if($^O eq 'msys') {
  120. # MSys shell has built-in command.
  121. chomp($cur_dir = `bash -c 'pwd -W'`);
  122. if($? != 0) {
  123. warn "Can't determine Win32 current directory.\n";
  124. return undef;
  125. }
  126. # Add final slash if required.
  127. $cur_dir .= '/' if length($cur_dir) > 3;
  128. }
  129. else {
  130. # Do not use 'cygpath' - it falsely succeed on paths like '/cygdrive'.
  131. $cur_dir = `cmd "/c;" echo %__CD__%`;
  132. if($? != 0 || substr($cur_dir, 0, 1) eq '%') {
  133. warn "Can't determine Win32 current directory.\n";
  134. return undef;
  135. }
  136. # Remove both '\r' and '\n'.
  137. $cur_dir =~ s{\n|\r}{}g;
  138. # Replace back slashes with forward slashes.
  139. $cur_dir =~ s{\\}{/}g;
  140. }
  141. return $cur_dir;
  142. }
  143. #######################################################################
  144. # Returns Win32 current drive letter with colon.
  145. #
  146. sub get_win32_current_drive {
  147. # Notice parameter "/c;" - it's required to turn off Msys's
  148. # transformation of '/c' and compatible with Cygwin.
  149. my $drive_letter = `cmd "/c;" echo %__CD__:~0,2%`;
  150. if($? != 0 || substr($drive_letter, 1, 1) ne ':') {
  151. warn "Can't determine current Win32 drive letter.\n";
  152. return undef;
  153. }
  154. return substr($drive_letter, 0, 2);
  155. }
  156. # Internal function. Converts path by using Msys's built-in transformation.
  157. # Returned path may contain duplicated and back slashes.
  158. sub do_msys_transform;
  159. # Internal function. Gets two parameters: first parameter must be single
  160. # drive letter ('c'), second optional parameter is path relative to drive's
  161. # current working directory. Returns Win32 absolute normalized path.
  162. sub get_abs_path_on_win32_drive;
  163. # Internal function. Tries to find or guess Win32 version of given
  164. # absolute Unix-style path. Other types of paths are not supported.
  165. # Returned paths contain only single forward slashes (no back and
  166. # duplicated slashes).
  167. # Last resort. Used only when other transformations are not available.
  168. sub do_dumb_guessed_transform;
  169. #######################################################################
  170. # Converts given path to system native format, i.e. to Win32 format on
  171. # Windows platform. Relative paths converted to relative, absolute
  172. # paths converted to absolute.
  173. #
  174. sub sys_native_path {
  175. my ($path) = @_;
  176. # Return untouched on non-Windows platforms.
  177. return $path unless (os_is_win());
  178. # Do not process empty path.
  179. return $path if ($path eq '');
  180. if($path =~ s{^([a-zA-Z]):$}{\u$1:}) {
  181. # Path is single drive with colon. (C:)
  182. # This type of paths is not processed correctly by 'cygpath'.
  183. # WARNING!
  184. # Be careful, this relative path can be accidentally transformed
  185. # into wrong absolute path by adding to it some '/dirname' with
  186. # slash at font.
  187. return $path;
  188. }
  189. elsif($path =~ m{^\\} || $path =~ m{^[a-zA-Z]:[^/\\]}) {
  190. # Path is a directory or filename on Win32 current drive or relative
  191. # path on current directory on specific Win32 drive.
  192. # ('\path' or 'D:path')
  193. # First type of paths is not processed by Msys transformation and
  194. # resolved to absolute path by 'cygpath'.
  195. # Second type is not processed by Msys transformation and may be
  196. # incorrectly processed by 'cygpath' (for paths like 'D:..\../.\')
  197. my $first_char = ucfirst(substr($path, 0, 1));
  198. # Replace any back and duplicated slashes with single forward slashes.
  199. $path =~ s{[\\/]+}{/}g;
  200. # Convert leading slash back to forward slash to indicate
  201. # directory on Win32 current drive or capitalize drive letter.
  202. substr($path, 0, 1) = $first_char;
  203. return $path;
  204. }
  205. elsif(should_use_cygpath()) {
  206. # 'cygpath' is available - use it.
  207. # Remove leading duplicated forward and back slashes, as they may
  208. # prevent transforming and may be not processed.
  209. $path =~ s{^([\\/])[\\/]+}{$1}g;
  210. my $has_final_slash = ($path =~ m{[/\\]$});
  211. # Use 'cygpath', '-m' means Win32 path with forward slashes.
  212. chomp($path = `cygpath -m '$path'`);
  213. if ($? != 0) {
  214. warn "Can't convert path by \"cygpath\".\n";
  215. return undef;
  216. }
  217. # 'cygpath' may remove last slash for existing directories.
  218. $path .= '/' if($has_final_slash);
  219. # Remove any duplicated forward slashes (added by 'cygpath' for root
  220. # directories)
  221. $path =~ s{//+}{/}g;
  222. return $path;
  223. }
  224. elsif($^O eq 'msys') {
  225. # Msys transforms automatically path to Windows native form in staring
  226. # program parameters if program is not Msys-based.
  227. $path = do_msys_transform($path);
  228. return undef unless defined $path;
  229. # Capitalize drive letter for Win32 paths.
  230. $path =~ s{^([a-z]:)}{\u$1};
  231. # Replace any back and duplicated slashes with single forward slashes.
  232. $path =~ s{[\\/]+}{/}g;
  233. return $path;
  234. }
  235. elsif($path =~ s{^([a-zA-Z]):[/\\]}{\u$1:/}) {
  236. # Path is already in Win32 form. ('C:\path')
  237. # Replace any back and duplicated slashes with single forward slashes.
  238. $path =~ s{[\\/]+}{/}g;
  239. return $path;
  240. }
  241. elsif($path !~ m{^/}) {
  242. # Path is in relative form. ('path/name', './path' or '../path')
  243. # Replace any back and duplicated slashes with single forward slashes.
  244. $path =~ s{[\\/]+}{/}g;
  245. return $path;
  246. }
  247. # OS is Windows, but not Msys, path is absolute, path is not in Win32
  248. # form and 'cygpath' is not available.
  249. return do_dumb_guessed_transform($path);
  250. }
  251. #######################################################################
  252. # Converts given path to system native absolute path, i.e. to Win32
  253. # absolute format on Windows platform. Both relative and absolute
  254. # formats are supported for input.
  255. #
  256. sub sys_native_abs_path {
  257. my ($path) = @_;
  258. unless(os_is_win()) {
  259. # Convert path to absolute form.
  260. $path = Cwd::abs_path($path);
  261. # Do not process further on non-Windows platforms.
  262. return $path;
  263. }
  264. if($path =~ m{^([a-zA-Z]):($|[^/\\].*$)}) {
  265. # Path is single drive with colon or relative path on Win32 drive.
  266. # ('C:' or 'C:path')
  267. # This kind of relative path is not processed correctly by 'cygpath'.
  268. # Get specified drive letter
  269. return get_abs_path_on_win32_drive($1, $2);
  270. }
  271. elsif($path eq '') {
  272. # Path is empty string. Return current directory.
  273. # Empty string processed correctly by 'cygpath'.
  274. return sys_native_current_path();
  275. }
  276. elsif(should_use_cygpath()) {
  277. # 'cygpath' is available - use it.
  278. my $has_final_slash = ($path =~ m{[\\/]$});
  279. # Remove leading duplicated forward and back slashes, as they may
  280. # prevent transforming and may be not processed.
  281. $path =~ s{^([\\/])[\\/]+}{$1}g;
  282. print "Inter result: \"$path\"\n";
  283. # Use 'cygpath', '-m' means Win32 path with forward slashes,
  284. # '-a' means absolute path
  285. chomp($path = `cygpath -m -a '$path'`);
  286. if($? != 0) {
  287. warn "Can't resolve path by usung \"cygpath\".\n";
  288. return undef;
  289. }
  290. # 'cygpath' may remove last slash for existing directories.
  291. $path .= '/' if($has_final_slash);
  292. # Remove any duplicated forward slashes (added by 'cygpath' for root
  293. # directories)
  294. $path =~ s{//+}{/}g;
  295. return $path
  296. }
  297. elsif($path =~ s{^([a-zA-Z]):[/\\]}{\u$1:/}) {
  298. # Path is already in Win32 form. ('C:\path')
  299. # Replace any possible back slashes with forward slashes,
  300. # remove any duplicated slashes, resolve relative dirs.
  301. return normalize_path($path);
  302. }
  303. elsif(substr($path, 0, 1) eq '\\' ) {
  304. # Path is directory or filename on Win32 current drive. ('\Windows')
  305. my $w32drive = get_win32_current_drive();
  306. return undef unless defined $w32drive;
  307. # Combine drive and path.
  308. # Replace any possible back slashes with forward slashes,
  309. # remove any duplicated slashes, resolve relative dirs.
  310. return normalize_path($w32drive . $path);
  311. }
  312. unless (substr($path, 0, 1) eq '/') {
  313. # Path is in relative form. Resolve relative directories in Unix form
  314. # *BEFORE* converting to Win32 form otherwise paths like
  315. # '../../../cygdrive/c/windows' will not be resolved.
  316. my $cur_dir;
  317. # MSys shell has built-in command.
  318. if($^O eq 'msys') {
  319. $cur_dir = `bash -c 'pwd -L'`;
  320. }
  321. else {
  322. $cur_dir = `pwd -L`;
  323. }
  324. if($? != 0) {
  325. warn "Can't determine current working directory.\n";
  326. return undef;
  327. }
  328. chomp($cur_dir);
  329. $path = $cur_dir . '/' . $path;
  330. }
  331. # Resolve relative dirs.
  332. $path = normalize_path($path);
  333. return undef unless defined $path;
  334. if($^O eq 'msys') {
  335. # Msys transforms automatically path to Windows native form in staring
  336. # program parameters if program is not Msys-based.
  337. $path = do_msys_transform($path);
  338. return undef unless defined $path;
  339. # Replace any back and duplicated slashes with single forward slashes.
  340. $path =~ s{[\\/]+}{/}g;
  341. return $path;
  342. }
  343. # OS is Windows, but not Msys, path is absolute, path is not in Win32
  344. # form and 'cygpath' is not available.
  345. return do_dumb_guessed_transform($path);
  346. }
  347. # Internal function. Converts given Unix-style absolute path to Win32 format.
  348. sub simple_transform_win32_to_unix;
  349. #######################################################################
  350. # Converts given path to build system format absolute path, i.e. to
  351. # Msys/Cygwin Unix-style absolute format on Windows platform. Both
  352. # relative and absolute formats are supported for input.
  353. #
  354. sub build_sys_abs_path {
  355. my ($path) = @_;
  356. unless(os_is_win()) {
  357. # Convert path to absolute form.
  358. $path = Cwd::abs_path($path);
  359. # Do not process further on non-Windows platforms.
  360. return $path;
  361. }
  362. if($path =~ m{^([a-zA-Z]):($|[^/\\].*$)}) {
  363. # Path is single drive with colon or relative path on Win32 drive.
  364. # ('C:' or 'C:path')
  365. # This kind of relative path is not processed correctly by 'cygpath'.
  366. # Get specified drive letter
  367. # Resolve relative dirs in Win32-style path or paths like 'D:/../c/'
  368. # will be resolved incorrectly.
  369. # Replace any possible back slashes with forward slashes,
  370. # remove any duplicated slashes.
  371. $path = get_abs_path_on_win32_drive($1, $2);
  372. return undef unless defined $path;
  373. return simple_transform_win32_to_unix($path);
  374. }
  375. elsif($path eq '') {
  376. # Path is empty string. Return current directory.
  377. # Empty string processed correctly by 'cygpath'.
  378. # MSys shell has built-in command.
  379. if($^O eq 'msys') {
  380. chomp($path = `bash -c 'pwd -L'`);
  381. }
  382. else {
  383. chomp($path = `pwd -L`);
  384. }
  385. if($? != 0) {
  386. warn "Can't determine Unix-style current working directory.\n";
  387. return undef;
  388. }
  389. # Add final slash if not at root dir.
  390. $path .= '/' if length($path) > 2;
  391. return $path;
  392. }
  393. elsif(should_use_cygpath()) {
  394. # 'cygpath' is available - use it.
  395. my $has_final_slash = ($path =~ m{[\\/]$});
  396. # Resolve relative directories, as they may be not resolved for
  397. # Unix-style paths.
  398. # Remove duplicated slashes, as they may be not processed.
  399. $path = normalize_path($path);
  400. return undef unless defined $path;
  401. # Use 'cygpath', '-u' means Unix-stile path,
  402. # '-a' means absolute path
  403. chomp($path = `cygpath -u -a '$path'`);
  404. if($? != 0) {
  405. warn "Can't resolve path by usung \"cygpath\".\n";
  406. return undef;
  407. }
  408. # 'cygpath' removes last slash if path is root dir on Win32 drive.
  409. # Restore it.
  410. $path .= '/' if($has_final_slash &&
  411. substr($path, length($path) - 1, 1) ne '/');
  412. return $path
  413. }
  414. elsif($path =~ m{^[a-zA-Z]:[/\\]}) {
  415. # Path is already in Win32 form. ('C:\path')
  416. # Resolve relative dirs in Win32-style path otherwise paths
  417. # like 'D:/../c/' will be resolved incorrectly.
  418. # Replace any possible back slashes with forward slashes,
  419. # remove any duplicated slashes.
  420. $path = normalize_path($path);
  421. return undef unless defined $path;
  422. return simple_transform_win32_to_unix($path);
  423. }
  424. elsif(substr($path, 0, 1) eq '\\') {
  425. # Path is directory or filename on Win32 current drive. ('\Windows')
  426. my $w32drive = get_win32_current_drive();
  427. return undef unless defined $w32drive;
  428. # Combine drive and path.
  429. # Resolve relative dirs in Win32-style path or paths like 'D:/../c/'
  430. # will be resolved incorrectly.
  431. # Replace any possible back slashes with forward slashes,
  432. # remove any duplicated slashes.
  433. $path = normalize_path($w32drive . $path);
  434. return undef unless defined $path;
  435. return simple_transform_win32_to_unix($path);
  436. }
  437. # Path is not in any Win32 form.
  438. unless (substr($path, 0, 1) eq '/') {
  439. # Path in relative form. Resolve relative directories in Unix form
  440. # *BEFORE* converting to Win32 form otherwise paths like
  441. # '../../../cygdrive/c/windows' will not be resolved.
  442. my $cur_dir;
  443. # MSys shell has built-in command.
  444. if($^O eq 'msys') {
  445. $cur_dir = `bash -c 'pwd -L'`;
  446. }
  447. else {
  448. $cur_dir = `pwd -L`;
  449. }
  450. if($? != 0) {
  451. warn "Can't determine current working directory.\n";
  452. return undef;
  453. }
  454. chomp($cur_dir);
  455. $path = $cur_dir . '/' . $path;
  456. }
  457. return normalize_path($path);
  458. }
  459. #######################################################################
  460. # Performs path "normalization": all slashes converted to forward
  461. # slashes (except leading slash), all duplicated slashes are replaced
  462. # with single slashes, all relative directories ('./' and '../') are
  463. # resolved if possible.
  464. # Path processed as string, directories are not checked for presence so
  465. # path for not yet existing directory can be "normalized".
  466. #
  467. sub normalize_path {
  468. my ($path) = @_;
  469. # Don't process empty paths.
  470. return $path if $path eq '';
  471. unless($path =~ m{(?:^|\\|/)\.{1,2}(?:\\|/|$)}) {
  472. # Speed up processing of simple paths.
  473. my $first_char = substr($path, 0, 1);
  474. $path =~ s{[\\/]+}{/}g;
  475. # Restore starting backslash if any.
  476. substr($path, 0, 1) = $first_char;
  477. return $path;
  478. }
  479. my @arr;
  480. my $prefix;
  481. my $have_root = 0;
  482. # Check whether path starts from Win32 drive. ('C:path' or 'C:\path')
  483. if($path =~ m{^([a-zA-Z]:(/|\\)?)(.*$)}) {
  484. $prefix = $1;
  485. $have_root = 1 if defined $2;
  486. # Process path separately from drive letter.
  487. @arr = split(m{\/|\\}, $3);
  488. # Replace backslash with forward slash if required.
  489. substr($prefix, 2, 1) = '/' if $have_root;
  490. }
  491. else {
  492. if($path =~ m{^(\/|\\)}) {
  493. $have_root = 1;
  494. $prefix = $1;
  495. }
  496. else {
  497. $prefix = '';
  498. }
  499. @arr = split(m{\/|\\}, $path);
  500. }
  501. my $p = 0;
  502. my @res;
  503. for my $el (@arr) {
  504. if(length($el) == 0 || $el eq '.') {
  505. next;
  506. }
  507. elsif($el eq '..' && @res > 0 && $res[$#res] ne '..') {
  508. pop @res;
  509. next;
  510. }
  511. push @res, $el;
  512. }
  513. if($have_root && @res > 0 && $res[0] eq '..') {
  514. warn "Error processing path \"$path\": " .
  515. "Parent directory of root directory does not exist!\n";
  516. return undef;
  517. }
  518. my $ret = $prefix . join('/', @res);
  519. $ret .= '/' if($path =~ m{\\$|/$} && scalar @res > 0);
  520. return $ret;
  521. }
  522. # Internal function. Converts path by using Msys's built-in
  523. # transformation.
  524. sub do_msys_transform {
  525. my ($path) = @_;
  526. return undef if $^O ne 'msys';
  527. return $path if $path eq '';
  528. # Remove leading double forward slashes, as they turn off Msys
  529. # transforming.
  530. $path =~ s{^/[/\\]+}{/};
  531. # Msys transforms automatically path to Windows native form in staring
  532. # program parameters if program is not Msys-based.
  533. # Note: already checked that $path is non-empty.
  534. $path = `cmd //c echo '$path'`;
  535. if($? != 0) {
  536. warn "Can't transform path into Win32 form by using Msys" .
  537. "internal transformation.\n";
  538. return undef;
  539. }
  540. # Remove double quotes, they are added for paths with spaces,
  541. # remove both '\r' and '\n'.
  542. $path =~ s{^\"|\"$|\"\r|\n|\r}{}g;
  543. return $path;
  544. }
  545. # Internal function. Gets two parameters: first parameter must be single
  546. # drive letter ('c'), second optional parameter is path relative to drive's
  547. # current working directory. Returns Win32 absolute normalized path.
  548. sub get_abs_path_on_win32_drive {
  549. my ($drv, $rel_path) = @_;
  550. my $res;
  551. # Get current directory on specified drive.
  552. # "/c;" is compatible with both Msys and Cygwin.
  553. my $cur_dir_on_drv = `cmd "/c;" echo %=$drv:%`;
  554. if($? != 0) {
  555. warn "Can't determine Win32 current directory on drive $drv:.\n";
  556. return undef;
  557. }
  558. if($cur_dir_on_drv =~ m{^[%]}) {
  559. # Current directory on drive is not set, default is
  560. # root directory.
  561. $res = ucfirst($drv) . ':/';
  562. }
  563. else {
  564. # Current directory on drive was set.
  565. # Remove both '\r' and '\n'.
  566. $cur_dir_on_drv =~ s{\n|\r}{}g;
  567. # Append relative path part.
  568. $res = $cur_dir_on_drv . '/';
  569. }
  570. $res .= $rel_path if defined $rel_path;
  571. # Replace any possible back slashes with forward slashes,
  572. # remove any duplicated slashes, resolve relative dirs.
  573. return normalize_path($res);
  574. }
  575. # Internal function. Tries to find or guess Win32 version of given
  576. # absolute Unix-style path. Other types of paths are not supported.
  577. # Returned paths contain only single forward slashes (no back and
  578. # duplicated slashes).
  579. # Last resort. Used only when other transformations are not available.
  580. sub do_dumb_guessed_transform {
  581. my ($path) = @_;
  582. # Replace any possible back slashes and duplicated forward slashes
  583. # with single forward slashes.
  584. $path =~ s{[/\\]+}{/}g;
  585. # Empty path is not valid.
  586. return undef if (length($path) == 0);
  587. # RE to find Win32 drive letter
  588. my $drv_ltr_re = drives_mounted_on_cygdrive() ?
  589. qr{^/cygdrive/([a-zA-Z])($|/.*$)} :
  590. qr{^/([a-zA-Z])($|/.*$)};
  591. # Check path whether path is Win32 directly mapped drive and try to
  592. # transform it assuming that drive letter is matched to Win32 drive letter.
  593. if($path =~ m{$drv_ltr_re}) {
  594. return ucfirst($1) . ':/' if(length($2) == 0);
  595. return ucfirst($1) . ':' . $2;
  596. }
  597. # This may be some custom mapped path. ('/mymount/path')
  598. # Must check longest possible path component as subdir can be mapped to
  599. # different directory. For example '/usr/bin/' can be mapped to '/bin/' or
  600. # '/bin/' can be mapped to '/usr/bin/'.
  601. my $check_path = $path;
  602. my $path_tail = '';
  603. do {
  604. if(-d $check_path) {
  605. my $res =
  606. `(cd "$check_path" && cmd /c "echo %__CD__%") 2>/dev/null`;
  607. if($? == 0 && substr($path, 0, 1) ne '%') {
  608. # Remove both '\r' and '\n'.
  609. $res =~ s{\n|\r}{}g;
  610. # Replace all back slashes with forward slashes.
  611. $res =~ s{\\}{/}g;
  612. if(length($path_tail) > 0) {
  613. return $res . $path_tail;
  614. }
  615. else {
  616. $res =~ s{/$}{} unless $check_path =~ m{/$};
  617. return $res;
  618. }
  619. }
  620. }
  621. if($check_path =~ m{(^.*/)([^/]+/*)}) {
  622. $check_path = $1;
  623. $path_tail = $2 . $path_tail;
  624. }
  625. else {
  626. # Shouldn't happens as root '/' directory should always
  627. # be resolvable.
  628. warn "Can't determine Win32 directory for path \"$path\".\n";
  629. return undef;
  630. }
  631. } while(1);
  632. }
  633. # Internal function. Converts given Unix-style absolute path to Win32 format.
  634. sub simple_transform_win32_to_unix {
  635. my ($path) = @_;
  636. if(should_use_cygpath()) {
  637. # 'cygpath' gives precise result.
  638. my $res;
  639. chomp($res = `cygpath -a -u '$path'`);
  640. if($? != 0) {
  641. warn "Can't determine Unix-style directory for Win32 " .
  642. "directory \"$path\".\n";
  643. return undef;
  644. }
  645. # 'cygpath' removes last slash if path is root dir on Win32 drive.
  646. $res .= '/' if(substr($res, length($res) - 1, 1) ne '/' &&
  647. $path =~ m{[/\\]$});
  648. return $res;
  649. }
  650. # 'cygpath' is not available, use guessed transformation.
  651. unless($path =~ s{^([a-zA-Z]):(?:/|\\)}{/\l$1/}) {
  652. warn "Can't determine Unix-style directory for Win32 " .
  653. "directory \"$path\".\n";
  654. return undef;
  655. }
  656. $path = '/cygdrive' . $path if(drives_mounted_on_cygdrive());
  657. return $path;
  658. }
  659. 1; # End of module