| 1 | #======================================================================== | ||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||
| 3 | # Badger::Filesystem::Universal | ||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||
| 5 | # DESCRIPTION | ||||||||||||||||||||||||||||
| 6 | # Subclass of Badger::Filesystem which implements a universal | ||||||||||||||||||||||||||||
| 7 | # filesystem for representing URIs. It always uses forward slashes | ||||||||||||||||||||||||||||
| 8 | # as path separators regardless of the local filesystem convention. | ||||||||||||||||||||||||||||
| 9 | # | ||||||||||||||||||||||||||||
| 10 | # AUTHOR | ||||||||||||||||||||||||||||
| 11 | # Andy Wardley <abw@wardley.org> | ||||||||||||||||||||||||||||
| 12 | # | ||||||||||||||||||||||||||||
| 13 | #======================================================================== | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | package Badger::Filesystem::Universal; | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 1 | ▶ | use Badger::Debug ':dump'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | use Badger::Class | ||||||||||||||||||||||||||||
| 19 | 1 | version => 0.01, | |||||||||||||||||||||||||||
| 20 | debug => 0, | ||||||||||||||||||||||||||||
| 21 | base => 'Badger::Filesystem', | ||||||||||||||||||||||||||||
| 22 | constants => 'HASH', | ||||||||||||||||||||||||||||
| 23 | constant => { | ||||||||||||||||||||||||||||
| 24 | UFS => __PACKAGE__, | ||||||||||||||||||||||||||||
| 25 | ROOTDIR => '/', | ||||||||||||||||||||||||||||
| 26 | CURDIR => '.', | ||||||||||||||||||||||||||||
| 27 | UPDIR => '..', | ||||||||||||||||||||||||||||
| 28 | FILESPEC => 'Badger::Filesystem::FileSpec::Universal', | ||||||||||||||||||||||||||||
| 29 | spec => 'Badger::Filesystem::FileSpec::Universal', | ||||||||||||||||||||||||||||
| 30 | }, | ||||||||||||||||||||||||||||
| 31 | exports => { | ||||||||||||||||||||||||||||
| 32 | any => 'UFS', | ||||||||||||||||||||||||||||
| 33 | 1 | ▶ | }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | #----------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 37 | # Replacement for File::Spec implementing that various methods that the | ||||||||||||||||||||||||||||
| 38 | # filesystem needs to construct paths. | ||||||||||||||||||||||||||||
| 39 | #----------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | package Badger::Filesystem::FileSpec::Universal; | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | use Badger::Class | ||||||||||||||||||||||||||||
| 44 | 1 | version => 0.01, | |||||||||||||||||||||||||||
| 45 | debug => 0, | ||||||||||||||||||||||||||||
| 46 | base => 'Badger::Base', | ||||||||||||||||||||||||||||
| 47 | constant => { | ||||||||||||||||||||||||||||
| 48 | SLASH => '/', | ||||||||||||||||||||||||||||
| 49 | SLASHRX => qr{/}, | ||||||||||||||||||||||||||||
| 50 | COLON => ':', | ||||||||||||||||||||||||||||
| 51 | rootdir => '/', | ||||||||||||||||||||||||||||
| 52 | curdir => '.', | ||||||||||||||||||||||||||||
| 53 | updir => '..', | ||||||||||||||||||||||||||||
| 54 | 1 | ▶ | }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | sub catdir { | ||||||||||||||||||||||||||||
| 58 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
catdir: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 59 | 0 | join(SLASH, @_); | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | sub catpath { | ||||||||||||||||||||||||||||
| 63 | 0 | ▶ | my ($self, $volume, $dir, $file) = @_; | ||||||||||||||||||||||||||
|
Subroutine
catpath: not called CC=13 SCAR=52.0
| |||||||||||||||||||||||||||||
| 64 | 0 | my $path = ''; | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | # yuk | ||||||||||||||||||||||||||||
| 67 | 0 | ▶ | $volume = undef unless defined $volume and length $volume; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $volume and length $volume
MC/DC0%0/2
defined $volume and length $volume
defined $volume
length $volume
Truth table0%0/3
A and B
A= defined $volumeB= length $volume
| |||||||||||||||||||||||||||||
| 68 | 0 | ▶ | $dir = undef unless defined $dir and length $dir; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $dir and length $dir
MC/DC0%0/2
defined $dir and length $dir
defined $dir
length $dir
Truth table0%0/3
A and B
A= defined $dirB= length $dir
| |||||||||||||||||||||||||||||
| 69 | 0 | ▶ | $file = undef unless defined $file and length $file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $file and length $file
MC/DC0%0/2
defined $file and length $file
defined $file
length $file
Truth table0%0/3
A and B
A= defined $fileB= length $file
| |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 0 | ▶ | $path .= $volume.COLON if defined $volume; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 0 | ▶ | $path .= SLASH if defined $volume and defined $dir; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $volume and defined $dir
MC/DC0%0/2
defined $volume and defined $dir
defined $volume
defined $dir
Truth table0%0/3
A and B
A= defined $volumeB= defined $dir
| |||||||||||||||||||||||||||||
| 73 | 0 | ▶ | $path .= $dir.SLASH if defined $dir; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 0 | ▶ | $path .= $file if defined $file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 0 | ▶ | $self->debug("catpath() [$volume] [$dir] [$file] => [$path]") if $DEBUG; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 0 | return $path; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | sub splitpath { | ||||||||||||||||||||||||||||
| 80 | 0 | ▶ | my ($self, $path) = @_; | ||||||||||||||||||||||||||
|
Subroutine
splitpath: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 81 | 0 | my ($volume, $dir, $file); | |||||||||||||||||||||||||||
| 82 | 0 | $dir = $path; | |||||||||||||||||||||||||||
| 83 | 0 | ▶ | $volume = $1 if $dir =~ s/^(\w+)://; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 0 | ▶ | $file = $1 if $dir =~ s/([^\/]+)$//; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | 0 | $dir =~ s{(?<=.)/$}{}; | |||||||||||||||||||||||||||
| 86 | 0 | $dir =~ s{//}{/}g; | |||||||||||||||||||||||||||
| 87 | 0 | ▶ | $self->debug("splitpath() [$path] => [$volume] [$dir] [$file]") if $DEBUG; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 0 | return ($volume, $dir, $file); | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | sub splitdir { | ||||||||||||||||||||||||||||
| 92 | 0 | ▶ | my ($self, $dir) = @_; | ||||||||||||||||||||||||||
|
Subroutine
splitdir: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 93 | 0 | ▶ | $self->debug("splitdir($dir) => [", join('] [', split(SLASHRX, $dir)), ']') if $DEBUG; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 0 | return split(SLASHRX, $dir); | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | sub file_name_is_absolute { | ||||||||||||||||||||||||||||
| 98 | 0 | ▶ | my ($self, $path) = @_; | ||||||||||||||||||||||||||
|
Subroutine
file_name_is_absolute: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 99 | 0 | $self->debug("testing $path"); | |||||||||||||||||||||||||||
| 100 | 0 | return $path =~ m{^/}; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | sub canonpath { | ||||||||||||||||||||||||||||
| 104 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
canonpath: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 105 | 0 | my ($volume, $dir, $name) = $self->splitpath(@_); | |||||||||||||||||||||||||||
| 106 | 0 | my @dirs = $self->splitdir($dir); | |||||||||||||||||||||||||||
| 107 | 0 | my ($node, @path); | |||||||||||||||||||||||||||
| 108 | 0 | ▶ | while (@dirs) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 0 | $node = shift @dirs; | |||||||||||||||||||||||||||
| 110 | 0 | ▶ | if ($node eq curdir) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 111 | # do nothing | ||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | elsif ($node eq updir) { | ||||||||||||||||||||||||||||
| 114 | 0 | ▶ | pop @path if @path; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | else { | ||||||||||||||||||||||||||||
| 117 | 0 | push(@path, $node); | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | 0 | return $self->catpath( | |||||||||||||||||||||||||||
| 121 | $volume, | ||||||||||||||||||||||||||||
| 122 | $self->catdir(@path), | ||||||||||||||||||||||||||||
| 123 | $name | ||||||||||||||||||||||||||||
| 124 | ); | ||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | sub abs2rel { | ||||||||||||||||||||||||||||
| 128 | 0 | ▶ | shift->todo; | ||||||||||||||||||||||||||
|
Subroutine
abs2rel: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | sub no_upwards { | ||||||||||||||||||||||||||||
| 132 | 0 | ▶ | shift->todo; | ||||||||||||||||||||||||||
|
Subroutine
no_upwards: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 1 | 1; | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||