summary refs log tree commit diff
path: root/qcow_utils/src/qcow_utils.rs
diff options
context:
space:
mode:
authorDylan Reid <dgreid@chromium.org>2018-01-18 13:39:51 -0800
committerchrome-bot <chrome-bot@chromium.org>2018-02-01 21:29:31 -0800
commit6ecbbfd72312a07c1b9cd686aa2594f072448d40 (patch)
tree8bbc337835e4cf936fd3b28603d7166bcf2abb4c /qcow_utils/src/qcow_utils.rs
parent29cd40a1d61c0d558768e0d32f07f7b7ad84ca63 (diff)
downloadcrosvm-6ecbbfd72312a07c1b9cd686aa2594f072448d40.tar
crosvm-6ecbbfd72312a07c1b9cd686aa2594f072448d40.tar.gz
crosvm-6ecbbfd72312a07c1b9cd686aa2594f072448d40.tar.bz2
crosvm-6ecbbfd72312a07c1b9cd686aa2594f072448d40.tar.lz
crosvm-6ecbbfd72312a07c1b9cd686aa2594f072448d40.tar.xz
crosvm-6ecbbfd72312a07c1b9cd686aa2594f072448d40.tar.zst
crosvm-6ecbbfd72312a07c1b9cd686aa2594f072448d40.zip
Add qcow_utils for exposing qcow functions as a C library
This C library will be use by the VM launcher to create the qcow2 files
used for persistent VM data.

CQ-DEPEND=CL:884263
BUG=none
TEST=cargo test --all -- --test-threads=1

Change-Id: Ibd7f71d2e3f1f72f781978f014865d2161f033f5
Signed-off-by: Dylan Reid <dgreid@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/875116
Diffstat (limited to 'qcow_utils/src/qcow_utils.rs')
-rw-r--r--qcow_utils/src/qcow_utils.rs47
1 files changed, 47 insertions, 0 deletions
diff --git a/qcow_utils/src/qcow_utils.rs b/qcow_utils/src/qcow_utils.rs
new file mode 100644
index 0000000..083db0b
--- /dev/null
+++ b/qcow_utils/src/qcow_utils.rs
@@ -0,0 +1,47 @@
+// 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.
+
+// Exported interface to basic qcow functionality to be used from C.
+
+extern crate libc;
+extern crate qcow;
+
+use std::ffi::CStr;
+use std::fs::OpenOptions;
+use std::os::raw::{c_char, c_int};
+use libc::EINVAL;
+
+use qcow::QcowHeader;
+
+#[no_mangle]
+pub extern "C" fn create_qcow_with_size(path: *const c_char,
+                                        virtual_size: u64) -> c_int {
+    let c_str = unsafe {
+        // NULL pointers are checked, but this will access any other invalid pointer passed from C
+        // code. It's the caller's responsibility to pass a valid pointer.
+        if path.is_null() {
+            return -EINVAL;
+        }
+        CStr::from_ptr(path)
+    };
+    let file_path = match c_str.to_str() {
+        Ok(s) => s,
+        Err(_) => return -EINVAL,
+    };
+
+    let h = QcowHeader::create_for_size(virtual_size);
+    let mut file = match OpenOptions::new()
+                            .create(true)
+                            .read(true)
+                            .write(true)
+                            .open(file_path) {
+        Ok(f) => f,
+        Err(_) => return -1,
+    };
+
+    match h.write_to(&mut file) {
+        Ok(()) => 0,
+        Err(_) => -1,
+    }
+}