Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Remove broken symlinks with IO::All

by Dylan (Monk)
on Aug 12, 2006 at 02:29 UTC ( #566997=snippet: print w/ replies, xml ) Need Help??

Description:

This is a short script using IO::All to remove broken symlinks.

Note, it has to redefine the readlink() method, because as of version 0.35 of IO::All, readlink doesn't handle relative symlinks.

IO::All sure makes my life easier!

#!/usr/bin/perl
use strict;
use warnings;
use IO::All::Link; # Because we redefine readlink()...
use IO::All;

my $file = shift @ARGV || '.';
my @ios = ( io($file) );

while (my $io = shift @ios) {
    if ($io->is_dir) {
        push @ios, $io->all;
    } elsif ($io->is_link and not $io->readlink->exists) {
        print "$io is broken, removing.\n";
        $io->unlink;
    }
}

# this has been submitted as a patch to IO::All...
no warnings 'redefine';
sub IO::All::Link::readlink {
    my $io = shift;
    my $path = CORE::readlink($io->name);
    unless (File::Spec->file_name_is_absolute($path)) {
        $path = File::Spec->join($io->filepath, $path);
    }
    IO::All->new($path);
}
Comment on Remove broken symlinks with IO::All
Download Code
Re: Remove broken symlinks with IO::All
by graff (Chancellor) on Aug 13, 2006 at 03:30 UTC
    Very nice -- thank you, especially for handling relative links. I would just add a couple things, at first glance:

    1. When deleting a stale symlink, the report about the deletion should include the stale target path.

    2. It would be prudent to have a "-n" mode (no deletions, just report the paths and targets of stale links).

    Sometimes, when you detect a stale link, it could be that you would rather just change the target (e.g. because the target was moved to a new disk volume), or it could be that the target is only "temporarily" inaccessible (e.g. because an nfs or samba server is down).

    At the very least, provide enough logging so that it's easy to re-install a deleted link, in case deletion was the wrong thing to do. Better yet, simply report stale links and their targets, and give the user a chance to decide what the appropriate action should be (delete it, replace it, or make sure a given network disk volume comes back online).

    Here's a script I wrote a while back to list all links (good and bad, including link chains:  link.1 -> link.2 -> ... -> data.file ). This was done before I knew about IO::All, so it uses unix "find" instead.

      It's mostly a replacement for 'cleanlinks', which comes with Xorg (and Xfree86, too). :)

Back to Snippets Section

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://566997]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2014-09-21 23:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (176 votes), past polls