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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
// Copyright 2022 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use std::fs::File;
use std::io::Error;
use std::io::ErrorKind;
use std::io::Result;

use crate::VolatileSlice;

/// A trait for flushing the contents of a file to disk.
/// This is equivalent to File's `sync_all` and `sync_data` methods, but wrapped in a trait so that
/// it can be implemented for other types.
pub trait FileSync {
    // Flush buffers related to this file to disk.
    fn fsync(&self) -> Result<()>;

    // Flush buffers related to this file's data to disk, avoiding updating extra metadata. Note
    // that an implementation may simply implement fsync for fdatasync.
    fn fdatasync(&self) -> Result<()>;
}

impl FileSync for File {
    fn fsync(&self) -> Result<()> {
        self.sync_all()
    }

    fn fdatasync(&self) -> Result<()> {
        self.sync_data()
    }
}

/// A trait for setting the size of a file.
/// This is equivalent to File's `set_len` method, but
/// wrapped in a trait so that it can be implemented for
/// other types.
pub trait FileSetLen {
    // Set the size of this file.
    // This is the moral equivalent of `ftruncate()`.
    fn set_len(&self, _len: u64) -> Result<()>;
}

impl FileSetLen for File {
    fn set_len(&self, len: u64) -> Result<()> {
        File::set_len(self, len)
    }
}

/// A trait for allocating disk space in a sparse file.
/// This is equivalent to fallocate() with no special flags.
pub trait FileAllocate {
    /// Allocate storage for the region of the file starting at `offset` and extending `len` bytes.
    fn allocate(&self, offset: u64, len: u64) -> Result<()>;
}

/// A trait for getting the size of a file.
/// This is equivalent to File's metadata().len() method,
/// but wrapped in a trait so that it can be implemented for
/// other types.
pub trait FileGetLen {
    /// Get the current length of the file in bytes.
    fn get_len(&self) -> Result<u64>;
}

impl FileGetLen for File {
    fn get_len(&self) -> Result<u64> {
        Ok(self.metadata()?.len())
    }
}

/// A trait similar to `Read` and `Write`, but uses volatile memory as buffers.
pub trait FileReadWriteVolatile {
    /// Read bytes from this file into the given slice, returning the number of bytes read on
    /// success.
    fn read_volatile(&mut self, slice: VolatileSlice) -> Result<usize>;

    /// Like `read_volatile`, except it reads to a slice of buffers. Data is copied to fill each
    /// buffer in order, with the final buffer written to possibly being only partially filled. This
    /// method must behave as a single call to `read_volatile` with the buffers concatenated would.
    /// The default implementation calls `read_volatile` with either the first nonempty buffer
    /// provided, or returns `Ok(0)` if none exists.
    fn read_vectored_volatile(&mut self, bufs: &[VolatileSlice]) -> Result<usize> {
        bufs.iter()
            .find(|b| b.size() > 0)
            .map(|&b| self.read_volatile(b))
            .unwrap_or(Ok(0))
    }

    /// Reads bytes from this into the given slice until all bytes in the slice are written, or an
    /// error is returned.
    fn read_exact_volatile(&mut self, mut slice: VolatileSlice) -> Result<()> {
        while slice.size() > 0 {
            let bytes_read = self.read_volatile(slice)?;
            if bytes_read == 0 {
                return Err(Error::from(ErrorKind::UnexpectedEof));
            }
            // Will panic if read_volatile read more bytes than we gave it, which would be worthy of
            // a panic.
            slice = slice.offset(bytes_read).unwrap();
        }
        Ok(())
    }

    /// Write bytes from the slice to the given file, returning the number of bytes written on
    /// success.
    fn write_volatile(&mut self, slice: VolatileSlice) -> Result<usize>;

    /// Like `write_volatile`, except that it writes from a slice of buffers. Data is copied from
    /// each buffer in order, with the final buffer read from possibly being only partially
    /// consumed. This method must behave as a call to `write_volatile` with the buffers
    /// concatenated would. The default implementation calls `write_volatile` with either the first
    /// nonempty buffer provided, or returns `Ok(0)` if none exists.
    fn write_vectored_volatile(&mut self, bufs: &[VolatileSlice]) -> Result<usize> {
        bufs.iter()
            .find(|b| b.size() > 0)
            .map(|&b| self.write_volatile(b))
            .unwrap_or(Ok(0))
    }

