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
449
450
451
452
// Copyright 2018 Urs Schulz
//
// This file is part of mmap-safe.
//
// mmap-safe is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// mmap-safe is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with mmap-safe.  If not, see <http://www.gnu.org/licenses/>.
//
//! This library provides a thin wrapper around the `memmap` crate in order to make file-backed
//! mapped memory safe in Rust. Although the crate tries it's best at ensuring safety, this can not
//! be fully guaranteed.
//!
//! # Safe and Unsafe usage
//! Since Linux and other Unix-Systems currently do not provide mandatory file locks (Linux does
//! but it's buggy and will be removed in the future), it is not possible to prevent parallel
//! access to a certain file. Therefore file-backed mapped memory is inherently unsafe.
//!
//! However, if you access a file only through this library and not through the [`std::fs::File`]
//! API, you are safe. This crate uses an advisory lock internally to make sure only one
//! [`MappedFile`] instance ever exists at the same time for the same file.
//!
//! Oh, and don't use it on network filesystems, I haven't tested that.
use std::fs::File;
use std::fs::OpenOptions;
use std::io;
use std::io::Seek;
use std::io::SeekFrom;
use std::marker::PhantomData;
use std::ops::Deref;
use std::ops::DerefMut;
use std::path::Path;

use fs2::FileExt;

use memmap::Mmap;
use memmap::MmapMut;

/// A file mapped to memory.
pub struct Mapping<'a> {
    mmap: Option<Mmap>,
    _lt: PhantomData<&'a ()>,
}


/// A file mutably mapped to memory.
pub struct MutMapping<'a> {
    mmap: Option<MmapMut>,
    _lt: PhantomData<&'a mut ()>,
}

impl<'a> MutMapping<'a> {
    pub fn flush(&self) -> io::Result<()> {
        self.mmap
            .as_ref()
            .map(|mm| mm.flush())
            .or(Some(Ok(())))
            .unwrap()
    }

    pub fn flush_range(&self, offset: usize, len: usize) -> io::Result<()> {
        self.mmap
            .as_ref()
            .map(|mm| mm.flush_range(offset, len))
            .or(Some(Ok(())))
            .unwrap()
    }
}

// We need this Drop in order to convince the borrow checker not to allow any new MutMapping
// instances before the old ones got fully drop()ed.
impl<'a> Drop for Mapping<'a> {
    fn drop(&mut self) {}
}

impl<'a> Drop for MutMapping<'a> {
    fn drop(&mut self) {}
}

impl<'a> Deref for Mapping<'a> {
    type Target = [u8];

    #[inline]
    fn deref(&self) -> &Self::Target {
        match self.mmap.as_ref() {
            Some(mm) => mm,
            None => &[],
        }
    }
}

impl<'a> AsRef<[u8]> for Mapping<'a> {
    #[inline]
    fn as_ref(&self) -> &[u8] {
        self.deref()
    }
}

impl<'a> Deref for MutMapping<'a> {
    type Target = [u8];

    #[inline]
    fn deref(&self) -> &Self::Target {
        match self.mmap.as_ref() {
            Some(mm) => mm,
            None => &[],
        }
    }
}

impl<'a> DerefMut for MutMapping<'a> {
    #[inline]
    fn deref_mut(&mut self) -> &mut [u8] {
        match self.mmap.as_mut() {
            Some(mm) => mm,
            None => &mut [],
        }
    }
}

impl<'a> AsRef<[u8]> for MutMapping<'a> {
    #[inline]
    fn as_ref(&self) -> &[u8] {
        self.deref()
    }
}

impl<'a> AsMut<[u8]> for MutMapping<'a> {
    #[inline]
    fn as_mut(&mut self) -> &mut [u8] {
        self.deref_mut()
    }
}

/// A thin, safe wrapper for memory-mapped files.
///
/// This wrapper ensures memory safety by only providing one mutable reference at a time and by
/// locking the file exclusively.
pub struct MappedFile {
    file: File,
    size: u64,
}

