dagolden / Path-Tiny

File path utility
41 stars 59 forks source link

Feature request: Get file/directory permissions #295

Closed scottchiefbaker closed 1 month ago

scottchiefbaker commented 1 month ago

Recently I need to determine if a file was world readable which requires fetching the permissions, and masking out the "other" permissions from the result. I ended up with this:

sub get_file_permissions {
    my $file = shift();

    my @x    = stat($file);
    my $mode = $x[2];

    my $user  = ($mode & 0700) >> 6;
    my $group = ($mode & 0070) >> 3;
    my $other = ($mode & 0007);

    my @ret = ($user, $group, $other);

    return @ret;
}

Is this something you would consider for inclusion in Path::Tiny? I can work on a PR if this is something you would be willing to land.

karenetheridge commented 1 month ago

Perhaps an interface like ->permissions('user'), ->permissions('group')..?

scottchiefbaker commented 1 month ago

That's not too bad... Alternately we could do something like

$bool = $path->is_readable('user');
$bool = $path->is_writable('group');
$bool = $path->is_executable('other');
scottchiefbaker commented 1 month ago

This ended up being not as complicated as I thought so I coded it up and landed a PR for it.

xdg commented 1 month ago

NB. PR is #296 .

xdg commented 1 month ago

Sorry it's taken me a while to get to this. After some considerations, I'm going to pass on this API extension. Path::Tiny already provides stat, so these sorts of checks are relatively straightforward uses of $path->stat->mode.