use crate::{fs::build_dir, parser};
use std::{fs::OpenOptions, io::Write, os::unix::fs::OpenOptionsExt};
pub fn create() -> Result<(), anyhow::Error> {
let mut configs = Vec::new();
let config_files = std::fs::read_dir("/usr/lib/tmpfiles.d")?;
for file in config_files {
match file {
Ok(f) => match parser::parse(&f.path()) {
Ok(config) => configs.push(config),
Err(e) => anyhow::bail!("Config parsing failed with error:\n{}", e),
},
Err(e) => anyhow::bail!("Config parsing failed with error:\n{}", e),
}
}
for config in &configs {
for line in config {
match line.file_type.as_str() {
"f" => {
match OpenOptions::new()
.create(true)
.append(true)
.mode(u32::from_str_radix(&line.mode, 8).unwrap())
.open(&line.path.as_os_str())
{
Ok(..) => continue,
Err(e) => anyhow::bail!("File creation failed with error:\n{}", e),
}
}
"f+" => {
match OpenOptions::new()
.create(true)
.append(true)
.truncate(true)
.mode(u32::from_str_radix(&line.mode, 8).unwrap())
.open(&line.path.as_os_str())
{
Ok(..) => continue,
Err(e) => anyhow::bail!("File creation failed with error:\n{}", e),
}
}
"w" => {
if !line.argument.contains("\n") {
let mut file = std::fs::File::open(&line.path.as_os_str()).unwrap();
match file.write_all(&line.argument.as_bytes()) {
Ok(..) => continue,
Err(e) => anyhow::bail!("File creation failed with error:\n{}", e),
}
}
}
"w+" => {
if line.argument.contains("\n") {
let mut file = std::fs::File::open(&line.path.as_os_str()).unwrap();
match file.write_all(&line.argument.as_bytes()) {
Ok(..) => continue,
Err(e) => anyhow::bail!("File creation failed with error:\n{}", e),
}
}
}
"d" => match build_dir(&line.path, u32::from_str_radix(&line.mode, 8).unwrap()) {
Ok(..) => continue,
Err(e) => anyhow::bail!("Directory creation failed with error:\n{}", e),
},
"D" => match build_dir(&line.path, u32::from_str_radix(&line.mode, 8).unwrap()) {
Ok(..) => continue,
Err(e) => anyhow::bail!("Directory creation failed with error:\n{}", e),
},
"e" => todo!(),
"v" => todo!(),
"q" => todo!(),
"Q" => todo!(),
"p" => todo!(),
"p+" => todo!(),
"L" => todo!(),
"L+" => todo!(),
"L?" => todo!(),
"c" => todo!(),
"c+" => todo!(),
"b" => todo!(),
"b+" => todo!(),
"C" => todo!(),
"C+" => todo!(),
"x" => todo!(),
"X" => todo!(),
"r" => todo!(),
"R" => todo!(),
"z" => todo!(),
"Z" => todo!(),
"t" => todo!(),
"T" => todo!(),
"h" => todo!(),
"H" => todo!(),
"a" => todo!(),
"a+" => todo!(),
"A" => todo!(),
"A+" => todo!(),
_ => anyhow::bail!("File type '{}' is not supported.", line.file_type),
};
}
}
Ok(())
}