Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
By itself, it seems that unix find will not keep track of the number of links associated with a given inode, and won't stop as soon as that number of links is found.

And of course, if the given path to search is not the root directory of a disk volume, it's possible that one or more links to the given target file will be outside the search space, so adding a bunch of code to ditch out early won't help.

But assuming you don't need to worry about that kind of anomaly, you can tailor the perl script to short-circuit the find task like this:

#!/usr/bin/perl use strict; use warnings; my ( $path, $file ) = @ARGV; die "Usage: $0 search/path data.file\n" unless ( -d $path and -f $file ); my ( $inode, $nlinks ) = ( stat _ )[1,3]; die "$file has no hard links\n" if $nlinks == 1; my ( $chld, $nfound, @found ); $SIG{HUP} = sub { $nfound++; `kill $chld` if $nfound == $nlinks }; $chld = open( FIND, "-|", "find $path -inum $inode -print0 -exec kill +-HUP $$ \\;" ) or die "find: $!\n"; $/ = chr(0); while ( <FIND> ) { chomp; push @found, $_; } printf( "found %d of %d links for %s in %s\n", scalar @found, $nlinks, $inode, $path ); print join( "\n", @found ), "\n";
My first attempt involved just checking the size of @found inside the while (<FIND>) loop, but it turns out that the output from the find process will be buffered, and perl will just wait until the process finishes.

The above script works because the child process sends a HUP signal to the parent each time a file is found (note the double backslash to escape the semi-colon properly). The parent kills the child as soon as the expected count is reached, the child's output buffer gets flushed, and the parent can finish up right away.

I tested it on a path that would normally take 30 seconds to traverse with unix find. I watched the initial output from the find run, and created some hard links in one of the directories that would be found early in the process. The above code found those links, reported them correctly, and finished in less than 1 second.

(UPDATE: Added a "die" condition if stat returns a link count of 1 -- no need to run find in this case.)

(Another update: shuffled the code slightly so that the signal handler gets set up before the child process gets started.)


In reply to Re^3: aborting File::Find::find by graff
in thread aborting File::Find::find by marvell

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-20 00:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found