Applying Symbolic chmod
Every file on Bob's server has a mode: a 12-bit number usually written as 4 octal digits. From the highest bit to the lowest, the bits are:
| octal value | meaning |
|---|---|
4000 |
setuid |
2000 |
setgid |
1000 |
sticky |
0400 0200 0100 |
read, write, execute for the user class u |
0040 0020 0010 |
read, write, execute for the group class g |
0004 0002 0001 |
read, write, execute for the others class o |
So each class owns three permission bits, with read worth 4, write 2 and execute 1 inside its octal
digit. The setuid bit belongs to u, the setgid bit to g and the sticky bit to o.
Bob changes modes with symbolic specs such as u+rwx,g=u,o-w,a+X,+t. Apply a spec to each of the
given files and print the resulting modes.
Grammar
A spec is a non-empty string matching spec below. Literal characters are in quotes, * means
"zero or more", and nothing else (no spaces) can appear:
spec = clause ( "," clause )*
clause = who* action action*
who = "u" | "g" | "o" | "a"
action = op ( perm* | copy )
op = "+" | "-" | "="
perm = "r" | "w" | "x" | "X" | "s" | "t"
copy = "u" | "g" | "o"
A copy letter always comes directly after an op and is followed by another op, a comma, or the
end of the spec. It is never mixed with perm letters.
Meaning
Clauses are applied from left to right, and within a clause its actions are applied from left to right. Each action sees the mode left by the previous one.
The who letters of a clause pick a set W of classes: u, g and o add that class, and a adds
all three. Letters can repeat. If the clause has no who letters, W is all three classes.
For one action, let M be the mode just before that action. The action builds a set B of bits:
r,w,xadd the read, write or execute bit of every class inW.Xadds the execute bit of every class inW, but only if the file is a directory orMhas at least one of the three execute bits (0111) set. Otherwise it adds nothing.sadds setuid ifuis inWand setgid ifgis inW. It adds nothing foro.tadds the sticky bit ifois inW. It adds nothing foruorg.- A copy letter
ctakes the three permission bits (read, write, execute) of classcinMand adds the same bits for every class inW. Special bits are never copied. - An empty list of perms gives an empty
B.
The operator then decides what happens to the bits in B:
+sets the bits ofB.-clears the bits ofB.=first clears everything owned by the classes inW(their read, write and execute bits and their special bit), then sets the bits ofB.Bwas computed fromMbefore the clearing.
There is no umask: an empty who list really means all classes.
Input
The first line contains an integer t (1 ≤ t ≤ 10^5), the number of files.
Each of the next t lines describes a file as T M S, separated by single spaces:
Tisffor a regular file ordfor a directory;Mis the current mode: 1 to 4 octal digits (0–7), possibly with leading zeros, with value at most7777in octal;Sis a spec following the grammar above, 1 ≤ |S| ≤ 10^6.
The total length of all specs is at most 10^6.
Output
For each file, print its mode after the spec is applied, as exactly 4 octal digits with leading zeros, one per line.
Notes
In the first example, the fourth file starts at 0007. u=rwx gives 0707. g=u copies the user's
rwx to the group, giving 0777. o-w then gives 0775. In the last line the empty who list
includes o, so +t sets the sticky bit.
In the second example, u=rw on 4755 also clears setuid, since = clears every bit the user class owns.
In o=u-w,g+u,+t on 0351, the first action copies the user's 3 (-wx) into others, giving 0353.
The second action, still in the same clause, removes others' write bit, giving 0351. Then g+u gives
0371, and +t sets sticky, for 1371. In the last line, uo+s sets only setuid, because s does
nothing for o.