[pbs-devel] [PATCH proxmox-backup 2/6] tools/fs: add helpers to get the mtime of a file

Thomas Lamprecht t.lamprecht at proxmox.com
Fri Nov 6 18:24:14 CET 2020


On 06.11.20 11:03, Dominik Csapak wrote:
> Signed-off-by: Dominik Csapak <d.csapak at proxmox.com>
> ---
>  src/tools/fs.rs | 22 ++++++++++++++++++++++
>  1 file changed, 22 insertions(+)
> 
> diff --git a/src/tools/fs.rs b/src/tools/fs.rs
> index 72a530d8..d7ab934a 100644
> --- a/src/tools/fs.rs
> +++ b/src/tools/fs.rs
> @@ -312,3 +312,25 @@ fn do_lock_dir_noblock(
>  
>      Ok(handle)
>  }
> +
> +pub fn get_file_mtime<P: AsRef<std::path::Path>>(path: P) -> Result<i64, Error> {
> +    let time = std::fs::metadata(path)?.modified()?;
> +
> +    let mtime: i64 = if time < std::time::SystemTime::UNIX_EPOCH {
> +        -(std::time::SystemTime::UNIX_EPOCH.duration_since(time)?.as_secs() as i64)
> +    } else {
> +        time.duration_since(std::time::SystemTime::UNIX_EPOCH)?.as_secs() as i64
> +    };
> +    Ok(mtime)
> +}
> +
> +pub async fn get_async_file_mtime<P: AsRef<std::path::Path>>(path: P) -> Result<i64, Error> {
> +    let time = tokio::fs::metadata(path).await?.modified()?;
> +
> +    let mtime: i64 = if time < std::time::SystemTime::UNIX_EPOCH {
> +        -(std::time::SystemTime::UNIX_EPOCH.duration_since(time)?.as_secs() as i64)
> +    } else {
> +        time.duration_since(std::time::SystemTime::UNIX_EPOCH)?.as_secs() as i64
> +    };
> +    Ok(mtime)
> +}
> 

wouldn't it be more sensible to implement just a system_time_to_i64 and use that
in combination with a "manual" modified call on use-site.
Avoids instancing multiple generics, and more flexible as one can have a file handle
or a path.





More information about the pbs-devel mailing list