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
// Copyright (c) 2023 Nick Piaddo
// SPDX-License-Identifier: Apache-2.0 OR MIT
//! Module for working with file system description files.
//!
//! ## Description
//!
//! On Linux, descriptive information about the devices the system can mount, or devices already
//! mounted are kept in files, respectively `/etc/fstab` and `/proc/mounts` (or the per-process
//! `/proc/<pid>/mountinfo` file).
//!
//! This modules provides tools to load, search, edit, create, or compare file system description
//! files.
//!
//! ## Examples
//!
//! ### Import `/etc/fstab` to RAM
//!
//! ```
//! use rsmount::tables::FsTab;
//!
//! fn main() -> rsmount::Result<()> {
//! let mut fstab = FsTab::new()?;
//!
//! // Configure the file importer.
//! fstab.import_without_comments();
//!
//! // Import `/etc/fstab` without comments lines in the file.
//! fstab.import_etc_fstab()?;
//!
//! Ok(())
//! }
//! ```
//!
//! ### Manually create an `fstab` file
//!
//! ```
//! # use tempfile::NamedTempFile;
//! use std::str::FromStr;
//! use rsmount::tables::FsTab;
//! use rsmount::entries::FsTabEntry;
//! use rsmount::device::BlockDevice;
//! use rsmount::device::Pseudo;
//! use rsmount::device::Source;
//! use rsmount::device::Tag;
//! use rsmount::fs::FileSystem;
//!
//! fn main() -> rsmount::Result<()> {
//! let mut fstab = FsTab::new()?;
//!
//! fstab.set_intro_comments("# /etc/fstab\n")?;
//! fstab.append_to_intro_comments("# Example from scratch\n")?;
//!
//! let uuid = Tag::from_str("UUID=dd476616-1ce4-415e-9dbd-8c2fa8f42f0f")?;
//! let entry1 = FsTabEntry::builder()
//! .source(uuid)
//! .target("/")
//! .file_system_type(FileSystem::Ext4)
//! // Comma-separated list of mount options.
//! .mount_options("rw,relatime")
//! // Interval, in days, between file system backups by the dump command on ext2/3/4
//! // file systems.
//! .backup_frequency(0)
//! // Order in which file systems are checked by the `fsck` command.
//! .fsck_checking_order(1)
//! .build()?;
//!
//! let block_device = BlockDevice::from_str("/dev/usbdisk")?;
//! let entry2 = FsTabEntry::builder()
//! .source(block_device)
//! .target("/media/usb")
//! .file_system_type(FileSystem::VFAT)
//! .mount_options("noauto")
//! .backup_frequency(0)
//! .fsck_checking_order(0)
//! .build()?;
//!
//! let entry3 = FsTabEntry::builder()
//! .source(Pseudo::None)
//! .target("/tmp")
//! .file_system_type(FileSystem::Tmpfs)
//! .mount_options("nosuid,nodev")
//! .backup_frequency(0)
//! .fsck_checking_order(0)
//! .build()?;
//!
//! fstab.push(entry1);
//! fstab.push(entry2);
//! fstab.push(entry3);
//!
//! # let temp_file = NamedTempFile::new().unwrap();
//! # let file_path = temp_file.path();
//! fstab.write_file(file_path)?;
//!
//! // Example output
//! //
//! // # /etc/fstab
//! // # Example from scratch
//! //
//! // UUID=dd476616-1ce4-415e-9dbd-8c2fa8f42f0f / ext4 rw,relatime 0 1
//! // /dev/usbdisk /media/usb vfat noauto 0 0
//! // none /tmp tmpfs nosuid,nodev 0 0
//!
//! Ok(())
//! }
//! ```
//!
//! ## Print `/proc/self/mountinfo` to the terminal
//!
//! ```
//! use rsmount::tables::MountInfo;
//!
//! fn main() -> rsmount::Result<()> {
//! let mut mountinfo = MountInfo::new()?;
//! // Import `/proc/self/mountinfo`.
//! mountinfo.import_mountinfo()?;
//!
//! println!("{}", mountinfo);
//!
//! // Example output
//! //
//! // 21 26 0:20 / /sys rw,nosuid,nodev,noexec,relatime - sysfs sysfs rw
//! // 23 26 0:21 / /proc rw,nosuid,nodev,noexec,relatime - proc proc rw
//! // 25 22 0:23 / /dev/shm rw,nosuid,nodev,noexec,relatime - tmpfs shm rw,inode64
//!
//! Ok(())
//! }
//! ```
//!
//! ## Compare `/etc/fstab` to another `fstab` file
//!
//!
//! ```
//! # use tempfile::NamedTempFile;
//! # use std::str::FromStr;
//! # use rsmount::entries::FsTabEntry;
//! # use rsmount::device::BlockDevice;
//! # use rsmount::device::Pseudo;
//! # use rsmount::device::Source;
//! # use rsmount::device::Tag;
//! # use rsmount::fs::FileSystem;
//! use rsmount::tables::FsTab;
//! use rsmount::tables::FsTabDiff;
//!
//! fn main() -> rsmount::Result<()> {
//! # let mut fstab = FsTab::new()?;
//! #
//! # fstab.set_intro_comments("# /etc/fstab\n")?;
//! # fstab.append_to_intro_comments("# Example from scratch\n")?;
//! #
//! # let uuid = Tag::from_str("UUID=dd476616-1ce4-415e-9dbd-8c2fa8f42f0f")?;
//! # let entry1 = FsTabEntry::builder()
//! # .source(uuid)
//! # .target("/")
//! # .file_system_type(FileSystem::Ext4)
//! # // Comma-separated list of mount options.
//! # .mount_options("rw,relatime")
//! # // Interval, in days, between file system backups by the dump command on ext2/3/4
//! # // file systems.
//! # .backup_frequency(0)
//! # // Order in which file systems are checked by the `fsck` command.
//! # .fsck_checking_order(1)
//! # .build()?;
//! #
//! # let block_device = BlockDevice::from_str("/dev/usbdisk")?;
//! # let entry2 = FsTabEntry::builder()
//! # .source(block_device)
//! # .target("/media/usb")
//! # .file_system_type(FileSystem::VFAT)
//! # .mount_options("noauto")
//! # .backup_frequency(0)
//! # .fsck_checking_order(0)
//! # .build()?;
//! #
//! # let entry3 = FsTabEntry::builder()
//! # .source(Pseudo::None)
//! # .target("/tmp")
//! # .file_system_type(FileSystem::Tmpfs)
//! # .mount_options("nosuid,nodev")
//! # .backup_frequency(0)
//! # .fsck_checking_order(0)
//! # .build()?;
//! #
//! # fstab.push(entry1);
//! # fstab.push(entry2);
//! # fstab.push(entry3);
//! #
//! # let temp_file = NamedTempFile::new().unwrap();
//! # let file_path = temp_file.path();
//! # fstab.write_file(file_path)?;
//! #
//! // Import `/etc/fstab`.
//! let mut etc_fstab = FsTab::new()?;
//! etc_fstab.import_etc_fstab()?;
//!
//! // Import a custom fstab file.
//! let custom_fstab = FsTab::new_from_file(file_path)?;
//!
//! // Compare them
//! let comparator = FsTabDiff::new(&etc_fstab, &custom_fstab)?;
//! let nb_differences = comparator.diff()?;
//!
//! assert!(nb_differences > 0);
//!
//! // Iterate over the differing entries.
//! for diff in comparator.iter() {
//! assert_ne!(diff.source(), diff.other())
//! }
//!
//! Ok(())
//! }
//! ```
// From dependency library
// From standard library
// From this library
pub use Comparison;
pub use FsTabDiff;
pub use FsTab;
pub use GcItem;
pub use MountInfoDiff;
pub use MountInfo;
pub use MountOption;
pub use ParserFlow;
pub use SwapsDiff;
pub use Swaps;
pub use TableMonitor;
pub use UTabDiff;
pub use UtabManager;
pub use UTab;