impl Drop for MappedFile {
    fn drop(&mut self) {
        self.file.unlock().unwrap();
    }
}

impl MappedFile {
    #[inline]
    pub fn open<P: AsRef<Path>>(path: P) -> io::Result<Self> {
        let file = OpenOptions::new().read(true).write(true).open(path)?;
        Self::new(file)
    }

    #[inline]
    pub fn create<P: AsRef<Path>>(path: P) -> io::Result<Self> {
        let file = OpenOptions::new()
            .create(true)
            .read(true)
            .write(true)
            .open(path)?;
        Self::new(file)
    }

    #[inline]
    pub fn new(mut file: File) -> io::Result<Self> {
        // lock file & get size
        file.try_lock_exclusive()?;
        let size = file.seek(SeekFrom::End(0))?;

        Ok(Self {
            file: file,
            size: size,
        })
    }

    /// Returns mapped memory for the file.
    #[inline]
    pub fn map(&self, offset: u64, size: usize) -> io::Result<Mapping> {
        let mmap = if size == 0 {
            None
        } else {
            Some(unsafe {
                memmap::MmapOptions::new()
                    .offset(offset)
                    .len(size)
                    .map(&self.file)?
            })
        };

        Ok(Mapping {
            mmap: mmap,
            _lt: PhantomData,
        })
    }

    /// Returns mutably-mapped memory for the file.
    #[inline]
    pub fn map_mut<'a>(&'a mut self, offset: u64, size: usize) -> io::Result<MutMapping<'a>> {
        let mmap = if size == 0 {
            None
        } else {
            Some(unsafe {
                memmap::MmapOptions::new()
                    .offset(offset)
                    .len(size)
                    .map_mut(&self.file)?
            })
        };

        Ok(MutMapping {
            mmap: mmap,
            _lt: PhantomData,
        })
    }

    /// Resizes the file. When grown, it is guaranteed that all mapped memory regions on this file
    /// stay valid. When the file gets shrunk, the behaviour of regions behind the new end of the
    /// file is undefined.
    #[inline]
    pub fn resize(&mut self, size: u64) -> io::Result<()> {
        self.file.set_len(size)?;
        self.size = size;
        Ok(())
    }

    /// Returns the current size of the file.
    #[inline]
    pub fn size(&self) -> u64 {
        self.size
    }

    /// See [`File::sync_data`]
    #[inline]
    pub fn sync_data(&mut self) -> io::Result<()> {
        self.file.sync_data()
    }

    /// See [`File::sync_all`]
    #[inline]
    pub fn sync_all(&mut self) -> io::Result<()> {
        self.file.sync_all()
    }

    /// Converts this instance into a mutable mapping.
    /// If you want to convert it back, call [`IntoMutMapping::unmap`].
    #[inline]
    pub fn into_mut_mapping(
        self,
        offset: u64,
        size: usize,
    ) -> Result<IntoMutMapping, (io::Error, Self)> {
        let mmap = if size == 0 {
            None
        } else {
            let mmap = unsafe {
                memmap::MmapOptions::new()
                    .offset(offset)
                    .len(size)
                    .map_mut(&self.file)
            };

            Some(match mmap {
                Ok(mm) => mm,
                Err(e) => return Err((e, self)),
            })
        };

        Ok(IntoMutMapping {
            mmap: mmap,
            file: Some(self),
        })
    }
}


/// Mutably mapped file obtained by [`MappedFile::into_mut_mapping()`].
pub struct IntoMutMapping {
    mmap: Option<MmapMut>,
    file: Option<MappedFile>,
}


impl IntoMutMapping {
    /// Unmaps the file from memory and returns back the [`MappedFile`] instance it originated
    /// from.
    #[inline]
    pub fn unmap(mut self) -> MappedFile {
        self.file.take().unwrap()
    }

