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
// Copyright 2019 Intel Corporation. All Rights Reserved.
//
// Copyright 2017 The Chromium OS Authors. All rights reserved.
//
// SPDX-License-Identifier: (Apache-2.0 AND BSD-3-Clause)
//! Enum and function for dealing with an allocated disk space
//! by [`fallocate`](http://man7.org/linux/man-pages/man2/fallocate.2.html).
use AsRawFd;
use crate;
/// Operation to be performed on a given range when calling [`fallocate`]
///
/// [`fallocate`]: fn.fallocate.html
/// A safe wrapper for [`fallocate`](http://man7.org/linux/man-pages/man2/fallocate.2.html).
///
/// Manipulate the file space with specified operation parameters.
///
/// # Arguments
///
/// * `file`: the file to be manipulate.
/// * `mode`: specify the operation to be performed on the given range.
/// * `keep_size`: file size won't be changed even if `offset` + `len` is greater
/// than the file size.
/// * `offset`: the position that manipulates the file from.
/// * `size`: the bytes of the operation range.
///
/// # Examples
///
/// ```
/// extern crate vmm_sys_util;
/// # use std::fs::OpenOptions;
/// # use std::path::PathBuf;
/// use vmm_sys_util::fallocate::{fallocate, FallocateMode};
/// use vmm_sys_util::tempdir::TempDir;
///
/// let tempdir = TempDir::new_with_prefix("/tmp/fallocate_test").unwrap();
/// let mut path = PathBuf::from(tempdir.as_path());
/// path.push("file");
/// let mut f = OpenOptions::new()
/// .read(true)
/// .write(true)
/// .create(true)
/// .open(&path)
/// .unwrap();
/// fallocate(&f, FallocateMode::PunchHole, true, 0, 1).unwrap();
/// ```