]> git.proxmox.com Git - proxmox-backup.git/blame - src/tools/fs.rs
pxar: introduce fs_feature_flags obtained from filesystem magic in Encoder
[proxmox-backup.git] / src / tools / fs.rs
CommitLineData
b4d5787d
WB
1//! File system helper utilities.
2
b12505da
WB
3use std::borrow::{Borrow, BorrowMut};
4use std::ops::{Deref, DerefMut};
1db41615 5use std::os::unix::io::{AsRawFd, RawFd};
b4d5787d 6
23fba9d7 7use failure::*;
b4d5787d
WB
8use nix::dir;
9use nix::dir::Dir;
53e1e7ca 10use regex::Regex;
b4d5787d
WB
11
12use crate::tools::borrow::Tied;
13
b12505da
WB
14/// This wraps nix::dir::Entry with the parent directory's file descriptor.
15pub struct ReadDirEntry {
16 entry: dir::Entry,
17 parent_fd: RawFd,
18}
19
20impl Into<dir::Entry> for ReadDirEntry {
21 fn into(self) -> dir::Entry {
22 self.entry
23 }
24}
25
26impl Deref for ReadDirEntry {
27 type Target = dir::Entry;
28
29 fn deref(&self) -> &Self::Target {
30 &self.entry
31 }
32}
33
34impl DerefMut for ReadDirEntry {
35 fn deref_mut(&mut self) -> &mut Self::Target {
36 &mut self.entry
37 }
38}
39
40impl AsRef<dir::Entry> for ReadDirEntry {
41 fn as_ref(&self) -> &dir::Entry {
42 &self.entry
43 }
44}
45
46impl AsMut<dir::Entry> for ReadDirEntry {
47 fn as_mut(&mut self) -> &mut dir::Entry {
48 &mut self.entry
49 }
50}
51
52impl Borrow<dir::Entry> for ReadDirEntry {
53 fn borrow(&self) -> &dir::Entry {
54 &self.entry
55 }
56}
57
58impl BorrowMut<dir::Entry> for ReadDirEntry {
59 fn borrow_mut(&mut self) -> &mut dir::Entry {
60 &mut self.entry
61 }
62}
63
64impl ReadDirEntry {
65 #[inline]
66 pub fn parent_fd(&self) -> RawFd {
67 self.parent_fd
68 }
32286b03
WB
69
70 pub unsafe fn file_name_utf8_unchecked(&self) -> &str {
71 std::str::from_utf8_unchecked(self.file_name().to_bytes())
72 }
b12505da
WB
73}
74
b4d5787d
WB
75// Since Tied<T, U> implements Deref to U, a Tied<Dir, Iterator> already implements Iterator.
76// This is simply a wrapper with a shorter type name mapping nix::Error to failure::Error.
77/// Wrapper over a pair of `nix::dir::Dir` and `nix::dir::Iter`, returned by `read_subdir()`.
78pub struct ReadDir {
2cc90617 79 iter: Tied<Dir, Iterator<Item = nix::Result<dir::Entry>> + Send>,
1db41615 80 dir_fd: RawFd,
b4d5787d
WB
81}
82
83impl Iterator for ReadDir {
1db41615 84 type Item = Result<ReadDirEntry, Error>;
b4d5787d
WB
85
86 fn next(&mut self) -> Option<Self::Item> {
1db41615
WB
87 self.iter.next().map(|res| {
88 res.map(|entry| ReadDirEntry { entry, parent_fd: self.dir_fd })
fc7f0352 89 .map_err(Error::from)
1db41615 90 })
b4d5787d
WB
91 }
92}
93
94/// Create an iterator over sub directory entries.
95/// This uses `openat` on `dirfd`, so `path` can be relative to that or an absolute path.
96pub fn read_subdir<P: ?Sized + nix::NixPath>(dirfd: RawFd, path: &P) -> Result<ReadDir, Error> {
97 use nix::fcntl::OFlag;
98 use nix::sys::stat::Mode;
99
100 let dir = Dir::openat(dirfd, path, OFlag::O_RDONLY, Mode::empty())?;
1db41615 101 let fd = dir.as_raw_fd();
b4d5787d 102 let iter = Tied::new(dir, |dir| {
2cc90617 103 Box::new(unsafe { (*dir).iter() }) as Box<Iterator<Item = nix::Result<dir::Entry>> + Send>
b4d5787d 104 });
1db41615 105 Ok(ReadDir { iter, dir_fd: fd })
b4d5787d 106}
a25f863a
WB
107
108/// Scan through a directory with a regular expression. This is simply a shortcut filtering the
109/// results of `read_subdir`. Non-UTF8 comaptible file names are silently ignored.
110pub fn scan_subdir<'a, P: ?Sized + nix::NixPath>(
111 dirfd: RawFd,
112 path: &P,
113 regex: &'a regex::Regex,
806d7a6a
WB
114) -> Result<impl Iterator<Item = Result<ReadDirEntry, Error>> + 'a, Error> {
115 Ok(read_subdir(dirfd, path)?.filter_file_name_regex(regex))
a25f863a 116}
23fba9d7
WB
117
118/// Helper trait to provide a combinators for directory entry iterators.
119pub trait FileIterOps<T, E>
120where
121 Self: Sized + Iterator<Item = Result<T, E>>,
122 T: Borrow<dir::Entry>,
123 E: Into<Error> + Send + Sync,
124{
125 /// Filter by file type. This is more convenient than using the `filter` method alone as this
126 /// also includes error handling and handling of files without a type (via an error).
127 fn filter_file_type(self, ty: dir::Type) -> FileTypeFilter<Self, T, E> {
128 FileTypeFilter { inner: self, ty }
129 }
53e1e7ca
WB
130
131 /// Filter by file name. Note that file names which aren't valid utf-8 will be treated as if
132 /// they do not match the pattern.
133 fn filter_file_name_regex<'a>(self, regex: &'a Regex) -> FileNameRegexFilter<'a, Self, T, E> {
134 FileNameRegexFilter { inner: self, regex }
135 }
23fba9d7
WB
136}
137
138impl<I, T, E> FileIterOps<T, E> for I
139where
140 I: Iterator<Item = Result<T, E>>,
141 T: Borrow<dir::Entry>,
142 E: Into<Error> + Send + Sync,
143{
144}
145
146/// This filters files from its inner iterator by a file type. Files with no type produce an error.
147pub struct FileTypeFilter<I, T, E>
148where
149 I: Iterator<Item = Result<T, E>>,
150 T: Borrow<dir::Entry>,
151 E: Into<Error> + Send + Sync,
152{
153 inner: I,
154 ty: nix::dir::Type,
155}
156
157impl<I, T, E> Iterator for FileTypeFilter<I, T, E>
158where
159 I: Iterator<Item = Result<T, E>>,
160 T: Borrow<dir::Entry>,
161 E: Into<Error> + Send + Sync,
162{
163 type Item = Result<T, Error>;
164
165 fn next(&mut self) -> Option<Self::Item> {
166 loop {
167 let item = self.inner.next()?.map_err(|e| e.into());
168 match item {
169 Ok(ref entry) => match entry.borrow().file_type() {
170 Some(ty) => {
171 if ty == self.ty {
172 return Some(item);
173 } else {
174 continue;
175 }
176 }
177 None => return Some(Err(format_err!("unable to detect file type"))),
178 },
179 Err(_) => return Some(item),
180 }
181 }
182 }
183}
184
53e1e7ca
WB
185/// This filters files by name via a Regex. Files whose file name aren't valid utf-8 are skipped
186/// silently.
187pub struct FileNameRegexFilter<'a, I, T, E>
188where
189 I: Iterator<Item = Result<T, E>>,
190 T: Borrow<dir::Entry>,
191{
192 inner: I,
193 regex: &'a Regex,
194}
195
196impl<I, T, E> Iterator for FileNameRegexFilter<'_, I, T, E>
197where
198 I: Iterator<Item = Result<T, E>>,
199 T: Borrow<dir::Entry>,
200{
201 type Item = Result<T, E>;
202
203 fn next(&mut self) -> Option<Self::Item> {
204 loop {
205 let item = self.inner.next()?;
206 match item {
207 Ok(ref entry) => {
208 if let Ok(name) = entry.borrow().file_name().to_str() {
209 if self.regex.is_match(name) {
210 return Some(item);
211 }
212 }
213 // file did not match regex or isn't valid utf-8
214 continue;
215 },
216 Err(_) => return Some(item),
217 }
218 }
219 }
220}
042babe4
CE
221
222use nix::{convert_ioctl_res, request_code_read, request_code_write, ioc};
223
224// /usr/include/linux/fs.h: #define FS_IOC_GETFLAGS _IOR('f', 1, long)
225// read Linux file system attributes (see man chattr)
226nix::ioctl_read!(read_attr_fd, b'f', 1, usize);
227
228// /usr/include/linux/msdos_fs.h: #define FAT_IOCTL_GET_ATTRIBUTES _IOR('r', 0x10, __u32)
229// read FAT file system attributes
230nix::ioctl_read!(read_fat_attr_fd, b'r', 0x10, u32);
231
232// From /usr/include/linux/fs.h
233// #define FS_IOC_FSGETXATTR _IOR('X', 31, struct fsxattr)
234// #define FS_IOC_FSSETXATTR _IOW('X', 32, struct fsxattr)
235nix::ioctl_read!(fs_ioc_fsgetxattr, b'X', 31, FSXAttr);
236nix::ioctl_write_ptr!(fs_ioc_fssetxattr, b'X', 32, FSXAttr);
237
238#[repr(C)]
239#[derive(Debug)]
240pub struct FSXAttr {
241 pub fsx_xflags: u32,
242 pub fsx_extsize: u32,
243 pub fsx_nextents: u32,
244 pub fsx_projid: u32,
245 pub fsx_cowextsize: u32,
246 pub fsx_pad: [u8; 8],
247}
248
249impl Default for FSXAttr {
250 fn default() -> Self {
251 FSXAttr {
252 fsx_xflags: 0u32,
253 fsx_extsize: 0u32,
254 fsx_nextents: 0u32,
255 fsx_projid: 0u32,
256 fsx_cowextsize: 0u32,
257 fsx_pad: [0u8; 8],
258 }
259 }
260}