implement filesystem_sync option

This commit is contained in:
postscriptum 2025-07-05 15:14:04 +03:00
parent c2d8336156
commit 0862525811
5 changed files with 86 additions and 58 deletions

View file

@ -21,6 +21,7 @@ pub struct Nex {
filename: String,
is_cleanup: bool,
is_debug: bool,
is_filesystem_sync: bool,
template: Template,
users: HashMap<String, PathBuf>,
}
@ -32,7 +33,7 @@ impl Nex {
time_format: String,
pattern: String,
attachment_mode: Option<String>,
(is_cleanup, is_debug): (bool, bool),
(is_cleanup, is_debug, is_filesystem_sync): (bool, bool, bool),
user_names: &Vec<String>,
) -> Result<Self> {
// validate filename
@ -61,9 +62,10 @@ impl Nex {
Ok(Self {
attachment: Attachment::init(attachment_mode)?,
filename,
template,
is_cleanup,
is_debug,
is_filesystem_sync,
template,
users,
})
}
@ -134,7 +136,11 @@ impl Nex {
if !fs::read_to_string(&s).is_ok_and(|this| this == state) {
fs::remove_dir_all(&d)?;
fs::create_dir_all(&d)?;
save(&s, timestamp.into(), state.as_bytes())?
if self.is_filesystem_sync {
sync(&s, timestamp.into(), state.as_bytes())?
} else {
fs::write(&s, state)?
}
} else {
if let Some(ref mut i) = index {
if let Some(ref a) = attachments {
@ -159,57 +165,65 @@ impl Nex {
} else {
Change::Created
};
save(
&f,
timestamp.into(),
self.template
.build(
updated,
content,
link,
tags,
attachments.map(|a| {
let mut b = Vec::with_capacity(a.len());
for (n, (name, media_type, source)) in a.into_iter().enumerate() {
b.push((
match source {
Source::Url(url) => url,
Source::File(from) => {
let to = attachment::filepath(&d, &from, n);
let uri = format!(
"{}/{}",
d.file_name().unwrap().to_string_lossy(),
to.file_name().unwrap().to_string_lossy()
);
self.attachment.sync(&from, &to, timestamp.into()).unwrap();
if let Some(ref mut i) = index {
i.push(to);
}
uri
}
},
{
let mut alt = Vec::with_capacity(2);
if !name.is_empty() {
alt.push(name)
}
if !media_type.is_empty() {
alt.push(format!("({media_type})"))
}
if alt.is_empty() {
None
} else {
Some(alt.join(" "))
}
},
))
}
b
}),
)
.as_bytes(),
)?;
let template = self.template.build(
updated,
content,
link,
tags,
attachments.map(|a| {
let mut b = Vec::with_capacity(a.len());
for (n, (name, media_type, source)) in a.into_iter().enumerate() {
b.push((
match source {
Source::Url(url) => url,
Source::File(from) => {
let to = attachment::filepath(&d, &from, n);
let uri = format!(
"{}/{}",
d.file_name().unwrap().to_string_lossy(),
to.file_name().unwrap().to_string_lossy()
);
self.attachment
.sync(
&from,
&to,
if self.is_filesystem_sync {
Some(timestamp.into())
} else {
None
},
)
.unwrap();
if let Some(ref mut i) = index {
i.push(to);
}
uri
}
},
{
let mut alt = Vec::with_capacity(2);
if !name.is_empty() {
alt.push(name)
}
if !media_type.is_empty() {
alt.push(format!("({media_type})"))
}
if alt.is_empty() {
None
} else {
Some(alt.join(" "))
}
},
))
}
b
}),
);
if self.is_filesystem_sync {
sync(&f, timestamp.into(), template.as_bytes())?
} else {
fs::write(&f, template)?
}
// move all paths processed to cleanup ignore
if let Some(ref mut i) = index {
i.extend([d, f, p, s]);
@ -268,7 +282,7 @@ impl Nex {
}
/// Wrapper function to change time updated for new file, according to the Snac time.
fn save(path: &PathBuf, modified: std::time::SystemTime, data: &[u8]) -> Result<()> {
fn sync(path: &PathBuf, modified: std::time::SystemTime, data: &[u8]) -> Result<()> {
use std::io::Write;
let mut f = fs::File::create(path)?;
f.write_all(data)?;