I've been trying to figure out how to use File::stat to check file modes but haven't had much luck understanding how it works from the documentation. My
goal is to check if a file is owned by a specific user, group owned by a
specific group and has mode 660.  I have the uid and gid checks down but
need help with with the mode check.

So I'm doing the following test:

use File::stat;
my $conffile = "/etc/slist.conf";
my $perms = stat($conffile)
   || die "can't find $conffile: $!\n";
my $mode = $perms->mode;
print "$mode\n";

against the following file:

----------   1 joe  uucp  311 Nov 14 15:20 slist.conf

When I run the code, here's what I get:

32768

The output varies as I change the file mode but I don't see the relation
between the code output and the mode. If someone can explain to me how this works, it would be greatly appreciated. I'm running this on a MacOS X box
but the code needs to run cross-unix-platform.

I did find the following example in Perl Cookbook but I can't make sense of the "if" and 2nd "unless" conditions so if someone can please explain what's
happening, that would be great.

# check whether group or other can write file.
    # use 066 to detect either reading or writing
    if ($info->mode & 022) {   # someone else can write this
        return 0 unless -d _;  # non-directories aren't safe
            # but directories with the sticky bit (01000) are
        return 0 unless $info->mode & 01000;
    }
    return 1;

Attachment: smime.p7s
Description: S/MIME cryptographic signature

Reply via email to