Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^2: Restrict file search within current filessystem using wanted subroutine

by madparu (Initiate)
on Apr 28, 2016 at 12:40 UTC ( [id://1161763]=note: print w/replies, xml ) Need Help??


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

Hello Hauke,

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

Thanks!

Replies are listed 'Best First'.
Re^3: Restrict file search within current filessystem using wanted subroutine
by haukex (Archbishop) on Apr 28, 2016 at 12:57 UTC

    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.

        Hi Anonymous,

        "All flavours of Unix" is a very broad question that is difficult to answer generally, but FWIW I believe stat(2) is a relatively well-standardized function. What specific flavors of Unix do you want to run your script on which you are concerned about?

        Also, have a look at perlport, which lists known issues with stat across various platforms.

        Hope this helps,
        -- Hauke D

        all flavours of Unix

        Well, I could take some generic linux and patch stat() out. Would it still be a Unix flavour? Perhaps. Does that imaginary patch make sense? Most likely not.

        So, what is a "flavour of Unix"?

        As a first rule of thumb, I would ask for POSIX compatibility. Not every Unix implements all of POSIX, and not every POSIX compatible system is a Unix. But:

        POSIX defines how stat and friends should behave. It also defines a minimal struct stat.

        In fact, for such old and essential functions like stat(), POSIX usually just documents what has been done for ages, and most operating systems derived from Unix in one or the other way behave reasonable similar.

        If you happen to run perl on Windows, you will find that many functions still work as if you were on a Unix, but there are some differences. For stat, the st_dev field seems to work, at least for volumes mapped to drive letters:

        C:\Users\alex>perl -MFile::stat -E "for my $d (qw( C: D: H: I: M: T: X +: )) { my $st=stat(qq<$d\\.>); say $d,' ',$st->dev() }" C: 2 D: 3 H: 7 I: 8 M: 12 T: 19 X: 23 C:\Users\alex>

        (Tested with Strawberry Perl 5.14.2 on Windows 7)

        Things go wrong when you remap directories to other drive letters using the subst command from the old days of MS-DOS:

        C:\Users\alex>subst n: c:\ C:\Users\alex>perl -MFile::stat -E "for my $d (qw( C: D: H: I: M: N: T +: X: )) { my $st=stat(qq<$d\\.>); say $d,' ',$st->dev() }" C: 2 D: 3 H: 7 I: 8 M: 12 N: 13 T: 19 X: 23 C:\Users\alex>

        C: and N: refer to the same volume on the same device, but the st_dev field returns different device numbers (2 vs. 13). I think it's just the drive letter, minus 'A' (i.e. 0=A:, 1=B:, ..., 25=Z:).

        Other parts of stat() are emulated in a way that is a little bit surprising, but still compatible enough for most cases, see Re^3: Inline.pm and untainting.

        (Note that other good things from Unix can't be emulated that well on Windows, notably fork(), exec(), and signals. Windows simply lacks the required APIs.)

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (9)
As of 2024-03-19 08:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found