#------------------------------------------------------------------------------ # Rebuild a path as an absolute long path to be usable in Windows system calls (from PR#283) # Inputs: 0) path # Returns: normalized long path # Note: This should only be called for Windows systems sub WindowsLongPath($) { my $path = shift; $path =~ tr{/}{\\}; # use backslashes print "WindowsLongPath input:" . $path . "\n"; return $path if ContainsWildcards($path) or $path =~ /^\\\\\?\\/; # already absolute path -> add long path prefix and return return "\\\\?\\$path" if $path =~ /^[a-z]:/i; ##FB # already complete UNC path > add long UNC path prefix and return print "WindowsLongPath return complete UNC:" . "\\\\?\\UNC" . substr $path, 1 if $path =~ /\\\\/; print "\n"; return "\\\\?\\UNC" . substr $path, 1 if $path =~ /\\\\/; ##FB_x # need current working directory to build absolute path return $path unless { eval { require Cwd } }; my $cwd = Cwd::getcwd(); $cwd =~ tr{/}{\\}; print "WindowsLongPath cwd:" . $cwd; print "\n"; my @cwdParts = split /\\/, $cwd; ##FB # UNC path, starts with \\. So first 2 elements are "" # Shift and put UNC in first element. if ( ($cwdParts[0] eq "") && ($cwdParts[1] eq "") ) { shift @cwdParts; $cwdParts[0] = "UNC"; } ##FB_x my @pathParts = split /\\/, $path; my @combinedParts = @cwdParts; my $part; # remove "." and ".." from path (not handled by Win32API functions) foreach $part (@pathParts) { if ($part eq '.' or $part eq '') { next; } elsif ($part eq '..') { pop @combinedParts if @combinedParts > 1; } else { push @combinedParts, $part; } } print "WindowsLongPath return relative UNC:" . '\\\\?\\' . join '\\', @combinedParts; print "\n"; return '\\\\?\\' . join '\\', @combinedParts; }