1 //! File system helper utilities.
3 use std
::borrow
::{Borrow, BorrowMut}
;
4 use std
::ops
::{Deref, DerefMut}
;
5 use std
::os
::unix
::io
::{AsRawFd, RawFd}
;
12 use crate::tools
::borrow
::Tied
;
14 /// This wraps nix::dir::Entry with the parent directory's file descriptor.
15 pub struct ReadDirEntry
{
20 impl Into
<dir
::Entry
> for ReadDirEntry
{
21 fn into(self) -> dir
::Entry
{
26 impl Deref
for ReadDirEntry
{
27 type Target
= dir
::Entry
;
29 fn deref(&self) -> &Self::Target
{
34 impl DerefMut
for ReadDirEntry
{
35 fn deref_mut(&mut self) -> &mut Self::Target
{
40 impl AsRef
<dir
::Entry
> for ReadDirEntry
{
41 fn as_ref(&self) -> &dir
::Entry
{
46 impl AsMut
<dir
::Entry
> for ReadDirEntry
{
47 fn as_mut(&mut self) -> &mut dir
::Entry
{
52 impl Borrow
<dir
::Entry
> for ReadDirEntry
{
53 fn borrow(&self) -> &dir
::Entry
{
58 impl BorrowMut
<dir
::Entry
> for ReadDirEntry
{
59 fn borrow_mut(&mut self) -> &mut dir
::Entry
{
66 pub fn parent_fd(&self) -> RawFd
{
70 pub unsafe fn file_name_utf8_unchecked(&self) -> &str {
71 std
::str::from_utf8_unchecked(self.file_name().to_bytes())
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()`.
79 iter
: Tied
<Dir
, dyn Iterator
<Item
= nix
::Result
<dir
::Entry
>> + Send
>,
83 impl Iterator
for ReadDir
{
84 type Item
= Result
<ReadDirEntry
, Error
>;
86 fn next(&mut self) -> Option
<Self::Item
> {
87 self.iter
.next().map(|res
| {
88 res
.map(|entry
| ReadDirEntry { entry, parent_fd: self.dir_fd }
)
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.
96 pub fn read_subdir
<P
: ?Sized
+ nix
::NixPath
>(dirfd
: RawFd
, path
: &P
) -> nix
::Result
<ReadDir
> {
97 use nix
::fcntl
::OFlag
;
98 use nix
::sys
::stat
::Mode
;
100 let dir
= Dir
::openat(dirfd
, path
, OFlag
::O_RDONLY
, Mode
::empty())?
;
101 let fd
= dir
.as_raw_fd();
102 let iter
= Tied
::new(dir
, |dir
| {
103 Box
::new(unsafe { (*dir).iter() }
)
104 as Box
<dyn Iterator
<Item
= nix
::Result
<dir
::Entry
>> + Send
>
106 Ok(ReadDir { iter, dir_fd: fd }
)
109 /// Scan through a directory with a regular expression. This is simply a shortcut filtering the
110 /// results of `read_subdir`. Non-UTF8 comaptible file names are silently ignored.
111 pub fn scan_subdir
<'a
, P
: ?Sized
+ nix
::NixPath
>(
114 regex
: &'a regex
::Regex
,
115 ) -> Result
<impl Iterator
<Item
= Result
<ReadDirEntry
, Error
>> + 'a
, Error
> {
116 Ok(read_subdir(dirfd
, path
)?
.filter_file_name_regex(regex
))
119 /// Helper trait to provide a combinators for directory entry iterators.
120 pub trait FileIterOps
<T
, E
>
122 Self: Sized
+ Iterator
<Item
= Result
<T
, E
>>,
123 T
: Borrow
<dir
::Entry
>,
124 E
: Into
<Error
> + Send
+ Sync
,
126 /// Filter by file type. This is more convenient than using the `filter` method alone as this
127 /// also includes error handling and handling of files without a type (via an error).
128 fn filter_file_type(self, ty
: dir
::Type
) -> FileTypeFilter
<Self, T
, E
> {
129 FileTypeFilter { inner: self, ty }
132 /// Filter by file name. Note that file names which aren't valid utf-8 will be treated as if
133 /// they do not match the pattern.
134 fn filter_file_name_regex(self, regex
: &Regex
) -> FileNameRegexFilter
<Self, T
, E
> {
135 FileNameRegexFilter { inner: self, regex }
139 impl<I
, T
, E
> FileIterOps
<T
, E
> for I
141 I
: Iterator
<Item
= Result
<T
, E
>>,
142 T
: Borrow
<dir
::Entry
>,
143 E
: Into
<Error
> + Send
+ Sync
,
147 /// This filters files from its inner iterator by a file type. Files with no type produce an error.
148 pub struct FileTypeFilter
<I
, T
, E
>
150 I
: Iterator
<Item
= Result
<T
, E
>>,
151 T
: Borrow
<dir
::Entry
>,
152 E
: Into
<Error
> + Send
+ Sync
,
158 impl<I
, T
, E
> Iterator
for FileTypeFilter
<I
, T
, E
>
160 I
: Iterator
<Item
= Result
<T
, E
>>,
161 T
: Borrow
<dir
::Entry
>,
162 E
: Into
<Error
> + Send
+ Sync
,
164 type Item
= Result
<T
, Error
>;
166 fn next(&mut self) -> Option
<Self::Item
> {
168 let item
= self.inner
.next()?
.map_err(|e
| e
.into());
170 Ok(ref entry
) => match entry
.borrow().file_type() {
178 None
=> return Some(Err(format_err
!("unable to detect file type"))),
180 Err(_
) => return Some(item
),
186 /// This filters files by name via a Regex. Files whose file name aren't valid utf-8 are skipped
188 pub struct FileNameRegexFilter
<'a
, I
, T
, E
>
190 I
: Iterator
<Item
= Result
<T
, E
>>,
191 T
: Borrow
<dir
::Entry
>,
197 impl<I
, T
, E
> Iterator
for FileNameRegexFilter
<'_
, I
, T
, E
>
199 I
: Iterator
<Item
= Result
<T
, E
>>,
200 T
: Borrow
<dir
::Entry
>,
202 type Item
= Result
<T
, E
>;
204 fn next(&mut self) -> Option
<Self::Item
> {
206 let item
= self.inner
.next()?
;
209 if let Ok(name
) = entry
.borrow().file_name().to_str() {
210 if self.regex
.is_match(name
) {
214 // file did not match regex or isn't valid utf-8
217 Err(_
) => return Some(item
),
223 // /usr/include/linux/fs.h: #define FS_IOC_GETFLAGS _IOR('f', 1, long)
224 // read Linux file system attributes (see man chattr)
225 nix
::ioctl_read
!(read_attr_fd
, b'f'
, 1, usize);
227 // /usr/include/linux/msdos_fs.h: #define FAT_IOCTL_GET_ATTRIBUTES _IOR('r', 0x10, __u32)
228 // read FAT file system attributes
229 nix
::ioctl_read
!(read_fat_attr_fd
, b'r'
, 0x10, u32);
231 // From /usr/include/linux/fs.h
232 // #define FS_IOC_FSGETXATTR _IOR('X', 31, struct fsxattr)
233 // #define FS_IOC_FSSETXATTR _IOW('X', 32, struct fsxattr)
234 nix
::ioctl_read
!(fs_ioc_fsgetxattr
, b'X'
, 31, FSXAttr
);
235 nix
::ioctl_write_ptr
!(fs_ioc_fssetxattr
, b'X'
, 32, FSXAttr
);
241 pub fsx_extsize
: u32,
242 pub fsx_nextents
: u32,
244 pub fsx_cowextsize
: u32,
245 pub fsx_pad
: [u8; 8],
248 impl Default
for FSXAttr
{
249 fn default() -> Self {
255 fsx_cowextsize
: 0u32,