How to check if directory is writeable by UID?

1.9k Views Asked by At

Can File::Find::Rule be used to determine if a directory is writeable by a given UID?

I have used the following test:

my $uid = 123; # or whatever...

my @subDirs = File::Find::Rule->permissions(isWriteable => 1, user => "$uid")->directory()->maxdepth(1)->in( $dir );
if (scalar @subDirs == 0) {
    die "$dir is not writeable...";
}
die "$dir is writeable";

But it always returns that the $dir is writeable, even for test directories that I know are restricted.

What adjustments can I make to this test to make it work?

EDIT

Using use File::Find::Rule qw(permissions) caused the script to crash. I have not ever needed to specify this option with previous usages of permissions() in older File::Find::Rule operations.

If I adjust the rule as follows, I get things working.

Here, I am testing for the presence of the parent directory in the subdirectory list:

my @subDirs = File::Find::Rule::Permissions->directory()->permissions(isWriteable => 1, user => "$uid")->maxdepth(1)->name(".")->in("$dir");

The parent directory of the subdirectories of $dir is .

And if that directory exists in the list of @subDirs, it must be writable.

Thanks to ikegami for suggesting this module.

1

There are 1 best solutions below

0
Alex Reynolds On BEST ANSWER

Here, I test for the presence of the parent directory in the subdirectory list:

my @subDirs = File::Find::Rule::Permissions->directory()->permissions(isWriteable => 1, user => "$uid")->maxdepth(1)->name(".")->in("$dir");

The parent directory of the subdirectories of $dir is .

If that directory exists in the list of @subDirs, it must be writable.