    /// Write bytes from the slice to the given file until all the bytes from the slice have been
    /// written, or an error is returned.
    fn write_all_volatile(&mut self, mut slice: VolatileSlice) -> Result<()> {
        while slice.size() > 0 {
            let bytes_written = self.write_volatile(slice)?;
            if bytes_written == 0 {
                return Err(Error::from(ErrorKind::WriteZero));
            }
            // Will panic if read_volatile read more bytes than we gave it, which would be worthy of
            // a panic.
            slice = slice.offset(bytes_written).unwrap();
        }
        Ok(())
    }
}

impl<'a, T: FileReadWriteVolatile + ?Sized> FileReadWriteVolatile for &'a mut T {
    fn read_volatile(&mut self, slice: VolatileSlice) -> Result<usize> {
        (**self).read_volatile(slice)
    }

    fn read_vectored_volatile(&mut self, bufs: &[VolatileSlice]) -> Result<usize> {
        (**self).read_vectored_volatile(bufs)
    }

    fn read_exact_volatile(&mut self, slice: VolatileSlice) -> Result<()> {
        (**self).read_exact_volatile(slice)
    }

    fn write_volatile(&mut self, slice: VolatileSlice) -> Result<usize> {
        (**self).write_volatile(slice)
    }

    fn write_vectored_volatile(&mut self, bufs: &[VolatileSlice]) -> Result<usize> {
        (**self).write_vectored_volatile(bufs)
    }

    fn write_all_volatile(&mut self, slice: VolatileSlice) -> Result<()> {
        (**self).write_all_volatile(slice)
    }
}

/// A trait similar to the unix `ReadExt` and `WriteExt` traits, but for volatile memory.
pub trait FileReadWriteAtVolatile {
    /// Reads bytes from this file at `offset` into the given slice, returning the number of bytes
    /// read on success. On Windows file pointer will update with the read, but on Linux the
    /// file pointer will not change.
    fn read_at_volatile(&self, slice: VolatileSlice, offset: u64) -> Result<usize>;

    /// Like `read_at_volatile`, except it reads to a slice of buffers. Data is copied to fill each
    /// buffer in order, with the final buffer written to possibly being only partially filled. This
    /// method must behave as a single call to `read_at_volatile` with the buffers concatenated
    /// would. The default implementation calls `read_at_volatile` with either the first nonempty
    /// buffer provided, or returns `Ok(0)` if none exists.
    /// On Windows file pointer will update with the read, but on Linux the file pointer will not
    /// change.
    fn read_vectored_at_volatile(&self, bufs: &[VolatileSlice], offset: u64) -> Result<usize> {
        if let Some(&slice) = bufs.first() {
            self.read_at_volatile(slice, offset)
        } else {
            Ok(0)
        }
    }

    /// Reads bytes from this file at `offset` into the given slice until all bytes in the slice are
    /// read, or an error is returned. On Windows file pointer will update with the read, but on
    /// Linux the file pointer will not change.
    fn read_exact_at_volatile(&self, mut slice: VolatileSlice, mut offset: u64) -> Result<()> {
        while slice.size() > 0 {
            match self.read_at_volatile(slice, offset) {
                Ok(0) => return Err(Error::from(ErrorKind::UnexpectedEof)),
                Ok(n) => {
                    slice = slice.offset(n).unwrap();
                    offset = offset.checked_add(n as u64).unwrap();
                }
                Err(ref e) if e.kind() == ErrorKind::Interrupted => {}
                Err(e) => return Err(e),
            }
        }
        Ok(())
    }

    /// Writes bytes to this file at `offset` from the given slice, returning the number of bytes
    /// written on success. On Windows file pointer will update with the write, but on Linux the
    /// file pointer will not change.
    fn write_at_volatile(&self, slice: VolatileSlice, offset: u64) -> Result<usize>;

    /// Like `write_at_volatile`, except that it writes from a slice of buffers. Data is copied
    /// from each buffer in order, with the final buffer read from possibly being only partially
    /// consumed. This method must behave as a call to `write_at_volatile` with the buffers
    /// concatenated would. The default implementation calls `write_at_volatile` with either the
    /// first nonempty buffer provided, or returns `Ok(0)` if none exists.
    /// On Windows file pointer will update with the write, but on Linux the file pointer will not
    /// change.
    fn write_vectored_at_volatile(&self, bufs: &[VolatileSlice], offset: u64) -> Result<usize> {
        if let Some(&slice) = bufs.first() {
            self.write_at_volatile(slice, offset)
        } else {
            Ok(0)
        }
    }

