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

Dominik Csapak d.csapak at proxmox.com
Fri Nov 6 11:03:39 CET 2020


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)
+}
-- 
2.20.1






More information about the pbs-devel mailing list