What does third symbol in "rw-" file mode mean?

680 views Asked by At

I have trouble understanding ls's manual regarding to file that has rw- mode. Here's the quote:

  1. If r, the file is readable; if -, it is not readable.

  2. If w, the file is writable; if -, it is not writable.

  3. The first of the following that applies:

    S If in the owner permissions, the file is not executable and set-user-ID mode is set. If in the group permissions, the file is not executable and set-group-ID mode is set.

    s If in the owner permissions, the file is executable and set-user-ID mode is set. If in the group permissions, the file is executable and setgroup-ID mode is set.

    x The file is executable or the directory is searchable.

    - The file is neither readable, writable, executable, nor set-user-ID nor set-group-ID mode, nor sticky.

In particular, it seem that two sections in bold contradict each other: according to the first one, since the mode begins with r, the file is readable, but according to the last one, the file is not readable. But, obviously, that is not the case.

So, what does that third section mean about file being "neither readable, writable..."?

2

There are 2 answers

2
cdarke On BEST ANSWER

Your ls man page is not the standard. The POSIX standard man page for ls does not word it in that way. Here is the relevant extract:

Each field shall have three character positions:

  1. If 'r' , the file is readable; if '-' , the file is not readable.

  2. If 'w' , the file is writable; if '-' , the file is not writable.

  3. The first of the following that applies:

    S

      If in <owner permissions>, the file is not executable and set-user-ID mode is set. If in <group permissions>, the file is not executable and set-group-ID mode is set.

    s

      If in <owner permissions>, the file is executable and set-user-ID mode is set. If in <group permissions>, the file is executable and set-group-ID mode is set.

    T

      If in <other permissions> and the file is a directory, search permission is not granted to others, and the restricted deletion flag is set.

    t

      If in <other permissions> and the file is a directory, search permission is granted to others, and the restricted deletion flag is set.

    x

      The file is executable or the directory is searchable.

    -

      None of the attributes of 'S' , 's' , 'T' , 't' , or 'x' applies.

Which I think makes more sense.

The type is not represented in the permissions bits – you are only looking at what ls(1) reports, not how it is stored. Traditionally the mode and type together use 32-bits, but that depends on the file system and on many it is now 64-bits. The permissions are only 9 bits in the inode.

See man 2 stat and search for st_mode. It is the low level C routine that ls(1) probably uses. On some platforms it is also available as a command-line program man 1 stat.

2
rojomoke On

Each numbered point in your quote applies in turn to each of the three characters.

If the first character is 'r', the file is readable

If the second character is 'w', the file is writable

If the third character is 'x/s/S', the file has the listed property

If the character is '-', then the file does not have that property.

The first three characters (after the directory identifier) apply to user permissions, the second three to group permissions, and the third to everyone else.