Skip to content

Commit d2c1263

Browse files
committed
Auto merge of #354 - brianp:fcntl-libc, r=kamalmarhubi
fcntl: Use bindings from libc instead of our own **Un-finished** and looking for help. As #264 was tagged "mentor, good first bug" Ref #264 I couldn't find `F_GET_SEALS`, or `F_ADD_SEALS` in libc. Is this a case where I should be making a PR over there? I'm not sure where exactly to address it in libc.
2 parents 3ed76e4 + 7568340 commit d2c1263

File tree

1 file changed

+30
-113
lines changed

1 file changed

+30
-113
lines changed

src/fcntl.rs

+30-113
Original file line numberDiff line numberDiff line change
@@ -1,109 +1,26 @@
11
use {Errno, Result, NixPath};
2-
use libc::{c_int, c_uint};
2+
use libc::{self, c_int, c_uint};
33
use sys::stat::Mode;
44
use std::os::unix::io::RawFd;
55

66
#[cfg(any(target_os = "linux", target_os = "android"))]
77
use sys::uio::IoVec; // For vmsplice
8-
#[cfg(any(target_os = "linux", target_os = "android"))]
9-
use libc;
108

119
pub use self::consts::*;
12-
pub use self::ffi::flock;
1310

11+
// TODO: The remainder of the ffi module should be removed afer work on
12+
// https://github.com/rust-lang/libc/issues/235 is resolved.
1413
#[allow(dead_code)]
1514
mod ffi {
16-
pub use libc::{open, fcntl};
17-
pub use self::os::*;
18-
pub use libc::flock as libc_flock;
19-
pub use libc::{LOCK_SH, LOCK_EX, LOCK_NB, LOCK_UN};
20-
21-
#[cfg(any(target_os = "linux", target_os = "android"))]
22-
mod os {
23-
use libc::{c_int, c_short, off_t, pid_t};
24-
25-
#[repr(C)]
26-
#[derive(Clone, Copy, Default, Debug)]
27-
pub struct flock {
28-
pub l_type: c_short,
29-
pub l_whence: c_short,
30-
pub l_start: off_t,
31-
pub l_len: off_t,
32-
pub l_pid: pid_t,
33-
34-
// not actually here, but brings in line with freebsd
35-
pub l_sysid: c_int,
36-
}
37-
38-
pub const F_DUPFD: c_int = 0;
39-
pub const F_DUPFD_CLOEXEC: c_int = 1030;
40-
pub const F_GETFD: c_int = 1;
41-
pub const F_SETFD: c_int = 2;
42-
pub const F_GETFL: c_int = 3;
43-
pub const F_SETFL: c_int = 4;
44-
pub const F_SETLK: c_int = 6;
45-
pub const F_SETLKW: c_int = 7;
46-
pub const F_GETLK: c_int = 5;
47-
48-
pub const F_ADD_SEALS: c_int = 1033;
49-
pub const F_GET_SEALS: c_int = 1034;
50-
51-
pub const F_SEAL_SEAL: c_int = 1;
52-
pub const F_SEAL_SHRINK: c_int = 2;
53-
pub const F_SEAL_GROW: c_int = 4;
54-
pub const F_SEAL_WRITE: c_int = 8;
55-
}
56-
57-
#[cfg(any(target_os = "macos", target_os = "freebsd", target_os = "dragonfly", target_os = "ios", target_os = "openbsd", target_os = "netbsd"))]
58-
mod os {
59-
use libc::{c_int, c_short, off_t, pid_t};
60-
61-
#[repr(C)]
62-
#[derive(Clone, Copy, Default, Debug)]
63-
pub struct flock {
64-
pub l_start: off_t,
65-
pub l_len: off_t,
66-
pub l_pid: pid_t,
67-
pub l_type: c_short,
68-
pub l_whence: c_short,
69-
70-
// not actually here, but brings in line with freebsd
71-
pub l_sysid: c_int,
72-
}
15+
use libc::c_int;
7316

74-
pub const F_DUPFD: c_int = 0;
75-
#[cfg(not(any(target_os = "dragonfly", target_os = "netbsd")))]
76-
pub const F_DUPFD_CLOEXEC: c_int = 67;
77-
#[cfg(target_os = "dragonfly")]
78-
pub const F_DUPFD_CLOEXEC: c_int = 17;
79-
#[cfg(target_os = "netbsd")]
80-
pub const F_DUPFD_CLOEXEC: c_int = 12;
81-
pub const F_GETFD: c_int = 1;
82-
pub const F_SETFD: c_int = 2;
83-
pub const F_GETFL: c_int = 3;
84-
pub const F_SETFL: c_int = 4;
85-
#[cfg(target_os = "netbsd")]
86-
pub const F_GETOWN: c_int = 5;
87-
#[cfg(target_os = "netbsd")]
88-
pub const F_SETOWN: c_int = 6;
89-
pub const F_GETLK: c_int = 7;
90-
pub const F_SETLK: c_int = 8;
91-
pub const F_SETLKW: c_int = 9;
92-
93-
#[cfg(target_os = "netbsd")]
94-
pub const F_CLOSEM: c_int = 10;
95-
#[cfg(target_os = "netbsd")]
96-
pub const F_MAXFD: c_int = 11;
97-
#[cfg(target_os = "netbsd")]
98-
pub const F_GETNOSIGPIPE: c_int = 13;
99-
#[cfg(target_os = "netbsd")]
100-
pub const F_SETNOSIGPIPE: c_int = 14;
101-
}
17+
pub const F_ADD_SEALS: c_int = 1033;
18+
pub const F_GET_SEALS: c_int = 1034;
10219
}
10320

10421
pub fn open<P: ?Sized + NixPath>(path: &P, oflag: OFlag, mode: Mode) -> Result<RawFd> {
10522
let fd = try!(path.with_nix_path(|cstr| {
106-
unsafe { ffi::open(cstr.as_ptr(), oflag.bits(), mode.bits() as c_uint) }
23+
unsafe { libc::open(cstr.as_ptr(), oflag.bits(), mode.bits() as c_uint) }
10724
}));
10825

10926
Errno::result(fd)
@@ -116,15 +33,15 @@ pub enum FcntlArg<'a> {
11633
F_SETFD(FdFlag), // FD_FLAGS
11734
F_GETFL,
11835
F_SETFL(OFlag), // O_NONBLOCK
119-
F_SETLK(&'a flock),
120-
F_SETLKW(&'a flock),
121-
F_GETLK(&'a mut flock),
36+
F_SETLK(&'a libc::flock),
37+
F_SETLKW(&'a libc::flock),
38+
F_GETLK(&'a mut libc::flock),
12239
#[cfg(any(target_os = "linux", target_os = "android"))]
123-
F_OFD_SETLK(&'a flock),
40+
F_OFD_SETLK(&'a libc::flock),
12441
#[cfg(any(target_os = "linux", target_os = "android"))]
125-
F_OFD_SETLKW(&'a flock),
42+
F_OFD_SETLKW(&'a libc::flock),
12643
#[cfg(any(target_os = "linux", target_os = "android"))]
127-
F_OFD_GETLK(&'a mut flock),
44+
F_OFD_GETLK(&'a mut libc::flock),
12845
#[cfg(target_os = "linux")]
12946
F_ADD_SEALS(SealFlag),
13047
#[cfg(target_os = "linux")]
@@ -139,19 +56,19 @@ pub fn fcntl(fd: RawFd, arg: FcntlArg) -> Result<c_int> {
13956

14057
let res = unsafe {
14158
match arg {
142-
F_DUPFD(rawfd) => ffi::fcntl(fd, ffi::F_DUPFD, rawfd),
143-
F_DUPFD_CLOEXEC(rawfd) => ffi::fcntl(fd, ffi::F_DUPFD_CLOEXEC, rawfd),
144-
F_GETFD => ffi::fcntl(fd, ffi::F_GETFD),
145-
F_SETFD(flag) => ffi::fcntl(fd, ffi::F_SETFD, flag.bits()),
146-
F_GETFL => ffi::fcntl(fd, ffi::F_GETFL),
147-
F_SETFL(flag) => ffi::fcntl(fd, ffi::F_SETFL, flag.bits()),
148-
F_SETLK(flock) => ffi::fcntl(fd, ffi::F_SETLK, flock),
149-
F_SETLKW(flock) => ffi::fcntl(fd, ffi::F_SETLKW, flock),
150-
F_GETLK(flock) => ffi::fcntl(fd, ffi::F_GETLK, flock),
59+
F_DUPFD(rawfd) => libc::fcntl(fd, libc::F_DUPFD, rawfd),
60+
F_DUPFD_CLOEXEC(rawfd) => libc::fcntl(fd, libc::F_DUPFD_CLOEXEC, rawfd),
61+
F_GETFD => libc::fcntl(fd, libc::F_GETFD),
62+
F_SETFD(flag) => libc::fcntl(fd, libc::F_SETFD, flag.bits()),
63+
F_GETFL => libc::fcntl(fd, libc::F_GETFL),
64+
F_SETFL(flag) => libc::fcntl(fd, libc::F_SETFL, flag.bits()),
65+
F_SETLK(flock) => libc::fcntl(fd, libc::F_SETLK, flock),
66+
F_SETLKW(flock) => libc::fcntl(fd, libc::F_SETLKW, flock),
67+
F_GETLK(flock) => libc::fcntl(fd, libc::F_GETLK, flock),
15168
#[cfg(target_os = "linux")]
152-
F_ADD_SEALS(flag) => ffi::fcntl(fd, ffi::F_ADD_SEALS, flag.bits()),
69+
F_ADD_SEALS(flag) => libc::fcntl(fd, ffi::F_ADD_SEALS, flag.bits()),
15370
#[cfg(target_os = "linux")]
154-
F_GET_SEALS => ffi::fcntl(fd, ffi::F_GET_SEALS),
71+
F_GET_SEALS => libc::fcntl(fd, ffi::F_GET_SEALS),
15572
#[cfg(any(target_os = "linux", target_os = "android"))]
15673
_ => unimplemented!()
15774
}
@@ -174,12 +91,12 @@ pub fn flock(fd: RawFd, arg: FlockArg) -> Result<()> {
17491

17592
let res = unsafe {
17693
match arg {
177-
LockShared => ffi::libc_flock(fd, ffi::LOCK_SH),
178-
LockExclusive => ffi::libc_flock(fd, ffi::LOCK_EX),
179-
Unlock => ffi::libc_flock(fd, ffi::LOCK_UN),
180-
LockSharedNonblock => ffi::libc_flock(fd, ffi::LOCK_SH | ffi::LOCK_NB),
181-
LockExclusiveNonblock => ffi::libc_flock(fd, ffi::LOCK_EX | ffi::LOCK_NB),
182-
UnlockNonblock => ffi::libc_flock(fd, ffi::LOCK_UN | ffi::LOCK_NB),
94+
LockShared => libc::flock(fd, libc::LOCK_SH),
95+
LockExclusive => libc::flock(fd, libc::LOCK_EX),
96+
Unlock => libc::flock(fd, libc::LOCK_UN),
97+
LockSharedNonblock => libc::flock(fd, libc::LOCK_SH | libc::LOCK_NB),
98+
LockExclusiveNonblock => libc::flock(fd, libc::LOCK_EX | libc::LOCK_NB),
99+
UnlockNonblock => libc::flock(fd, libc::LOCK_UN | libc::LOCK_NB),
183100
}
184101
};
185102

0 commit comments

Comments
 (0)