virtual_fs/
cow_file.rs

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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
//! Used for /dev/zero - infinitely returns zero
//! which is useful for commands like `dd if=/dev/zero of=bigfile.img size=1G`

use derive_more::Debug;
use replace_with::replace_with_or_abort;
use std::pin::Pin;
use std::task::{Context, Poll};
use std::{
    future::Future,
    io::{self, *},
};

use tokio::io::{AsyncRead, AsyncReadExt, AsyncSeek, AsyncSeekExt, AsyncWrite};

use crate::{BufferFile, VirtualFile};

#[derive(Debug)]
enum CowState {
    ReadOnly(Box<dyn VirtualFile + Send + Sync>),
    Copying {
        #[debug(skip)]
        future: Pin<Box<dyn Future<Output = io::Result<BufferFile>> + Send + Sync>>,
        requested_size: Option<u64>,
        requested_position: Option<SeekFrom>,
        cached_size: u64,
    },
    Copied(BufferFile),
}

impl CowState {
    fn inner_mut(&mut self) -> &mut (dyn VirtualFile + Send + Sync) {
        match self {
            Self::ReadOnly(inner) => inner.as_mut(),
            Self::Copying { .. } => panic!("Cannot access inner file while copying"),
            Self::Copied(inner) => inner,
        }
    }
}

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

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),
        }
    }

    async fn copy(mut inner: Box<dyn VirtualFile + Send + Sync>) -> io::Result<BufferFile> {
        let initial_position = inner.seek(SeekFrom::Current(0)).await?;
        inner.seek(SeekFrom::Start(0)).await?;

        let mut buffer = [0u8; 8192];
        let mut buffer_file = BufferFile::default();
        loop {
            let read_bytes = inner.read_buf(&mut &mut buffer[..]).await?;
            if read_bytes == 0 {
                break;
            }
            buffer_file.data.write_all(&buffer[0..read_bytes])?;
        }

        buffer_file.seek(SeekFrom::Start(initial_position)).await?;

        Ok(buffer_file)
    }

    fn poll_copy_progress(&mut self, cx: &mut Context) -> Poll<io::Result<()>> {
        match self.state {
            CowState::Copying {
                ref mut future,
                requested_size,
                requested_position,
                ..
            } => match future.as_mut().poll(cx) {
                Poll::Ready(Ok(mut buf)) => {
                    if let Some(requested_size) = requested_size {
                        buf.set_len(requested_size)?;
                    }
                    if let Some(requested_position) = requested_position {
                        Pin::new(&mut buf).start_seek(requested_position)?;
                    }
                    self.state = CowState::Copied(buf);
                    Poll::Ready(Ok(()))
                }
                Poll::Ready(Err(e)) => Poll::Ready(Err(e)),
                Poll::Pending => Poll::Pending,
            },
            _ => Poll::Ready(Ok(())),
        }
    }

    fn start_copy(&mut self) {
        replace_with_or_abort(&mut self.state, |state| match state {
            CowState::ReadOnly(inner) => CowState::Copying {
                cached_size: inner.size(),
                requested_size: None,
                requested_position: None,
                future: Box::pin(Self::copy(inner)),
            },
            state => state,
        });
    }

    fn poll_copy_start_and_progress(&mut self, cx: &mut Context) -> Poll<io::Result<()>> {
        self.start_copy();
        self.poll_copy_progress(cx)
    }
}

impl AsyncSeek for CopyOnWriteFile {
    fn start_seek(mut self: Pin<&mut Self>, position: io::SeekFrom) -> io::Result<()> {
        match self.state {
            CowState::Copying {
                ref mut requested_position,
                ..
            } => {
                *requested_position = Some(position);
                Ok(())
            }

            _ => Pin::new(self.state.inner_mut()).start_seek(position),
        }
    }

