diff options
author | Daniel Verkamp <dverkamp@chromium.org> | 2019-11-13 09:39:41 -0800 |
---|---|---|
committer | Commit Bot <commit-bot@chromium.org> | 2019-11-27 21:22:41 +0000 |
commit | 4cc280bcff161e08a44c83fbd5384e324b8c3585 (patch) | |
tree | 0e8413a1ec9696faeb8d5efb995fc1636fd466c7 /sys_util | |
parent | 624c51bee3a8356c3e4bbdd6aef21ea45b0f4d59 (diff) | |
download | crosvm-4cc280bcff161e08a44c83fbd5384e324b8c3585.tar crosvm-4cc280bcff161e08a44c83fbd5384e324b8c3585.tar.gz crosvm-4cc280bcff161e08a44c83fbd5384e324b8c3585.tar.bz2 crosvm-4cc280bcff161e08a44c83fbd5384e324b8c3585.tar.lz crosvm-4cc280bcff161e08a44c83fbd5384e324b8c3585.tar.xz crosvm-4cc280bcff161e08a44c83fbd5384e324b8c3585.tar.zst crosvm-4cc280bcff161e08a44c83fbd5384e324b8c3585.zip |
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 <dverkamp@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1913961 Tested-by: kokoro <noreply+kokoro@google.com> Reviewed-by: Dylan Reid <dgreid@chromium.org>
Diffstat (limited to 'sys_util')
-rw-r--r-- | sys_util/src/file_traits.rs | 15 | ||||
-rw-r--r-- | sys_util/src/lib.rs | 2 |
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; |