Skip to content

Commit 54de396

Browse files
committed
Unify inotify public interface across backends
1 parent e8d9949 commit 54de396

File tree

5 files changed

+77
-95
lines changed

5 files changed

+77
-95
lines changed

src/backend/libc/fs/inotify.rs

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
//! inotify support for working with inotifies
22
33
use crate::backend::c;
4-
use crate::backend::conv::{borrowed_fd, c_str, ret, ret_c_int, ret_owned_fd};
5-
use crate::fd::{BorrowedFd, OwnedFd};
6-
use crate::io;
74
use bitflags::bitflags;
85

96
bitflags! {
@@ -79,53 +76,3 @@ bitflags! {
7976
const _ = !0;
8077
}
8178
}
82-
83-
/// `inotify_init1(flags)`—Creates a new inotify object.
84-
///
85-
/// Use the [`CreateFlags::CLOEXEC`] flag to prevent the resulting file
86-
/// descriptor from being implicitly passed across `exec` boundaries.
87-
#[doc(alias = "inotify_init1")]
88-
pub fn inotify_init(flags: CreateFlags) -> io::Result<OwnedFd> {
89-
// SAFETY: `inotify_init1` has no safety preconditions.
90-
unsafe { ret_owned_fd(c::inotify_init1(bitflags_bits!(flags))) }
91-
}
92-
93-
/// `inotify_add_watch(self, path, flags)`—Adds a watch to inotify.
94-
///
95-
/// This registers or updates a watch for the filesystem path `path` and
96-
/// returns a watch descriptor corresponding to this watch.
97-
///
98-
/// Note: Due to the existence of hardlinks, providing two different paths to
99-
/// this method may result in it returning the same watch descriptor. An
100-
/// application should keep track of this externally to avoid logic errors.
101-
pub fn inotify_add_watch<P: crate::path::Arg>(
102-
inot: BorrowedFd<'_>,
103-
path: P,
104-
flags: WatchFlags,
105-
) -> io::Result<i32> {
106-
path.into_with_c_str(|path| {
107-
// SAFETY: The fd and path we are passing is guaranteed valid by the
108-
// type system.
109-
unsafe {
110-
ret_c_int(c::inotify_add_watch(
111-
borrowed_fd(inot),
112-
c_str(path),
113-
flags.bits(),
114-
))
115-
}
116-
})
117-
}
118-
119-
/// `inotify_rm_watch(self, wd)`—Removes a watch from this inotify.
120-
///
121-
/// The watch descriptor provided should have previously been returned by
122-
/// [`inotify_add_watch`] and not previously have been removed.
123-
#[doc(alias = "inotify_rm_watch")]
124-
pub fn inotify_remove_watch(inot: BorrowedFd<'_>, wd: i32) -> io::Result<()> {
125-
// Android's `inotify_rm_watch` takes `u32` despite that
126-
// `inotify_add_watch` expects a `i32`.
127-
#[cfg(target_os = "android")]
128-
let wd = wd as u32;
129-
// SAFETY: The fd is valid and closing an arbitrary wd is valid.
130-
unsafe { ret(c::inotify_rm_watch(borrowed_fd(inot), wd)) }
131-
}

src/backend/libc/fs/syscalls.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2549,3 +2549,35 @@ fn test_sizes() {
25492549
#[cfg(fix_y2038)]
25502550
assert!(core::mem::size_of::<[c::timespec; 2]>() < core::mem::size_of::<Timestamps>());
25512551
}
2552+
2553+
#[inline]
2554+
#[cfg(linux_kernel)]
2555+
pub(crate) fn inotify_init1(flags: super::inotify::CreateFlags) -> io::Result<OwnedFd> {
2556+
unsafe { ret_owned_fd(c::inotify_init1(bitflags_bits!(flags))) }
2557+
}
2558+
2559+
#[inline]
2560+
#[cfg(linux_kernel)]
2561+
pub(crate) fn inotify_add_watch(
2562+
inot: BorrowedFd<'_>,
2563+
path: &CStr,
2564+
flags: super::inotify::WatchFlags,
2565+
) -> io::Result<i32> {
2566+
unsafe {
2567+
ret_c_int(c::inotify_add_watch(
2568+
borrowed_fd(inot),
2569+
c_str(path),
2570+
flags.bits(),
2571+
))
2572+
}
2573+
}
2574+
2575+
#[inline]
2576+
#[cfg(linux_kernel)]
2577+
pub(crate) fn inotify_rm_watch(inot: BorrowedFd<'_>, wd: i32) -> io::Result<()> {
2578+
// Android's `inotify_rm_watch` takes `u32` despite that
2579+
// `inotify_add_watch` expects a `i32`.
2580+
#[cfg(target_os = "android")]
2581+
let wd = wd as u32;
2582+
unsafe { ret(c::inotify_rm_watch(borrowed_fd(inot), wd)) }
2583+
}

