Merge "Adds temporary chown to sparse_copy."

This commit is contained in:
Jenkins
2012-02-27 22:18:38 +00:00
committed by Gerrit Code Review

View File

@@ -1523,3 +1523,23 @@ def read_file_as_root(file_path):
return out
except exception.ProcessExecutionError:
raise exception.FileNotFound(file_path=file_path)
@contextlib.contextmanager
def temporary_chown(path, owner_uid=None):
"""Temporarily chown a path.
:params owner_uid: UID of temporary owner (defaults to current user)
"""
if owner_uid is None:
owner_uid = os.getuid()
orig_uid = os.stat(path).st_uid
if orig_uid != owner_uid:
execute('chown', owner_uid, path, run_as_root=True)
try:
yield
finally:
if orig_uid != owner_uid:
execute('chown', orig_uid, path, run_as_root=True)