mirror of
https://github.com/neocturne/MinedMap.git
synced 2025-07-10 17:29:06 +02:00
Store source modified timestamp with processed, lightmap and map tiles
Allows to check whether the source is newer than the last update of the output files.
This commit is contained in:
parent
e18d4cea82
commit
628a702fd7
4 changed files with 75 additions and 15 deletions
45
src/io/fs.rs
45
src/io/fs.rs
|
@ -2,19 +2,34 @@ use std::{
|
|||
fs::{self, File},
|
||||
io::{BufReader, BufWriter, Read, Write},
|
||||
path::{Path, PathBuf},
|
||||
time::SystemTime,
|
||||
};
|
||||
|
||||
use anyhow::{Context, Ok, Result};
|
||||
use serde::Serialize;
|
||||
|
||||
fn tmpfile_name(path: &Path) -> PathBuf {
|
||||
#[derive(Debug, Serialize)]
|
||||
struct FileMeta {
|
||||
timestamp: SystemTime,
|
||||
}
|
||||
|
||||
fn suffix_name(path: &Path, suffix: &str) -> PathBuf {
|
||||
let mut file_name = path.file_name().unwrap_or_default().to_os_string();
|
||||
file_name.push(".tmp");
|
||||
file_name.push(suffix);
|
||||
|
||||
let mut ret = path.to_path_buf();
|
||||
ret.set_file_name(file_name);
|
||||
ret
|
||||
}
|
||||
|
||||
fn tmpfile_name(path: &Path) -> PathBuf {
|
||||
suffix_name(path, ".tmp")
|
||||
}
|
||||
|
||||
fn metafile_name(path: &Path) -> PathBuf {
|
||||
suffix_name(path, ".meta")
|
||||
}
|
||||
|
||||
pub fn create_dir_all(path: &Path) -> Result<()> {
|
||||
fs::create_dir_all(path)
|
||||
.with_context(|| format!("Failed to create directory {}", path.display(),))
|
||||
|
@ -84,3 +99,29 @@ where
|
|||
|
||||
ret
|
||||
}
|
||||
|
||||
pub fn modified_timestamp(path: &Path) -> Result<SystemTime> {
|
||||
fs::metadata(path)
|
||||
.and_then(|meta| meta.modified())
|
||||
.with_context(|| {
|
||||
format!(
|
||||
"Failed to get modified timestamp of file {}",
|
||||
path.display()
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
pub fn create_with_timestamp<T, F>(path: &Path, timestamp: SystemTime, f: F) -> Result<T>
|
||||
where
|
||||
F: FnOnce(&mut BufWriter<File>) -> Result<T>,
|
||||
{
|
||||
let ret = create_with_tmpfile(path, f)?;
|
||||
|
||||
let meta_path = metafile_name(path);
|
||||
create(&meta_path, |file| {
|
||||
serde_json::to_writer(file, &FileMeta { timestamp })?;
|
||||
Ok(())
|
||||
})?;
|
||||
|
||||
Ok(ret)
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ use std::{
|
|||
fs::File,
|
||||
io::{Read, Write},
|
||||
path::Path,
|
||||
time::SystemTime,
|
||||
};
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
|
@ -9,8 +10,8 @@ use serde::{de::DeserializeOwned, Serialize};
|
|||
|
||||
use super::fs;
|
||||
|
||||
pub fn write<T: Serialize>(path: &Path, value: &T) -> Result<()> {
|
||||
fs::create_with_tmpfile(path, |file| {
|
||||
pub fn write<T: Serialize>(path: &Path, value: &T, timestamp: SystemTime) -> Result<()> {
|
||||
fs::create_with_timestamp(path, timestamp, |file| {
|
||||
let data = bincode::serialize(value)?;
|
||||
let len = u32::try_from(data.len())?;
|
||||
let compressed = zstd::bulk::compress(&data, 1)?;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue