Skip to content

Commit e462c57

Browse files
Andy Grovercarllerche
authored andcommitted
Implement more fcntl operations
Derive some more traits on flock to make life easier Change fcntl to return Result<c_int> so we can get results of F_GET* ops. Change pipe2_setflags to match.
1 parent aa0863b commit e462c57

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

src/fcntl.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use {Error, Result, NixPath};
22
use errno::Errno;
3-
use libc::mode_t;
3+
use libc::{mode_t, c_int};
44
use sys::stat::Mode;
55
use std::os::unix::io::RawFd;
66

@@ -17,7 +17,7 @@ mod ffi {
1717
use libc::{c_int, c_short, off_t, pid_t};
1818

1919
#[repr(C)]
20-
#[derive(Clone, Copy)]
20+
#[derive(Clone, Copy, Default, Debug)]
2121
pub struct flock {
2222
pub l_type: c_short,
2323
pub l_whence: c_short,
@@ -45,7 +45,7 @@ mod ffi {
4545
use libc::{c_int, c_short, off_t, pid_t};
4646

4747
#[repr(C)]
48-
#[derive(Clone, Copy)]
48+
#[derive(Clone, Copy, Default, Debug)]
4949
pub struct flock {
5050
pub l_start: off_t,
5151
pub l_len: off_t,
@@ -102,13 +102,21 @@ pub enum FcntlArg<'a> {
102102
}
103103

104104
// TODO: Figure out how to handle value fcntl returns
105-
pub fn fcntl(fd: RawFd, arg: FcntlArg) -> Result<()> {
105+
pub fn fcntl(fd: RawFd, arg: FcntlArg) -> Result<c_int> {
106106
use self::FcntlArg::*;
107107

108108
let res = unsafe {
109109
match arg {
110+
F_DUPFD(rawfd) => ffi::fcntl(fd, ffi::F_DUPFD, rawfd),
111+
F_DUPFD_CLOEXEC(rawfd) => ffi::fcntl(fd, ffi::F_DUPFD_CLOEXEC, rawfd),
112+
F_GETFD => ffi::fcntl(fd, ffi::F_GETFD),
110113
F_SETFD(flag) => ffi::fcntl(fd, ffi::F_SETFD, flag.bits()),
114+
F_GETFL => ffi::fcntl(fd, ffi::F_GETFL),
111115
F_SETFL(flag) => ffi::fcntl(fd, ffi::F_SETFL, flag.bits()),
116+
F_SETLK(flock) => ffi::fcntl(fd, ffi::F_SETLK, flock),
117+
F_SETLKW(flock) => ffi::fcntl(fd, ffi::F_SETLKW, flock),
118+
F_GETLK(flock) => ffi::fcntl(fd, ffi::F_GETLK, flock),
119+
#[cfg(any(target_os = "linux", target_os = "android"))]
112120
_ => unimplemented!()
113121
}
114122
};
@@ -117,7 +125,7 @@ pub fn fcntl(fd: RawFd, arg: FcntlArg) -> Result<()> {
117125
return Err(Error::Sys(Errno::last()));
118126
}
119127

120-
Ok(())
128+
Ok(res)
121129
}
122130

123131
#[cfg(any(target_os = "linux", target_os = "android"))]

src/unistd.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ pub fn pipe2(flags: OFlag) -> Result<(RawFd, RawFd)> {
254254
}
255255

256256
fn pipe2_setflags(fd1: RawFd, fd2: RawFd, flags: OFlag) -> Result<()> {
257-
let mut res = Ok(());
257+
let mut res = Ok(0);
258258

259259
if flags.contains(O_CLOEXEC) {
260260
res = res

0 commit comments

Comments
 (0)