Re: Change in behavior to stat(1)

2011-03-05 Thread Doug Barton

On 03/04/2011 11:05, Jilles Tjoelker wrote:

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?


A better answer to your original question was already given, but for
that command, isn't it sufficient to do

   if ! [ -e $link ]; then rm $link; fi

All test(1)'s primaries that test things about files follow symlinks,
except for -h/-L.


I'd do '[ -e "$link" ] || unlink $link' but Jilles is definitely right 
that simply using 'test -e' is the way to go.


Stephen, sorry to hear that the change in behavior to stat(1) was 
troubling to you. A little bit of the history might be useful. I 
originally imported stat(1) from NetBSD in 2002, but did not keep up 
with the improvements that NetBSD made to it. I recently found time to 
catch up with the work that they've done, and the change to the behavior 
of readlink seemed like a useful one so I brought it over. hopefully it 
won't cause too many more problems. :)



Doug

--

Nothin' ever doesn't change, but nothin' changes much.
-- OK Go

Breadth of IT experience, and depth of knowledge in the DNS.
Yours for the right price.  :)  http://SupersetSolutions.com/

___
freebsd-stable@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-stable
To unsubscribe, send any mail to "freebsd-stable-unsubscr...@freebsd.org"


Re: Change in behavior to stat(1)

2011-03-04 Thread Jilles Tjoelker
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?

A better answer to your original question was already given, but for
that command, isn't it sufficient to do

  if ! [ -e $link ]; then rm $link; fi

All test(1)'s primaries that test things about files follow symlinks,
except for -h/-L.

-- 
Jilles Tjoelker
___
freebsd-stable@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-stable
To unsubscribe, send any mail to "freebsd-stable-unsubscr...@freebsd.org"


Re: Change in behavior to stat(1)

2011-02-28 Thread Stephen Montgomery-Smith

Jeremy Chadwick wrote:


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)


Brilliant!

Since this is *precisely* the purpose of my script, I think I will use 
this code instead,

___
freebsd-stable@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-stable
To unsubscribe, send any mail to "freebsd-stable-unsubscr...@freebsd.org"


Re: Change in behavior to stat(1)

2011-02-28 Thread Jeremy Chadwick
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   j...@parodius.com |
| Parodius Networking   http://www.parodius.com/ |
| UNIX Systems Administrator  Mountain View, CA, USA |
| Making life hard for others since 1977.   PGP 4BD6C0CB |

___
freebsd-stable@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-stable
To unsubscribe, send any mail to "freebsd-stable-unsubscr...@freebsd.org"


Re: Change in behavior to stat(1)

2011-02-28 Thread Ronald Klop

On Mon, 28 Feb 2011 23:39:10 +0100, jhell  wrote:



On Mon, 28 Feb 2011 12:15, stephen@ 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?

Stephen



You might find sysutils/symlinks interesting. I have been using it a  
long time and have not had to consider adjusting much in the way of  
shell scripting to remove dirty links.


  -c == change absolute/messy links to relative
  -d == delete dangling links
  -o == warn about links across file systems
  -r == recurse into subdirs
  -s == shorten lengthy links
  -t == show what would be done by -c
  -v == verbose (show all symlinks)


Quite interesting though how such a little tweak has caused a massive  
expansion of your command line and required utils.



Good luck,



Find has some voodoo for handling links also.

Ronald.
___
freebsd-stable@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-stable
To unsubscribe, send any mail to "freebsd-stable-unsubscr...@freebsd.org"


Re: Change in behavior to stat(1)

2011-02-28 Thread jhell


On Mon, 28 Feb 2011 12:15, stephen@ 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?

Stephen



You might find sysutils/symlinks interesting. I have been using it a long 
time and have not had to consider adjusting much in the way of shell 
scripting to remove dirty links.


 -c == change absolute/messy links to relative
 -d == delete dangling links
 -o == warn about links across file systems
 -r == recurse into subdirs
 -s == shorten lengthy links
 -t == show what would be done by -c
 -v == verbose (show all symlinks)


Quite interesting though how such a little tweak has caused a massive 
expansion of your command line and required utils.



Good luck,

--

 jhell

___
freebsd-stable@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-stable
To unsubscribe, send any mail to "freebsd-stable-unsubscr...@freebsd.org"


Change in behavior to stat(1)

2011-02-28 Thread Stephen Montgomery-Smith
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?

Stephen
___
freebsd-stable@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-stable
To unsubscribe, send any mail to "freebsd-stable-unsubscr...@freebsd.org"