I think you should wrap this around each mkdir but nothing else. At any point later, things may succeed or fail, leaving an indeterminate state. You need to put it back into a consistent state at the beginning, independent of what is there or not.