Q: Is file readable by all users?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Troll

    Q: Is file readable by all users?

    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


  • Troll

    #2
    Re: Is file readable by all users?


    "Troll" <abuse@microsof t.com> wrote in message
    news:MDsbb.1162 24$bo1.40748@ne ws-server.bigpond. net.au...[color=blue]
    > 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
    >
    >[/color]


    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 :)





    Comment

    • Troll

      #3
      Re: Is file readable by all users?

      *snip*
      [color=blue]
      > 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[/color]
      all[color=blue]
      > 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 :)
      >[/color]

      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...





      Comment

      • Troll

        #4
        Re: Is file readable by all users?

        *snip*[color=blue][color=green]
        > > then the file is readable by all users.
        > > How can I assign the above printf output to a scalar?
        > >
        > > Any pointers welcome :)
        > >[/color]
        >
        > 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...
        >
        >[/color]


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

        Thanks.




        Comment

        • Irene Mettias

          #5
          Re: Q: Is file readable by all users?

          "Troll" <abuse@microsof t.com> wrote in message news:<MDsbb.116 224$bo1.40748@n ews-server.bigpond. net.au>...[color=blue]
          > 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[/color]

          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

          Comment

          Working...