Skip to content

Commit 7077238

Browse files
authored
Expose OFlags::LARGEFILE and document its behavior (#1020)
This flag can be reported by `fcntl_getfl` but is ignored currently. It is also used in FUSE requests. But typical users do not need to care about it since it will be added automatically by rustix and/or libc on relevent calls.
1 parent 7ed40bc commit 7077238

File tree

5 files changed

+31
-11
lines changed

5 files changed

+31
-11
lines changed

src/backend/libc/c.rs

+10
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,16 @@ pub(crate) const MSG_DONTWAIT: c_int = libc::MSG_NONBLOCK;
9191
#[cfg(feature = "net")]
9292
pub(crate) const SO_NOSIGPIPE: c_int = 0x0800;
9393

94+
// It is defined as 0 in libc under 64-bit platforms, but is automatically set by kernel.
95+
// https://github.com/torvalds/linux/blob/v6.7/fs/open.c#L1458-L1459
96+
#[cfg(linux_kernel)]
97+
pub(crate) const O_LARGEFILE: c_int = linux_raw_sys::general::O_LARGEFILE as _;
98+
99+
// Gated under `_LARGEFILE_SOURCE` but automatically set by the kernel.
100+
// https://github.com/illumos/illumos-gate/blob/fb2cb638e5604b214d8ea8d4f01ad2e77b437c17/usr/src/ucbhead/sys/fcntl.h#L64
101+
#[cfg(target_os = "illumos")]
102+
pub(crate) const O_LARGEFILE: c_int = 0x2000;
103+
94104
// On PowerPC, the regular `termios` has the `termios2` fields and there is no
95105
// `termios2`. linux-raw-sys has aliases `termios2` to `termios` to cover this
96106
// difference, but we still need to manually import it since `libc` doesn't

src/backend/libc/fs/types.rs

+8
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,14 @@ bitflags! {
328328
#[cfg(target_os = "freebsd")]
329329
const EMPTY_PATH = bitcast!(c::O_EMPTY_PATH);
330330

331+
/// `O_LARGEFILE`
332+
///
333+
/// Note that rustix and/or libc will automatically set this flag when appropriate on
334+
/// `open(2)` and friends, thus typical users do not need to care about it.
335+
/// It will may be reported in return of `fcntl_getfl`, though.
336+
#[cfg(any(linux_kernel, target_os = "illumos"))]
337+
const LARGEFILE = bitcast!(c::O_LARGEFILE);
338+
331339
/// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
332340
const _ = !0;
333341
}

src/backend/linux_raw/fs/syscalls.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ use {
5858
#[inline]
5959
pub(crate) fn open(path: &CStr, flags: OFlags, mode: Mode) -> io::Result<OwnedFd> {
6060
// Always enable support for large files.
61-
let flags = flags | OFlags::from_bits_retain(c::O_LARGEFILE);
61+
let flags = flags | OFlags::LARGEFILE;
6262

6363
#[cfg(any(target_arch = "aarch64", target_arch = "riscv64"))]
6464
{
@@ -78,7 +78,7 @@ pub(crate) fn openat(
7878
mode: Mode,
7979
) -> io::Result<OwnedFd> {
8080
// Always enable support for large files.
81-
let flags = flags | OFlags::from_bits_retain(c::O_LARGEFILE);
81+
let flags = flags | OFlags::LARGEFILE;
8282

8383
unsafe { ret_owned_fd(syscall_readonly!(__NR_openat, dirfd, path, flags, mode)) }
8484
}

src/backend/linux_raw/fs/types.rs

+7
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,13 @@ bitflags! {
250250
/// `O_DIRECT`
251251
const DIRECT = linux_raw_sys::general::O_DIRECT;
252252

253+
/// `O_LARGEFILE`
254+
///
255+
/// Note that rustix and/or libc will automatically set this flag when appropriate on
256+
/// `open(2)` and friends, thus typical users do not need to care about it.
257+
/// It will may be reported in return of `fcntl_getfl`, though.
258+
const LARGEFILE = linux_raw_sys::general::O_LARGEFILE;
259+
253260
/// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
254261
const _ = !0;
255262
}

tests/fs/file.rs

+4-9
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,10 @@ fn test_file() {
112112
// Test `fcntl_getfl`.
113113
let fl = rustix::fs::fcntl_getfl(&file).unwrap();
114114

115-
// On Linux, rustix automatically sets `O_LARGEFILE`, so clear it here so
116-
// that we can test that no other bits are present.
117-
#[cfg(linux_kernel)]
118-
let fl = fl - rustix::fs::OFlags::from_bits_retain(linux_raw_sys::general::O_LARGEFILE);
119-
120-
// On illumos, the system automatically sets `O_LARGEFILE`, so clear it
121-
// here so that we can test that no other bits are present.
122-
#[cfg(target_os = "illumos")]
123-
let fl = fl - rustix::fs::OFlags::from_bits_retain(0x2000);
115+
// Clear O_LARGEFILE, which may be set by rustix on 32-bit Linux or automatically by some
116+
// kernel on 64-bit (Linux and illumos).
117+
#[cfg(any(linux_kernel, target_os = "illumos"))]
118+
let fl = fl - rustix::fs::OFlags::LARGEFILE;
124119

125120
assert_eq!(fl, rustix::fs::OFlags::empty());
126121

0 commit comments

Comments
 (0)