File test operator (-r) returns 'not exist' rather than 'not readable'

J

Justin

Hi,

I've noticed that the -r operator returns undef if used on a file in an
unreadable directory (where undef indicates the file is missing) - I'd
like -r to return false (unreadable) in such a case.

I'm comparing our back-up file-list to the file-system. I need to know
if a file exists or if I just can't read it.

ls returns 'Permission Denied' which is what I'd like -r to return.

Any ideas?

Cheers,

Justin

Here's an example of what I mean:

[/tmp] mkdir Test
[/tmp] touch Test/testfile
[/tmp] ls -l Test
total 0
-rw-rw-r-- 1 ja 0 Oct 30 12:58 testfile
[/tmp] chmod 000 Test
[/tmp] perl -e 'print "Not Exist\n" unless defined (-r "Test/testfile")'
Not Exist
[/tmp] ls Test/testfile
ls: Test/testfile: Permission denied
[/tmp] perl -e 'print "Not Exist\n" unless defined (-r "Test")'
[/tmp]
 
M

Mike Stok

Hi,

I've noticed that the -r operator returns undef if used on a file in an
unreadable directory (where undef indicates the file is missing) - I'd
like -r to return false (unreadable) in such a case.

I'm comparing our back-up file-list to the file-system. I need to know
if a file exists or if I just can't read it.

ls returns 'Permission Denied' which is what I'd like -r to return.

Any ideas?
[/tmp] perl -e 'print "Not Exist\n" unless defined (-r "Test/testfile")'
Not Exist

[mike@ratdog src]$ touch foo
[mike@ratdog src]$ chmod 0 foo
[mike@ratdog src]$ perl -e 'print "Not Exist\n" unless -e "foo"'
[mike@ratdog src]$ perl -e 'print "Not Readable\n" unless -r "foo"'
Not Readable
[mike@ratdog src]$

Hope this helps,

Mike
 
S

Steve Grazzini

Justin said:
I've noticed that the -r operator returns undef if used on a
file in an unreadable directory (where undef indicates the file
is missing) - I'd like -r to return false (unreadable) in such
a case.

It doesn't matter whether the directory is readable -- it's the
execute bit (search bit) that determines whether you can stat() a
file in that directory.
I'm comparing our back-up file-list to the file-system. I need to know
if a file exists or if I just can't read it.

If you can't read *or* search the directory, you simply can't know
whether the file exists or not. If you can read it, you could use
opendir() to check...
ls returns 'Permission Denied' which is what I'd like -r to return.

But this is easy; just check $! if the file test returns undef.
 
J

Justin

Steve said:
Justin said:
I've noticed that the -r operator returns undef if used on a
file in an unreadable directory (where undef indicates the file
is missing) - I'd like -r to return false (unreadable) in such
a case. [snip]
ls returns 'Permission Denied' which is what I'd like -r to return.

But this is easy; just check $! if the file test returns undef.

That's the key - I checked if $! eq 'Permission denied' after an undef
-r, that tells me if it failed due to an unreadable dir.

Thanks Steve - much appreciated!

Justin
 
B

Ben Morrow

Justin said:
Steve said:
Justin said:
I've noticed that the -r operator returns undef if used on a
file in an unreadable directory (where undef indicates the file
is missing) - I'd like -r to return false (unreadable) in such
a case. [snip]
ls returns 'Permission Denied' which is what I'd like -r to return.

But this is easy; just check $! if the file test returns undef.

That's the key - I checked if $! eq 'Permission denied' after an undef
-r, that tells me if it failed due to an unreadable dir.

Better would be to use Errno (or perl -MErrno) and check $!{EACCES}.

Ben
 

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

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,188
Latest member
Crypto TaxSoftware

Latest Threads

Top