summary refs log tree commit diff
path: root/sys_util/src
diff options
context:
space:
mode:
Diffstat (limited to 'sys_util/src')
-rw-r--r--sys_util/src/file_traits.rs15
-rw-r--r--sys_util/src/lib.rs2
2 files changed, 16 insertions, 1 deletions
diff --git a/sys_util/src/file_traits.rs b/sys_util/src/file_traits.rs
index 0e4d919..60ba050 100644
--- a/sys_util/src/file_traits.rs
+++ b/sys_util/src/file_traits.rs
@@ -41,6 +41,21 @@ impl FileSetLen for File {
     }
 }
 
+/// A trait for getting the size of a file.
+/// This is equivalent to File's metadata().len() method,
+/// but wrapped in a trait so that it can be implemented for
+/// other types.
+pub trait FileGetLen {
+    /// Get the current length of the file in bytes.
+    fn get_len(&self) -> Result<u64>;
+}
+
+impl FileGetLen for File {
+    fn get_len(&self) -> Result<u64> {
+        Ok(self.metadata()?.len())
+    }
+}
+
 /// A trait similar to `Read` and `Write`, but uses volatile memory as buffers.
 pub trait FileReadWriteVolatile {
     /// Read bytes from this file into the given slice, returning the number of bytes read on
diff --git a/sys_util/src/lib.rs b/sys_util/src/lib.rs
index 31b1662..7f97650 100644
--- a/sys_util/src/lib.rs
+++ b/sys_util/src/lib.rs
@@ -64,7 +64,7 @@ pub use crate::timerfd::*;
 pub use poll_token_derive::*;
 
 pub use crate::file_traits::{
-    AsRawFds, FileReadWriteAtVolatile, FileReadWriteVolatile, FileSetLen, FileSync,
+    AsRawFds, FileGetLen, FileReadWriteAtVolatile, FileReadWriteVolatile, FileSetLen, FileSync,
 };
 pub use crate::guest_memory::Error as GuestMemoryError;
 pub use crate::mmap::Error as MmapError;