From 4cc280bcff161e08a44c83fbd5384e324b8c3585 Mon Sep 17 00:00:00 2001 From: Daniel Verkamp Date: Wed, 13 Nov 2019 09:39:41 -0800 Subject: disk: add get_len() to eliminate need for Seek This new trait allows DiskFile implementors to provide the length of the file directly rather than using SeekFrom::End with seek(). BUG=None TEST=./build_test TEST=Boot Termina in crosvm Change-Id: I9447ebb43dbd5fbb32a3a6b6d2fc969b9406cdbc Signed-off-by: Daniel Verkamp Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1913961 Tested-by: kokoro Reviewed-by: Dylan Reid --- sys_util/src/file_traits.rs | 15 +++++++++++++++ sys_util/src/lib.rs | 2 +- 2 files changed, 16 insertions(+), 1 deletion(-) (limited to 'sys_util/src') 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; +} + +impl FileGetLen for File { + fn get_len(&self) -> Result { + 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; -- cgit 1.4.1