    /// Writes bytes to this file at `offset` from the given slice until all bytes in the slice
    /// are written, or an error is returned. On Windows file pointer will update with the write,
    /// but on Linux the file pointer will not change.
    fn write_all_at_volatile(&self, mut slice: VolatileSlice, mut offset: u64) -> Result<()> {
        while slice.size() > 0 {
            match self.write_at_volatile(slice, offset) {
                Ok(0) => return Err(Error::from(ErrorKind::WriteZero)),
                Ok(n) => {
                    slice = slice.offset(n).unwrap();
                    offset = offset.checked_add(n as u64).unwrap();
                }
                Err(ref e) if e.kind() == ErrorKind::Interrupted => {}
                Err(e) => return Err(e),
            }
        }
        Ok(())
    }
}

impl<'a, T: FileReadWriteAtVolatile + ?Sized> FileReadWriteAtVolatile for &'a mut T {
    fn read_at_volatile(&self, slice: VolatileSlice, offset: u64) -> Result<usize> {
        (**self).read_at_volatile(slice, offset)
    }

    fn read_vectored_at_volatile(&self, bufs: &[VolatileSlice], offset: u64) -> Result<usize> {
        (**self).read_vectored_at_volatile(bufs, offset)
    }

    fn read_exact_at_volatile(&self, slice: VolatileSlice, offset: u64) -> Result<()> {
        (**self).read_exact_at_volatile(slice, offset)
    }

    fn write_at_volatile(&self, slice: VolatileSlice, offset: u64) -> Result<usize> {
        (**self).write_at_volatile(slice, offset)
    }

    fn write_vectored_at_volatile(&self, bufs: &[VolatileSlice], offset: u64) -> Result<usize> {
        (**self).write_vectored_at_volatile(bufs, offset)
    }

    fn write_all_at_volatile(&self, slice: VolatileSlice, offset: u64) -> Result<()> {
        (**self).write_all_at_volatile(slice, offset)
    }
}

impl<'a, T: FileReadWriteAtVolatile + ?Sized> FileReadWriteAtVolatile for &'a T {
    fn read_at_volatile(&self, slice: VolatileSlice, offset: u64) -> Result<usize> {
        (**self).read_at_volatile(slice, offset)
    }

    fn read_vectored_at_volatile(&self, bufs: &[VolatileSlice], offset: u64) -> Result<usize> {
        (**self).read_vectored_at_volatile(bufs, offset)
    }

    fn read_exact_at_volatile(&self, slice: VolatileSlice, offset: u64) -> Result<()> {
        (**self).read_exact_at_volatile(slice, offset)
    }

    fn write_at_volatile(&self, slice: VolatileSlice, offset: u64) -> Result<usize> {
        (**self).write_at_volatile(slice, offset)
    }

    fn write_vectored_at_volatile(&self, bufs: &[VolatileSlice], offset: u64) -> Result<usize> {
        (**self).write_vectored_at_volatile(bufs, offset)
    }

    fn write_all_at_volatile(&self, slice: VolatileSlice, offset: u64) -> Result<()> {
        (**self).write_all_at_volatile(slice, offset)
    }
}

#[cfg(test)]
mod tests {
    use std::io::Read;
    use std::io::Seek;
    use std::io::SeekFrom;
    use std::io::Write;

    use tempfile::tempfile;

    use super::*;

    #[test]
    fn read_file() -> Result<()> {
        let mut f = tempfile()?;
        f.write_all(b"AAAAAAAAAAbbbbbbbbbbAAAAA")
            .expect("Failed to write bytes");
        f.seek(SeekFrom::Start(0))?;

        let mut omem = [0u8; 30];
        let om = &mut omem[..];
        let buf = VolatileSlice::new(om);
        f.read_volatile(buf).expect("read_volatile failed.");

        f.seek(SeekFrom::Start(0))?;

        let mut mem = [0u8; 30];
        let (m1, rest) = mem.split_at_mut(10);
        let (m2, m3) = rest.split_at_mut(10);
        let buf1 = VolatileSlice::new(m1);
        let buf2 = VolatileSlice::new(m2);
        let buf3 = VolatileSlice::new(m3);
        let bufs = [buf1, buf2, buf3];

        f.read_vectored_volatile(&bufs)
            .expect("read_vectored_volatile failed.");

        assert_eq!(&mem[..], b"AAAAAAAAAAbbbbbbbbbbAAAAA\0\0\0\0\0");
        Ok(())
    }

