diff options
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) |