Skip to content

Commit c4ac6b6

Browse files
Add ioctl_getflags and ioctl_setflags (#973)
* chore: bump libc * feat: add ioctl_getflags and ioctl_setflags
1 parent 10361e7 commit c4ac6b6

File tree

3 files changed

+78
-1
lines changed

3 files changed

+78
-1
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ once_cell = { version = "1.5.2", optional = true }
3838
[target.'cfg(all(not(rustix_use_libc), not(miri), target_os = "linux", target_endian = "little", any(target_arch = "arm", all(target_arch = "aarch64", target_pointer_width = "64"), target_arch = "riscv64", all(rustix_use_experimental_asm, target_arch = "powerpc64"), all(rustix_use_experimental_asm, target_arch = "mips"), all(rustix_use_experimental_asm, target_arch = "mips32r6"), all(rustix_use_experimental_asm, target_arch = "mips64"), all(rustix_use_experimental_asm, target_arch = "mips64r6"), target_arch = "x86", all(target_arch = "x86_64", target_pointer_width = "64"))))'.dependencies]
3939
linux-raw-sys = { version = "0.4.11", default-features = false, features = ["general", "errno", "ioctl", "no_std", "elf"] }
4040
libc_errno = { package = "errno", version = "0.3.8", default-features = false, optional = true }
41-
libc = { version = "0.2.150", default-features = false, features = ["extra_traits"], optional = true }
41+
libc = { version = "0.2.151", default-features = false, features = ["extra_traits"], optional = true }
4242

4343
# Dependencies for platforms where only libc is supported:
4444
#

src/backend/linux_raw/c.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ pub(crate) use linux_raw_sys::general::{
4242
};
4343

4444
pub(crate) use linux_raw_sys::ioctl::{BLKPBSZGET, BLKSSZGET, FICLONE};
45+
#[cfg(target_pointer_width = "32")]
46+
pub(crate) use linux_raw_sys::ioctl::{FS_IOC32_GETFLAGS, FS_IOC32_SETFLAGS};
47+
#[cfg(target_pointer_width = "64")]
48+
pub(crate) use linux_raw_sys::ioctl::{FS_IOC_GETFLAGS, FS_IOC_SETFLAGS};
4549

4650
#[cfg(feature = "io_uring")]
4751
pub(crate) use linux_raw_sys::{general::open_how, io_uring::*};

src/fs/ioctl.rs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ use {
99
backend::c,
1010
};
1111

12+
use bitflags::bitflags;
13+
1214
#[cfg(all(linux_kernel, not(any(target_arch = "sparc", target_arch = "sparc64"))))]
1315
use crate::fd::{AsRawFd, BorrowedFd};
1416

@@ -90,3 +92,74 @@ unsafe impl ioctl::Ioctl for Ficlone<'_> {
9092
Ok(())
9193
}
9294
}
95+
96+
#[cfg(linux_kernel)]
97+
bitflags! {
98+
/// `FS_*` constants for use with [`ioctl_getflags`][crate::io::ioctl::ioctl_getflags].
99+
pub struct IFlags: c::c_uint {
100+
/// `FS_APPEND_FL`
101+
const APPEND = linux_raw_sys::general::FS_APPEND_FL;
102+
/// `FS_COMPR_FL`
103+
const COMPRESSED = linux_raw_sys::general::FS_COMPR_FL;
104+
/// `FS_DIRSYNC_FL`
105+
const DIRSYNC = linux_raw_sys::general::FS_DIRSYNC_FL;
106+
/// `FS_IMMUTABLE_FL`
107+
const IMMUTABLE = linux_raw_sys::general::FS_IMMUTABLE_FL;
108+
/// `FS_JOURNAL_DATA_FL`
109+
const JOURNALING = linux_raw_sys::general::FS_JOURNAL_DATA_FL;
110+
/// `FS_NOATIME_FL`
111+
const NOATIME = linux_raw_sys::general::FS_NOATIME_FL;
112+
/// `FS_NOCOW_FL`
113+
const NOCOW = linux_raw_sys::general::FS_NOCOW_FL;
114+
/// `FS_NODUMP_FL`
115+
const NODUMP = linux_raw_sys::general::FS_NODUMP_FL;
116+
/// `FS_NOTAIL_FL`
117+
const NOTAIL = linux_raw_sys::general::FS_NOTAIL_FL;
118+
/// `FS_PROJINHERIT_FL`
119+
const PROJECT_INHERIT = linux_raw_sys::general::FS_PROJINHERIT_FL;
120+
/// `FS_SECRM_FL`
121+
const SECURE_REMOVAL = linux_raw_sys::general::FS_SECRM_FL;
122+
/// `FS_SYNC_FL`
123+
const SYNC = linux_raw_sys::general::FS_SYNC_FL;
124+
/// `FS_TOPDIR_FL`
125+
const TOPDIR = linux_raw_sys::general::FS_TOPDIR_FL;
126+
/// `FS_UNRM_FL`
127+
const UNRM = linux_raw_sys::general::FS_UNRM_FL;
128+
}
129+
}
130+
131+
/// `ioctl(fd, FS_IOC_GETFLAGS)`—Returns the [inode flags] attributes
132+
///
133+
/// [inode flags]: https://man7.org/linux/man-pages/man2/ioctl_iflags.2.html
134+
#[cfg(linux_kernel)]
135+
#[inline]
136+
#[doc(alias = "FS_IOC_GETFLAGS")]
137+
pub fn ioctl_getflags<Fd: AsFd>(fd: Fd) -> io::Result<IFlags> {
138+
unsafe {
139+
#[cfg(target_pointer_width = "32")]
140+
let ctl = ioctl::Getter::<ioctl::BadOpcode<{ c::FS_IOC32_GETFLAGS }>, u32>::new();
141+
#[cfg(target_pointer_width = "64")]
142+
let ctl = ioctl::Getter::<ioctl::BadOpcode<{ c::FS_IOC_GETFLAGS }>, u32>::new();
143+
144+
ioctl::ioctl(fd, ctl).map(IFlags::from_bits_retain)
145+
}
146+
}
147+
148+
/// `ioctl(fd, FS_IOC_SETFLAGS)`—Modify the [inode flags] attributes
149+
///
150+
/// [inode flags]: https://man7.org/linux/man-pages/man2/ioctl_iflags.2.html
151+
#[cfg(linux_kernel)]
152+
#[inline]
153+
#[doc(alias = "FS_IOC_SETFLAGS")]
154+
pub fn ioctl_setflags<Fd: AsFd>(fd: Fd, flags: IFlags) -> io::Result<()> {
155+
unsafe {
156+
#[cfg(target_pointer_width = "32")]
157+
let ctl =
158+
ioctl::Setter::<ioctl::BadOpcode<{ c::FS_IOC32_SETFLAGS }>, u32>::new(flags.bits());
159+
160+
#[cfg(target_pointer_width = "64")]
161+
let ctl = ioctl::Setter::<ioctl::BadOpcode<{ c::FS_IOC_SETFLAGS }>, u32>::new(flags.bits());
162+
163+
ioctl::ioctl(fd, ctl)
164+
}
165+
}

0 commit comments

Comments
 (0)