MCPcopy Create free account
hub / github.com/NVIDIA/OpenShell / rewrite_passwd_at

Function rewrite_passwd_at

crates/openshell-supervisor-process/src/process.rs:1027–1062  ·  view source on GitHub ↗
(path: &Path, uid: &str, gid: &str)

Source from the content-addressed store, hash-verified

1025
1026#[cfg(unix)]
1027fn rewrite_passwd_at(path: &Path, uid: &str, gid: &str) -> Result<()> {
1028 let content = std::fs::read_to_string(path).into_diagnostic()?;
1029
1030 let mut found = false;
1031 let mut lines: Vec<String> = content
1032 .lines()
1033 .map(|line| {
1034 if line.starts_with("sandbox:") {
1035 found = true;
1036 let fields: Vec<&str> = line.split(':').collect();
1037 if fields.len() >= 7 {
1038 format!(
1039 "{}:{}:{}:{}:{}:{}:{}",
1040 fields[0], fields[1], uid, gid, fields[4], fields[5], fields[6]
1041 )
1042 } else {
1043 line.to_string()
1044 }
1045 } else {
1046 line.to_string()
1047 }
1048 })
1049 .collect();
1050
1051 if !found {
1052 lines.push(format!("sandbox:x:{uid}:{gid}::/sandbox:/bin/sh"));
1053 }
1054
1055 let mut output = lines.join("\n");
1056 if content.ends_with('\n') || !found {
1057 output.push('\n');
1058 }
1059
1060 std::fs::write(path, output).into_diagnostic()?;
1061 Ok(())
1062}
1063
1064#[cfg(unix)]
1065fn rewrite_group_at(path: &Path, gid: &str) -> Result<()> {

Calls 2

lenMethod · 0.80
pushMethod · 0.80