2
0

pathhelp.pm 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  1. ###########################################################################
  2. # _ _ ____ _
  3. # Project ___| | | | _ \| |
  4. # / __| | | | |_) | |
  5. # | (__| |_| | _ <| |___
  6. # \___|\___/|_| \_\_____|
  7. #
  8. # Copyright (C) 2016, 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.haxx.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 = `pwd -L`;
  317. if($? != 0) {
  318. warn "Can't determine current working directory.\n";
  319. return undef;
  320. }
  321. chomp($cur_dir);
  322. $path = $cur_dir . '/' . $path;
  323. }
  324. # Resolve relative dirs.
  325. $path = normalize_path($path);
  326. return undef unless defined $path;
  327. if($^O eq 'msys') {
  328. # Msys transforms automatically path to Windows native form in staring
  329. # program parameters if program is not Msys-based.
  330. $path = do_msys_transform($path);
  331. return undef unless defined $path;
  332. # Replace any back and duplicated slashes with single forward slashes.
  333. $path =~ s{[\\/]+}{/}g;
  334. return $path;
  335. }
  336. # OS is Windows, but not Msys, path is absolute, path is not in Win32
  337. # form and 'cygpath' is not available.
  338. return do_dumb_guessed_transform($path);
  339. }
  340. # Internal function. Converts given Unix-style absolute path to Win32 format.
  341. sub simple_transform_win32_to_unix;
  342. #######################################################################
  343. # Converts given path to build system format absolute path, i.e. to
  344. # Msys/Cygwin Unix-style absolute format on Windows platform. Both
  345. # relative and absolute formats are supported for input.
  346. #
  347. sub build_sys_abs_path {
  348. my ($path) = @_;
  349. unless(os_is_win()) {
  350. # Convert path to absolute form.
  351. $path = Cwd::abs_path($path);
  352. # Do not process further on non-Windows platforms.
  353. return $path;
  354. }
  355. if($path =~ m{^([a-zA-Z]):($|[^/\\].*$)}) {
  356. # Path is single drive with colon or relative path on Win32 drive.
  357. # ('C:' or 'C:path')
  358. # This kind of relative path is not processed correctly by 'cygpath'.
  359. # Get specified drive letter
  360. # Resolve relative dirs in Win32-style path or paths like 'D:/../c/'
  361. # will be resolved incorrectly.
  362. # Replace any possible back slashes with forward slashes,
  363. # remove any duplicated slashes.
  364. $path = get_abs_path_on_win32_drive($1, $2);
  365. return undef unless defined $path;
  366. return simple_transform_win32_to_unix($path);
  367. }
  368. elsif($path eq '') {
  369. # Path is empty string. Return current directory.
  370. # Empty string processed correctly by 'cygpath'.
  371. chomp($path = `pwd -L`);
  372. if($? != 0) {
  373. warn "Can't determine Unix-style current working directory.\n";
  374. return undef;
  375. }
  376. # Add final slash if not at root dir.
  377. $path .= '/' if length($path) > 2;
  378. return $path;
  379. }
  380. elsif(should_use_cygpath()) {
  381. # 'cygpath' is available - use it.
  382. my $has_final_slash = ($path =~ m{[\\/]$});
  383. # Resolve relative directories, as they may be not resolved for
  384. # Unix-style paths.
  385. # Remove duplicated slashes, as they may be not processed.
  386. $path = normalize_path($path);
  387. return undef unless defined $path;
  388. # Use 'cygpath', '-u' means Unix-stile path,
  389. # '-a' means absolute path
  390. chomp($path = `cygpath -u -a '$path'`);
  391. if($? != 0) {
  392. warn "Can't resolve path by usung \"cygpath\".\n";
  393. return undef;
  394. }
  395. # 'cygpath' removes last slash if path is root dir on Win32 drive.
  396. # Restore it.
  397. $path .= '/' if($has_final_slash &&
  398. substr($path, length($path) - 1, 1) ne '/');
  399. return $path
  400. }
  401. elsif($path =~ m{^[a-zA-Z]:[/\\]}) {
  402. # Path is already in Win32 form. ('C:\path')
  403. # Resolve relative dirs in Win32-style path otherwise paths
  404. # like 'D:/../c/' will be resolved incorrectly.
  405. # Replace any possible back slashes with forward slashes,
  406. # remove any duplicated slashes.
  407. $path = normalize_path($path);
  408. return undef unless defined $path;
  409. return simple_transform_win32_to_unix($path);
  410. }
  411. elsif(substr($path, 0, 1) eq '\\') {
  412. # Path is directory or filename on Win32 current drive. ('\Windows')
  413. my $w32drive = get_win32_current_drive();
  414. return undef unless defined $w32drive;
  415. # Combine drive and path.
  416. # Resolve relative dirs in Win32-style path or paths like 'D:/../c/'
  417. # will be resolved incorrectly.
  418. # Replace any possible back slashes with forward slashes,
  419. # remove any duplicated slashes.
  420. $path = normalize_path($w32drive . $path);
  421. return undef unless defined $path;
  422. return simple_transform_win32_to_unix($path);
  423. }
  424. # Path is not in any Win32 form.
  425. unless (substr($path, 0, 1) eq '/') {
  426. # Path in relative form. Resolve relative directories in Unix form
  427. # *BEFORE* converting to Win32 form otherwise paths like
  428. # '../../../cygdrive/c/windows' will not be resolved.
  429. my $cur_dir = `pwd -L`;
  430. if($? != 0) {
  431. warn "Can't determine current working directory.\n";
  432. return undef;
  433. }
  434. chomp($cur_dir);
  435. $path = $cur_dir . '/' . $path;
  436. }
  437. return normalize_path($path);
  438. }
  439. #######################################################################
  440. # Performs path "normalization": all slashes converted to forward
  441. # slashes (except leading slash), all duplicated slashes are replaced
  442. # with single slashes, all relative directories ('./' and '../') are
  443. # resolved if possible.
  444. # Path processed as string, directories are not checked for presence so
  445. # path for not yet existing directory can be "normalized".
  446. #
  447. sub normalize_path {
  448. my ($path) = @_;
  449. # Don't process empty paths.
  450. return $path if $path eq '';
  451. unless($path =~ m{(?:^|\\|/)\.{1,2}(?:\\|/|$)}) {
  452. # Speed up processing of simple paths.
  453. my $first_char = substr($path, 0, 1);
  454. $path =~ s{[\\/]+}{/}g;
  455. # Restore starting backslash if any.
  456. substr($path, 0, 1) = $first_char;
  457. return $path;
  458. }
  459. my @arr;
  460. my $prefix;
  461. my $have_root = 0;
  462. # Check whether path starts from Win32 drive. ('C:path' or 'C:\path')
  463. if($path =~ m{^([a-zA-Z]:(/|\\)?)(.*$)}) {
  464. $prefix = $1;
  465. $have_root = 1 if defined $2;
  466. # Process path separately from drive letter.
  467. @arr = split(m{\/|\\}, $3);
  468. # Replace backslash with forward slash if required.
  469. substr($prefix, 2, 1) = '/' if $have_root;
  470. }
  471. else {
  472. if($path =~ m{^(\/|\\)}) {
  473. $have_root = 1;
  474. $prefix = $1;
  475. }
  476. else {
  477. $prefix = '';
  478. }
  479. @arr = split(m{\/|\\}, $path);
  480. }
  481. my $p = 0;
  482. my @res;
  483. for my $el (@arr) {
  484. if(length($el) == 0 || $el eq '.') {
  485. next;
  486. }
  487. elsif($el eq '..' && @res > 0 && $res[$#res] ne '..') {
  488. pop @res;
  489. next;
  490. }
  491. push @res, $el;
  492. }
  493. if($have_root && @res > 0 && $res[0] eq '..') {
  494. warn "Error processing path \"$path\": " .
  495. "Parent directory of root directory does not exist!\n";
  496. return undef;
  497. }
  498. my $ret = $prefix . join('/', @res);
  499. $ret .= '/' if($path =~ m{\\$|/$} && scalar @res > 0);
  500. return $ret;
  501. }
  502. # Internal function. Converts path by using Msys's built-in
  503. # transformation.
  504. sub do_msys_transform {
  505. my ($path) = @_;
  506. return undef if $^O ne 'msys';
  507. return $path if $path eq '';
  508. # Remove leading double forward slashes, as they turn off Msys
  509. # transforming.
  510. $path =~ s{^/[/\\]+}{/};
  511. # Msys transforms automatically path to Windows native form in staring
  512. # program parameters if program is not Msys-based.
  513. # Note: already checked that $path is non-empty.
  514. $path = `cmd //c echo '$path'`;
  515. if($? != 0) {
  516. warn "Can't transform path into Win32 form by using Msys" .
  517. "internal transformation.\n";
  518. return undef;
  519. }
  520. # Remove double quotes, they are added for paths with spaces,
  521. # remove both '\r' and '\n'.
  522. $path =~ s{^\"|\"$|\"\r|\n|\r}{}g;
  523. return $path;
  524. }
  525. # Internal function. Gets two parameters: first parameter must be single
  526. # drive letter ('c'), second optional parameter is path relative to drive's
  527. # current working directory. Returns Win32 absolute normalized path.
  528. sub get_abs_path_on_win32_drive {
  529. my ($drv, $rel_path) = @_;
  530. my $res;
  531. # Get current directory on specified drive.
  532. # "/c;" is compatible with both Msys and Cygwin.
  533. my $cur_dir_on_drv = `cmd "/c;" echo %=$drv:%`;
  534. if($? != 0) {
  535. warn "Can't determine Win32 current directory on drive $drv:.\n";
  536. return undef;
  537. }
  538. if($cur_dir_on_drv =~ m{^[%]}) {
  539. # Current directory on drive is not set, default is
  540. # root directory.
  541. $res = ucfirst($drv) . ':/';
  542. }
  543. else {
  544. # Current directory on drive was set.
  545. # Remove both '\r' and '\n'.
  546. $cur_dir_on_drv =~ s{\n|\r}{}g;
  547. # Append relative path part.
  548. $res = $cur_dir_on_drv . '/';
  549. }
  550. $res .= $rel_path if defined $rel_path;
  551. # Replace any possible back slashes with forward slashes,
  552. # remove any duplicated slashes, resolve relative dirs.
  553. return normalize_path($res);
  554. }
  555. # Internal function. Tries to find or guess Win32 version of given
  556. # absolute Unix-style path. Other types of paths are not supported.
  557. # Returned paths contain only single forward slashes (no back and
  558. # duplicated slashes).
  559. # Last resort. Used only when other transformations are not available.
  560. sub do_dumb_guessed_transform {
  561. my ($path) = @_;
  562. # Replace any possible back slashes and duplicated forward slashes
  563. # with single forward slashes.
  564. $path =~ s{[/\\]+}{/}g;
  565. # Empty path is not valid.
  566. return undef if (length($path) == 0);
  567. # RE to find Win32 drive letter
  568. my $drv_ltr_re = drives_mounted_on_cygdrive() ?
  569. qr{^/cygdrive/([a-zA-Z])($|/.*$)} :
  570. qr{^/([a-zA-Z])($|/.*$)};
  571. # Check path whether path is Win32 directly mapped drive and try to
  572. # transform it assuming that drive letter is matched to Win32 drive letter.
  573. if($path =~ m{$drv_ltr_re}) {
  574. return ucfirst($1) . ':/' if(length($2) == 0);
  575. return ucfirst($1) . ':' . $2;
  576. }
  577. # This may be some custom mapped path. ('/mymount/path')
  578. # Must check longest possible path component as subdir can be mapped to
  579. # different directory. For example '/usr/bin/' can be mapped to '/bin/' or
  580. # '/bin/' can be mapped to '/usr/bin/'.
  581. my $check_path = $path;
  582. my $path_tail = '';
  583. do {
  584. if(-d $check_path) {
  585. my $res =
  586. `(cd "$check_path" && cmd /c "echo %__CD__%") 2>/dev/null`;
  587. if($? == 0 && substr($path, 0, 1) ne '%') {
  588. # Remove both '\r' and '\n'.
  589. $res =~ s{\n|\r}{}g;
  590. # Replace all back slashes with forward slashes.
  591. $res =~ s{\\}{/}g;
  592. if(length($path_tail) > 0) {
  593. return $res . $path_tail;
  594. }
  595. else {
  596. $res =~ s{/$}{} unless $check_path =~ m{/$};
  597. return $res;
  598. }
  599. }
  600. }
  601. if($check_path =~ m{(^.*/)([^/]+/*)}) {
  602. $check_path = $1;
  603. $path_tail = $2 . $path_tail;
  604. }
  605. else {
  606. # Shouldn't happens as root '/' directory should always
  607. # be resolvable.
  608. warn "Can't determine Win32 directory for path \"$path\".\n";
  609. return undef;
  610. }
  611. } while(1);
  612. }
  613. # Internal function. Converts given Unix-style absolute path to Win32 format.
  614. sub simple_transform_win32_to_unix {
  615. my ($path) = @_;
  616. if(should_use_cygpath()) {
  617. # 'cygpath' gives precise result.
  618. my $res;
  619. chomp($res = `cygpath -a -u '$path'`);
  620. if($? != 0) {
  621. warn "Can't determine Unix-style directory for Win32 " .
  622. "directory \"$path\".\n";
  623. return undef;
  624. }
  625. # 'cygpath' removes last slash if path is root dir on Win32 drive.
  626. $res .= '/' if(substr($res, length($res) - 1, 1) ne '/' &&
  627. $path =~ m{[/\\]$});
  628. return $res;
  629. }
  630. # 'cygpath' is not available, use guessed transformation.
  631. unless($path =~ s{^([a-zA-Z]):(?:/|\\)}{/\l$1/}) {
  632. warn "Can't determine Unix-style directory for Win32 " .
  633. "directory \"$path\".\n";
  634. return undef;
  635. }
  636. $path = '/cygdrive' . $path if(drives_mounted_on_cygdrive());
  637. return $path;
  638. }
  639. 1; # End of module