Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Dynamically determinig fstab file name

by rgren925 (Beadle)
on Nov 23, 2022 at 00:28 UTC ( [id://11148311]=perlquestion: print w/replies, xml ) Need Help??

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

I have a script that runs on linux, solaris, hp-ux and aix.

It uses Sys::Filesystem to get fs info. Occasionally the account this script runs under does not have read permission to the fstab file. Depending on platform, this file has different names (/etc/fstab, /etc/vfstab, etc.).

I would like a way to dynamically determine what fstab file Sys::Filesystem is using and check upfront for readability. The doc for Sys::Filesystem says the default at runtime is $Sys::Filesystem::FSTAB but that (variable?) has no value when I try to print it.

Any ideas?

Thanks, Rick

Replies are listed 'Best First'.
Re: Dynamically determinig fstab file name
by Fletch (Bishop) on Nov 23, 2022 at 00:43 UTC

    Reading The Fine Source it appears that variable is just used as the fallback fstab argument used IFF it's defined and the caller hasn't passed that key to the constructor. It doesn't look like it's explicitly populated. That being said it looks like you could be slightly naughty (in breaking encapsulation) and peek inside the object and pull it out with something like $foo->{fstab} after you have an instance.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: Dynamically determinig fstab file name
by kcott (Archbishop) on Nov 23, 2022 at 13:24 UTC

    G'day Rick,

    As far as I can tell, Sys::Filesystem does not report what fstab file it uses. It relies on the various Sys::Filesystem::<OS> modules, e.g. Sys::Filesystem::AIX, for the filesystem information. See the "Sys-Filesystem distro page" for a list of those modules.

    If you simply want to perform some upfront sanity checks, perhaps code similar to the following might suffice.

    #!/usr/bin/env perl use 5.010; use strict; use warnings; use Sys::Filesystem (); if (Sys::Filesystem::->supported()) { my $fs = Sys::Filesystem::->new(); say '$Sys::Filesystem::FSTAB: ', $Sys::Filesystem::FSTAB // 'undefined'; say '$fs->{fstab}: ', $fs->{fstab} // 'undefined'; say "O/S '$^O' supported."; say 'Using: ', ref($fs->{filesystems}); my @fs_list = $fs->filesystems(); if (@fs_list) { say 'Filesystems found:'; say "\t$_" for @fs_list; } else { warn "No filesystems found.\n"; } use Data::Dump; say '=' x 60; say 'Sys::Filesystem Object:'; say '=' x 60; dd $fs; say '-' x 60; } else { warn "O/S '$^O' not supported.\n"; }

    I've included code to produce a lot of additional information which you may find interesting, but probably isn't wanted in production.

    I'm strongly against breaking encapsulation and urge you to stick with the published API. I only included $fs->{fstab} and $fs->{filesystems} for demonstration purposes.

    I don't have any of the four OSes you list available at home. Here's the output on my Cygwin system.

    $Sys::Filesystem::FSTAB: undefined $fs->{fstab}: undefined O/S 'cygwin' supported. Using: Sys::Filesystem::Cygwin Filesystems found: / /cygdrive/c /cygdrive/d /usr/bin /usr/lib ============================================================ Sys::Filesystem Object: ============================================================ bless({ aliases => { boot_order => ["fs_mntno"], check_frequency => ["fs_freq"], check_order => ["fs_passno"], device => ["fs_spec", "dev"], filesystem => ["fs_file", "mount_point"], format => ["fs_vfstype", "vfs", "vfstype"] +, label => ["fs_label"], mount_point => ["fs_file", "filesystem"], options => ["fs_mntops"], type => ["fs_vfstype", "vfs"], volume => ["fs_volume", "fs_vol", "vol"], }, canondev => undef, filesystems => bless({ "/" => { device => "C:/cygwin64", fs_file => "/", fs_mntops => "binary,auto", fs_spec => "C:/cygwin64", fs_vfstype => "ntfs", mount_point => "/", mounted => 1, }, "/cygdrive/c" => { device => "C:", fs_file => "/cygdrive/c", fs_mntops => "binary,posix=0,user,noumount,auto +", fs_spec => "C:", fs_vfstype => "ntfs", mount_point => "/cygdrive/c", mounted => 1, }, "/cygdrive/d" => { device => "D:", fs_file => "/cygdrive/d", fs_mntops => "binary,posix=0,user,noumount,auto +", fs_spec => "D:", fs_vfstype => "ntfs", mount_point => "/cygdrive/d", mounted => 1, }, "/usr/bin" => { device => "C:/cygwin64/bin", fs_file => "/usr/bin", fs_mntops => "binary,auto", fs_spec => "C:/cygwin64/bin", fs_vfstype => "ntfs", mount_point => "/usr/bin", mounted => 1, }, "/usr/lib" => { device => "C:/cygwin64/lib", fs_file => "/usr/lib", fs_mntops => "binary,auto", fs_spec => "C:/cygwin64/lib", fs_vfstype => "ntfs", mount_point => "/usr/lib", mounted => 1, }, }, "Sys::Filesystem::Cygwin"), fstab => undef, mtab => undef, }, "Sys::Filesystem") ------------------------------------------------------------

    — Ken

      Hi Ken

      Thanks very much for the reply.

      I just tried your code on RHEL 7. Here's the salient output:

      $Sys::Filesystem::FSTAB: undefined $fs->{fstab}: undefined O/S 'linux' supported. Using: Sys::Filesystem::Linux

      An environment issue?

      Rick

        "An environment issue?"

        I don't understand the question. What are you asking about the environment? What issue do think exists?

        Other than "[Ll]inux", instead of "[Cc]ygwin", that's identical to my first four lines of output. I assume you got, but didn't post, subsequent lines about "filesystems found", followed by dd $fs output.

        — Ken

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11148311]
Approved by kcott
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-04-26 09:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found