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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
//! Used for /dev/zero - infinitely returns zero
//! which is useful for commands like `dd if=/dev/zero of=bigfile.img size=1G`

use futures::future::BoxFuture;
use replace_with::replace_with_or_abort;
use std::io::{self, *};
use std::pin::Pin;
use std::task::{Context, Poll};

use tokio::io::{AsyncRead, AsyncSeek, AsyncWrite, ReadBuf};

use crate::{BufferFile, VirtualFile};

#[derive(Debug)]
enum CowState {
    ReadOnly(Box<dyn VirtualFile + Send + Sync>),
    Copying {
        pos: u64,
        inner: Box<dyn VirtualFile + Send + Sync>,
    },
    Copied,
}
impl CowState {
    fn as_ref(&self) -> Option<&(dyn VirtualFile + Send + Sync)> {
        match self {
            Self::ReadOnly(inner) => Some(inner.as_ref()),
            Self::Copying { inner, .. } => Some(inner.as_ref()),
            _ => None,
        }
    }
    fn as_mut(&mut self) -> Option<&mut Box<dyn VirtualFile + Send + Sync>> {
        match self {
            Self::ReadOnly(inner) => Some(inner),
            Self::Copying { inner, .. } => Some(inner),
            _ => None,
        }
    }
}

#[derive(Debug)]
pub struct CopyOnWriteFile {
    last_accessed: u64,
    last_modified: u64,
    created_time: u64,
    state: CowState,
    buf: BufferFile,
}

impl CopyOnWriteFile {
    pub fn new(inner: Box<dyn VirtualFile + Send + Sync>) -> Self {
        Self {
            last_accessed: inner.last_accessed(),
            last_modified: inner.last_modified(),
            created_time: inner.created_time(),
            state: CowState::ReadOnly(inner),
            buf: BufferFile::default(),
        }
    }
    fn poll_copy_progress(&mut self, cx: &mut Context) -> Poll<io::Result<()>> {
        if let CowState::Copying { ref mut inner, pos } = &mut self.state {
            let mut temp = [0u8; 8192];

            while *pos < inner.size() {
                let mut read_temp = ReadBuf::new(&mut temp);

                if let Err(err) = Pin::new(inner.as_mut()).start_seek(SeekFrom::Start(*pos)) {
                    return Poll::Ready(Err(err));
                }
                match Pin::new(inner.as_mut()).poll_complete(cx).map_ok(|_| ()) {
                    Poll::Ready(Ok(())) => {}
                    p => return p,
                }
                match Pin::new(inner.as_mut()).poll_read(cx, &mut read_temp) {
                    Poll::Pending => return Poll::Pending,
                    Poll::Ready(Err(err)) => return Poll::Ready(Err(err)),
                    Poll::Ready(Ok(())) => {}
                }
                if read_temp.remaining() == 0 {
                    return Poll::Pending;
                }
                *pos += read_temp.remaining() as u64;

                self.buf.data.write_all(read_temp.filled()).unwrap();
            }
            self.state = CowState::Copied;
        }
        Poll::Ready(Ok(()))
    }
    fn poll_copy_start_and_progress(&mut self, cx: &mut Context) -> Poll<io::Result<()>> {
        replace_with_or_abort(&mut self.state, |state| match state {
            CowState::ReadOnly(inner) => CowState::Copying { pos: 0, inner },
            state => state,
        });
        self.poll_copy_progress(cx)
    }
}

impl AsyncSeek for CopyOnWriteFile {
    fn start_seek(mut self: Pin<&mut Self>, position: io::SeekFrom) -> io::Result<()> {
        let data = Pin::new(&mut self.buf);
        data.start_seek(position)?;

        if let Some(inner) = self.state.as_mut() {
            Pin::new(inner.as_mut()).start_seek(position)?;
        }

        Ok(())
    }

    fn poll_complete(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<u64>> {
        match self.state.as_mut() {
            Some(inner) => Pin::new(inner.as_mut()).poll_complete(cx),
            None => {
                let data = Pin::new(&mut self.buf);
                data.poll_complete(cx)
            }
        }
    }
}

impl AsyncWrite for CopyOnWriteFile {
    fn poll_write(
        mut self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &[u8],
    ) -> Poll<io::Result<usize>> {
        match self.poll_copy_start_and_progress(cx) {
            Poll::Pending => return Poll::Pending,
            Poll::Ready(Err(err)) => return Poll::Ready(Err(err)),
            Poll::Ready(Ok(())) => {}
        }
        let data = Pin::new(&mut self.buf);
        data.poll_write(cx, buf)
    }

    fn poll_write_vectored(
        mut self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        bufs: &[io::IoSlice<'_>],
    ) -> Poll<io::Result<usize>> {
        match self.poll_copy_start_and_progress(cx) {
            Poll::Pending => return Poll::Pending,
            Poll::Ready(Err(err)) => return Poll::Ready(Err(err)),
            Poll::Ready(Ok(())) => {}
        }
        let data = Pin::new(&mut self.buf);
        data.poll_write_vectored(cx, bufs)
    }

    fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
        match self.poll_copy_start_and_progress(cx) {
            Poll::Ready(Ok(())) => {}
            p => return p,
        }
        let data = Pin::new(&mut self.buf);
        data.poll_flush(cx)
    }

    fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
        match self.poll_copy_start_and_progress(cx) {
            Poll::Ready(Ok(())) => {}
            p => return p,
        }
        let data = Pin::new(&mut self.buf);
        data.poll_shutdown(cx)
    }
}

impl AsyncRead for CopyOnWriteFile {
    fn poll_read(
        mut self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &mut tokio::io::ReadBuf<'_>,
    ) -> Poll<io::Result<()>> {
        match self.poll_copy_progress(cx) {
            Poll::Ready(Ok(())) => {}
            p => return p,
        }
        match self.state.as_mut() {
            Some(inner) => Pin::new(inner.as_mut()).poll_read(cx, buf),
            None => {
                let data = Pin::new(&mut self.buf);
                data.poll_read(cx, buf)
            }
        }
    }
}

impl VirtualFile for CopyOnWriteFile {
    fn last_accessed(&self) -> u64 {
        self.last_accessed
    }
    fn last_modified(&self) -> u64 {
        self.last_modified
    }
    fn created_time(&self) -> u64 {
        self.created_time
    }
    fn size(&self) -> u64 {
        match self.state.as_ref() {
            Some(inner) => inner.size(),
            None => self.buf.size(),
        }
    }
    fn set_len(&mut self, new_size: u64) -> crate::Result<()> {
        self.buf.set_len(new_size)
    }
    fn unlink(&mut self) -> BoxFuture<'static, crate::Result<()>> {
        let ret = self.buf.set_len(0);
        Box::pin(async move { ret })
    }
    fn poll_read_ready(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<usize>> {
        match self.poll_copy_progress(cx) {
            Poll::Pending => return Poll::Pending,
            Poll::Ready(Err(err)) => return Poll::Ready(Err(err)),
            Poll::Ready(Ok(())) => {}
        }
        match self.state.as_mut() {
            Some(inner) => Pin::new(inner.as_mut()).poll_read_ready(cx),
            None => {
                let data: Pin<&mut BufferFile> = Pin::new(&mut self.buf);
                data.poll_read_ready(cx)
            }
        }
    }

    fn poll_write_ready(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<usize>> {
        self.poll_copy_progress(cx).map_ok(|_| 8192)
    }
}