I have a path:
/usr/vsa/pkgs/python3/3.6.3a/bin/python3.6. There are two links in the path:
/usr/vsa -> /root/site/tools/gauv
/usr/vsa/pkgs/python3/3.6.3a -> 3.6.3
I'm trying to get an array of all subpaths of that path, including the links. So the final array should contain:
/usr/
/usr/vsa
/root/
/root/site
/root/site/tools
/root/site/tools/gauv
/root/site/tools/gauv/pkgs
/root/site/tools/gauv/python3
/root/site/tools/gauv/python3/3.6.3a
/root/site/tools/gauv/python3/3.6.3
/root/site/tools/gauv/python3/3.6.3/bin
/root/site/tools/gauv/python3/3.6.3/bin/python3.6
What I've tried:
my @arr;
foreach my $c (split("/", $current_path)) {
if ($c eq "") {
next;
}
$res = "$res/$c";
push(@arr,$res);
}
my @checked_links;
while (1) {
my $old = "";
my $new = "";
foreach my $c (@arr) {
if (grep("/^$c/",@checked_links)) {
next;
}
if (-l $c) {
my $link = readlink($c);
my $res;
$old = $c;
if (index($link,"/") == -1) {
my $dirname = dirname($c);
my $basename = basename($c);
$res = "$dirname/$link";
} else {
$res = $link;
}
unshift(@arr,$res);
$new = $link;
last;
}
}
if ($old ne "") {
for (my $i = 0; $i < scalar(@arr); $i++) {
if (@arr[$i] eq $old) {
next; # ignore
}
if (index(@arr[$i],$old) == -1) {
next;
}
$changed = 1;
@arr[$i] =~ s/$old/$new/;
}
push(@checked_links,$old);
}
if ($changed == 0) {
last;
}
}
The idea was to split
$current_path (the path) by "/" and add each subpath to the array.
Then iterate over the array and check if the subpath is an link. If it is, I need to "fix" each path that I already have in the array.
That's the part that I'm having trouble with. I want to edit the array while I'm iterating through it and getting trouble with it.
So I will explain how I tried to approach it - I will iterate over the array of subpaths.
Once I came across with a new link (not in
@checked_links) I will add it to the array of subpaths and replace old path with new path in each path (which is not exactly equal to the path).
I'm having problem with relative paths. I'm not sure how to handle this part. If it's only basname (does not contain /) then I tried to replace get dirname of the path and add the new basename. But what if the link is relative (for example ../../abc)?
What would be the best way to solve it? Also, I believe I really got it complicated because I have a infinite while loop, iterating over the array again and again... Is there a better approach here?
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.