eden: apfs helper: make chown a bit nicer

Summary:
I think I might want to call this from more than one place in
a later diff, so extract it into a helper function.

Reviewed By: chadaustin

Differential Revision: D19770166

fbshipit-source-id: e044003736c6ba21984a9129da1df50ce92b2f35
This commit is contained in:
Wez Furlong 2020-02-10 08:27:04 -08:00 committed by Facebook Github Bot
parent 28f7b4a0b0
commit 0c4d6d82f2

View File

@ -295,17 +295,23 @@ fn mount_scratch_space_on(mount_point: &str) -> Result<()> {
// Make sure that we own the mounted directory; the default is mounted
// with root:wheel ownership, and that isn't desirable
let mount_point_cstr = std::ffi::CString::new(mount_point)
.context("creating a C string from the mount point path")?;
let rc = unsafe { libc::chown(mount_point_cstr.as_ptr(), metadata.uid(), metadata.gid()) };
if rc != 0 {
let err = std::io::Error::last_os_error();
bail!("failed to chown the mount point back to the owner: {}", err);
}
chown(mount_point, metadata.uid(), metadata.gid())?;
Ok(())
}
fn chown(path: &str, uid: u32, gid: u32) -> Result<()> {
let cstr = std::ffi::CString::new(path)
.with_context(|| format!("creating a C string from path `{}`", path))?;
let rc = unsafe { libc::chown(cstr.as_ptr(), uid, gid) };
if rc != 0 {
let err = std::io::Error::last_os_error();
Err(err).with_context(|| format!("failed to chown {} to uid={}, gid={}", path, uid, gid))
} else {
Ok(())
}
}
/// Encode a mount point as a volume name.
/// The story here is that diskutil allows any user to create an APFS
/// volume, but requires root privs to mount it into the VFS.