Skip to main content

webdav_server/storage/
file.rs

1use std::{
2    fs::Metadata as StdMeta, os::unix::fs::MetadataExt, time::UNIX_EPOCH,
3};
4
5use blake3::Hash;
6
7use crate::storage::Result;
8
9pub struct Metadata {
10    pub hash: Hash,
11    pub last_modified: i64,
12    pub size: i64,
13}
14
15impl Metadata {
16    /// Attempts to construct a new `Metadata` instance from standard library metadata.
17    ///
18    /// # Errors
19    /// Returns an error if the system time is earlier than `UNIX_EPOCH` or if integer conversions fail.
20    pub fn try_new(metadata: &StdMeta, hash: Hash) -> Result<Self> {
21        Ok(Self {
22            hash,
23            last_modified: metadata
24                .modified()?
25                .duration_since(UNIX_EPOCH)?
26                .as_secs()
27                .try_into()?,
28            size: metadata.size().try_into()?,
29        })
30    }
31}