diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2021-06-19 17:48:31 +0200 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2021-06-19 17:58:37 +0200 |
commit | a162424dc18e95e8497a412f6964395bb4595371 (patch) | |
tree | a7432a580afd612111e74bb5c05eeef360c2f229 /src | |
parent | e6f9a3da1d9b0e5eb09b48cc1ae0ade11fbf16f2 (diff) | |
download | rebel-a162424dc18e95e8497a412f6964395bb4595371.tar rebel-a162424dc18e95e8497a412f6964395bb4595371.zip |
util: add create_as() helper
Diffstat (limited to 'src')
-rw-r--r-- | src/runner/runc/run.rs | 16 | ||||
-rw-r--r-- | src/util.rs | 13 |
2 files changed, 19 insertions, 10 deletions
diff --git a/src/runner/runc/run.rs b/src/runner/runc/run.rs index 0735128..39adbc1 100644 --- a/src/runner/runc/run.rs +++ b/src/runner/runc/run.rs @@ -1,5 +1,5 @@ use std::{ - fs::{DirBuilder, File}, + fs::DirBuilder, io, path::{Path, PathBuf}, process, @@ -68,15 +68,11 @@ fn output_filename(task: TaskRef) -> PathBuf { } fn collect_output(task: TaskRef, task_def: Task) -> Result<(), io::Error> { - // Temporarily switch to the user running Rebel to get the right - // owner for the tar files - let file = { - let _setegid = util::setegid(unshare::BUILD_GID)?; - let _seteuid = util::seteuid(unshare::BUILD_UID)?; - - File::create(output_filename(task))? - }; - + let file = util::create_as( + output_filename(task), + Some(unshare::BUILD_UID), + Some(unshare::BUILD_GID), + )?; util::tar::pack(file, "build/tmp/runc/workdir", task_def.output.iter())?.sync_all() } diff --git a/src/util.rs b/src/util.rs index d388a59..460f735 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,7 +1,9 @@ pub mod tar; use std::{ + fs::File, io::{Error, ErrorKind, Result}, + path::Path, process::ExitStatus, result, }; @@ -96,3 +98,14 @@ pub fn setegid(gid: unistd::Gid) -> Result<SetEGID> { unistd::setegid(gid).to_io_result()?; Ok(SetEGID(old_gid)) } + +pub fn create_as<P: AsRef<Path>>( + path: P, + uid: Option<unistd::Uid>, + gid: Option<unistd::Gid>, +) -> Result<File> { + let _setegid = gid.map(setegid).transpose()?; + let _seteuid = uid.map(seteuid).transpose()?; + + File::create(path) +} |