Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added lseek and seek64 #377

Merged
merged 1 commit into from
Jun 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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