Skip to content

Commit

Permalink
Auto merge of #377 - aoprisan:master, r=posborne
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
homu committed Jun 20, 2016
2 parents d9f6316 + 0eb8b4b commit 0440d26
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
35 changes: 34 additions & 1 deletion src/unistd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,40 @@ pub fn write(fd: RawFd, buf: &[u8]) -> Result<usize> {
Errno::result(res).map(|r| r as usize)
}

pub enum Whence {
SeekSet,
SeekCur,
SeekEnd,
SeekData,
SeekHole
}

impl Whence {
fn to_libc_type(&self) -> c_int {
match self {
&Whence::SeekSet => libc::SEEK_SET,
&Whence::SeekCur => libc::SEEK_CUR,
&Whence::SeekEnd => libc::SEEK_END,
&Whence::SeekData => 3,
&Whence::SeekHole => 4
}
}

}

pub fn lseek(fd: RawFd, offset: libc::off_t, whence: Whence) -> Result<libc::off_t> {
let res = unsafe { libc::lseek(fd, offset, whence.to_libc_type()) };

Errno::result(res).map(|r| r as libc::off_t)
}

#[cfg(any(target_os = "linux", target_os = "android"))]
pub fn lseek64(fd: RawFd, offset: libc::off64_t, whence: Whence) -> Result<libc::off64_t> {
let res = unsafe { libc::lseek64(fd, offset, whence.to_libc_type()) };

Errno::result(res).map(|r| r as libc::off64_t)
}

pub fn pipe() -> Result<(RawFd, RawFd)> {
unsafe {
let mut fds: [c_int; 2] = mem::uninitialized();
Expand Down Expand Up @@ -292,7 +326,6 @@ pub fn unlink<P: ?Sized + NixPath>(path: &P) -> Result<()> {
libc::unlink(cstr.as_ptr())
}
}));

Errno::result(res).map(drop)
}

Expand Down
39 changes: 39 additions & 0 deletions test/test_unistd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ use nix::unistd::ForkResult::*;
use nix::sys::wait::*;
use std::ffi::CString;

use std::io::{Write, Read};
use tempfile::tempfile;
use libc::off_t;
use std::os::unix::prelude::*;



#[test]
fn test_fork_and_waitpid() {
let pid = fork();
Expand Down Expand Up @@ -112,6 +119,38 @@ macro_rules! execve_test_factory(
)
);

#[test]
fn test_lseek() {
const CONTENTS: &'static [u8] = b"abcdef123456";
let mut tmp = tempfile().unwrap();
tmp.write(CONTENTS).unwrap();

let offset: off_t = 5;
lseek(tmp.as_raw_fd(), offset, Whence::SeekSet).unwrap();

let mut buf = String::new();
tmp.read_to_string(&mut buf).unwrap();
assert_eq!(b"f123456", buf.as_bytes());

close(tmp.as_raw_fd()).unwrap();
}

#[cfg(any(target_os = "linux", target_os = "android"))]
#[test]
fn test_lseek64() {
const CONTENTS: &'static [u8] = b"abcdef123456";
let mut tmp = tempfile().unwrap();
tmp.write(CONTENTS).unwrap();

lseek64(tmp.as_raw_fd(), 5, Whence::SeekSet).unwrap();

let mut buf = String::new();
tmp.read_to_string(&mut buf).unwrap();
assert_eq!(b"f123456", buf.as_bytes());

close(tmp.as_raw_fd()).unwrap();
}

execve_test_factory!(test_execve, execve, b"/bin/sh", b"/system/bin/sh");

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

0 comments on commit 0440d26

Please sign in to comment.