Add append support for filesystem files

This commit is contained in:
Ziwen Li(Frank)
2026-05-21 10:35:33 +12:00
parent 948bfdcce9
commit be3e5097bc
2 changed files with 22 additions and 3 deletions
+9
View File
@@ -1,5 +1,9 @@
use std::fmt::Display;
fn is_false(value: &bool) -> bool {
!*value
}
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct FileConfig {
pub path: String,
@@ -15,6 +19,8 @@ pub struct FileConfig {
pub recursive_chown: bool,
#[serde(default)]
pub postinstall: bool,
#[serde(default, skip_serializing_if = "is_false")]
pub append: bool,
}
impl FileConfig {
@@ -68,6 +74,9 @@ impl Display for FileConfig {
if self.postinstall {
write!(f, "!")?;
}
if self.append {
write!(f, " append=yes")?;
}
}
if let Some(uid) = self.uid {
write!(f, " uid={}", uid)?;
+13 -3
View File
@@ -60,9 +60,19 @@ impl crate::FileConfig {
})?;
Ok(())
} else {
println!("Create file {}", target_file.display());
let mut file = File::create(&target_file)
.with_context(|| format!("failed to create file {}", target_file.display()))?;
let action = if self.append { "Append" } else { "Create" };
println!("{action} file {}", target_file.display());
let mut file = if self.append {
fs::OpenOptions::new()
.create(true)
.append(true)
.open(&target_file)
.with_context(|| format!("failed to append file {}", target_file.display()))?
} else {
File::create(&target_file)
.with_context(|| format!("failed to create file {}", target_file.display()))?
};
file.write_all(self.data.as_bytes())?;
self.apply_perms(target_file)