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

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

by Anonymous Monk
on May 12, 2016 at 09:27 UTC ( [id://1162846]=note: print w/replies, xml ) Need Help??


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

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

Replies are listed 'Best First'.
Re^5: Restrict file search within current filessystem using wanted subroutine
by haukex (Archbishop) on May 12, 2016 at 09:47 UTC

    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

      Hi Hauke,

      Trying to run it on Linux, AIX, HP-UX and Solaris.

      --madparu

        Hi madparu,

        Unfortunately I don't know enough about all of those systems to give you a good answer. But since AIX, HP-UX and Solaris are (in various versions) POSIX certified, and none of perlport, perlsolaris, perlhpux, or perlaix seem to mention any issues with stat on those systems, I'd make the educated guess that the "dev" field of stat should work the same on all of those OSes. Update: Just to be clear, I don't mean that the device IDs will be the same across those OSes - what I mean is that I think the above method of detecting file system boundaries should work on each of those OSes.

        Of course, you can just try it out on each system:

        perl -le 'print "$_\tdev=",(stat)[0] for @ARGV' /var /var/log

        Regards,
        -- Hauke D

Re^5: Restrict file search within current filessystem using wanted subroutine
by afoken (Chancellor) on May 12, 2016 at 20:25 UTC
    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://1162846]
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found