提交 6311856b 编写于 作者: J Jeff Olson

std: slight refactor on UvFilestream seek behavior, pre-seek-refactor

上级 94b84a85
......@@ -170,15 +170,9 @@ fn tell(&self) -> u64 {
}
fn seek(&mut self, pos: i64, style: SeekStyle) {
use libc::{SEEK_SET, SEEK_CUR, SEEK_END};
let whence = match style {
SeekSet => SEEK_SET,
SeekCur => SEEK_CUR,
SeekEnd => SEEK_END
} as i64;
match self.fd.seek(pos, whence) {
match self.fd.seek(pos, style) {
Ok(_) => {
// successful seek resets EOF indocator
// successful seek resets EOF indicator
self.last_nread = -1;
()
},
......
......@@ -17,6 +17,7 @@
use rt::uv::uvio;
use path::Path;
use super::io::support::PathLike;
use super::io::{SeekStyle};
// XXX: ~object doesn't work currently so these are some placeholder
// types to use instead
......@@ -118,7 +119,7 @@ pub trait RtioFileStream {
fn write(&mut self, buf: &[u8]) -> Result<(), IoError>;
fn pread(&mut self, buf: &mut [u8], offset: u64) -> Result<int, IoError>;
fn pwrite(&mut self, buf: &[u8], offset: u64) -> Result<(), IoError>;
fn seek(&mut self, pos: i64, whence: i64) -> Result<(), IoError>;
fn seek(&mut self, pos: i64, whence: SeekStyle) -> Result<u64, IoError>;
fn tell(&self) -> Result<u64, IoError>;
fn flush(&mut self) -> Result<(), IoError>;
}
......@@ -21,7 +21,7 @@
use result::*;
use rt::io::IoError;
use rt::io::net::ip::{SocketAddr, IpAddr};
use rt::io::{standard_error, OtherIoError};
use rt::io::{standard_error, OtherIoError, SeekStyle, SeekSet, SeekCur, SeekEnd};
use rt::local::Local;
use rt::rtio::*;
use rt::sched::{Scheduler, SchedHandle};
......@@ -31,7 +31,7 @@
use rt::uv::net::{UvIpv4SocketAddr, UvIpv6SocketAddr};
use unstable::sync::Exclusive;
use super::super::io::support::PathLike;
use libc::{lseek, c_long, SEEK_CUR};
use libc::{lseek, c_long};
#[cfg(test)] use container::Container;
#[cfg(test)] use unstable::run_in_bare_thread;
......@@ -1122,6 +1122,22 @@ fn base_write(&mut self, buf: &[u8], offset: i64) -> Result<(), IoError> {
};
result_cell.take()
}
fn seek_common(&mut self, pos: i64, whence: c_int) ->
Result<u64, IoError>{
#[fixed_stack_segment]; #[inline(never)];
unsafe {
match lseek((*self.fd), pos as c_long, whence) {
-1 => {
Err(IoError {
kind: OtherIoError,
desc: "Failed to lseek.",
detail: None
})
},
n => Ok(n as u64)
}
}
}
}
impl Drop for UvFileStream {
......@@ -1155,35 +1171,20 @@ fn pread(&mut self, buf: &mut [u8], offset: u64) -> Result<int, IoError> {
fn pwrite(&mut self, buf: &[u8], offset: u64) -> Result<(), IoError> {
self.base_write(buf, offset as i64)
}
fn seek(&mut self, pos: i64, whence: i64) -> Result<(), IoError> {
#[fixed_stack_segment]; #[inline(never)];
unsafe {
match lseek((*self.fd), pos as c_long, whence as c_int) {
-1 => {
Err(IoError {
kind: OtherIoError,
desc: "Failed to lseek.",
detail: None
})
},
_ => Ok(())
}
}
fn seek(&mut self, pos: i64, whence: SeekStyle) -> Result<u64, IoError> {
use libc::{SEEK_SET, SEEK_CUR, SEEK_END};
let whence = match whence {
SeekSet => SEEK_SET,
SeekCur => SEEK_CUR,
SeekEnd => SEEK_END
};
self.seek_common(pos, whence)
}
fn tell(&self) -> Result<u64, IoError> {
#[fixed_stack_segment]; #[inline(never)];
unsafe {
match lseek((*self.fd), 0, SEEK_CUR) {
-1 => {
Err(IoError {
kind: OtherIoError,
desc: "Failed to lseek, needed to tell().",
detail: None
})
},
n=> Ok(n as u64)
}
}
use libc::SEEK_CUR;
// this is temporary
let self_ = unsafe { cast::transmute::<&UvFileStream, &mut UvFileStream>(self) };
self_.seek_common(0, SEEK_CUR)
}
fn flush(&mut self) -> Result<(), IoError> {
Ok(())
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册