| 1 | package Tree::FSMethods; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use 5.010001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use Code::Includable::Tree::NodeMethods; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Path::Naive; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use Scalar::Util qw(refaddr); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use Storable qw(dclone); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use String::Wildcard::Bash; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 2 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |||||||||||||||||||||||||||
| 14 | 2 | our $DATE = '2021-10-07'; # DATE | |||||||||||||||||||||||||||
| 15 | 2 | our $DIST = 'Tree-FSMethods'; # DIST | |||||||||||||||||||||||||||
| 16 | 2 | our $VERSION = '0.004'; # VERSION | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 19 | 0 | my ($class, %args) = (shift, @_); | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 0 | ▶ | if ($args{tree}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | 0 | $args{_curpath} = "/"; | |||||||||||||||||||||||||||
| 23 | 0 | $args{_curnode} = $args{tree}; | |||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | 0 | ▶ | if ($args{tree2}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 0 | $args{_curpath2} = "/"; | |||||||||||||||||||||||||||
| 27 | 0 | $args{_curnode2} = $args{tree2}; | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 0 | bless \%args, $class; | |||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | # note: only reads from _curnode & _curpath | ||||||||||||||||||||||||||||
| 34 | 0 | ▶ | sub _read_curdir { | ||||||||||||||||||||||||||
|
Subroutine
_read_curdir: not called CC=15 SCAR=54.8
| |||||||||||||||||||||||||||||
| 35 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 0 | my %entries_by_name; | |||||||||||||||||||||||||||
| 38 | my @entries; | ||||||||||||||||||||||||||||
| 39 | 0 | my $order = 0; | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | NODE: | ||||||||||||||||||||||||||||
| 42 | 0 | for my $node ( | |||||||||||||||||||||||||||
| 43 | Code::Includable::Tree::NodeMethods::_children_as_list( | ||||||||||||||||||||||||||||
| 44 | $self->{_curnode})) { | ||||||||||||||||||||||||||||
| 45 | 0 | my $name; | |||||||||||||||||||||||||||
| 46 | ASSIGN_NAME: { | ||||||||||||||||||||||||||||
| 47 | 0 | my @methods; | |||||||||||||||||||||||||||
| 48 | 0 | ▶ | if (defined $self->{filename_method}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 0 | push @methods, $self->{filename_method}; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | 0 | push @methods, "filename", "title"; | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 0 | for my $method (@methods) { | |||||||||||||||||||||||||||
| 54 | 0 | ▶ | if (ref $method eq 'CODE') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 55 | 0 | $name = $method->($node); | |||||||||||||||||||||||||||
| 56 | } elsif ($node->can($method)) { | ||||||||||||||||||||||||||||
| 57 | 0 | $name = $node->$method; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | 0 | ▶ | last if defined $name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | 0 | ▶ | last if defined $name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 0 | $name = "$node"; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | HANDLE_INVALID: { | ||||||||||||||||||||||||||||
| 67 | 0 | ▶ | if ($name eq '') { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 68 | 0 | $name = "unnamed"; | |||||||||||||||||||||||||||
| 69 | } elsif ($name eq '.') { | ||||||||||||||||||||||||||||
| 70 | 0 | $name = '.(dot)'; | |||||||||||||||||||||||||||
| 71 | } elsif ($name eq '.') { | ||||||||||||||||||||||||||||
| 72 | 0 | $name = '..(dot-dot)'; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | 0 | $name =~ s!/!_!g; | |||||||||||||||||||||||||||
| 75 | 0 | ▶ | $name = substr($name, 0, 250) if length $name > 250; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | HANDLE_DUPLICATES: { | ||||||||||||||||||||||||||||
| 79 | 0 | ▶ | last unless exists $entries_by_name{$name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 0 | my $suffix = "2"; | |||||||||||||||||||||||||||
| 81 | 0 | while (1) { | |||||||||||||||||||||||||||
| 82 | 0 | my $new_name = "$name.$suffix"; | |||||||||||||||||||||||||||
| 83 | 0 | do { $name = $new_name; last } | |||||||||||||||||||||||||||
| 84 | 0 | ▶ | unless exists $entries_by_name{$new_name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | 0 | $suffix++; | |||||||||||||||||||||||||||
| 86 | 0 | ▶ | die "_read_curdir: Too many duplicate names ($name)" if $suffix >= 9999; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | my $entry = { | ||||||||||||||||||||||||||||
| 91 | order => $order, | ||||||||||||||||||||||||||||
| 92 | name => $name, | ||||||||||||||||||||||||||||
| 93 | node => $node, | ||||||||||||||||||||||||||||
| 94 | 0 | path => Path::Naive::concat_path($self->{_curpath}, $name), | |||||||||||||||||||||||||||
| 95 | }; | ||||||||||||||||||||||||||||
| 96 | 0 | $entries_by_name{$name} = $entry; | |||||||||||||||||||||||||||
| 97 | 0 | push @entries, $entry; | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 0 | $order++; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 0 | @entries; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | # returns: (path exists, @entries) | ||||||||||||||||||||||||||||
| 106 | 0 | ▶ | sub _traverse { | ||||||||||||||||||||||||||
|
Subroutine
_traverse: not called CC=18 SCAR=58.3
| |||||||||||||||||||||||||||||
| 107 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 108 | 0 | my ($which_obj, $path_wildcard) = @_; | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 0 | ▶ | my $rootnode = $which_obj == 1 ? $self->{tree} : $self->{tree2}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 0 | ▶ | my $curnode = $which_obj == 1 ? $self->{_curnode} : $self->{_curnode2}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 0 | ▶ | my $curpath = $which_obj == 1 ? $self->{_curpath} : $self->{_curpath2}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 0 | ▶ | die "_traverse: No object loaded yet" unless $curnode; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | # starting point of traversal | ||||||||||||||||||||||||||||
| 117 | 0 | ▶ | my $node = Path::Naive::is_abs_path($path_wildcard) ? $rootnode : $curnode; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | 0 | ▶ | my $starting_path = Path::Naive::is_abs_path($path_wildcard) ? "/" : $curpath; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 0 | my @path_elems = Path::Naive::split_path($path_wildcard); | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 0 | my @entries = ({path=>$starting_path, node=>$node}); | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 0 | my $i = 0; | |||||||||||||||||||||||||||
| 124 | 0 | my $path_exists = 1; | |||||||||||||||||||||||||||
| 125 | PATH_ELEM: | ||||||||||||||||||||||||||||
| 126 | 0 | for my $path_elem (@path_elems) { | |||||||||||||||||||||||||||
| 127 | 0 | $i++; | |||||||||||||||||||||||||||
| 128 | 0 | ▶ | if ($path_elem eq '.') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 0 | for (@entries) { | |||||||||||||||||||||||||||
| 130 | 0 | $_->{path} = Path::Naive::concat_path($_->{path}, "."); | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | 0 | next PATH_ELEM; | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | 0 | ▶ | if ($path_elem eq '..') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | 0 | for (@entries) { | |||||||||||||||||||||||||||
| 136 | 0 | $_->{path} = Path::Naive::concat_path($_->{path}, ".."); | |||||||||||||||||||||||||||
| 137 | # we allow ../ even on root node; it will just come back to root | ||||||||||||||||||||||||||||
| 138 | 0 | my $parent = $_->{node}->parent; | |||||||||||||||||||||||||||
| 139 | 0 | ▶ | $_->{node} = $parent if $parent; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | 0 | next PATH_ELEM; | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 0 | my $path_elem_contains_wildcard = String::Wildcard::Bash::contains_wildcard($path_elem); | |||||||||||||||||||||||||||
| 145 | 0 | my $path_elem_re; | |||||||||||||||||||||||||||
| 146 | 0 | ▶ | if ($path_elem_contains_wildcard) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | 0 | $path_elem_re = String::Wildcard::Bash::convert_wildcard_to_re($path_elem); | |||||||||||||||||||||||||||
| 148 | 0 | $path_elem_re = qr/\A$path_elem_re\z/; | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | 0 | my @new_entries; | |||||||||||||||||||||||||||
| 151 | 0 | for my $entry (@entries) { | |||||||||||||||||||||||||||
| 152 | 0 | local $self->{_curnode} = $entry->{node}; | |||||||||||||||||||||||||||
| 153 | 0 | local $self->{_curpath} = $entry->{path}; | |||||||||||||||||||||||||||
| 154 | 0 | my @dir = $self->_read_curdir; | |||||||||||||||||||||||||||
| 155 | 0 | ▶ | if ($path_elem_contains_wildcard) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | 0 | push @new_entries, grep { $_->{name} =~ $path_elem_re } @dir; | |||||||||||||||||||||||||||
| 157 | } else { | ||||||||||||||||||||||||||||
| 158 | 0 | push @new_entries, grep { $_->{name} eq $path_elem } @dir; | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | 0 | ▶ | unless (@new_entries) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | 0 | ▶ | $path_exists = 0 if $i < @path_elems; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | 0 | @entries = (); | |||||||||||||||||||||||||||
| 163 | 0 | last PATH_ELEM; | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | 0 | @entries = @new_entries; | |||||||||||||||||||||||||||
| 167 | } # for path_elem | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 0 | ($path_exists, @entries); | |||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 0 | ▶ | sub _cd { | ||||||||||||||||||||||||||
|
Subroutine
_cd: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 173 | 0 | my ($self, $which_obj, $path_wildcard) = @_; | |||||||||||||||||||||||||||
| 174 | 0 | my ($path_exists, @entries) = $self->_traverse($which_obj, $path_wildcard); | |||||||||||||||||||||||||||
| 175 | 0 | ▶ | die "cd: No such path '$path_wildcard'" unless @entries; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 176 | 0 | ▶ | die "cd: Ambiguous path '$path_wildcard'" unless @entries < 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 177 | 0 | ▶ | if ($which_obj == 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 178 | 0 | $self->{_curnode} = $entries[0]{node}; | |||||||||||||||||||||||||||
| 179 | 0 | $self->{_curpath} = Path::Naive::normalize_path($entries[0]{path}); | |||||||||||||||||||||||||||
| 180 | } else { | ||||||||||||||||||||||||||||
| 181 | 0 | $self->{_curnode2} = $entries[0]{node}; | |||||||||||||||||||||||||||
| 182 | 0 | $self->{_curpath2} = Path::Naive::normalize_path($entries[0]{path}); | |||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | 0 | ($self->{_curnode}, $self->{_curpath}); | |||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 0 | ▶ | sub cd { | ||||||||||||||||||||||||||
|
Subroutine
cd: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 188 | 0 | my ($self, $path_wildcard) = @_; | |||||||||||||||||||||||||||
| 189 | 0 | $self->_cd(1, $path_wildcard); | |||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 0 | ▶ | sub cd2 { | ||||||||||||||||||||||||||
|
Subroutine
cd2: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 193 | 0 | my ($self, $path_wildcard) = @_; | |||||||||||||||||||||||||||
| 194 | 0 | $self->_cd(2, $path_wildcard); | |||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | 0 | ▶ | sub _ls { | ||||||||||||||||||||||||||
|
Subroutine
_ls: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||
| 198 | 0 | my ($self, $which_obj, $path_wildcard) = @_; | |||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 0 | my $specifies_path = 1; | |||||||||||||||||||||||||||
| 201 | 0 | ▶ | unless (defined $path_wildcard) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 202 | 0 | $path_wildcard = '*'; | |||||||||||||||||||||||||||
| 203 | 0 | $specifies_path = 0; | |||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | 0 | ▶ | my $cwd = $which_obj == 1 ? $self->{_curpath} : $self->{_curpath2}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | 0 | my ($path_exists, @entries) = $self->_traverse($which_obj, $path_wildcard); | |||||||||||||||||||||||||||
| 209 | 0 | ▶ | die "ls: No such path '$path_wildcard' (cwd=$cwd)" unless $path_exists; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 210 | 0 | ▶ | die "ls: No such path '$path_wildcard' (cwd=$cwd)" if !@entries && $specifies_path; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not @entries and $specifies_path
MC/DC0%0/2
not @entries and $specifies_path
not @entries
$specifies_path
Truth table0%0/3
not(A) and B
A= not @entriesB= $specifies_path
| |||||||||||||||||||||||||||||
| 211 | 0 | @entries; | |||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | 0 | ▶ | sub ls { | ||||||||||||||||||||||||||
|
Subroutine
ls: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 215 | 0 | my ($self, $path_wildcard) = @_; | |||||||||||||||||||||||||||
| 216 | 0 | $self->_ls(1, $path_wildcard); | |||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | 0 | ▶ | sub ls2 { | ||||||||||||||||||||||||||
|
Subroutine
ls2: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 220 | 0 | my ($self, $path_wildcard) = @_; | |||||||||||||||||||||||||||
| 221 | 0 | $self->_ls(2, $path_wildcard); | |||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | 0 | ▶ | sub _showtree0 { | ||||||||||||||||||||||||||
|
Subroutine
_showtree0: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 225 | 0 | require Tree::Object::Hash; | |||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | 0 | my ($self, $path, $node) = @_; | |||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | 0 | my @entries = $self->_read_curdir; | |||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | 0 | my @children; | |||||||||||||||||||||||||||
| 232 | 0 | for my $entry (@entries) { | |||||||||||||||||||||||||||
| 233 | 0 | my $child = Tree::Object::Hash->new; | |||||||||||||||||||||||||||
| 234 | 0 | $child->parent($entry->{node}); | |||||||||||||||||||||||||||
| 235 | 0 | $child->{filename} = $entry->{name}; | |||||||||||||||||||||||||||
| 236 | 0 | push @children, $child; | |||||||||||||||||||||||||||
| 237 | 0 | local $self->{_curnode} = $entry->{node}; | |||||||||||||||||||||||||||
| 238 | 0 | local $self->{_curpath} = Path::Naive::concat_path($self->{_curpath}, $entry->{name}); | |||||||||||||||||||||||||||
| 239 | 0 | $self->_showtree0("$path/$entry->{name}", $child); | |||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||
| 241 | 0 | $node->children(\@children); | |||||||||||||||||||||||||||
| 242 | 0 | $node; | |||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | 0 | ▶ | sub _showtree { | ||||||||||||||||||||||||||
|
Subroutine
_showtree: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 246 | 0 | require Tree::Object::Hash; | |||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 249 | 0 | my $which_obj = shift; | |||||||||||||||||||||||||||
| 250 | 0 | ▶ | my $starting_path = shift // '.'; | ||||||||||||||||||||||||||
|
Condition0%0/2
shift() // "."
MC/DC0%0/1
shift() // "."
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | 0 | my $tree; | |||||||||||||||||||||||||||
| 253 | { | ||||||||||||||||||||||||||||
| 254 | 0 | ▶ | local $self->{_curnode} = $which_obj == 1 ? $self->{_curnode} : $self->{_curnode2}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 255 | 0 | ▶ | local $self->{_curpath} = $which_obj == 1 ? $self->{_curpath} : $self->{_curpath2}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | 0 | $self->cd($starting_path); | |||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | 0 | my $node = Tree::Object::Hash->new; | |||||||||||||||||||||||||||
| 260 | 0 | $node->{filename} = $starting_path; | |||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | 0 | $tree = $self->_showtree0($starting_path, $node); | |||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | 0 | require Tree::To::TextLines; | |||||||||||||||||||||||||||
| 266 | Tree::To::TextLines::render_tree_as_text({ | ||||||||||||||||||||||||||||
| 267 | show_guideline => 1, | ||||||||||||||||||||||||||||
| 268 | 0 | ▶ | on_show_node => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 269 | 0 | my ($node, $level, $seniority, $is_last_child, $opts) = @_; | |||||||||||||||||||||||||||
| 270 | 0 | $node->{filename}; | |||||||||||||||||||||||||||
| 271 | }, | ||||||||||||||||||||||||||||
| 272 | 0 | }, $tree); | |||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | 0 | ▶ | sub showtree { | ||||||||||||||||||||||||||
|
Subroutine
showtree: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 276 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 277 | 0 | $self->_showtree(1, @_); | |||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | 0 | ▶ | sub showtree2 { | ||||||||||||||||||||||||||
|
Subroutine
showtree2: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 281 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 282 | 0 | $self->_showtree(2, @_); | |||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | 0 | ▶ | sub _cwd { | ||||||||||||||||||||||||||
|
Subroutine
_cwd: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 286 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 287 | 0 | my $which_obj = shift; | |||||||||||||||||||||||||||
| 288 | 0 | ▶ | $which_obj == 1 ? $self->{_curpath} : $self->{_curpath2}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 289 | } | ||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | 0 | ▶ | sub cwd { | ||||||||||||||||||||||||||
|
Subroutine
cwd: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 292 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 293 | 0 | $self->_cwd(1); | |||||||||||||||||||||||||||
| 294 | } | ||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | 0 | ▶ | sub cwd2 { | ||||||||||||||||||||||||||
|
Subroutine
cwd2: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 297 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 298 | 0 | $self->_cwd(2); | |||||||||||||||||||||||||||
| 299 | } | ||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||
| 301 | 0 | ▶ | sub _cp_or_mv { | ||||||||||||||||||||||||||
|
Subroutine
_cp_or_mv: not called CC=15 SCAR=54.8
| |||||||||||||||||||||||||||||
| 302 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 303 | 0 | my $which_cmd = shift; | |||||||||||||||||||||||||||
| 304 | 0 | my ($src_path_wildcard, $target_path) = @_; | |||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | 0 | ▶ | length($src_path_wildcard) or die "$which_cmd: Please specify source path"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 307 | 0 | ▶ | length($target_path) or die "$which_cmd: Please specify target path"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | # we can move/copy either to 'tree' or 'tree2'. default to 'tree2' when it | ||||||||||||||||||||||||||||
| 310 | # is defined, falls back to 'tree'. | ||||||||||||||||||||||||||||
| 311 | 0 | ▶ | my $target_obj = defined $self->{_curnode2} ? 2:1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | 0 | my ($source_path_exists, @source_entries) = | |||||||||||||||||||||||||||
| 314 | $self->_traverse(1, $src_path_wildcard); | ||||||||||||||||||||||||||||
| 315 | 0 | ▶ | die "$which_cmd: No such source path '$src_path_wildcard'" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 316 | unless $source_path_exists; | ||||||||||||||||||||||||||||
| 317 | 0 | ▶ | die "$which_cmd: No matching source files for '$src_path_wildcard'" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 318 | unless @source_entries; | ||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||
| 320 | 0 | my ($target_path_exists, @target_entries) = | |||||||||||||||||||||||||||
| 321 | $self->_traverse($target_obj, $target_path); | ||||||||||||||||||||||||||||
| 322 | 0 | ▶ | die "$which_cmd: No such target path '$target_path'" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 323 | unless $target_path_exists; | ||||||||||||||||||||||||||||
| 324 | 0 | ▶ | die "$which_cmd: No matching target files for '$target_path'" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 325 | unless @target_entries; | ||||||||||||||||||||||||||||
| 326 | 0 | ▶ | die "$which_cmd: Ambiguous target '$target_path'" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 327 | unless @target_entries < 2; | ||||||||||||||||||||||||||||
| 328 | 0 | my $target_entry = $target_entries[0]; | |||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||
| 330 | 0 | ▶ | if ($which_cmd eq 'cp') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 331 | # clone it first | ||||||||||||||||||||||||||||
| 332 | 0 | @source_entries = map { dclone($_) } @source_entries; | |||||||||||||||||||||||||||
| 333 | 0 | ▶ | if ($self->can("before_cp")) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 334 | 0 | $self->before_cp([map {$_->{node}} @source_entries], | |||||||||||||||||||||||||||
| 335 | 0 | $target_entry->{node}); | |||||||||||||||||||||||||||
| 336 | } | ||||||||||||||||||||||||||||
| 337 | } elsif ($which_cmd eq 'mv') { | ||||||||||||||||||||||||||||
| 338 | # remove the nodes from their original parents | ||||||||||||||||||||||||||||
| 339 | 0 | for my $entry (@source_entries) { | |||||||||||||||||||||||||||
| 340 | 0 | Code::Includable::Tree::NodeMethods::remove($entry->{node}); | |||||||||||||||||||||||||||
| 341 | } | ||||||||||||||||||||||||||||
| 342 | 0 | ▶ | if ($self->can("before_mv")) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 343 | 0 | $self->before_mv([map {$_->{node}} @source_entries], | |||||||||||||||||||||||||||
| 344 | 0 | $target_entry->{node}); | |||||||||||||||||||||||||||
| 345 | } | ||||||||||||||||||||||||||||
| 346 | } else { | ||||||||||||||||||||||||||||
| 347 | 0 | die "BUG: which_cmd must be cp/mv"; | |||||||||||||||||||||||||||
| 348 | } | ||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||
| 350 | # put as children of the target parent | ||||||||||||||||||||||||||||
| 351 | 0 | push @{ $target_entry->{node}{children} }, | |||||||||||||||||||||||||||
| 352 | 0 | map { $_->{node} } @source_entries; | |||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | # assign new (target) parent | ||||||||||||||||||||||||||||
| 355 | 0 | for my $entry (@source_entries) { | |||||||||||||||||||||||||||
| 356 | 0 | $entry->{node}->parent( $target_entry->{node} ); | |||||||||||||||||||||||||||
| 357 | } | ||||||||||||||||||||||||||||
| 358 | } | ||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | 0 | ▶ | sub cp { | ||||||||||||||||||||||||||
|
Subroutine
cp: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 361 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 362 | 0 | $self->_cp_or_mv('cp', @_); | |||||||||||||||||||||||||||
| 363 | } | ||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | 0 | ▶ | sub mv { | ||||||||||||||||||||||||||
|
Subroutine
mv: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 366 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 367 | 0 | $self->_cp_or_mv('mv', @_); | |||||||||||||||||||||||||||
| 368 | } | ||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||
| 370 | 0 | ▶ | sub _rm { | ||||||||||||||||||||||||||
|
Subroutine
_rm: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 371 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 372 | 0 | my $which_obj = shift; | |||||||||||||||||||||||||||
| 373 | 0 | my $path_wildcard = shift; | |||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||
| 375 | 0 | my ($path_exists, @entries) = | |||||||||||||||||||||||||||
| 376 | $self->_traverse($which_obj, $path_wildcard); | ||||||||||||||||||||||||||||
| 377 | 0 | ▶ | die "rm: No such path '$path_wildcard'" unless $path_exists; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 378 | 0 | ▶ | die "rm: No matching files for '$path_wildcard'" unless @entries; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||
| 380 | 0 | for my $entry (@entries) { | |||||||||||||||||||||||||||
| 381 | 0 | Code::Includable::Tree::NodeMethods::remove($entry->{node}); | |||||||||||||||||||||||||||
| 382 | } | ||||||||||||||||||||||||||||
| 383 | } | ||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||
| 385 | 0 | ▶ | sub rm { | ||||||||||||||||||||||||||
|
Subroutine
rm: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 386 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 387 | 0 | $self->_rm(1, @_); | |||||||||||||||||||||||||||
| 388 | } | ||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||
| 390 | 0 | ▶ | sub rm2 { | ||||||||||||||||||||||||||
|
Subroutine
rm2: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 391 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 392 | 0 | $self->_rm(2, @_); | |||||||||||||||||||||||||||
| 393 | } | ||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||
| 395 | 0 | ▶ | sub _mkdir { | ||||||||||||||||||||||||||
|
Subroutine
_mkdir: not called CC=15 SCAR=54.8
| |||||||||||||||||||||||||||||
| 396 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 397 | 0 | my $which_obj = shift; | |||||||||||||||||||||||||||
| 398 | 0 | ▶ | my $opts = ref($_[0]) eq 'HASH' ? shift : {}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 399 | 0 | my $path = shift; | |||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||
| 401 | 0 | ▶ | my $rootnode = $which_obj == 1 ? $self->{tree} : $self->{tree2}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 402 | 0 | ▶ | my $curpath = $which_obj == 1 ? $self->{_curpath} : $self->{_curpath2}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 403 | 0 | ▶ | die "BUG: curpath '$curpath' is not absolute" unless Path::Naive::is_abs_path($curpath); # sanity check | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 404 | 0 | my @path_elems = Path::Naive::split_path( | |||||||||||||||||||||||||||
| 405 | Path::Naive::concat_and_normalize_path($curpath, $path) | ||||||||||||||||||||||||||||
| 406 | ); | ||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||
| 408 | 0 | ▶ | die "mkdir: / already exists" unless @path_elems; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||
| 410 | 0 | local $self->{_curnode} = $rootnode; | |||||||||||||||||||||||||||
| 411 | 0 | local $self->{_curpath} = "/"; | |||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||
| 413 | 0 | my $made_dir; | |||||||||||||||||||||||||||
| 414 | 0 | for my $i (0..$#path_elems) { | |||||||||||||||||||||||||||
| 415 | 0 | my $path_elem = $path_elems[$i]; | |||||||||||||||||||||||||||
| 416 | 0 | my @entries = $self->_read_curdir; | |||||||||||||||||||||||||||
| 417 | 0 | my $found_entry; | |||||||||||||||||||||||||||
| 418 | 0 | for my $entry (@entries) { | |||||||||||||||||||||||||||
| 419 | 0 | ▶ | if ($entry->{name} eq $path_elem) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 420 | 0 | $found_entry = $entry; last; | |||||||||||||||||||||||||||
| 421 | } | ||||||||||||||||||||||||||||
| 422 | } | ||||||||||||||||||||||||||||
| 423 | 0 | ▶ | if ($found_entry) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 424 | 0 | $self->{_curnode} = $found_entry->{node}; | |||||||||||||||||||||||||||
| 425 | 0 | $self->{_curpath} = $found_entry->{path}; | |||||||||||||||||||||||||||
| 426 | 0 | next; | |||||||||||||||||||||||||||
| 427 | } | ||||||||||||||||||||||||||||
| 428 | 0 | ▶ | if ($opts->{parents} || $i == $#path_elems) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$opts->{"parents"} or $i == $#path_elems
MC/DC0%0/2
$opts->{"parents"} or $i == $#path_elems
$opts->{"parents"}
$i == $#path_elems
Truth table0%0/3
A or B
A= $opts->{"parents"}B= $i == $#path_elems
| |||||||||||||||||||||||||||||
| 429 | 0 | my $new_node = $self->on_mkdir($self->{_curnode}, $path_elem); | |||||||||||||||||||||||||||
| 430 | # reread and find out the actual filename we're given | ||||||||||||||||||||||||||||
| 431 | 0 | @entries = $self->_read_curdir; | |||||||||||||||||||||||||||
| 432 | 0 | my @new_entry = grep { refaddr($_->{node}) == refaddr($new_node) } @entries; | |||||||||||||||||||||||||||
| 433 | 0 | ▶ | die "BUG: Can't create '$path_elem' at $self->{_curpath}: node not found" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 434 | unless @new_entry == 1; | ||||||||||||||||||||||||||||
| 435 | 0 | $self->{_curnode} = $new_node; | |||||||||||||||||||||||||||
| 436 | 0 | $self->{_curpath} = $new_entry[0]{path}; | |||||||||||||||||||||||||||
| 437 | 0 | $made_dir++; | |||||||||||||||||||||||||||
| 438 | } else { | ||||||||||||||||||||||||||||
| 439 | 0 | die "mkdir: No such path '".Path::Naive::concat_path($self->{_curpath}, $path_elem)."'"; | |||||||||||||||||||||||||||
| 440 | } | ||||||||||||||||||||||||||||
| 441 | } | ||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||
| 443 | die "mkdir: $path already exists" | ||||||||||||||||||||||||||||
| 444 | 0 | ▶ | if !$made_dir && !$opts->{parents}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$made_dir or $opts->{"parents"}
MC/DC0%0/2
$made_dir or $opts->{"parents"}
$made_dir
$opts->{"parents"}
Truth table0%0/3
A or B
A= $made_dirB= $opts->{"parents"}
| |||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||
| 446 | 0 | $self->{_curpath}; | |||||||||||||||||||||||||||
| 447 | } | ||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||
| 449 | 0 | ▶ | sub mkdir { | ||||||||||||||||||||||||||
|
Subroutine
mkdir: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 450 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 451 | 0 | $self->_mkdir(1, @_); | |||||||||||||||||||||||||||
| 452 | } | ||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||
| 454 | 0 | ▶ | sub mkdir2 { | ||||||||||||||||||||||||||
|
Subroutine
mkdir2: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 455 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 456 | 0 | $self->_mkdir(2, @_); | |||||||||||||||||||||||||||
| 457 | } | ||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||
| 459 | 2 | 1; | |||||||||||||||||||||||||||
| 460 | # ABSTRACT: Perform filesystem-like operations on object tree(s) | ||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||
| 462 | __END__ | ||||||||||||||||||||||||||||