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

Discussion in 'Perl Misc' started by Justin, Oct 30, 2003.

  1. Justin

    Justin Guest

    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]
     
    Justin, Oct 30, 2003
    #1
    1. Advertisements

  2. Justin

    Mike Stok Guest

    In article <3fa16552$0$560$>,
    Justin <> wrote:
    >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

    --
    | The "`Stok' disclaimers" apply.
    http://www.stok.co.uk/~mike/ | GPG PGP Key 1024D/059913DA
    | Fingerprint 0570 71CD 6790 7C28 3D60
    http://www.exegenix.com/ | 75D2 9EC4 C1C0 0599 13DA
     
    Mike Stok, Oct 30, 2003
    #2
    1. Advertisements

  3. Justin <> wrote:
    > 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.

    --
    Steve
     
    Steve Grazzini, Oct 30, 2003
    #3
  4. Justin

    Justin Guest

    Re: File test operator (-r) returns 'not exist' rather than 'notreadable'

    Steve Grazzini wrote:
    > Justin <> wrote:
    >
    >>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
     
    Justin, Oct 30, 2003
    #4
  5. Justin

    Ben Morrow Guest

    Re: File test operator (-r) returns 'not exist' rather than 'notreadable'

    Justin <> wrote:
    > Steve Grazzini wrote:
    > > Justin <> wrote:
    > >
    > >>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

    --
    Musica Dei donum optimi, trahit homines, trahit deos. |
    Musica truces molit animos, tristesque mentes erigit. |
    Musica vel ipsas arbores et horridas movet feras. |
     
    Ben Morrow, Oct 30, 2003
    #5
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fuzzyman
    Replies:
    3
    Views:
    602
    Andrew MacIntyre
    Dec 5, 2003
  2. Robert Brewer
    Replies:
    0
    Views:
    582
    Robert Brewer
    Dec 5, 2003
  3. tiewknvc9
    Replies:
    12
    Views:
    1,458
    Chris Uppal
    Feb 19, 2007
  4. =?Utf-8?B?U2hhbg==?=
    Replies:
    0
    Views:
    385
    =?Utf-8?B?U2hhbg==?=
    Jun 29, 2007
  5. Replies:
    0
    Views:
    502
Loading...

Share This Page