mirror of
https://codeberg.org/icewind/galton.git
synced 2026-06-03 18:34:08 +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::path::{Path, PathBuf};
|
||||||
use std::time::{SystemTime, UNIX_EPOCH};
|
use std::time::UNIX_EPOCH;
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
||||||
pub struct FileInfo {
|
pub struct FileInfo {
|
||||||
|
|
@ -14,16 +14,20 @@ impl FileInfo {
|
||||||
pub fn load<P: AsRef<Path>>(path: P) -> Result<FileInfo, FileError> {
|
pub fn load<P: AsRef<Path>>(path: P) -> Result<FileInfo, FileError> {
|
||||||
let path = path.as_ref();
|
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 {
|
let path = path.to_str().ok_or_else(|| FileError::InvalidPath {
|
||||||
path: path.into(),
|
path: path.into(),
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
let mtime = match stat {
|
let mtime = stat.modified().map_err(|error| FileError::Stat {
|
||||||
Ok(stat) => stat.modified().unwrap_or_else(|_| SystemTime::now()),
|
path: path.into(),
|
||||||
Err(_) => SystemTime::now()
|
error
|
||||||
}.duration_since(UNIX_EPOCH).unwrap_or_default().as_secs();
|
})?;
|
||||||
|
let mtime = mtime.duration_since(UNIX_EPOCH).unwrap_or_default().as_secs();
|
||||||
|
|
||||||
let mut file = FileInfo {
|
let mut file = FileInfo {
|
||||||
path: path.into(),
|
path: path.into(),
|
||||||
|
|
@ -58,4 +62,9 @@ impl FileInfo {
|
||||||
pub enum FileError {
|
pub enum FileError {
|
||||||
#[error("Invalid path: {}", path.display())]
|
#[error("Invalid path: {}", path.display())]
|
||||||
InvalidPath { path: PathBuf },
|
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