mirror of
https://codeberg.org/icewind/galton.git
synced 2026-06-03 10:24:07 +02:00
mtime errors
This commit is contained in:
parent
6d8b8d363e
commit
5083cd1591
1 changed files with 15 additions and 6 deletions
21
src/file.rs
21
src/file.rs
|
|
@ -1,5 +1,5 @@
|
|||
use std::path::{Path, PathBuf};
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
use std::time::UNIX_EPOCH;
|
||||
use thiserror::Error;
|
||||
|
||||
pub struct FileInfo {
|
||||
|
|
@ -14,16 +14,20 @@ impl FileInfo {
|
|||
pub fn load<P: AsRef<Path>>(path: P) -> Result<FileInfo, FileError> {
|
||||
let path = path.as_ref();
|
||||
|
||||
let stat = path.metadata();
|
||||
let stat = path.metadata().map_err(|error| FileError::Stat {
|
||||
path: path.into(),
|
||||
error
|
||||
})?;
|
||||
|
||||
let path = path.to_str().ok_or_else(|| FileError::InvalidPath {
|
||||
path: path.into(),
|
||||
})?;
|
||||
|
||||
let mtime = match stat {
|
||||
Ok(stat) => stat.modified().unwrap_or_else(|_| SystemTime::now()),
|
||||
Err(_) => SystemTime::now()
|
||||
}.duration_since(UNIX_EPOCH).unwrap_or_default().as_secs();
|
||||
let mtime = stat.modified().map_err(|error| FileError::Stat {
|
||||
path: path.into(),
|
||||
error
|
||||
})?;
|
||||
let mtime = mtime.duration_since(UNIX_EPOCH).unwrap_or_default().as_secs();
|
||||
|
||||
let mut file = FileInfo {
|
||||
path: path.into(),
|
||||
|
|
@ -58,4 +62,9 @@ impl FileInfo {
|
|||
pub enum FileError {
|
||||
#[error("Invalid path: {}", path.display())]
|
||||
InvalidPath { path: PathBuf },
|
||||
#[error("Failed to get metadata for {}: {error:#}", path.display())]
|
||||
Stat {
|
||||
path: PathBuf,
|
||||
error: std::io::Error,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue