dagolden / Path-Tiny

File path utility
41 stars 58 forks source link

Feature request: Get file/directory permissions #295

Closed scottchiefbaker closed 2 months ago

scottchiefbaker commented 2 months 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 2 months ago

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

scottchiefbaker commented 2 months 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 2 months ago

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

xdg commented 2 months ago

NB. PR is #296 .

xdg commented 2 months 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.