Adds temporary chown to sparse_copy.
`sparse_copy` needs read and write access to the devices. Since we cannot shell out to a run-as-root command here, we temporarily take ownership of the device. Change-Id: I891c38dbcba7177286dca729684c88ac065bd085
This commit is contained in:
@@ -1523,3 +1523,23 @@ def read_file_as_root(file_path):
|
|||||||
return out
|
return out
|
||||||
except exception.ProcessExecutionError:
|
except exception.ProcessExecutionError:
|
||||||
raise exception.FileNotFound(file_path=file_path)
|
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)
|
||||||
|
|||||||
Reference in New Issue
Block a user