| 1063 | |
| 1064 | #[cfg(unix)] |
| 1065 | fn rewrite_group_at(path: &Path, gid: &str) -> Result<()> { |
| 1066 | let content = std::fs::read_to_string(path).into_diagnostic()?; |
| 1067 | |
| 1068 | let mut found = false; |
| 1069 | let mut lines: Vec<String> = content |
| 1070 | .lines() |
| 1071 | .map(|line| { |
| 1072 | if line.starts_with("sandbox:") { |
| 1073 | found = true; |
| 1074 | let fields: Vec<&str> = line.split(':').collect(); |
| 1075 | if fields.len() >= 4 { |
| 1076 | format!("{}:{}:{}:{}", fields[0], fields[1], gid, fields[3]) |
| 1077 | } else { |
| 1078 | line.to_string() |
| 1079 | } |
| 1080 | } else { |
| 1081 | line.to_string() |
| 1082 | } |
| 1083 | }) |
| 1084 | .collect(); |
| 1085 | |
| 1086 | if !found { |
| 1087 | lines.push(format!("sandbox:x:{gid}:")); |
| 1088 | } |
| 1089 | |
| 1090 | let mut output = lines.join("\n"); |
| 1091 | if content.ends_with('\n') || !found { |
| 1092 | output.push('\n'); |
| 1093 | } |
| 1094 | |
| 1095 | std::fs::write(path, output).into_diagnostic()?; |
| 1096 | Ok(()) |
| 1097 | } |
| 1098 | |
| 1099 | /// Recursively chown a directory tree to the given UID/GID. |
| 1100 | /// |