Skip to content

Commit 4bd811f

Browse files
authored
Implement the new module structure for shm and inotify (#1127)
* Implement the new module structure for shm and inotify Signed-off-by: Alex Saveau <[email protected]> * Use module prefixes in the docs for shm and inotify Signed-off-by: Alex Saveau <[email protected]> * Fix inotify names for new module structure Signed-off-by: Alex Saveau <[email protected]> --------- Signed-off-by: Alex Saveau <[email protected]>
1 parent c700ad7 commit 4bd811f

File tree

7 files changed

+59
-33
lines changed

7 files changed

+59
-33
lines changed

src/backend/libc/fs/inotify.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ use crate::backend::c;
44
use bitflags::bitflags;
55

66
bitflags! {
7-
/// `IN_*` for use with [`inotify_init`].
7+
/// `IN_*` for use with [`inotify::init`].
88
///
9-
/// [`inotify_init`]: crate::fs::inotify::inotify_init
9+
/// [`inotify::init`]: crate::fs::inotify::init
1010
#[repr(transparent)]
1111
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
1212
pub struct CreateFlags: u32 {
@@ -21,9 +21,9 @@ bitflags! {
2121
}
2222

2323
bitflags! {
24-
/// `IN*` for use with [`inotify_add_watch`].
24+
/// `IN*` for use with [`inotify::add_watch`].
2525
///
26-
/// [`inotify_add_watch`]: crate::fs::inotify::inotify_add_watch
26+
/// [`inotify::add_watch`]: crate::fs::inotify::add_watch
2727
#[repr(transparent)]
2828
#[derive(Default, Copy, Clone, Eq, PartialEq, Hash, Debug)]
2929
pub struct WatchFlags: u32 {
@@ -78,9 +78,9 @@ bitflags! {
7878
}
7979

8080
bitflags! {
81-
/// `IN*` for use with [`InotifyReader`].
81+
/// `IN*` for use with [`inotify::Reader`].
8282
///
83-
/// [`InotifyReader`]: crate::fs::inotify::InotifyReader
83+
/// [`inotify::Reader`]: crate::fs::inotify::Reader
8484
#[repr(transparent)]
8585
#[derive(Default, Copy, Clone, Eq, PartialEq, Hash, Debug)]
8686
pub struct ReadFlags: u32 {

src/backend/libc/shm/types.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ use crate::backend::c;
22
use bitflags::bitflags;
33

44
bitflags! {
5-
/// `O_*` constants for use with [`shm_open`].
5+
/// `O_*` constants for use with [`shm::open`].
66
///
7-
/// [`shm_open`]: crate:shm::shm_open
7+
/// [`shm::open`]: crate:shm::open
88
#[repr(transparent)]
99
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
1010
pub struct ShmOFlags: u32 {

src/backend/linux_raw/fs/inotify.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ use crate::backend::c;
44
use bitflags::bitflags;
55

66
bitflags! {
7-
/// `IN_*` for use with [`inotify_init`].
7+
/// `IN_*` for use with [`inotify::init`].
88
///
9-
/// [`inotify_init`]: crate::fs::inotify::inotify_init
9+
/// [`inotify::init`]: crate::fs::inotify::init
1010
#[repr(transparent)]
1111
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
1212
pub struct CreateFlags: c::c_uint {
@@ -21,9 +21,9 @@ bitflags! {
2121
}
2222

2323
bitflags! {
24-
/// `IN*` for use with [`inotify_add_watch`].
24+
/// `IN*` for use with [`inotify::add_watch`].
2525
///
26-
/// [`inotify_add_watch`]: crate::fs::inotify::inotify_add_watch
26+
/// [`inotify::add_watch`]: crate::fs::inotify::add_watch
2727
#[repr(transparent)]
2828
#[derive(Default, Copy, Clone, Eq, PartialEq, Hash, Debug)]
2929
pub struct WatchFlags: c::c_uint {
@@ -78,9 +78,9 @@ bitflags! {
7878
}
7979

8080
bitflags! {
81-
/// `IN*` for use with [`InotifyReader`].
81+
/// `IN*` for use with [`inotify::Reader`].
8282
///
83-
/// [`InotifyReader`]: crate::fs::inotify::InotifyReader
83+
/// [`inotify::Reader`]: crate::fs::inotify::InotifyReader
8484
#[repr(transparent)]
8585
#[derive(Default, Copy, Clone, Eq, PartialEq, Hash, Debug)]
8686
pub struct ReadFlags: c::c_uint {

src/backend/linux_raw/shm/types.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ use crate::backend::c;
22
use bitflags::bitflags;
33

44
bitflags! {
5-
/// `O_*` constants for use with [`shm_open`].
5+
/// `O_*` constants for use with [`shm::open`].
66
///
7-
/// [`shm_open`]: crate:shm::shm_open
7+
/// [`shm::open`]: crate:shm::open
88
#[repr(transparent)]
99
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
1010
pub struct ShmOFlags: c::c_uint {

src/fs/inotify.rs

+22-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
//! inotify support for working with inotifies
22
3+
#![allow(unused_qualifications)]
4+
5+
use super::inotify;
36
pub use crate::backend::fs::inotify::{CreateFlags, ReadFlags, WatchFlags};
47
use crate::backend::fs::syscalls;
58
use crate::fd::{AsFd, OwnedFd};
@@ -9,13 +12,23 @@ use crate::io::{read_uninit, Errno};
912
use core::mem::{align_of, size_of, MaybeUninit};
1013
use linux_raw_sys::general::inotify_event;
1114

15+
#[deprecated(note = "Use add_watch.")]
16+
#[doc(hidden)]
17+
pub use add_watch as inotify_add_watch;
18+
#[deprecated(note = "Use init.")]
19+
#[doc(hidden)]
20+
pub use init as inotify_init;
21+
#[deprecated(note = "Use remove_watch.")]
22+
#[doc(hidden)]
23+
pub use remove_watch as inotify_remove_watch;
24+
1225
/// `inotify_init1(flags)`—Creates a new inotify object.
1326
///
1427
/// Use the [`CreateFlags::CLOEXEC`] flag to prevent the resulting file
1528
/// descriptor from being implicitly passed across `exec` boundaries.
1629
#[doc(alias = "inotify_init1")]
1730
#[inline]
18-
pub fn inotify_init(flags: CreateFlags) -> io::Result<OwnedFd> {
31+
pub fn init(flags: inotify::CreateFlags) -> io::Result<OwnedFd> {
1932
syscalls::inotify_init1(flags)
2033
}
2134

@@ -27,22 +40,23 @@ pub fn inotify_init(flags: CreateFlags) -> io::Result<OwnedFd> {
2740
/// Note: Due to the existence of hardlinks, providing two different paths to
2841
/// this method may result in it returning the same watch descriptor. An
2942
/// application should keep track of this externally to avoid logic errors.
43+
#[doc(alias = "inotify_add_watch")]
3044
#[inline]
31-
pub fn inotify_add_watch<P: crate::path::Arg>(
45+
pub fn add_watch<P: crate::path::Arg>(
3246
inot: impl AsFd,
3347
path: P,
34-
flags: WatchFlags,
48+
flags: inotify::WatchFlags,
3549
) -> io::Result<i32> {
3650
path.into_with_c_str(|path| syscalls::inotify_add_watch(inot.as_fd(), path, flags))
3751
}
3852

3953
/// `inotify_rm_watch(self, wd)`—Removes a watch from this inotify.
4054
///
4155
/// The watch descriptor provided should have previously been returned by
42-
/// [`inotify_add_watch`] and not previously have been removed.
56+
/// [`inotify::add_watch`] and not previously have been removed.
4357
#[doc(alias = "inotify_rm_watch")]
4458
#[inline]
45-
pub fn inotify_remove_watch(inot: impl AsFd, wd: i32) -> io::Result<()> {
59+
pub fn remove_watch(inot: impl AsFd, wd: i32) -> io::Result<()> {
4660
syscalls::inotify_rm_watch(inot.as_fd(), wd)
4761
}
4862

@@ -52,14 +66,14 @@ pub fn inotify_remove_watch(inot: impl AsFd, wd: i32) -> io::Result<()> {
5266
/// based on it.
5367
///
5468
/// [`RawDir`]: crate::fs::raw_dir::RawDir
55-
pub struct InotifyReader<'buf, Fd: AsFd> {
69+
pub struct Reader<'buf, Fd: AsFd> {
5670
fd: Fd,
5771
buf: &'buf mut [MaybeUninit<u8>],
5872
initialized: usize,
5973
offset: usize,
6074
}
6175

62-
impl<'buf, Fd: AsFd> InotifyReader<'buf, Fd> {
76+
impl<'buf, Fd: AsFd> Reader<'buf, Fd> {
6377
/// Create a new iterator from the given file descriptor and buffer.
6478
pub fn new(fd: Fd, buf: &'buf mut [MaybeUninit<u8>]) -> Self {
6579
Self {
@@ -114,7 +128,7 @@ impl<'a> InotifyEvent<'a> {
114128
}
115129
}
116130

117-
impl<'buf, Fd: AsFd> InotifyReader<'buf, Fd> {
131+
impl<'buf, Fd: AsFd> Reader<'buf, Fd> {
118132
/// Read the next inotify event.
119133
#[allow(unsafe_code)]
120134
pub fn next(&mut self) -> io::Result<InotifyEvent> {

src/shm.rs

+17-3
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,30 @@
11
//! POSIX shared memory
22
3+
#![allow(unused_qualifications)]
4+
35
use crate::fd::OwnedFd;
46
use crate::{backend, io, path};
57

8+
use super::shm;
69
pub use crate::backend::fs::types::Mode;
10+
pub use crate::backend::shm::types::ShmOFlags as OFlags;
11+
#[deprecated(note = "Use OFlags.")]
12+
#[doc(hidden)]
713
pub use crate::backend::shm::types::ShmOFlags;
14+
#[deprecated(note = "Use open.")]
15+
#[doc(hidden)]
16+
pub use open as shm_open;
17+
#[deprecated(note = "Use unlink.")]
18+
#[doc(hidden)]
19+
pub use unlink as shm_unlink;
820

921
/// `shm_open(name, oflags, mode)`—Opens a shared memory object.
1022
///
1123
/// For portability, `name` should begin with a slash, contain no other
1224
/// slashes, and be no longer than an implementation-defined limit (255 on
1325
/// Linux).
1426
///
15-
/// Exactly one of [`ShmOFlags::RDONLY`] and [`ShmOFlags::RDWR`] should be
27+
/// Exactly one of [`shm::OFlags::RDONLY`] and [`shm::OFlags::RDWR`] should be
1628
/// passed. The file descriptor will be opened with `FD_CLOEXEC` set.
1729
///
1830
/// # References
@@ -21,8 +33,9 @@ pub use crate::backend::shm::types::ShmOFlags;
2133
///
2234
/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/shm_open.html
2335
/// [Linux]: https://man7.org/linux/man-pages/man3/shm_open.3.html
36+
#[doc(alias = "shm_open")]
2437
#[inline]
25-
pub fn shm_open<P: path::Arg>(name: P, flags: ShmOFlags, mode: Mode) -> io::Result<OwnedFd> {
38+
pub fn open<P: path::Arg>(name: P, flags: shm::OFlags, mode: Mode) -> io::Result<OwnedFd> {
2639
name.into_with_c_str(|name| backend::shm::syscalls::shm_open(name, flags, mode))
2740
}
2841

@@ -34,7 +47,8 @@ pub fn shm_open<P: path::Arg>(name: P, flags: ShmOFlags, mode: Mode) -> io::Resu
3447
///
3548
/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/shm_unlink.html
3649
/// [Linux]: https://man7.org/linux/man-pages/man3/shm_unlink.3.html
50+
#[doc(alias = "shm_unlink")]
3751
#[inline]
38-
pub fn shm_unlink<P: path::Arg>(name: P) -> io::Result<()> {
52+
pub fn unlink<P: path::Arg>(name: P) -> io::Result<()> {
3953
name.into_with_c_str(backend::shm::syscalls::shm_unlink)
4054
}

tests/fs/inotify.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
use rustix::fs::inotify::{
2-
inotify_add_watch, inotify_init, CreateFlags, InotifyReader, WatchFlags,
3-
};
1+
use rustix::fs::inotify::{self, CreateFlags, WatchFlags};
42
use rustix::io::Errno;
53
use std::fmt::Write;
64
use std::fs::{create_dir_all, remove_file, rename, File};
75
use std::mem::MaybeUninit;
86

97
#[test]
108
fn test_inotify_iter() {
11-
let inotify = inotify_init(CreateFlags::NONBLOCK).unwrap();
9+
let inotify = inotify::init(CreateFlags::NONBLOCK).unwrap();
1210
create_dir_all("/tmp/.rustix-inotify-test").unwrap();
13-
inotify_add_watch(
11+
inotify::add_watch(
1412
&inotify,
1513
"/tmp/.rustix-inotify-test",
1614
WatchFlags::ALL_EVENTS,
@@ -29,7 +27,7 @@ fn test_inotify_iter() {
2927
let mut cookie = 0;
3028

3129
let mut buf = [MaybeUninit::uninit(); 512];
32-
let mut iter = InotifyReader::new(inotify, &mut buf);
30+
let mut iter = inotify::Reader::new(inotify, &mut buf);
3331
loop {
3432
let e = match iter.next() {
3533
Err(Errno::WOULDBLOCK) => break,

0 commit comments

Comments
 (0)