diff options
author | Jenkins <jenkins@review.openstack.org> | 2012-02-27 22:18:38 +0000 |
---|---|---|
committer | Gerrit Code Review <review@openstack.org> | 2012-02-27 22:18:38 +0000 |
commit | f01b9b8dd25d763e652259a0f99264d93661b29f (patch) | |
tree | e296cb5cc89ca1243f41a9692fcecedf0fd9d5e9 /nova/utils.py | |
parent | 81a1b0a15ad5b0a656bd56d9f87715a8707f986e (diff) | |
parent | 6315eb5086a96e4ba614ba01c9bbfaec8bd8504b (diff) | |
download | nova-f01b9b8dd25d763e652259a0f99264d93661b29f.tar.gz nova-f01b9b8dd25d763e652259a0f99264d93661b29f.tar.xz nova-f01b9b8dd25d763e652259a0f99264d93661b29f.zip |
Merge "Adds temporary chown to sparse_copy."
Diffstat (limited to 'nova/utils.py')
-rw-r--r-- | nova/utils.py | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/nova/utils.py b/nova/utils.py index 0f3e61897..6bb0dd0f2 100644 --- a/nova/utils.py +++ b/nova/utils.py @@ -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) |