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