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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#[cfg(all(feature = "async", not(feature = "std")))]
use alloc::boxed::Box;

#[cfg(feature = "async")]
use async_trait::async_trait;

use crate::error::Error;
use crate::types::SectorID;

pub type Sector = [u8; 512];

pub(crate) fn flatten(sector: &[Sector]) -> &[u8] {
    unsafe { core::slice::from_raw_parts(&sector[0][0], sector.len() * 512) }
}

#[cfg_attr(feature = "async", async_trait)]
#[cfg_attr(not(feature = "async"), deasync::deasync)]
pub trait IO: Clone {
    type Error: core::fmt::Debug;
    /// Default to 512
    fn set_sector_size(&mut self, size: usize) -> Result<(), Self::Error>;
    async fn read<'a>(&'a mut self, id: SectorID) -> Result<&'a [Sector], Self::Error>;
    /// Caller guarantees bytes.len() <= SECTOR_SIZE - offset
    async fn write(&mut self, id: SectorID, offset: usize, data: &[u8]) -> Result<(), Self::Error>;
    async fn flush(&mut self) -> Result<(), Self::Error>;
}

#[derive(Clone)]
pub(crate) struct IOWrapper<IO>(IO);

#[cfg_attr(not(feature = "async"), deasync::deasync)]
impl<E, IO: crate::io::IO<Error = E>> IOWrapper<IO> {
    pub(crate) fn new(io: IO) -> Self {
        Self(io)
    }

    pub(crate) async fn read<'a>(&'a mut self, sector: SectorID) -> Result<&'a [Sector], Error<E>> {
        self.0.read(sector).await.map_err(|e| Error::IO(e))
    }

    pub(crate) async fn write(
        &mut self,
        id: SectorID,
        offset: usize,
        data: &[u8],
    ) -> Result<(), Error<E>> {
        let result = self.0.write(id, offset, data).await;
        result.map_err(|e| Error::IO(e))
    }

    pub(crate) async fn flush(&mut self) -> Result<(), Error<E>> {
        self.0.flush().await.map_err(|e| Error::IO(e))
    }
}

#[cfg(feature = "std")]
pub mod std {
    use std::mem::MaybeUninit;
    use std::slice::from_raw_parts;

    #[cfg(feature = "async")]
    use async_std as std_;
    #[cfg(not(feature = "async"))]
    use std as std_;
    use std_::{
        fs::File,
        io::prelude::*,
        io::SeekFrom,
        path::Path,
        sync::{Arc, Mutex},
    };

    #[cfg(feature = "async")]
    use async_trait::async_trait;

    use crate::types::SectorID;

    const MAX_SECTOR_SIZE: usize = 4096;

    #[derive(Debug)]
    pub struct FileIO {
        file: Arc<Mutex<File>>,
        sector_size: usize,
        buffer: MaybeUninit<[u8; MAX_SECTOR_SIZE]>,
    }

    impl Clone for FileIO {
        fn clone(&self) -> Self {
            Self {
                file: self.file.clone(),
                sector_size: self.sector_size,
                buffer: MaybeUninit::uninit(),
            }
        }
    }

    #[cfg_attr(not(feature = "async"), deasync::deasync)]
    impl FileIO {
        pub async fn open<P: AsRef<Path>>(filepath: P) -> std::io::Result<Self> {
            let mut options = match () {
                #[cfg(feature = "async")]
                () => async_std::fs::OpenOptions::new(),
                #[cfg(not(feature = "async"))]
                () => File::options(),
            };
            let result = options.read(true).write(true).open(filepath).await;
            result.map(|file| Self {
                file: Arc::new(Mutex::new(file)),
                sector_size: 512,
                buffer: MaybeUninit::uninit(),
            })
        }
    }

    #[cfg_attr(feature = "async", async_trait)]
    #[cfg_attr(not(feature = "async"), deasync::deasync)]
    impl super::IO for FileIO {
        type Error = std::io::Error;

        fn set_sector_size(&mut self, size: usize) -> Result<(), Self::Error> {
            self.sector_size = size;
            Ok(())
        }

        async fn read<'a>(&'a mut self, sector: SectorID) -> Result<&'a [[u8; 512]], Self::Error> {
            let seek = SeekFrom::Start(u64::from(sector) * self.sector_size as u64);

            let mut file = match () {
                #[cfg(not(feature = "async"))]
                () => self.file.lock().unwrap(),
                #[cfg(feature = "async")]
                () => self.file.lock().await,
            };
            file.seek(seek).await?;
            let buffer = unsafe { self.buffer.assume_init_mut() };
            file.read_exact(&mut buffer[..self.sector_size]).await?;

            Ok(unsafe { from_raw_parts(buffer.as_ptr() as *const _, self.sector_size / 512) })
        }

        async fn write(
            &mut self,
            sector: SectorID,
            offset: usize,
            buf: &[u8],
        ) -> Result<(), Self::Error> {
            let mut file = match () {
                #[cfg(not(feature = "async"))]
                () => self.file.lock().unwrap(),
                #[cfg(feature = "async")]
                () => self.file.lock().await,
            };
            let seek = SeekFrom::Start(u64::from(sector) * self.sector_size as u64 + offset as u64);
            file.seek(seek).await?;
            file.write_all(buf).await.map(|_| ())
        }

        async fn flush(&mut self) -> Result<(), Self::Error> {
            let mut file = match () {
                #[cfg(not(feature = "async"))]
                () => self.file.lock().unwrap(),
                #[cfg(feature = "async")]
                () => self.file.lock().await,
            };
            file.flush().await
        }
    }
}