fmmap 0.3.2

A flexible and convenient high-level mmap for zero-copy file I/O.
Documentation
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
use std::os::windows::fs::MetadataExt;
use std::time::{SystemTime, UNIX_EPOCH};
use crate::error::{Error, ErrorKind, Result};
use crate::MetaData;
use crate::metadata::{DiskMetaData, EmptyMetaData, MemoryMetaData};

/// Utility methods to MetaData
#[enum_dispatch]
pub trait MetaDataExt {
    /// Returns the last access time of this metadata.
    ///
    /// The returned value corresponds to the atime field of stat on Unix platforms and the ftLastAccessTime field on Windows platforms.
    ///
    /// Note that not all platforms will keep this field update in a file’s metadata,
    /// for example Windows has an option to disable updating
    /// this time when files are accessed and Linux similarly has noatime.
    fn accessed(&self) -> std::result::Result<SystemTime, Error>;

    /// Returns the creation time listed in this metadata.
    ///
    /// The returned value corresponds to the `btime` field of `statx` on Linux kernel starting from to 4.11,
    /// the `birthtime` field of stat on other Unix platforms,
    /// and the `ftCreationTime` field on Windows platforms.
    fn created(&self) -> std::result::Result<SystemTime, Error>;

    /// Returns true if this metadata is for a regular file.
    ///
    /// It will be false for symlink metadata obtained from [`symlink_metadata`].
    ///
    /// When the goal is simply to read from (or write to) the source,
    /// the most reliable way to test the source can be read (or written to) is to open it.
    /// Only using is_file can break workflows like diff <( prog_a ) on a Unix-like system for example.
    ///
    /// [`symlink_metadata`]: https://doc.rust-lang.org/std/fs/fn.symlink_metadata.html
    fn is_file(&self) -> bool;

    /// Returns `true` if this metadata is for a symbolic link.
    #[cfg(feature = "nightly")]
    #[cfg_attr(docsrs, doc(cfg(feature = "nightly")))]
    fn is_symlink(&self) -> bool;

    /// Returns the size of the file, in bytes, this metadata is for.
    fn len(&self) -> u64;

    /// Returns the last modification time listed in this metadata.
    ///
    /// The returned value corresponds to the `mtime` field of `stat` on Unix platforms
    /// and the `ftLastWriteTime` field on Windows platforms.
    ///
    /// # Errors
    /// This field might not be available on all platforms, and
    /// will return an `Err` on platforms where it is not available.
    fn modified(&self) -> std::result::Result<SystemTime, Error>;

    /// Returns the value of the `dwFileAttributes` field of this metadata.
    fn file_attributes(&self) -> u32;

    /// Returns the value of the `ftCreationTime` field of this metadata.
    fn creation_time(&self) -> u64;

    /// Returns the value of the `ftLastAccessTime` field of this metadata.
    fn last_access_time(&self) -> u64;

    /// Returns the value of the `ftLastWriteTime` field of this metadata.
    fn last_write_time(&self) -> u64;

    /// Returns the value of the `nFileSize{High,Low}` fields of this metadata.
    fn file_size(&self) -> u64;

    /// Returns the value of the `dwVolumeSerialNumber` field of this metadata.
    #[cfg(feature = "nightly")]
    #[cfg_attr(docsrs, doc(cfg(feature = "nightly")))]
    fn volume_serial_number(&self) -> Option<u32>;

    /// Returns the value of the `nNumberOfLinks` field of this metadata.
    #[cfg(feature = "nightly")]
    #[cfg_attr(docsrs, doc(cfg(feature = "nightly")))]
    fn number_of_links(&self) -> Option<u32>;

    /// Returns the value of the `nFileIndex{Low,High}` fields of this metadata.
    #[cfg(feature = "nightly")]
    #[cfg_attr(docsrs, doc(cfg(feature = "nightly")))]
    fn file_index(&self) -> Option<u64>;
}

#[cfg(windows)]
impl MetaDataExt for MemoryMetaData {
    fn accessed(&self) -> Result<SystemTime> {
        Ok(self.create_at)
    }

    #[inline]
    fn created(&self) -> Result<SystemTime> {
        Ok(self.create_at)
    }

    fn is_file(&self) -> bool {
        false
    }

    #[cfg(feature = "nightly")]
    fn is_symlink(&self) -> bool {
        false
    }

    #[inline]
    fn len(&self) -> u64 {
        self.size
    }

    fn modified(&self) -> Result<SystemTime> {
        Ok(self.create_at)
    }

    fn file_attributes(&self) -> u32 {
        0
    }

    fn creation_time(&self) -> u64 {
        self.create_at.duration_since(UNIX_EPOCH).unwrap().as_secs()
    }

    fn last_access_time(&self) -> u64 {
        self.create_at.duration_since(UNIX_EPOCH).unwrap().as_secs()
    }

