aboutsummaryrefslogtreecommitdiffstats
path: root/disktest-rawio/src/lib.rs
blob: 6a53c4b824bcd5685b34be3d68c6a4d8cf8e82d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// -*- coding: utf-8 -*-
//
// disktest - Storage tester
//
// Copyright 2020-2024 Michael Büsch <m@bues.ch>
//
// Licensed under the Apache License version 2.0
// or the MIT license, at your option.
// SPDX-License-Identifier: Apache-2.0 OR MIT
//

#[cfg(not(any(target_os = "linux", target_os = "android", target_os = "windows")))]
std::compile_error!(
    "Your operating system is not supported, yet. \
     Please open an issue on GitHub: \
     https://github.com/mbuesch/disktest/issues"
);

use anyhow as ah;
use std::path::Path;

#[cfg(any(target_os = "linux", target_os = "android"))]
mod linux;

#[cfg(target_os = "windows")]
mod windows;

pub const DEFAULT_SECTOR_SIZE: u32 = 512;

/// OS interface for raw I/O.
trait RawIoOsIntf {
    fn get_sector_size(&self) -> Option<u32>;
    fn drop_file_caches(&mut self, offset: u64, size: u64) -> ah::Result<()>;
    fn close(&mut self) -> ah::Result<()>;
    fn sync(&mut self) -> ah::Result<()>;
    fn set_len(&mut self, size: u64) -> ah::Result<()>;
    fn seek(&mut self, offset: u64) -> ah::Result<u64>;
    fn read(&mut self, buffer: &mut [u8]) -> ah::Result<RawIoResult>;
    fn write(&mut self, buffer: &[u8]) -> ah::Result<RawIoResult>;
}

/// Raw I/O operation result code.
pub enum RawIoResult {
    /// Ok, number of processed bytes.
    Ok(usize),
    /// Out of disk space.
    Enospc,
}

/// Raw device I/O abstraction.
pub struct RawIo {
    os: Box<dyn RawIoOsIntf>,
}

impl RawIo {
    /// Open a file or device.
    pub fn new(path: &Path, create: bool, read: bool, write: bool) -> ah::Result<Self> {
        #[cfg(any(target_os = "linux", target_os = "android"))]
        let os = Box::new(linux::RawIoLinux::new(path, create, read, write)?);

        #[cfg(target_os = "windows")]
        let os = Box::new(windows::RawIoWindows::new(path, create, read, write)?);

        Ok(Self { os })
    }

    /// Get the physical sector size of the file or device.
    /// Returns None, if this is not a raw device.
    pub fn get_sector_size(&self) -> Option<u32> {
        self.os.get_sector_size()
    }

    /// Close the file, flush all buffers and drop all caches.
    /// This function ensures that subsequent reads are not read from RAM cache.
    pub fn drop_file_caches(mut self, offset: u64, size: u64) -> ah::Result<()> {
        self.os.drop_file_caches(offset, size)
    }

    /// Close the file and flush all buffers.
    /// (This does not affect the caches).
    pub fn close(&mut self) -> ah::Result<()> {
        self.os.close()
    }

    /// Flush all buffers.
    /// (This does not affect the caches).
    pub fn sync(&mut self) -> ah::Result<()> {
        self.os.sync()
    }

    #[cfg_attr(not(test), allow(dead_code))]
    pub fn set_len(&mut self, size: u64) -> ah::Result<()> {
        self.os.set_len(size)
    }

    /// Seek to a file offset.
    pub fn seek(&mut self, offset: u64) -> ah::Result<u64> {
        self.os.seek(offset)
    }

    /// Read a chunk of data.
    pub fn read(&mut self, buffer: &mut [u8]) -> ah::Result<RawIoResult> {
        self.os.read(buffer)
    }

    /// Write a chunk of data.
    pub fn write(&mut self, buffer: &[u8]) -> ah::Result<RawIoResult> {
        self.os.write(buffer)
    }
}

// vim: ts=4 sw=4 expandtab
bues.ch cgit interface