src/backend/linux_raw/fs/inotify.rs

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
//! inotify support for working with inotifies
22
33
use crate::backend::c;
4-
use crate::backend::fs::syscalls;
5-
use crate::fd::{BorrowedFd, OwnedFd};
6-
use crate::io;
74
use bitflags::bitflags;
85

96
bitflags! {
@@ -79,40 +76,3 @@ bitflags! {
7976
const _ = !0;
8077
}
8178
}
82-
83-
/// `inotify_init1(flags)`—Creates a new inotify object.
84-
///
85-
/// Use the [`CreateFlags::CLOEXEC`] flag to prevent the resulting file
86-
/// descriptor from being implicitly passed across `exec` boundaries.
87-
#[doc(alias = "inotify_init1")]
88-
#[inline]
89-
pub fn inotify_init(flags: CreateFlags) -> io::Result<OwnedFd> {
90-
syscalls::inotify_init1(flags)
91-
}
92-
93-
/// `inotify_add_watch(self, path, flags)`—Adds a watch to inotify.
94-
///
95-
/// This registers or updates a watch for the filesystem path `path` and
96-
/// returns a watch descriptor corresponding to this watch.
97-
///
98-
/// Note: Due to the existence of hardlinks, providing two different paths to
99-
/// this method may result in it returning the same watch descriptor. An
100-
/// application should keep track of this externally to avoid logic errors.
101-
#[inline]
102-
pub fn inotify_add_watch<P: crate::path::Arg>(
103-
inot: BorrowedFd<'_>,
104-
path: P,
105-
flags: WatchFlags,
106-
) -> io::Result<i32> {
107-
path.into_with_c_str(|path| syscalls::inotify_add_watch(inot, path, flags))
108-
}
109-
110-
/// `inotify_rm_watch(self, wd)`—Removes a watch from this inotify.
111-
///
112-
/// The watch descriptor provided should have previously been returned by
113-
/// [`inotify_add_watch`] and not previously have been removed.
114-
#[doc(alias = "inotify_rm_watch")]
115-
#[inline]
116-
pub fn inotify_remove_watch(inot: BorrowedFd<'_>, wd: i32) -> io::Result<()> {
117-
syscalls::inotify_rm_watch(inot, wd)
118-
}

src/fs/inotify.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//! inotify support for working with inotifies
2+
3+
pub use crate::backend::fs::inotify::{CreateFlags, WatchFlags};
4+
use crate::backend::fs::syscalls;
5+
use crate::fd::{BorrowedFd, OwnedFd};
6+
use crate::io;
7+
8+
/// `inotify_init1(flags)`—Creates a new inotify object.
9+
///
10+
/// Use the [`CreateFlags::CLOEXEC`] flag to prevent the resulting file
11+
/// descriptor from being implicitly passed across `exec` boundaries.
12+
#[doc(alias = "inotify_init1")]
13+
#[inline]
14+
pub fn inotify_init(flags: CreateFlags) -> io::Result<OwnedFd> {
15+
syscalls::inotify_init1(flags)
16+
}
17+
18+
/// `inotify_add_watch(self, path, flags)`—Adds a watch to inotify.
19+
///
20+
/// This registers or updates a watch for the filesystem path `path` and
21+
/// returns a watch descriptor corresponding to this watch.
22+
///
23+
/// Note: Due to the existence of hardlinks, providing two different paths to
24+
/// this method may result in it returning the same watch descriptor. An
25+
/// application should keep track of this externally to avoid logic errors.
26+
#[inline]
27+
pub fn inotify_add_watch<P: crate::path::Arg>(
28+
inot: BorrowedFd<'_>,
29+
path: P,
30+
flags: WatchFlags,
31+
) -> io::Result<i32> {
32+
path.into_with_c_str(|path| syscalls::inotify_add_watch(inot, path, flags))
33+
}
34+
35+
/// `inotify_rm_watch(self, wd)`—Removes a watch from this inotify.
36+
///
37+
/// The watch descriptor provided should have previously been returned by
38+
/// [`inotify_add_watch`] and not previously have been removed.
39+
#[doc(alias = "inotify_rm_watch")]
40+
#[inline]
41+
pub fn inotify_remove_watch(inot: BorrowedFd<'_>, wd: i32) -> io::Result<()> {
42+
syscalls::inotify_rm_watch(inot, wd)
43+
}

src/fs/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ mod getpath;
3333
#[cfg(not(target_os = "wasi"))] // WASI doesn't have get[gpu]id.
3434
mod id;
3535
#[cfg(linux_kernel)]
36+
pub mod inotify;
37+
#[cfg(linux_kernel)]
3638
mod ioctl;
3739
#[cfg(not(any(
3840
target_os = "espidf",
@@ -66,8 +68,6 @@ mod sync;
6668
#[cfg(any(apple, linux_kernel, target_os = "hurd"))]
6769
mod xattr;
6870

69-
#[cfg(linux_kernel)]
70-
pub use crate::backend::fs::inotify;
7171
pub use abs::*;
7272
#[cfg(not(target_os = "redox"))]
7373
pub use at::*;

0 commit comments

Comments
 (0)