Make cleanup_invalid_links() more explicit

And add some debug.
This commit is contained in:
Adam Spiers 2020-11-02 00:54:15 +00:00
parent 86f4694d96
commit 832135e269

View file

@ -1003,6 +1003,9 @@ sub cleanup_invalid_links {
my $self = shift; my $self = shift;
my ($dir) = @_; my ($dir) = @_;
my $cwd = getcwd();
debug(2, 1, "cleanup_invalid_links for $dir (pwd=$cwd)");
if (not -d $dir) { if (not -d $dir) {
error("cleanup_invalid_links() called with a non-directory: $dir"); error("cleanup_invalid_links() called with a non-directory: $dir");
} }
@ -1019,23 +1022,35 @@ sub cleanup_invalid_links {
my $node_path = join_paths($dir, $node); my $node_path = join_paths($dir, $node);
if (-l $node_path and not exists $self->{link_task_for}{$node_path}) { next unless -l $node_path;
# Where is the link pointing? debug(2, 2, "checking validity of link $node_path");
# (don't use read_a_link() here)
my $source = readlink($node_path);
if (not $source) {
error("Could not read link $node_path");
}
if ( if (exists $self->{link_task_for}{$node_path}) {
not -e join_paths($dir, $source) and # bad link die "huh? link_task_for $node_path";
$self->path_owned_by_package($node_path, $source) # owned by stow }
){
debug(2, 0, "--- removing stale link: $node_path => " . # Where is the link pointing?
join_paths($dir, $source)); # (don't use read_a_link() here)
$self->do_unlink($node_path); my $source = readlink($node_path);
} if (not $source) {
error("Could not read link $node_path");
}
if (-e join_paths($dir, $source)) {
debug(4, 3, "link target $source exists; skipping clean up");
next;
}
debug(2, 2,
"checking whether valid link $node_path -> $source is " .
"owned by stow");
if ($self->path_owned_by_package($node_path, $source)) {
# owned by stow
debug(2, 0, "--- removing stale link: $node_path => " .
join_paths($dir, $source));
$self->do_unlink($node_path);
} }
} }
return; return;