    fn last_write_time(&self) -> u64 {
        self.create_at.duration_since(UNIX_EPOCH).unwrap().as_secs()
    }

    fn file_size(&self) -> u64 {
        self.size
    }

    #[cfg(feature = "nightly")]
    #[cfg_attr(docsrs, doc(cfg(feature = "nightly")))]
    fn volume_serial_number(&self) -> Option<u32> {
        None
    }

    #[cfg(feature = "nightly")]
    #[cfg_attr(docsrs, doc(cfg(feature = "nightly")))]
    fn number_of_links(&self) -> Option<u32> {
        None
    }

    #[cfg(feature = "nightly")]
    #[cfg_attr(docsrs, doc(cfg(feature = "nightly")))]
    fn file_index(&self) -> Option<u64> {
        None
    }
}

#[cfg(windows)]
impl MetaDataExt for DiskMetaData {
    fn accessed(&self) -> Result<SystemTime> {
        self.inner.accessed().map_err(|e| Error::new(ErrorKind::IO, e))
    }

    fn created(&self) -> Result<SystemTime> {
        self.inner.created().map_err(|e| Error::new(ErrorKind::IO, e))
    }

    fn is_file(&self) -> bool {
        self.inner.is_file()
    }

    #[cfg(feature = "nightly")]
    #[cfg_attr(docsrs, doc(cfg(feature = "nightly")))]
    fn is_symlink(&self) -> bool {
        self.inner.is_symlink()
    }

    fn len(&self) -> u64 {
        self.inner.len()
    }

    fn modified(&self) -> Result<SystemTime> {
        self.inner.modified().map_err(|e| Error::new(ErrorKind::IO, e))
    }

    fn file_attributes(&self) -> u32 {
        self.inner.file_attributes()
    }

    fn creation_time(&self) -> u64 {
        self.inner.creation_time()
    }

    fn last_access_time(&self) -> u64 {
        self.inner.last_access_time()
    }

    fn last_write_time(&self) -> u64 {
        self.inner.last_write_time()
    }

    fn file_size(&self) -> u64 {
        self.inner.file_size()
    }

    #[cfg(feature = "nightly")]
    #[cfg_attr(docsrs, doc(cfg(feature = "nightly")))]
    fn volume_serial_number(&self) -> Option<u32> {
        self.inner.volume_serial_number()
    }

    #[cfg(feature = "nightly")]
    #[cfg_attr(docsrs, doc(cfg(feature = "nightly")))]
    fn number_of_links(&self) -> Option<u32> {
        self.inner.number_of_links()
    }

    #[cfg(feature = "nightly")]
    #[cfg_attr(docsrs, doc(cfg(feature = "nightly")))]
    fn file_index(&self) -> Option<u64> {
        self.inner.file_index()
    }
}

#[cfg(windows)]
impl MetaDataExt for EmptyMetaData {
    fn accessed(&self) -> Result<SystemTime> {
        Ok(UNIX_EPOCH)
    }

    fn created(&self) -> Result<SystemTime> {
        Ok(UNIX_EPOCH)
    }

    fn is_file(&self) -> bool {
        false
    }

    #[cfg(feature = "nightly")]
    #[cfg_attr(docsrs, doc(cfg(feature = "nightly")))]
    fn is_symlink(&self) -> bool {
        false
    }

    fn len(&self) -> u64 {
        0
    }

    fn modified(&self) -> Result<SystemTime> {
        Ok(UNIX_EPOCH)
    }

    fn file_attributes(&self) -> u32 {
        0
    }

    fn creation_time(&self) -> u64 {
        0
    }

    fn last_access_time(&self) -> u64 {
        0
    }

    fn last_write_time(&self) -> u64 {
        0
    }

    fn file_size(&self) -> u64 {
        0
    }

    #[cfg(feature = "nightly")]
    #[cfg_attr(docsrs, doc(cfg(feature = "nightly")))]
    fn volume_serial_number(&self) -> Option<u32> {
        None
    }

    #[cfg(feature = "nightly")]
    #[cfg_attr(docsrs, doc(cfg(feature = "nightly")))]
    fn number_of_links(&self) -> Option<u32> {
        None
    }

    #[cfg(feature = "nightly")]
    #[cfg_attr(docsrs, doc(cfg(feature = "nightly")))]
    fn file_index(&self) -> Option<u64> {
        None
    }
}

#[cfg(windows)]
impl MetaDataExt for MetaData {
    fn accessed(&self) -> std::result::Result<SystemTime, Error> {
        self.inner.accessed()
    }

    fn created(&self) -> std::result::Result<SystemTime, Error> {
        self.inner.created()
    }

    fn is_file(&self) -> bool {
        self.inner.is_file()
    }

    #[cfg(feature = "nightly")]
    #[cfg_attr(docsrs, doc(cfg(feature = "nightly")))]
    fn is_symlink(&self) -> bool {
        self.inner.is_symlink()
    }