    fn poll_complete(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<u64>> {
        match self.poll_copy_progress(cx) {
            Poll::Ready(Ok(())) => {}
            Poll::Pending => return Poll::Pending,
            Poll::Ready(Err(err)) => return Poll::Ready(Err(err)),
        }

        Pin::new(self.state.inner_mut()).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(())) => {}
        }
        Pin::new(self.state.inner_mut()).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(())) => {}
        }
        Pin::new(self.state.inner_mut()).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,
        }
        Pin::new(self.state.inner_mut()).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,
        }
        Pin::new(self.state.inner_mut()).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,
        }
        Pin::new(self.state.inner_mut()).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 set_times(&mut self, atime: Option<u64>, mtime: Option<u64>) -> crate::Result<()> {
        if let Some(atime) = atime {
            self.last_accessed = atime;
        }
        if let Some(mtime) = mtime {
            self.last_modified = mtime;
        }

        Ok(())
    }

    fn size(&self) -> u64 {
        match &self.state {
            CowState::ReadOnly(inner) => inner.size(),
            CowState::Copying {
                requested_size: Some(size),
                ..
            } => *size,
            CowState::Copying { cached_size, .. } => *cached_size,
            CowState::Copied(buffer_file) => buffer_file.size(),
        }
    }

    fn set_len(&mut self, new_size: u64) -> crate::Result<()> {
        match self.state {
            CowState::ReadOnly(_) => {
                self.start_copy();
                let CowState::Copying {
                    ref mut requested_size,
                    ..
                } = self.state
                else {
                    unreachable!()
                };
                *requested_size = Some(new_size);
            }

            CowState::Copying {
                ref mut requested_size,
                ..
            } => {
                *requested_size = Some(new_size);
            }

            CowState::Copied(ref mut buf) => {
                buf.set_len(new_size)?;
            }
        }

        Ok(())
    }

    fn unlink(&mut self) -> crate::Result<()> {
        // TODO: one can imagine interrupting an in-progress copy here
        self.set_len(0)
    }

    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(())) => {}
        }
        Pin::new(self.state.inner_mut()).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)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    // This is as weird a test as it gets, yes, but I'm (unashamedly!) cramming
    // everything we know was wrong with the impl into this one test to save time.
    #[tokio::test]
    async fn cow_file_works() {
        let mut data = Vec::with_capacity(16385);
        for i in 0..16385 {
            data.push(i as u8);
        }
        let inner = BufferFile {
            data: Cursor::new(data),
        };
        let mut file = CopyOnWriteFile::new(Box::new(inner));

        assert!(matches!(file.state, CowState::ReadOnly(_)));
        assert_eq!(file.size(), 16385);
        assert_ne!(file.created_time(), 0);
        assert_ne!(file.last_accessed(), 0);
        assert_ne!(file.last_modified(), 0);

        let mut buf = [0u8; 4];
        let read = file.read_exact(buf.as_mut()).await.unwrap();
        assert_eq!(read, 4);
        assert_eq!(buf, [0, 1, 2, 3]);
        assert_eq!(file.seek(SeekFrom::Current(0)).await.unwrap(), 4);
        assert!(matches!(file.state, CowState::ReadOnly { .. }));

        // After this call, the file will "start" copying, but the actual
        // future won't be polled until we try to read or write.
        file.start_copy();
        assert!(matches!(file.state, CowState::Copying { .. }));
        assert_eq!(file.size(), 16385);

        // The cached length should be returned while copying
        file.set_len(16400).unwrap();
        assert!(matches!(file.state, CowState::Copying { .. }));
        assert_eq!(file.size(), 16400);

        // Now try to read from the file, which will trigger the copy
        let read = file.read_exact(buf.as_mut()).await.unwrap();
        assert!(matches!(file.state, CowState::Copied { .. }));
        assert_eq!(read, 4);
        assert_eq!(buf, [4, 5, 6, 7]);
        assert_eq!(file.seek(SeekFrom::Current(0)).await.unwrap(), 8);
        assert_eq!(file.size(), 16400);

        file.seek(SeekFrom::Start(16383)).await.unwrap();
        let read = file.read_exact(buf.as_mut()).await.unwrap();
        assert_eq!(read, 4);
        // set_len should have filled the rest with zeroes
        assert_eq!(buf, [(16383 % 256) as u8, (16384 % 256) as u8, 0, 0]);
        assert_eq!(file.seek(SeekFrom::Current(0)).await.unwrap(), 16387);
        assert!(matches!(file.state, CowState::Copied { .. }));
    }
}