Skip to content

Commit 0440d26

Browse files
committed
Auto merge of #377 - aoprisan:master, r=posborne
Added lseek and seek64 I have added lseek and seek64 to unistd, the last one targeting Linux/Android only. I wasn't sure where to place the Whence enum, or if it's a nice of doing, I am quite fresh to Rust.
2 parents d9f6316 + 0eb8b4b commit 0440d26

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

src/unistd.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,40 @@ pub fn write(fd: RawFd, buf: &[u8]) -> Result<usize> {
214214
Errno::result(res).map(|r| r as usize)
215215
}
216216

217+
pub enum Whence {
218+
SeekSet,
219+
SeekCur,
220+
SeekEnd,
221+
SeekData,
222+
SeekHole
223+
}
224+
225+
impl Whence {
226+
fn to_libc_type(&self) -> c_int {
227+
match self {
228+
&Whence::SeekSet => libc::SEEK_SET,
229+
&Whence::SeekCur => libc::SEEK_CUR,
230+
&Whence::SeekEnd => libc::SEEK_END,
231+
&Whence::SeekData => 3,
232+
&Whence::SeekHole => 4
233+
}
234+
}
235+
236+
}
237+
238+
pub fn lseek(fd: RawFd, offset: libc::off_t, whence: Whence) -> Result<libc::off_t> {
239+
let res = unsafe { libc::lseek(fd, offset, whence.to_libc_type()) };
240+
241+
Errno::result(res).map(|r| r as libc::off_t)
242+
}
243+
244+
#[cfg(any(target_os = "linux", target_os = "android"))]
245+
pub fn lseek64(fd: RawFd, offset: libc::off64_t, whence: Whence) -> Result<libc::off64_t> {
246+
let res = unsafe { libc::lseek64(fd, offset, whence.to_libc_type()) };
247+
248+
Errno::result(res).map(|r| r as libc::off64_t)
249+
}
250+
217251
pub fn pipe() -> Result<(RawFd, RawFd)> {
218252
unsafe {
219253
let mut fds: [c_int; 2] = mem::uninitialized();
@@ -292,7 +326,6 @@ pub fn unlink<P: ?Sized + NixPath>(path: &P) -> Result<()> {
292326
libc::unlink(cstr.as_ptr())
293327
}
294328
}));
295-
296329
Errno::result(res).map(drop)
297330
}
298331

test/test_unistd.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ use nix::unistd::ForkResult::*;
33
use nix::sys::wait::*;
44
use std::ffi::CString;
55

6+
use std::io::{Write, Read};
7+
use tempfile::tempfile;
8+
use libc::off_t;
9+
use std::os::unix::prelude::*;
10+
11+
12+
613
#[test]
714
fn test_fork_and_waitpid() {
815
let pid = fork();
@@ -112,6 +119,38 @@ macro_rules! execve_test_factory(
112119
)
113120
);
114121

122+
#[test]
123+
fn test_lseek() {
124+
const CONTENTS: &'static [u8] = b"abcdef123456";
125+
let mut tmp = tempfile().unwrap();
126+
tmp.write(CONTENTS).unwrap();
127+
128+
let offset: off_t = 5;
129+
lseek(tmp.as_raw_fd(), offset, Whence::SeekSet).unwrap();
130+
131+
let mut buf = String::new();
132+
tmp.read_to_string(&mut buf).unwrap();
133+
assert_eq!(b"f123456", buf.as_bytes());
134+
135+
close(tmp.as_raw_fd()).unwrap();
136+
}
137+
138+
#[cfg(any(target_os = "linux", target_os = "android"))]
139+
#[test]
140+
fn test_lseek64() {
141+
const CONTENTS: &'static [u8] = b"abcdef123456";
142+
let mut tmp = tempfile().unwrap();
143+
tmp.write(CONTENTS).unwrap();
144+
145+
lseek64(tmp.as_raw_fd(), 5, Whence::SeekSet).unwrap();
146+
147+
let mut buf = String::new();
148+
tmp.read_to_string(&mut buf).unwrap();
149+
assert_eq!(b"f123456", buf.as_bytes());
150+
151+
close(tmp.as_raw_fd()).unwrap();
152+
}
153+
115154
execve_test_factory!(test_execve, execve, b"/bin/sh", b"/system/bin/sh");
116155

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

0 commit comments

Comments
 (0)