diff options
Diffstat (limited to 'src/util.rs')
-rw-r--r-- | src/util.rs | 55 |
1 files changed, 39 insertions, 16 deletions
diff --git a/src/util.rs b/src/util.rs index bf78404..eac71f1 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,4 +1,3 @@ -use crate::error::SiteError; use libc::{timespec, utimensat, AT_FDCWD, AT_SYMLINK_NOFOLLOW}; use log::{debug, error, trace}; use std::ffi::CString; @@ -6,34 +5,58 @@ use std::os::unix::ffi::OsStrExt; use std::path::{Component, Path, PathBuf}; use std::time::{SystemTime, UNIX_EPOCH}; -pub fn canonicalize(path: &Path) -> Result<PathBuf, SiteError> { +#[derive(Debug, thiserror::Error)] +pub enum UtilError { + #[error("failed to canonicalize path {0}")] + Canonicalize(PathBuf, #[source] std::io::Error), + + #[error("failed to convert time to Unix time")] + UnixTime(#[source] std::time::SystemTimeError), + + #[error("failed to create directory {0}")] + CreateDir(PathBuf, #[source] std::io::Error), + + #[error("failed to copy {0} to {1}")] + CopyFile(PathBuf, PathBuf, #[source] std::io::Error), + + #[error("failed to get file metadata: {0}")] + FileMetadata(PathBuf, #[source] std::io::Error), + + #[error("failed to get file modification time: {0}")] + FileMtime(PathBuf, #[source] std::io::Error), + + #[error("failed to set modification time for file {0}")] + Utimensat(PathBuf, #[source] std::io::Error), +} + +pub fn canonicalize(path: &Path) -> Result<PathBuf, UtilError> { path.canonicalize() - .map_err(|e| SiteError::Canonicalize(path.into(), e)) + .map_err(|e| UtilError::Canonicalize(path.into(), e)) } -pub fn mkdir(path: &Path) -> Result<(), SiteError> { +pub fn mkdir(path: &Path) -> Result<(), UtilError> { debug!("creating directory {}", path.display()); - std::fs::create_dir_all(path).map_err(|e| SiteError::CreateDir(path.into(), e))?; + std::fs::create_dir_all(path).map_err(|e| UtilError::CreateDir(path.into(), e))?; Ok(()) } -pub fn copy(src: &Path, dest: &Path) -> Result<(), SiteError> { +pub fn copy(src: &Path, dest: &Path) -> Result<(), UtilError> { trace!("copying: {} -> {}", src.display(), dest.display()); - std::fs::copy(src, dest).map_err(|e| SiteError::CopyFile(src.into(), dest.into(), e))?; + std::fs::copy(src, dest).map_err(|e| UtilError::CopyFile(src.into(), dest.into(), e))?; let mtime = get_mtime(src)?; set_mtime(dest, mtime)?; Ok(()) } -pub fn get_mtime(src: &Path) -> Result<SystemTime, SiteError> { - let metadata = std::fs::metadata(src).map_err(|e| SiteError::FileMetadata(src.into(), e))?; +pub fn get_mtime(src: &Path) -> Result<SystemTime, UtilError> { + let metadata = std::fs::metadata(src).map_err(|e| UtilError::FileMetadata(src.into(), e))?; let mtime = metadata .modified() - .map_err(|e| SiteError::FileMtime(src.into(), e))?; + .map_err(|e| UtilError::FileMtime(src.into(), e))?; Ok(mtime) } -pub fn set_mtime(filename: &Path, mtime: SystemTime) -> Result<(), SiteError> { +pub fn set_mtime(filename: &Path, mtime: SystemTime) -> Result<(), UtilError> { trace!( "set_mtime: filename={} mtime={:?}", filename.display(), @@ -53,19 +76,19 @@ pub fn set_mtime(filename: &Path, mtime: SystemTime) -> Result<(), SiteError> { if utimensat(AT_FDCWD, path.as_ptr(), times, AT_SYMLINK_NOFOLLOW) == -1 { let error = std::io::Error::last_os_error(); error!("utimensat failed on {:?}", path); - return Err(SiteError::Utimensat(pathbuf, error)); + return Err(UtilError::Utimensat(pathbuf, error)); } } Ok(()) } -pub fn copy_file_from_source(filename: &Path, output: &Path) -> Result<(), SiteError> { +pub fn copy_file_from_source(filename: &Path, output: &Path) -> Result<(), UtilError> { debug!("copying {} -> {}", filename.display(), output.display()); if let Some(parent) = output.parent() { trace!("parent: {}", parent.display()); if !parent.exists() { trace!("create parent {}", parent.display()); - std::fs::create_dir_all(parent).map_err(|e| SiteError::CreateDir(parent.into(), e))?; + std::fs::create_dir_all(parent).map_err(|e| UtilError::CreateDir(parent.into(), e))?; } } else { trace!("does not have parent: {}", output.display()); @@ -116,10 +139,10 @@ pub fn make_path_absolute(path: &Path) -> PathBuf { Path::new("/").join(path) } -fn timespec(time: SystemTime) -> Result<timespec, SiteError> { +fn timespec(time: SystemTime) -> Result<timespec, UtilError> { let dur = time .duration_since(UNIX_EPOCH) - .map_err(SiteError::UnixTime)?; + .map_err(UtilError::UnixTime)?; let tv_sec = dur.as_secs() as libc::time_t; let tv_nsec = dur.subsec_nanos() as libc::c_long; Ok(timespec { tv_sec, tv_nsec }) |