    /// Flushes unwritten changes to disc.
    #[inline]
    pub fn flush(&self) -> io::Result<()> {
        self.mmap
            .as_ref()
            .map(|mm| mm.flush())
            .or(Some(Ok(())))
            .unwrap()
    }

    /// Flushes unwritten changes in the specific range to disc.
    #[inline]
    pub fn flush_range(&self, offset: usize, len: usize) -> io::Result<()> {
        self.mmap
            .as_ref()
            .map(|mm| mm.flush_range(offset, len))
            .or(Some(Ok(())))
            .unwrap()
    }

    /// Returns the current size of the file.
    pub fn size(&self) -> u64 {
        self.file.as_ref().unwrap().size()
    }
}

impl<'a> Deref for IntoMutMapping {
    type Target = [u8];

    #[inline]
    fn deref(&self) -> &Self::Target {
        match self.mmap.as_ref() {
            Some(mm) => mm,
            None => &[],
        }
    }
}

impl<'a> DerefMut for IntoMutMapping {
    #[inline]
    fn deref_mut(&mut self) -> &mut [u8] {
        match self.mmap.as_mut() {
            Some(mm) => mm,
            None => &mut [],
        }
    }
}

impl<'a> AsRef<[u8]> for IntoMutMapping {
    #[inline]
    fn as_ref(&self) -> &[u8] {
        self.deref()
    }
}

impl<'a> AsMut<[u8]> for IntoMutMapping {
    #[inline]
    fn as_mut(&mut self) -> &mut [u8] {
        self.deref_mut()
    }
}


#[cfg(test)]
mod tests {
    // TODO: change test file names, maybe something temporary
    use super::MappedFile;
    use std::fs::remove_file;
    use std::path::PathBuf;

    fn temp_file_name() -> PathBuf {
        use rand::Rng;

        let mut p = std::env::temp_dir();
        p.push(
            String::from("cargo-test.mmap-safe.") + &rand::thread_rng()
                .sample_iter(&rand::distributions::Alphanumeric)
                .take(12)
                .collect::<String>(),
        );
        p
    }

    #[test]
    fn only_one_instance() {
        let file = temp_file_name();
        let mut mf = MappedFile::create(&file).unwrap();
        mf.resize(10).unwrap();

        assert!(MappedFile::open(&file).is_err());
        assert!(MappedFile::create(&file).is_err());

        remove_file(file).unwrap();
    }

    #[test]
    fn write_works() {
        let file = temp_file_name();
        let mut mf = MappedFile::create(&file).unwrap();
        mf.resize(10).unwrap();

        {
            let mut mapping = mf.map_mut(0, 10).unwrap();
            mapping[0] = 0x42;
        }

        let mapping = mf.map(0, 10).unwrap();
        assert_eq!(mapping[0], 0x42);

        remove_file(file).unwrap();
    }

    #[test]
    fn multiple_immutable_mappings() {
        let file = temp_file_name();
        let mut mf = MappedFile::create(&file).unwrap();
        mf.resize(10).unwrap();

        {
            let mut mm = mf.map_mut(0, 10).unwrap();
            mm[0] = 0x42;
        }

        let mm1 = mf.map(0, 10).unwrap();
        let mm2 = mf.map(0, 10).unwrap();
        assert_eq!(mm1[0], 0x42);
        assert_eq!(mm2[0], 0x42);

        remove_file(file).unwrap();
    }

    #[test]
    fn empty_files() {
        let file = temp_file_name();
        let mut mf = MappedFile::create(&file).unwrap();

        {
            let mm = mf.map_mut(0, 0).unwrap();
            assert_eq!(mm.len(), 0);
        }

        {
            let mm = mf.map(0, 0).unwrap();
            assert_eq!(mm.len(), 0);
        }

        mf.into_mut_mapping(0, 0).map_err(|(e, _)| e).unwrap();

        remove_file(file).unwrap();
    }
}