Q: Is file readable by all users?

T

Troll

Hi,

Would something like:

if (-r $name) {
: # do nothing
} else {
print "File is not readable...\n";
}

be enough to test if a file is readable by all users?
I'm a little unsure which bit does the -r relate to.

What about if I wanted to test whether the file is readable by the owner's
group but not everyone else?

Cheers
 
T

Troll

Troll said:
Hi,

Would something like:

if (-r $name) {
: # do nothing
} else {
print "File is not readable...\n";
}

be enough to test if a file is readable by all users?
I'm a little unsure which bit does the -r relate to.

What about if I wanted to test whether the file is readable by the owner's
group but not everyone else?

Cheers


I had a look at stat and can read the permissions. But what I need to do
then is change the file permissions ONLY if the file is not readable by all
users.
Should I be trying to capture the output of:

$mode = (stat "test.txt") [2];
printf "%0\n", $mode & 07777; # this gives me the original permissions

and then sed-ing it so as to read the last character? If the last char > 3
then the file is readable by all users.
How can I assign the above printf output to a scalar?

Any pointers welcome :)
 
T

Troll

*snip*
I had a look at stat and can read the permissions. But what I need to do
then is change the file permissions ONLY if the file is not readable by all
users.
Should I be trying to capture the output of:

$mode = (stat "test.txt") [2];
printf "%0\n", $mode & 07777; # this gives me the original permissions

and then sed-ing it so as to read the last character? If the last char > 3
then the file is readable by all users.
How can I assign the above printf output to a scalar?

Any pointers welcome :)

Ok, got it.
$permissions = sprintf "%0\n", $mode & 07777;

Silly Q: How do I check if the last digit is > 3? My mind is blank atm...
 
T

Troll

*snip*
Ok, got it.
$permissions = sprintf "%0\n", $mode & 07777;

Silly Q: How do I check if the last digit is > 3? My mind is blank atm...


Is there something simpler than:
if ($permissions =~ m/^..[4-7]/) {

Thanks.
 
I

Irene Mettias

Troll said:
Hi,

Would something like:

if (-r $name) {
: # do nothing
} else {
print "File is not readable...\n";
}

be enough to test if a file is readable by all users?
I'm a little unsure which bit does the -r relate to.

What about if I wanted to test whether the file is readable by the owner's
group but not everyone else?

Cheers

The -r test only checks if the user running the program can read that
file.
To check if all users can read a file use the 'mode' returned by the
stat() function (3rd returned element).

use Fcntl ':mode';

$mode = (stat($filename))[2];
$other_read = $mode & S_IROTH;

Check 'perldoc perlfunc' for a full example on how to interpret
'mode'.

Hope that helps.

Irene
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top