disktest_rawio/
lib.rs

1// -*- coding: utf-8 -*-
2//
3// disktest - Storage tester
4//
5// Copyright 2020-2024 Michael Büsch <m@bues.ch>
6//
7// Licensed under the Apache License version 2.0
8// or the MIT license, at your option.
9// SPDX-License-Identifier: Apache-2.0 OR MIT
10//
11
12#[cfg(not(any(target_os = "linux", target_os = "android", target_os = "windows")))]
13std::compile_error!(
14    "Your operating system is not supported, yet. \
15     Please open an issue on GitHub: \
16     https://github.com/mbuesch/disktest/issues"
17);
18
19use anyhow as ah;
20use std::path::Path;
21
22#[cfg(any(target_os = "linux", target_os = "android"))]
23mod linux;
24
25#[cfg(target_os = "windows")]
26mod windows;
27
28pub const DEFAULT_SECTOR_SIZE: u32 = 512;
29
30/// OS interface for raw I/O.
31pub trait RawIoOsIntf: Sized {
32    /// Open a file or device.
33    fn new(path: &Path, create: bool, read: bool, write: bool) -> ah::Result<Self>;
34
35    /// Get the physical sector size of the file or device.
36    /// Returns None, if this is not a raw device.
37    fn get_sector_size(&self) -> Option<u32>;
38
39    /// Close the file, flush all buffers and drop all caches.
40    /// This function ensures that subsequent reads are not read from RAM cache.
41    fn drop_file_caches(&mut self, offset: u64, size: u64) -> ah::Result<()>;
42
43    /// Close the file and flush all buffers.
44    /// (This does not affect the caches).
45    fn close(&mut self) -> ah::Result<()>;
46
47    /// Flush all buffers.
48    /// (This does not affect the caches).
49    fn sync(&mut self) -> ah::Result<()>;
50
51    /// Truncate or extend the file length to the given size.
52    /// This method is for unit testing only.
53    fn set_len(&mut self, size: u64) -> ah::Result<()>;
54
55    /// Seek to a file offset.
56    fn seek(&mut self, offset: u64) -> ah::Result<u64>;
57
58    /// Read a chunk of data.
59    fn read(&mut self, buffer: &mut [u8]) -> ah::Result<RawIoResult>;
60
61    /// Write a chunk of data.
62    fn write(&mut self, buffer: &[u8]) -> ah::Result<RawIoResult>;
63}
64
65/// Raw I/O operation result code.
66pub enum RawIoResult {
67    /// Ok, number of processed bytes.
68    Ok(usize),
69    /// Out of disk space.
70    Enospc,
71}
72
73#[cfg(any(target_os = "linux", target_os = "android"))]
74pub use crate::linux::RawIoLinux as RawIo;
75
76#[cfg(target_os = "windows")]
77pub use crate::windows::RawIoWindows as RawIo;
78
79// vim: ts=4 sw=4 expandtab