    fn len(&self) -> u64 {
        self.inner.len()
    }

    fn modified(&self) -> std::result::Result<SystemTime, Error> {
        self.inner.modified()
    }

    fn file_attributes(&self) -> u32 {
        self.inner.file_attributes()
    }

    fn creation_time(&self) -> u64 {
        self.inner.creation_time()
    }

    fn last_access_time(&self) -> u64 {
        self.inner.last_access_time()
    }

    fn last_write_time(&self) -> u64 {
        self.inner.last_write_time()
    }

    fn file_size(&self) -> u64 {
        self.inner.file_size()
    }

    #[cfg(feature = "nightly")]
    #[cfg_attr(docsrs, doc(cfg(feature = "nightly")))]
    fn volume_serial_number(&self) -> Option<u32> {
        self.inner.volume_serial_number()
    }

    #[cfg(feature = "nightly")]
    #[cfg_attr(docsrs, doc(cfg(feature = "nightly")))]
    fn number_of_links(&self) -> Option<u32> {
        self.inner.number_of_links()
    }

    #[cfg(feature = "nightly")]
    #[cfg_attr(docsrs, doc(cfg(feature = "nightly")))]
    fn file_index(&self) -> Option<u64> {
        self.inner.file_index()
    }
}

#[cfg(test)]
mod tests {
    use std::time::UNIX_EPOCH;
    use bytes::Bytes;
    use crate::empty::EmptyMmapFile;
    use crate::{MetaDataExt, MmapFileExt, MmapFileMutExt, Options};
    use crate::raw::MemoryMmapFile;
    use crate::tests::get_random_filename;

    #[test]
    fn test_metadata() {
        let mut file = Options::new()
            .max_size("Hello, fmmap!".len() as u64)
            .create_mmap_file_mut(get_random_filename())
            .unwrap();
        file.set_remove_on_drop(true);
        file.write_all("Hello, fmmap!".as_bytes(), 0).unwrap();

        let meta = file.metadata().unwrap();
        meta.accessed().unwrap();
        meta.created().unwrap();
        assert!(meta.is_file());
        #[cfg(feature = "nightly")]
        assert!(!meta.is_symlink());
        assert_eq!(meta.len(), "Hello, fmmap!".len() as u64);
        assert_eq!(meta.file_size(), "Hello, fmmap!".len() as u64);
        meta.file_attributes();
        meta.creation_time();
        meta.last_access_time();
        meta.last_write_time();
        #[cfg(feature = "nightly")]
        assert!(meta.volume_serial_number().is_some());
        #[cfg(feature = "nightly")]
        assert!(meta.number_of_links().is_some());
        #[cfg(feature = "nightly")]
        assert!(meta.file_index().is_some());
    }

    #[test]
    fn test_memory_metadata() {
        let file = MemoryMmapFile::new("test.mem", Bytes::from("Hello, fmmap!"));
        let meta = file.metadata().unwrap();

        assert!(!meta.is_file());
        #[cfg(feature = "nightly")]
        assert!(!meta.is_symlink());
        assert_eq!(meta.len(), "Hello, fmmap!".len() as u64);
        assert_eq!(meta.file_size(), "Hello, fmmap!".len() as u64);
        assert_eq!(meta.file_attributes(), 0);
        assert!(meta.modified().unwrap() == meta.created().unwrap() && meta.created().unwrap() == meta.accessed().unwrap());
        assert!(meta.creation_time() == meta.last_access_time() && meta.last_access_time() == meta.last_write_time());
        #[cfg(feature = "nightly")]
        assert_eq!(meta.volume_serial_number(), None);
        #[cfg(feature = "nightly")]
        assert_eq!(meta.number_of_links(), None);
        #[cfg(feature = "nightly")]
        assert_eq!(meta.file_index(), None);
    }

    #[test]
    fn test_empty_metadata() {
        let file = EmptyMmapFile::default();
        let meta = file.metadata().unwrap();

        assert_eq!(meta.accessed().unwrap(), UNIX_EPOCH);
        assert_eq!(meta.created().unwrap(), UNIX_EPOCH);
        assert!(!meta.is_file());
        #[cfg(feature = "nightly")]
        assert!(!meta.is_symlink());
        assert_eq!(meta.len(), 0);
        assert_eq!(meta.modified().unwrap(), UNIX_EPOCH);
        assert_eq!(meta.file_attributes(), 0);
        assert_eq!(meta.creation_time(), 0);
        assert_eq!(meta.last_access_time(), 0);
        assert_eq!(meta.last_write_time(), 0);
        assert_eq!(meta.file_size(), 0);
        #[cfg(feature = "nightly")]
        assert_eq!(meta.volume_serial_number(), None);
        #[cfg(feature = "nightly")]
        assert_eq!(meta.number_of_links(), None);
        #[cfg(feature = "nightly")]
        assert_eq!(meta.file_index(), None);
    }
}