Pau*_*aul 8 filesystems tree perl cpan infinite-loop
在服务器故障,如何列出符号链接链?(不是我的问题)讨论列出所有符号链接并跟随它们.为了使这个可行,我们首先考虑一个目录.
我想编写一个实用程序的简短实用程序.将符号链接中的对放入哈希中然后处理哈希看起来很容易.
但后来我可能有类似的东西:
ls -l
total 0
lrwxrwxrwx 1 pjb pjb 1 2010-02-23 08:48 a -> b
lrwxrwxrwx 1 pjb pjb 1 2010-02-23 08:48 b -> c
lrwxrwxrwx 1 pjb pjb 1 2010-02-23 09:03 c -> a
lrwxrwxrwx 1 pjb pjb 1 2010-02-23 09:17 trap -> b
lrwxrwxrwx 1 pjb pjb 1 2010-02-23 09:17 x -> y
lrwxrwxrwx 1 pjb pjb 1 2010-02-23 09:17 y -> b
Run Code Online (Sandbox Code Playgroud)
很明显,这a->b->c是一个循环,并且该陷阱指向一个循环,但要知道x循环中的点我需要跟随一点.
一个哈希表示是:
a => b
b => c
c => a
trap => b
x => y
y => b
Run Code Online (Sandbox Code Playgroud)
但是一旦我知道循环是什么,反向表示更好地将循环标记为坏起点.
所以这里有一些问题:
有一个图表上CPAN模块,你可以在下面的使用方法:
#! /usr/bin/perl
use warnings;
use strict;
use Graph;
my $g = Graph->new;
my $dir = @ARGV ? shift : ".";
opendir my $dh, $dir or die "$0: opendir $dir: $!";
while (defined(my $name = readdir $dh)) {
my $path = $dir . "/" . $name;
if (-l $path) {
my $dest = readlink $path;
die "$0: readlink $path: $!" unless defined $dest;
$g->add_edge($name => $dest);
}
else {
$g->add_vertex($name);
}
}
my @cycle = $g->find_a_cycle;
if (@cycle) {
$" = ' -> '; #" # highlighting error
print "$0: $dir: at least one cycle: @cycle\n";
}
else {
print "$0: $dir: no cycles\n";
}
Run Code Online (Sandbox Code Playgroud)
例如,在与您的问题中的结构类似的目录中,输出为
$ ../has-cycle ../has-cycle: .: at least one cycle: c -> a -> b