Added safe wrappers to chown, lchown and chmod.

To prevent race conditions in path lookup when we change attributes.

Continuation of issue #3693.