Plankton has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I am trying to find a way to avoid having to resort to shell when checking whether or not a directory is a mount point of a filesystem.

I looked at the file test operators but did find one that would do what I want. I can always do something like:

my $ismount =`/bin/mount | grep /opt/mymountpoint`;
but I want to avoid shelling out :)))

  • Comment on How best test that a directory is a mount point of a mounted filesystem?
  • Download Code

Replies are listed 'Best First'.
Re: How best test that a directory is a mount point of a mounted filesystem?
by merlyn (Sage) on Jan 27, 2009 at 18:03 UTC
    Do you mean "potential mount point", or "currently active mount point"? For the latter, compare the dev number from stat for the directory, and for the directory with /.. appended. If the dev numbers are different, it's an active mount point.
Re: How best test that a directory is a mount point of a mounted filesystem?
by kyle (Abbot) on Jan 27, 2009 at 17:33 UTC

    What's your OS? On my Linux system, I can open and read /proc/mounts to get the same thing mount would tell me.

      The OS will most likely be Linux but this could get run a some legacy Solaris systems.

        IIRC solaris uses /etc/mnttab, so you could do something like this:

        my ($mtab, $mounted); $mounted = 0; if($^O eq 'linux') { $mtab = '/etc/mnttab'; } else { $mtab = '/etc/mtab'; } open my $mounts, "<$mtab" or die($!); while(<$mounts>) { if($_ =~ /\S+ (\S+) .*/) { $mounted = 1 if($1 eq '/opt/mymountpoint'); } }
Re: How best test that a directory is a mount point of a mounted filesystem?
by leocharre (Priest) on Jan 27, 2009 at 19:30 UTC
    Check this out: Sys::Filesystem::ID

    If you look at the code.. The sub _arg_to_mount_point() takes a filepath and returns mount point.

    my $mount_point = _arg_to_mount_point('/usr/share/stuff/file2.pdf');

    Might have to be altered for more.. Hmm.. Could that be of use?

    Sys::Filesystem rocks.