Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Restrict file search within current filessystem using wanted subroutine

by haukex (Archbishop)
on Apr 28, 2016 at 12:15 UTC ( [id://1161759]=note: print w/replies, xml ) Need Help??


in reply to Restrict file search within current filessystem using wanted subroutine

Hi madparu,

There is a little-known feature of File::Find from find2perl, the variable $File::Find::topdev, which holds the device number of the path currently being searched under, which you can compare with the device number of the current file (stat). Stick the following at the top of your wanted function, and your search will be limited to the filesystems of the paths which you tell File::Find::find to search under.

if ( $File::Find::topdev != (stat)[0] ) { $File::Find::prune = 1; return }

Update: Fixed stat vs. stat(_), apologies. My test code was doing a stat beforehand, so stat(_) worked fine. But if the piece of code above is the very first thing in your wanted function, you should stat $_ first, and only then use the special filehandle _. (The exception is when the follow option is set, then File::Find guarantees that an lstat has been called.)

And by the way, Use strict and warnings!

Hope this helps,
-- Hauke D

Replies are listed 'Best First'.
Re^2: Restrict file search within current filessystem using wanted subroutine
by madparu (Initiate) on Apr 28, 2016 at 12:40 UTC
    Hello Hauke,

    A little confused... Can you please update the code I posted with your recommended piece of code to understand better.

    Thanks!

      Hi madparu,

      Copy the two lines of code I posted above and paste them into your script between the lines "wanted => sub {" and "return unless -f;". If you have any problems, feel free to ask (and post your code along with any error messages you may be getting).

      Simply put, what the two lines do is compare the device number (see stat) of the path you are searching under to the device number of the file currently being inspected. If they don't match, meaning the file currently being inspected is on a different file system than where the search began, then the code tells File::Find not to descend into that directory by setting $File::Find::prune and to not further process the file (return).

      Regards,
      -- Hauke D

        Hello Hauke,

        It works perfectly well..Thanks a lot.. Will get back if I have any more doubts on this

        Hi Hauke, Does stat works on all flavours of Unix? I dont see any issues on Linux and AIX.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2024-04-25 13:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found