apply Snac time updated to the new files

This commit is contained in:
postscriptum 2025-07-05 14:32:24 +03:00
parent 7baba55272
commit c2d8336156
2 changed files with 66 additions and 50 deletions

View file

@ -68,8 +68,7 @@ impl Nex {
}) })
} }
// Sync the Snac post with the Nex entry // Sync Snac post with Nex entry
// * @TODO apply the post time to the files
pub fn sync( pub fn sync(
&self, &self,
name: &str, name: &str,
@ -130,11 +129,12 @@ impl Nex {
// if this meta file exists and has timestamp changed, also cleanup attachment files to update // if this meta file exists and has timestamp changed, also cleanup attachment files to update
let mut s = PathBuf::from(&d); let mut s = PathBuf::from(&d);
s.push(".state"); s.push(".state");
let state = updated.unwrap_or(published).to_string(); let timestamp = updated.unwrap_or(published);
let state = timestamp.to_string();
if !fs::read_to_string(&s).is_ok_and(|this| this == state) { if !fs::read_to_string(&s).is_ok_and(|this| this == state) {
fs::remove_dir_all(&d)?; fs::remove_dir_all(&d)?;
fs::create_dir_all(&d)?; fs::create_dir_all(&d)?;
fs::write(&s, state)? save(&s, timestamp.into(), state.as_bytes())?
} else { } else {
if let Some(ref mut i) = index { if let Some(ref mut i) = index {
if let Some(ref a) = attachments { if let Some(ref a) = attachments {
@ -159,9 +159,11 @@ impl Nex {
} else { } else {
Change::Created Change::Created
}; };
fs::write( save(
&f, &f,
self.template.build( timestamp.into(),
self.template
.build(
updated, updated,
content, content,
link, link,
@ -179,7 +181,7 @@ impl Nex {
d.file_name().unwrap().to_string_lossy(), d.file_name().unwrap().to_string_lossy(),
to.file_name().unwrap().to_string_lossy() to.file_name().unwrap().to_string_lossy()
); );
self.attachment.sync(&from, &to).unwrap(); self.attachment.sync(&from, &to, timestamp.into()).unwrap();
if let Some(ref mut i) = index { if let Some(ref mut i) = index {
i.push(to); i.push(to);
} }
@ -204,7 +206,8 @@ impl Nex {
} }
b b
}), }),
), )
.as_bytes(),
)?; )?;
// move all paths processed to cleanup ignore // move all paths processed to cleanup ignore
@ -263,3 +266,12 @@ impl Nex {
self.is_cleanup self.is_cleanup
} }
} }
/// 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<()> {
use std::io::Write;
let mut f = fs::File::create(path)?;
f.write_all(data)?;
f.set_modified(modified)?; // it's important to call after write
Ok(())
}

View file

@ -1,5 +1,8 @@
use anyhow::{Result, bail}; use anyhow::{Result, bail};
use std::path::{Path, PathBuf}; use std::{
path::{Path, PathBuf},
time::SystemTime,
};
pub enum Attachment { pub enum Attachment {
Copy, Copy,
@ -20,11 +23,12 @@ impl Attachment {
None => Self::Disabled, None => Self::Disabled,
}) })
} }
pub fn sync(&self, source: &PathBuf, target: &PathBuf) -> Result<()> { pub fn sync(&self, source: &PathBuf, target: &PathBuf, modified: SystemTime) -> Result<()> {
use std::{fs, os}; use std::{fs, os};
match self { match self {
Attachment::Copy => { Attachment::Copy => {
fs::copy(source, target)?; fs::copy(source, target)?;
fs::File::open(target)?.set_modified(modified)?;
#[cfg(any(target_os = "linux", target_os = "macos"))] #[cfg(any(target_os = "linux", target_os = "macos"))]
{ {
use std::{fs::Permissions, os::unix::fs::PermissionsExt}; use std::{fs::Permissions, os::unix::fs::PermissionsExt};