Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 28 Feb 2011 15:32:07 -0800
From:      Jeremy Chadwick <freebsd@jdc.parodius.com>
To:        Stephen Montgomery-Smith <stephen@missouri.edu>
Cc:        FreeBSD Stable <freebsd-stable@freebsd.org>
Subject:   Re: Change in behavior to stat(1)
Message-ID:  <20110228233207.GA33800@icarus.home.lan>
In-Reply-To: <4D6BD83B.3040609@missouri.edu>
References:  <4D6BD83B.3040609@missouri.edu>

next in thread | previous in thread | raw e-mail | index | archive | help
On Mon, Feb 28, 2011 at 11:15:39AM -0600, Stephen Montgomery-Smith wrote:
> I had a little script that would remove broken links.  I used to do
> it like this:
> 
> if ! stat -L $link > /dev/null; then rm $link; fi
> 
> But recently (some time in February according to the CVS records)
> stat was changed so that stat -L would use lstat(2) if the link is
> broken.
> 
> So I had to change it to
> 
> if stat -L $link | awk '{print $3}' | grep l > /dev/null;
> then rm $link; fi
> 
> but it is a lot less elegant.
> 
> What is the proper accepted way to remove broken links?

If your complaint is the literal length of the line, you should be
able to change your one-liner to:

if stat -L -f %Sp $link | grep l > /dev/null; then rm $link; fi

Though I agree this is less elegant.  Unrelated (but worth noting), be
aware your one-liner will break horribly with files that contains
spaces; use "$link" instead.

Possibly you could use the example from the find(1) man page:

   find -L /usr/ports/packages -type l -exec rm -- {} +
           Delete all broken symbolic links in /usr/ports/packages.

(Note that the "+" on the end is not a typo, see the man page)

Otherwise, possibly someone should add a flag to stat(1) that inhibits
falling back on lstat(2).

-- 
| Jeremy Chadwick                                   jdc@parodius.com |
| Parodius Networking                       http://www.parodius.com/ |
| UNIX Systems Administrator                  Mountain View, CA, USA |
| Making life hard for others since 1977.               PGP 4BD6C0CB |




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20110228233207.GA33800>