    #[test]
    fn write_file() -> Result<()> {
        let mut f = tempfile()?;

        let mut omem = [0u8; 25];
        let om = &mut omem[..];
        let buf = VolatileSlice::new(om);
        buf.write_bytes(65);
        f.write_volatile(buf).expect("write_volatile failed.");

        f.seek(SeekFrom::Start(0))?;

        let mut filebuf = [0u8; 25];
        f.read_exact(&mut filebuf).expect("Failed to read filebuf");
        assert_eq!(&filebuf, b"AAAAAAAAAAAAAAAAAAAAAAAAA");
        Ok(())
    }

    #[test]
    fn write_vectored_file() -> Result<()> {
        let mut f = tempfile()?;

        let mut mem = [0u8; 30];
        let (m1, rest) = mem.split_at_mut(10);
        let (m2, m3) = rest.split_at_mut(10);
        let buf1 = VolatileSlice::new(m1);
        let buf2 = VolatileSlice::new(m2);
        let buf3 = VolatileSlice::new(m3);
        buf1.write_bytes(65);
        buf2.write_bytes(98);
        buf3.write_bytes(65);
        let bufs = [buf1, buf2, buf3];
        f.write_vectored_volatile(&bufs)
            .expect("write_vectored_volatile failed.");

        f.seek(SeekFrom::Start(0))?;

        let mut filebuf = [0u8; 30];
        f.read_exact(&mut filebuf).expect("Failed to read filebuf.");
        assert_eq!(&filebuf, b"AAAAAAAAAAbbbbbbbbbbAAAAAAAAAA");
        Ok(())
    }

    #[test]
    fn read_at_file() -> Result<()> {
        let mut f = tempfile()?;
        f.write_all(b"AAAAAAAAAAbbbbbbbbbbAAAAA")
            .expect("Failed to write bytes.");

        let mut omem = [0u8; 20];
        let om = &mut omem[..];
        let buf = VolatileSlice::new(om);
        f.read_at_volatile(buf, 10)
            .expect("read_at_volatile failed.");

        assert_eq!(om, b"bbbbbbbbbbAAAAA\0\0\0\0\0");

        let mut mem = [0u8; 20];
        let (m1, m2) = mem.split_at_mut(10);
        let buf1 = VolatileSlice::new(m1);
        let buf2 = VolatileSlice::new(m2);
        let bufs = [buf1, buf2];

        f.read_vectored_at_volatile(&bufs, 10)
            .expect("read_vectored_at_volatile failed.");

        assert_eq!(&mem[..], b"bbbbbbbbbbAAAAA\0\0\0\0\0");
        Ok(())
    }

    #[test]
    fn write_at_file() -> Result<()> {
        let mut f = tempfile()?;
        f.write_all(b"ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ")
            .expect("Failed to write bytes");

        let mut omem = [0u8; 15];
        let om = &mut omem[..];
        let buf = VolatileSlice::new(om);
        buf.write_bytes(65);
        f.write_at_volatile(buf, 10)
            .expect("write_at_volatile failed.");

        f.seek(SeekFrom::Start(0))?;

        let mut filebuf = [0u8; 30];
        f.read_exact(&mut filebuf).expect("Failed to read filebuf.");
        assert_eq!(&filebuf, b"ZZZZZZZZZZAAAAAAAAAAAAAAAZZZZZ");
        Ok(())
    }

    #[test]
    fn write_vectored_at_file() -> Result<()> {
        let mut f = tempfile()?;
        f.write_all(b"ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ")
            .expect("Failed to write bytes");

        let mut mem = [0u8; 30];
        let (m1, m2) = mem.split_at_mut(10);
        let buf1 = VolatileSlice::new(m1);
        let buf2 = VolatileSlice::new(m2);
        buf1.write_bytes(65);
        buf2.write_bytes(98);
        let bufs = [buf1, buf2];
        f.write_vectored_at_volatile(&bufs, 10)
            .expect("write_vectored_at_volatile failed.");

        f.seek(SeekFrom::Start(0))?;

        let mut filebuf = [0u8; 30];
        f.read_exact(&mut filebuf).expect("Failed to read filebuf.");
        assert_eq!(&filebuf, b"ZZZZZZZZZZAAAAAAAAAAbbbbbbbbbb");
        Ok(())
    }
}