2
0

pathhelp.pm 26 KB

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