summary refs log tree commit diff
path: root/sys_util/src/file_traits.rs
diff options
context:
space:
mode:
authorDaniel Verkamp <dverkamp@chromium.org>2018-12-06 15:39:31 -0800
committerchrome-bot <chrome-bot@chromium.org>2019-01-05 20:08:26 -0800
commit6d47e1b0056d5175a4ae67bf4469ed5c83081c84 (patch)
treecb054cc570d5f0e4ecb35b6eb147cb7e31ededdd /sys_util/src/file_traits.rs
parentd1eceeca7bfd12d4d1236346d7cdcc89b5e949e2 (diff)
downloadcrosvm-6d47e1b0056d5175a4ae67bf4469ed5c83081c84.tar
crosvm-6d47e1b0056d5175a4ae67bf4469ed5c83081c84.tar.gz
crosvm-6d47e1b0056d5175a4ae67bf4469ed5c83081c84.tar.bz2
crosvm-6d47e1b0056d5175a4ae67bf4469ed5c83081c84.tar.lz
crosvm-6d47e1b0056d5175a4ae67bf4469ed5c83081c84.tar.xz
crosvm-6d47e1b0056d5175a4ae67bf4469ed5c83081c84.tar.zst
crosvm-6d47e1b0056d5175a4ae67bf4469ed5c83081c84.zip
sys_util: add set_len() trait
Generalize file_sync into file_traits so that we can add another
wrapper, this time for the set_len() method implemented directly on
File.  This will also be implemented on QcowFile.

BUG=chromium:858815
TEST=build_test

Change-Id: I43fbd1968a844c8cac359973a63babcc26942204
Signed-off-by: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/1394148
Commit-Ready: ChromeOS CL Exonerator Bot <chromiumos-cl-exonerator@appspot.gserviceaccount.com>
Reviewed-by: David Tolnay <dtolnay@chromium.org>
Reviewed-by: Dylan Reid <dgreid@chromium.org>
Reviewed-by: Zach Reizner <zachr@chromium.org>
Diffstat (limited to 'sys_util/src/file_traits.rs')
-rw-r--r--sys_util/src/file_traits.rs37
1 files changed, 37 insertions, 0 deletions
diff --git a/sys_util/src/file_traits.rs b/sys_util/src/file_traits.rs
new file mode 100644
index 0000000..2dfc28d
--- /dev/null
+++ b/sys_util/src/file_traits.rs
@@ -0,0 +1,37 @@
+// Copyright 2018 The Chromium OS Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+use std::fs::File;
+use std::io::Result;
+
+/// A trait for flushing the contents of a file to disk.
+/// This is equivalent to File's `sync_all` method, but
+/// wrapped in a trait so that it can be implemented for
+/// other types.
+pub trait FileSync {
+    // Flush buffers related to this file to disk.
+    fn fsync(&mut self) -> Result<()>;
+}
+
+impl FileSync for File {
+    fn fsync(&mut self) -> Result<()> {
+        self.sync_all()
+    }
+}
+
+/// A trait for setting the size of a file.
+/// This is equivalent to File's `set_len` method, but
+/// wrapped in a trait so that it can be implemented for
+/// other types.
+pub trait FileSetLen {
+    // Set the size of this file.
+    // This is the moral equivalent of `ftruncate()`.
+    fn set_len(&self, _len: u64) -> Result<()>;
+}
+
+impl FileSetLen for File {
+    fn set_len(&self, len: u64) -> Result<()> {
+        File::set_len(self, len)
+    }
+}