File Coverage

blib/lib/URI/file/Mac.pm
Criterion Covered Total %
statement 71 80 88.7
branch 36 50 72.0
condition 8 9 88.8
subroutine 6 7 85.7
pod 0 2 0.0
total 121 148 81.7


line stmt bran cond sub pod time code
1             package URI::file::Mac;
2              
3 3     3   24 use strict;
  3         7  
  3         100  
4 3     3   17 use warnings;
  3         6  
  3         79  
5              
6 3     3   22 use parent 'URI::file::Base';
  3         7  
  3         15  
7              
8 3     3   235 use URI::Escape qw(uri_unescape);
  3         6  
  3         2633  
9              
10             our $VERSION = '5.20';
11              
12             sub _file_extract_path
13             {
14 12     12   21 my $class = shift;
15 12         23 my $path = shift;
16              
17 12         18 my @pre;
18 12 100       54 if ($path =~ s/^(:+)//) {
19 10 100       35 if (length($1) == 1) {
20 6 100       15 @pre = (".") unless length($path);
21             } else {
22 4         12 @pre = ("..") x (length($1) - 1);
23             }
24             } else { #absolute
25 2         5 $pre[0] = "";
26             }
27              
28 12         25 my $isdir = ($path =~ s/:$//);
29 12         21 $path =~ s,([%/;]), URI::Escape::escape_char($1),eg;
  0         0  
30              
31 12         27 my @path = split(/:/, $path, -1);
32 12         32 for (@path) {
33 12 100 100     51 if ($_ eq "." || $_ eq "..") {
34 4         10 $_ = "%2E" x length($_);
35             }
36 12 50       26 $_ = ".." unless length($_);
37             }
38 12 50       25 push (@path,"") if $isdir;
39 12         48 (join("/", @pre, @path), 1);
40             }
41              
42              
43             sub file
44             {
45 27     27 0 43 my $class = shift;
46 27         34 my $uri = shift;
47 27         37 my @path;
48              
49 27         62 my $auth = $uri->authority;
50 27 100       67 if (defined $auth) {
51 11 100 100     49 if (lc($auth) ne "localhost" && $auth ne "") {
52 5         14 my $u_auth = uri_unescape($auth);
53 5 50       21 if (!$class->_file_is_localhost($u_auth)) {
54             # some other host (use it as volume name)
55 5         70 @path = ("", $auth);
56             # XXX or just return to make it illegal;
57             }
58             }
59             }
60 27         59 my @ps = split("/", $uri->path, -1);
61 27 100       70 shift @ps if @path;
62 27         54 push(@path, @ps);
63              
64 27         39 my $pre = "";
65 27 50       77 if (!@path) {
    100          
66 0         0 return; # empty path; XXX return ":" instead?
67             } elsif ($path[0] eq "") {
68             # absolute
69 16         39 shift(@path);
70 16 100       42 if (@path == 1) {
71 2 50       10 return if $path[0] eq ""; # not root directory
72 0         0 push(@path, ""); # volume only, effectively append ":"
73             }
74 14         39 @ps = @path;
75 14         26 @path = ();
76 14         19 my $part;
77 14         23 for (@ps) { #fix up "." and "..", including interior, in relatives
78 28 50       52 next if $_ eq ".";
79 28 50       50 $part = $_ eq ".." ? "" : $_;
80 28         45 push(@path,$part);
81             }
82 14 50       31 if ($ps[-1] eq "..") { #if this happens, we need another :
83 0         0 push(@path,"");
84             }
85            
86             } else {
87 11         18 $pre = ":";
88 11         27 @ps = @path;
89 11         16 @path = ();
90 11         15 my $part;
91 11         22 for (@ps) { #fix up "." and "..", including interior, in relatives
92 17 100       36 next if $_ eq ".";
93 15 100       32 $part = $_ eq ".." ? "" : $_;
94 15         23 push(@path,$part);
95             }
96 11 100       29 if ($ps[-1] eq "..") { #if this happens, we need another :
97 2         6 push(@path,"");
98             }
99            
100             }
101 25 50 66     79 return unless $pre || @path;
102 25         43 for (@path) {
103 40         67 s/;.*//; # get rid of parameters
104             #return unless length; # XXX
105 40         80 $_ = uri_unescape($_);
106 40 50       84 return if /\0/;
107 40 100       103 return if /:/; # Should we?
108             }
109 20         89 $pre . join(":", @path);
110             }
111              
112             sub dir
113             {
114 0     0 0   my $class = shift;
115 0           my $path = $class->file(@_);
116 0 0         return unless defined $path;
117 0 0         $path .= ":" unless $path =~ /:$/;
118 0           $path;
119             }
120              
121             1;