problems / 158

Applying Symbolic chmod

medium parsing bitmask bit-manipulation simulation strings 2000 ms · 256 MB

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, x add the read, write or execute bit of every class in W.
  • X adds the execute bit of every class in W, but only if the file is a directory or M has at least one of the three execute bits (0111) set. Otherwise it adds nothing.
  • s adds setuid if u is in W and setgid if g is in W. It adds nothing for o.
  • t adds the sticky bit if o is in W. It adds nothing for u or g.
  • A copy letter c takes the three permission bits (read, write, execute) of class c in M and adds the same bits for every class in W. 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 of B.
  • - clears the bits of B.
  • = first clears everything owned by the classes in W (their read, write and execute bits and their special bit), then sets the bits of B. B was computed from M before 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:

  • T is f for a regular file or d for a directory;
  • M is the current mode: 1 to 4 octal digits (0–7), possibly with leading zeros, with value at most 7777 in octal;
  • S is 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.

Example 1

Input

5
f 644 u+x
f 0644 a+X
d 755 go-rx
f 7 u=rwx,g=u,o-w
d 0 +t

Output

0744
0644
0700
0775
1000

Example 2

Input

5
f 4755 u=rw
d 2775 g-s,o=
f 0640 u+x,a+X
f 0351 o=u-w,g+u,+t
d 1777 u=g,go=,uo+s

Output

0655
0770
0751
1371
4700

Hints · reveal one at a time if you're stuck

Hint 1

Represent the mode as a 12-bit number and each who list as a small set of classes. Each action reads the mode as it stands just before it, so a single left-to-right scan of the spec can apply actions as it parses them.

Hint 2

For each action, first build the set of bits it refers to from the current mode and the who set, then combine it with the mode using the operator. For = you must work out the bits from the mode before clearing anything, or a copy like a=u gives the wrong result.

Hint 3

Parse the spec by index rather than by repeatedly trimming the string, since specs can be a million characters long. Remember that X depends on the current mode and on whether the file is a directory, and that special bits only apply to the classes named in the spec.

report an issue Prefer your own editor? sooks prep 158 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.