ovedpo15 has asked for the wisdom of the Perl Monks concerning the following question:
Basically I split find the canonical path of the given path, split it and create a vertex of each subpath, with an edge to the next one. The $original_path_flag represents full paths (you will understand later why I need it).sub add_path { my ($self,$path) = @_; my $canonical_path = get_canonical_path($path); my @subpaths = splitdir($canonical_path); for my $index (0..$#subpaths) { next if (@subpaths[$index] eq ""); # Ignore empty strings my $parent_path = abs_path(catdir(@subpaths[0..($index-1)])); my $current_path = catdir($parent_path,@subpaths[$index]); my $original_path_flag = ($index eq $#subpaths); next if ($graph->has_vertex($current_path)); if (-l $current_path) { my @resloved_links = resolve_symlink($current_path); my $target_path = $resloved_links[1]; $self->add_path($target_path); $graph->add_edge($parent_path,$current_path); $graph->add_edge($current_path,$target_path); $graph->set_vertex_attributes($current_path, { "type" => " +link", "target" => $target_path, "original" => $original_path_flag }) +; } elsif (-f $current_path) { $graph->add_edge($parent_path,$current_path); $graph->set_vertex_attributes($current_path, { "type" => " +file", "original" => $original_path_flag }); } elsif (-d $current_path) { $graph->add_edge($parent_path,$current_path); $graph->set_vertex_attributes($current_path, { "type" => " +dir", "original" => $original_path_flag }); } } }
Where is_regex_matches returns true if $vertex matches one of the regex expressions. It works good but I want to add support to links in the regex expressions. Since $graph->unique_vertices returns the vertices and each one contains only the absolute paths, then running a regex with link on the way, will fail. Just to make it more clear, consider:sub run_rexes { my ($self,$rexes_aref) = @_; foreach my $vertex ($graph->unique_vertices) { next unless ($graph->get_vertex_attribute($vertex, 'original') +); if (is_regex_matches($vertex,$rexes_aref)) { $graph->set_vertex_attribute($vertex, "marked", 1); } } }
So a regex like so: ^/a/b/c/file$ will work, but ^/p/file$ will not work./a/b/c/file /p -> /a/b/c
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Graph which represents the filesystem
by kcott (Archbishop) on Apr 18, 2022 at 18:59 UTC |