Asking for the state is expensive due to the number of file stat calls involved. Do this once before line 133 and save the result into a local variable so that all three of these references reuse the same computed value.

It may be worth using a switch statement here instead of if.