From dabbac1d6c06c00f24148ffc58b42455496fdd3c Mon Sep 17 00:00:00 2001 From: Jeff Olson Date: Thu, 15 Aug 2013 23:54:54 -0700 Subject: std: working tests for low-level libuv open, write and close operations --- src/rt/rust_uv.cpp | 47 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 43 insertions(+), 4 deletions(-) (limited to 'src/rt/rust_uv.cpp') diff --git a/src/rt/rust_uv.cpp b/src/rt/rust_uv.cpp index b5d6e02b46a..a788b0f71a4 100644 --- a/src/rt/rust_uv.cpp +++ b/src/rt/rust_uv.cpp @@ -18,16 +18,13 @@ #include #endif +#include #include "uv.h" #include "rust_globals.h" extern "C" void* rust_uv_loop_new() { -// XXX libuv doesn't always ignore SIGPIPE even though we don't need it. -#ifndef __WIN32__ - signal(SIGPIPE, SIG_IGN); -#endif return (void*)uv_loop_new(); } @@ -517,3 +514,45 @@ extern "C" uintptr_t rust_uv_req_type_max() { return UV_REQ_TYPE_MAX; } + +extern "C" int +rust_uv_fs_open(uv_loop_t* loop, uv_fs_t* req, const char* path, int flags, + int mode, uv_fs_cb cb) { + return uv_fs_open(loop, req, path, flags, mode, cb); +} +extern "C" int +rust_uv_fs_close(uv_loop_t* loop, uv_fs_t* req, uv_file fd, uv_fs_cb cb) { + return uv_fs_close(loop, req, fd, cb); +} +extern "C" void +rust_uv_fs_req_cleanup(uv_fs_t* req) { + uv_fs_req_cleanup(req); +} +extern "C" int +rust_uv_get_O_RDONLY() { + return O_RDONLY; +} +extern "C" int +rust_uv_get_O_WRONLY() { + return O_WRONLY; +} +extern "C" int +rust_uv_get_O_RDWR() { + return O_RDWR; +} +extern "C" int +rust_uv_get_O_CREAT() { + return O_CREAT; +} +extern "C" int +rust_uv_get_O_TRUNC() { + return O_TRUNC; +} +extern "C" int +rust_uv_get_result_from_fs_req(uv_fs_t* req) { + return req->result; +} +extern "C" uv_loop_t* +rust_uv_get_loop_from_fs_req(uv_fs_t* req) { + return req->loop; +} -- cgit 1.4.1-3-g733a5 From c49c2921b0250fee51b935e3d164cc1bdb8a9445 Mon Sep 17 00:00:00 2001 From: Jeff Olson Date: Fri, 16 Aug 2013 17:47:35 -0700 Subject: std: add read and unlink to low-level FileDescriptor + end-to-end CRUD test --- src/libstd/rt/uv/file.rs | 159 +++++++++++++++++++++++++++++++---------------- src/libstd/rt/uv/uvll.rs | 13 ++++ src/rt/rust_uv.cpp | 35 +++++++++-- src/rt/rustrt.def.in | 2 + 4 files changed, 151 insertions(+), 58 deletions(-) (limited to 'src/rt/rust_uv.cpp') diff --git a/src/libstd/rt/uv/file.rs b/src/libstd/rt/uv/file.rs index 2a16a34070d..ef8c131688b 100644 --- a/src/libstd/rt/uv/file.rs +++ b/src/libstd/rt/uv/file.rs @@ -13,12 +13,14 @@ use ptr::null; use libc::c_void; use rt::uv::{Request, NativeHandle, Loop, FsCallback, Buf, status_to_maybe_uv_error_with_loop, - vec_to_uv_buf};//, vec_from_uv_buf}; + vec_to_uv_buf, vec_from_uv_buf}; use rt::uv::uvll; use rt::uv::uvll::*; use path::Path; use cast::transmute; use libc::{c_int}; +use option::{None, Some, Option}; +use vec; pub struct FsRequest(*uvll::uv_fs_t); impl Request for FsRequest; @@ -110,7 +112,15 @@ impl FsRequest { fn cleanup_and_delete(self) { unsafe { let data = uvll::get_data_for_req(self.native_handle()); - let _data = transmute::<*c_void, ~RequestData>(data); + let mut _data = transmute::<*c_void, ~RequestData>(data); + // if set we're going to convert the buf param back into + // a rust vec, as that's the mechanism by which the raw + // uv_buf_t's .base field gets freed. We immediately discard + // the result + if _data.buf.is_some() { + let buf = _data.buf.take_unwrap(); + vec_from_uv_buf(buf); + } uvll::set_data_for_req(self.native_handle(), null::<()>()); uvll::fs_req_cleanup(self.native_handle()); free_req(self.native_handle() as *c_void) @@ -146,6 +156,15 @@ impl FileDescriptor { }) } + pub fn unlink(loop_: Loop, path: Path, cb: FsCallback) -> int { + let req = FsRequest::new(Some(cb)); + path.to_str().to_c_str().with_ref(|p| unsafe { + uvll::fs_unlink(loop_.native_handle(), + req.native_handle(), p, complete_cb) as int + }) + } + + // as per bnoordhuis in #libuv: offset >= 0 uses prwrite instead of write pub fn write(&self, loop_: Loop, buf: ~[u8], offset: i64, cb: FsCallback) -> int { let mut req = FsRequest::new(Some(cb)); @@ -161,6 +180,31 @@ impl FileDescriptor { } } + // really contemplated having this just take a read_len param and have + // the buf live in the scope of this request.. but decided that exposing + // an unsafe mechanism that takes a buf_ptr and len would be much more + // flexible, but the caller is now in the position of managing that + // buf (with all of the sadface that this entails) + pub fn read(&self, loop_: Loop, buf_ptr: Option<*c_void>, len: uint, offset: i64, cb: FsCallback) + -> int { + let mut req = FsRequest::new(Some(cb)); + req.get_req_data().raw_fd = Some(self.native_handle()); + unsafe { + let buf_ptr = match buf_ptr { + Some(ptr) => ptr, + None => { + let buf = vec::from_elem(len, 0u8); + let buf = vec_to_uv_buf(buf); + req.get_req_data().buf = Some(buf); + buf.base as *c_void + } + }; + uvll::fs_read(loop_.native_handle(), req.native_handle(), + self.native_handle(), buf_ptr, + len, offset, complete_cb) as int + } + } + pub fn close(self, loop_: Loop, cb: FsCallback) -> int { let req = FsRequest::new(Some(cb)); unsafe { @@ -205,90 +249,99 @@ impl NativeHandle for FileDescriptor { mod test { use super::*; //use rt::test::*; + use libc::{STDOUT_FILENO}; + use str; use unstable::run_in_bare_thread; use path::Path; - use rt::uv::{Loop};//, slice_to_uv_buf}; - - // this is equiv to touch, i guess? - fn file_test_touch_impl() { - debug!("hello?") - do run_in_bare_thread { - debug!("In bare thread") - let mut loop_ = Loop::new(); - let flags = map_flag(O_RDWR) | - map_flag(O_CREAT); - // 0644 - let mode = map_mode(S_IWUSR) | - map_mode(S_IRUSR) | - map_mode(S_IRGRP) | - map_mode(S_IROTH); - do FileDescriptor::open(loop_, Path("./foo.txt"), flags, mode) - |req, uverr| { - let loop_ = req.get_loop(); - assert!(uverr.is_none()); - let fd = FileDescriptor::from_open_req(req); - do fd.close(loop_) |_, uverr| { - assert!(uverr.is_none()); - }; - }; - loop_.run(); - } - } - - #[test] - fn file_test_touch() { - file_test_touch_impl(); - } + use rt::uv::{Loop, vec_from_uv_buf};//, slice_to_uv_buf}; + use option::{None}; - fn file_test_tee_impl() { + fn file_test_full_simple_impl() { debug!("hello?") do run_in_bare_thread { debug!("In bare thread") let mut loop_ = Loop::new(); - let flags = map_flag(O_RDWR) | + let create_flags = map_flag(O_RDWR) | map_flag(O_CREAT); + let read_flags = map_flag(O_RDONLY); // 0644 let mode = map_mode(S_IWUSR) | map_mode(S_IRUSR) | map_mode(S_IRGRP) | map_mode(S_IROTH); - do FileDescriptor::open(loop_, Path("./file_tee_test.txt"), flags, mode) + let path_str = "./file_full_simple.txt"; + let write_val = "hello"; + do FileDescriptor::open(loop_, Path(path_str), create_flags, mode) |req, uverr| { let loop_ = req.get_loop(); assert!(uverr.is_none()); let fd = FileDescriptor::from_open_req(req); - let msg: ~[u8] = "hello world".as_bytes().to_owned(); + let msg: ~[u8] = write_val.as_bytes().to_owned(); let raw_fd = fd.native_handle(); do fd.write(loop_, msg, -1) |_, uverr| { let fd = FileDescriptor(raw_fd); - do fd.close(loop_) |_, _| { + do fd.close(loop_) |req, _| { + let loop_ = req.get_loop(); assert!(uverr.is_none()); + do FileDescriptor::open(loop_, Path(path_str), read_flags,0) + |req, uverr| { + assert!(uverr.is_none()); + let loop_ = req.get_loop(); + let len = 1028; + let fd = FileDescriptor::from_open_req(req); + let raw_fd = fd.native_handle(); + do fd.read(loop_, None, len, 0) |req, uverr| { + assert!(uverr.is_none()); + let loop_ = req.get_loop(); + // we know nread >=0 because uverr is none.. + let nread = req.get_result() as uint; + // nread == 0 would be EOF + if nread > 0 { + let buf = vec_from_uv_buf( + req.get_req_data().buf.take_unwrap()) + .take_unwrap(); + let read_str = str::from_bytes( + buf.slice(0, + nread)); + assert!(read_str == ~"hello"); + do FileDescriptor(raw_fd).close(loop_) |_,uverr| { + assert!(uverr.is_none()); + do FileDescriptor::unlink(loop_, Path(path_str)) + |_,uverr| { + assert!(uverr.is_none()); + }; + }; + } + }; + }; }; }; }; loop_.run(); + loop_.close(); } } #[test] - fn file_test_tee() { - file_test_tee_impl(); + fn file_test_full_simple() { + file_test_full_simple_impl(); } - fn naive_print(input: ~str) { - do run_in_bare_thread { - let mut loop_ = Loop::new(); - let stdout = FileDescriptor(1); - let msg = input.as_bytes().to_owned(); - do stdout.write(loop_, msg, -1) |_, uverr| { - assert!(uverr.is_none()); - }; - loop_.run(); - } + fn naive_print(loop_: Loop, input: ~str) { + let stdout = FileDescriptor(STDOUT_FILENO); + let msg = input.as_bytes().to_owned(); + do stdout.write(loop_, msg, -1) |_, uverr| { + assert!(uverr.is_none()); + }; } #[test] - fn file_test_println() { - naive_print(~"oh yeah.\n"); + fn file_test_write_to_stdout() { + do run_in_bare_thread { + let mut loop_ = Loop::new(); + naive_print(loop_, ~"zanzibar!\n"); + loop_.run(); + loop_.close(); + }; } } diff --git a/src/libstd/rt/uv/uvll.rs b/src/libstd/rt/uv/uvll.rs index 0fbf45fca97..1b257b708c4 100644 --- a/src/libstd/rt/uv/uvll.rs +++ b/src/libstd/rt/uv/uvll.rs @@ -621,10 +621,19 @@ pub unsafe fn fs_open(loop_ptr: *uv_loop_t, req: *uv_fs_t, path: *c_char, flags: cb: *u8) -> c_int { rust_uv_fs_open(loop_ptr, req, path, flags as c_int, mode as c_int, cb) } + +pub unsafe fn fs_unlink(loop_ptr: *uv_loop_t, req: *uv_fs_t, path: *c_char, + cb: *u8) -> c_int { + rust_uv_fs_unlink(loop_ptr, req, path, cb) +} pub unsafe fn fs_write(loop_ptr: *uv_loop_t, req: *uv_fs_t, fd: c_int, buf: *c_void, len: uint, offset: i64, cb: *u8) -> c_int { rust_uv_fs_write(loop_ptr, req, fd, buf, len as c_uint, offset, cb) } +pub unsafe fn fs_read(loop_ptr: *uv_loop_t, req: *uv_fs_t, fd: c_int, buf: *c_void, + len: uint, offset: i64, cb: *u8) -> c_int { + rust_uv_fs_read(loop_ptr, req, fd, buf, len as c_uint, offset, cb) +} pub unsafe fn fs_close(loop_ptr: *uv_loop_t, req: *uv_fs_t, fd: c_int, cb: *u8) -> c_int { rust_uv_fs_close(loop_ptr, req, fd, cb) @@ -817,8 +826,12 @@ extern { fn rust_uv_timer_stop(handle: *uv_timer_t) -> c_int; fn rust_uv_fs_open(loop_ptr: *c_void, req: *uv_fs_t, path: *c_char, flags: c_int, mode: c_int, cb: *u8) -> c_int; + fn rust_uv_fs_unlink(loop_ptr: *c_void, req: *uv_fs_t, path: *c_char, + cb: *u8) -> c_int; fn rust_uv_fs_write(loop_ptr: *c_void, req: *uv_fs_t, fd: c_int, buf: *c_void, len: c_uint, offset: i64, cb: *u8) -> c_int; + fn rust_uv_fs_read(loop_ptr: *c_void, req: *uv_fs_t, fd: c_int, + buf: *c_void, len: c_uint, offset: i64, cb: *u8) -> c_int; fn rust_uv_fs_close(loop_ptr: *c_void, req: *uv_fs_t, fd: c_int, cb: *u8) -> c_int; fn rust_uv_fs_req_cleanup(req: *uv_fs_t); diff --git a/src/rt/rust_uv.cpp b/src/rt/rust_uv.cpp index a788b0f71a4..e08b4ac21eb 100644 --- a/src/rt/rust_uv.cpp +++ b/src/rt/rust_uv.cpp @@ -13,11 +13,6 @@ #include #endif -#ifndef __WIN32__ -// for signal -#include -#endif - #include #include "uv.h" @@ -521,6 +516,20 @@ rust_uv_fs_open(uv_loop_t* loop, uv_fs_t* req, const char* path, int flags, return uv_fs_open(loop, req, path, flags, mode, cb); } extern "C" int +rust_uv_fs_unlink(uv_loop_t* loop, uv_fs_t* req, const char* path, uv_fs_cb cb) { + return uv_fs_unlink(loop, req, path, cb); +} +extern "C" int +rust_uv_fs_write(uv_loop_t* loop, uv_fs_t* req, uv_file fd, void* buf, + size_t len, int64_t offset, uv_fs_cb cb) { + return uv_fs_write(loop, req, fd, buf, len, offset, cb); +} +extern "C" int +rust_uv_fs_read(uv_loop_t* loop, uv_fs_t* req, uv_file fd, void* buf, + size_t len, int64_t offset, uv_fs_cb cb) { + return uv_fs_read(loop, req, fd, buf, len, offset, cb); +} +extern "C" int rust_uv_fs_close(uv_loop_t* loop, uv_fs_t* req, uv_file fd, uv_fs_cb cb) { return uv_fs_close(loop, req, fd, cb); } @@ -549,6 +558,22 @@ rust_uv_get_O_TRUNC() { return O_TRUNC; } extern "C" int +rust_uv_get_S_IWUSR() { + return S_IWUSR; +} +extern "C" int +rust_uv_get_S_IRUSR() { + return S_IRUSR; +} +extern "C" int +rust_uv_get_S_IRGRP() { + return S_IRGRP; +} +extern "C" int +rust_uv_get_S_IROTH() { + return S_IROTH; +} +extern "C" int rust_uv_get_result_from_fs_req(uv_fs_t* req) { return req->result; } diff --git a/src/rt/rustrt.def.in b/src/rt/rustrt.def.in index d342ffa194a..824a3931771 100644 --- a/src/rt/rustrt.def.in +++ b/src/rt/rustrt.def.in @@ -109,7 +109,9 @@ rust_uv_idle_init rust_uv_idle_start rust_uv_idle_stop rust_uv_fs_open +rust_uv_fs_unlink rust_uv_fs_write +rust_uv_fs_read rust_uv_fs_close rust_uv_get_O_RDONLY rust_uv_get_O_WRONLY -- cgit 1.4.1-3-g733a5 From f60bd75f4d8b776cf1e777b59d2d2262d03818a7 Mon Sep 17 00:00:00 2001 From: Jeff Olson Date: Mon, 19 Aug 2013 16:06:31 -0700 Subject: std: remove fcntl const bindings + making valgrind clean w/ no owned vecs --- src/libstd/rt/uv/file.rs | 192 +++++++++++++++++------------------------------ src/libstd/rt/uv/uvll.rs | 18 ----- src/rt/rust_uv.cpp | 36 --------- src/rt/rustrt.def.in | 9 --- 4 files changed, 68 insertions(+), 187 deletions(-) (limited to 'src/rt/rust_uv.cpp') diff --git a/src/libstd/rt/uv/file.rs b/src/libstd/rt/uv/file.rs index 35e425ce659..f3f6c325162 100644 --- a/src/libstd/rt/uv/file.rs +++ b/src/libstd/rt/uv/file.rs @@ -12,60 +12,19 @@ use prelude::*; use ptr::null; use libc::c_void; use rt::uv::{Request, NativeHandle, Loop, FsCallback, Buf, - status_to_maybe_uv_error_with_loop, - vec_to_uv_buf, vec_from_uv_buf}; + status_to_maybe_uv_error_with_loop}; use rt::uv::uvll; use rt::uv::uvll::*; use path::Path; use cast::transmute; use libc::{c_int}; use option::{None, Some, Option}; -use vec; pub struct FsRequest(*uvll::uv_fs_t); impl Request for FsRequest; -#[allow(non_camel_case_types)] -pub enum UvFileFlag { - O_RDONLY, - O_WRONLY, - O_RDWR, - O_CREAT, - O_TRUNC -} -// just want enough to get 0644 -#[allow(non_camel_case_types)] -pub enum UvFileMode { - S_IWUSR, - S_IRUSR, - S_IRGRP, - S_IROTH -} -pub fn map_flag(v: UvFileFlag) -> int { - unsafe { - match v { - O_RDONLY => uvll::get_O_RDONLY() as int, - O_WRONLY => uvll::get_O_WRONLY() as int, - O_RDWR => uvll::get_O_RDWR() as int, - O_CREAT => uvll::get_O_CREAT() as int, - O_TRUNC => uvll::get_O_TRUNC() as int - } - } -} -pub fn map_mode(v: UvFileMode) -> int { - unsafe { - match v { - S_IWUSR => uvll::get_S_IWUSR() as int, - S_IRUSR => uvll::get_S_IRUSR() as int, - S_IRGRP => uvll::get_S_IRGRP() as int, - S_IROTH => uvll::get_S_IROTH() as int - } - } -} - pub struct RequestData { complete_cb: Option, - buf: Option, raw_fd: Option } @@ -82,7 +41,6 @@ impl FsRequest { let fs_req = (self.native_handle()) as *uvll::uv_write_t; let data = ~RequestData { complete_cb: cb, - buf: None, raw_fd: None }; unsafe { @@ -112,15 +70,7 @@ impl FsRequest { fn cleanup_and_delete(self) { unsafe { let data = uvll::get_data_for_req(self.native_handle()); - let mut _data = transmute::<*c_void, ~RequestData>(data); - // if set we're going to convert the buf param back into - // a rust vec, as that's the mechanism by which the raw - // uv_buf_t's .base field gets freed. We immediately discard - // the result - if _data.buf.is_some() { - let buf = _data.buf.take_unwrap(); - vec_from_uv_buf(buf); - } + let _data = transmute::<*c_void, ~RequestData>(data); uvll::set_data_for_req(self.native_handle(), null::<()>()); uvll::fs_req_cleanup(self.native_handle()); free_req(self.native_handle() as *c_void) @@ -192,7 +142,7 @@ impl FileDescriptor { } // as per bnoordhuis in #libuv: offset >= 0 uses prwrite instead of write - fn write_common(&self, loop_: Loop, buf: ~[u8], offset: i64, cb: Option) + fn write_common(&self, loop_: Loop, buf: Buf, offset: i64, cb: Option) -> int { let complete_cb_ptr = match cb { Some(_) => compl_cb, @@ -200,10 +150,8 @@ impl FileDescriptor { }; let is_sync = cb.is_none(); let mut req = FsRequest::new(cb); - let len = buf.len(); - let buf = vec_to_uv_buf(buf); let base_ptr = buf.base as *c_void; - req.get_req_data().buf = Some(buf); + let len = buf.len as uint; req.get_req_data().raw_fd = Some(self.native_handle()); let result = unsafe { uvll::fs_write(loop_.native_handle(), req.native_handle(), @@ -213,17 +161,17 @@ impl FileDescriptor { if is_sync { req.cleanup_and_delete(); } result } - pub fn write(&self, loop_: Loop, buf: ~[u8], offset: i64, cb: FsCallback) + pub fn write(&self, loop_: Loop, buf: Buf, offset: i64, cb: FsCallback) -> int { self.write_common(loop_, buf, offset, Some(cb)) } - pub fn write_sync(&self, loop_: Loop, buf: ~[u8], offset: i64) + pub fn write_sync(&self, loop_: Loop, buf: Buf, offset: i64) -> int { self.write_common(loop_, buf, offset, None) } - fn read_common(&self, loop_: Loop, buf_ptr: Option<*c_void>, - len: uint, offset: i64, cb: Option) + fn read_common(&self, loop_: Loop, buf: Buf, + offset: i64, cb: Option) -> int { let complete_cb_ptr = match cb { Some(_) => compl_cb, @@ -232,31 +180,22 @@ impl FileDescriptor { let is_sync = cb.is_none(); let mut req = FsRequest::new(cb); req.get_req_data().raw_fd = Some(self.native_handle()); + let buf_ptr = buf.base as *c_void; let result = unsafe { - let buf_ptr = match buf_ptr { - Some(ptr) => ptr, - None => { - let buf = vec::from_elem(len, 0u8); - let buf = vec_to_uv_buf(buf); - req.get_req_data().buf = Some(buf); - buf.base as *c_void - } - }; uvll::fs_read(loop_.native_handle(), req.native_handle(), self.native_handle(), buf_ptr, - len, offset, complete_cb_ptr) as int + buf.len as uint, offset, complete_cb_ptr) as int }; if is_sync { req.cleanup_and_delete(); } result } - pub fn read(&self, loop_: Loop, buf_ptr: Option<*c_void>, - len: uint, offset: i64, cb: FsCallback) + pub fn read(&self, loop_: Loop, buf: Buf, offset: i64, cb: FsCallback) -> int { - self.read_common(loop_, buf_ptr, len, offset, Some(cb)) + self.read_common(loop_, buf, offset, Some(cb)) } - pub fn read_sync(&self, loop_: Loop, buf_ptr: Option<*c_void>, len: uint, offset: i64) + pub fn read_sync(&self, loop_: Loop, buf: Buf, offset: i64) -> int { - self.read_common(loop_, buf_ptr, len, offset, None) + self.read_common(loop_, buf, offset, None) } fn close_common(self, loop_: Loop, cb: Option) -> int { @@ -316,61 +255,67 @@ impl NativeHandle for FileDescriptor { mod test { use super::*; //use rt::test::*; - use option::{Some}; - use libc::{STDOUT_FILENO, c_void}; + use libc::{STDOUT_FILENO}; use vec; use str; use unstable::run_in_bare_thread; use path::Path; - use rt::uv::{Loop, vec_to_uv_buf, vec_from_uv_buf, + use rt::uv::{Loop, Buf, slice_to_uv_buf, status_to_maybe_uv_error_with_loop}; - use option::{None}; + use libc::{O_CREAT, O_RDWR, O_RDONLY, + S_IWUSR, S_IRUSR}; //NOTE: need defs for S_**GRP|S_**OTH in libc:: ... + //S_IRGRP, S_IROTH}; fn file_test_full_simple_impl() { do run_in_bare_thread { let mut loop_ = Loop::new(); - let create_flags = map_flag(O_RDWR) | - map_flag(O_CREAT); - let read_flags = map_flag(O_RDONLY); - // 0644 - let mode = map_mode(S_IWUSR) | - map_mode(S_IRUSR) | - map_mode(S_IRGRP) | - map_mode(S_IROTH); + let create_flags = O_RDWR | O_CREAT; + let read_flags = O_RDONLY; + // 0644 BZZT! WRONG! 0600! See below. + let mode = S_IWUSR |S_IRUSR; + // these aren't defined in std::libc :( + //map_mode(S_IRGRP) | + //map_mode(S_IROTH); let path_str = "./file_full_simple.txt"; - let write_val = "hello"; - do FileDescriptor::open(loop_, Path(path_str), create_flags, mode) + let write_val = "hello".as_bytes().to_owned(); + let write_buf = slice_to_uv_buf(write_val); + let write_buf_ptr: *Buf = &write_buf; + let read_buf_len = 1028; + let read_mem = vec::from_elem(read_buf_len, 0u8); + let read_buf = slice_to_uv_buf(read_mem); + let read_buf_ptr: *Buf = &read_buf; + do FileDescriptor::open(loop_, Path(path_str), create_flags as int, mode as int) |req, uverr| { let loop_ = req.get_loop(); assert!(uverr.is_none()); let fd = FileDescriptor::from_open_req(req); - let msg: ~[u8] = write_val.as_bytes().to_owned(); let raw_fd = fd.native_handle(); - do fd.write(loop_, msg, -1) |_, uverr| { + let buf = unsafe { *write_buf_ptr }; + do fd.write(loop_, buf, -1) |_, uverr| { let fd = FileDescriptor(raw_fd); do fd.close(loop_) |req, _| { let loop_ = req.get_loop(); assert!(uverr.is_none()); - do FileDescriptor::open(loop_, Path(path_str), read_flags,0) + do FileDescriptor::open(loop_, Path(path_str), read_flags as int,0) |req, uverr| { assert!(uverr.is_none()); let loop_ = req.get_loop(); - let len = 1028; let fd = FileDescriptor::from_open_req(req); let raw_fd = fd.native_handle(); - do fd.read(loop_, None, len, 0) |req, uverr| { + let read_buf = unsafe { *read_buf_ptr }; + do fd.read(loop_, read_buf, 0) |req, uverr| { assert!(uverr.is_none()); let loop_ = req.get_loop(); // we know nread >=0 because uverr is none.. let nread = req.get_result() as uint; // nread == 0 would be EOF if nread > 0 { - let buf = vec_from_uv_buf( - req.get_req_data().buf.take_unwrap()) - .take_unwrap(); - let read_str = str::from_bytes( - buf.slice(0, - nread)); + let read_str = unsafe { + let read_buf = *read_buf_ptr; + str::from_bytes( + vec::from_buf( + read_buf.base, nread)) + }; assert!(read_str == ~"hello"); do FileDescriptor(raw_fd).close(loop_) |_,uverr| { assert!(uverr.is_none()); @@ -393,24 +338,25 @@ mod test { do run_in_bare_thread { // setup let mut loop_ = Loop::new(); - let create_flags = map_flag(O_RDWR) | - map_flag(O_CREAT); - let read_flags = map_flag(O_RDONLY); + let create_flags = O_RDWR | + O_CREAT; + let read_flags = O_RDONLY; // 0644 - let mode = map_mode(S_IWUSR) | - map_mode(S_IRUSR) | - map_mode(S_IRGRP) | - map_mode(S_IROTH); + let mode = S_IWUSR | + S_IRUSR; + //S_IRGRP | + //S_IROTH; let path_str = "./file_full_simple_sync.txt"; - let write_val = "hello"; + let write_val = "hello".as_bytes().to_owned(); + let write_buf = slice_to_uv_buf(write_val); // open/create - let result = FileDescriptor::open_sync(loop_, Path(path_str), create_flags, mode); + let result = FileDescriptor::open_sync(loop_, Path(path_str), + create_flags as int, mode as int); assert!(status_to_maybe_uv_error_with_loop( loop_.native_handle(), result as i32).is_none()); let fd = FileDescriptor(result as i32); - let msg: ~[u8] = write_val.as_bytes().to_owned(); // write - let result = fd.write_sync(loop_, msg, -1); + let result = fd.write_sync(loop_, write_buf, -1); assert!(status_to_maybe_uv_error_with_loop( loop_.native_handle(), result as i32).is_none()); // close @@ -418,25 +364,24 @@ mod test { assert!(status_to_maybe_uv_error_with_loop( loop_.native_handle(), result as i32).is_none()); // re-open - let result = FileDescriptor::open_sync(loop_, Path(path_str), read_flags,0); + let result = FileDescriptor::open_sync(loop_, Path(path_str), + read_flags as int,0); assert!(status_to_maybe_uv_error_with_loop( loop_.native_handle(), result as i32).is_none()); let len = 1028; let fd = FileDescriptor(result as i32); // read - let buf: ~[u8] = vec::from_elem(len, 0u8); - let buf = vec_to_uv_buf(buf); - let buf_ptr = buf.base as *c_void; - let result = fd.read_sync(loop_, Some(buf_ptr), len, 0); + let read_mem: ~[u8] = vec::from_elem(len, 0u8); + let buf = slice_to_uv_buf(read_mem); + let result = fd.read_sync(loop_, buf, 0); assert!(status_to_maybe_uv_error_with_loop( loop_.native_handle(), result as i32).is_none()); let nread = result; // nread == 0 would be EOF.. we know it's >= zero because otherwise // the above assert would fail if nread > 0 { - let buf = vec_from_uv_buf(buf).take_unwrap(); let read_str = str::from_bytes( - buf.slice(0, nread as uint)); + read_mem.slice(0, nread as uint)); assert!(read_str == ~"hello"); // close let result = fd.close_sync(loop_); @@ -461,19 +406,18 @@ mod test { file_test_full_simple_impl_sync(); } - fn naive_print(loop_: Loop, input: ~str) { + fn naive_print(loop_: Loop, input: &str) { let stdout = FileDescriptor(STDOUT_FILENO); - let msg = input.as_bytes().to_owned(); - do stdout.write(loop_, msg, -1) |_, uverr| { - assert!(uverr.is_none()); - }; + let write_val = input.as_bytes(); + let write_buf = slice_to_uv_buf(write_val); + stdout.write_sync(loop_, write_buf, 0); } #[test] fn file_test_write_to_stdout() { do run_in_bare_thread { let mut loop_ = Loop::new(); - naive_print(loop_, ~"zanzibar!\n"); + naive_print(loop_, "zanzibar!\n"); loop_.run(); loop_.close(); }; diff --git a/src/libstd/rt/uv/uvll.rs b/src/libstd/rt/uv/uvll.rs index 1b257b708c4..caac418b733 100644 --- a/src/libstd/rt/uv/uvll.rs +++ b/src/libstd/rt/uv/uvll.rs @@ -643,15 +643,6 @@ pub unsafe fn fs_req_cleanup(req: *uv_fs_t) { } // data access helpers -pub unsafe fn get_O_RDONLY() -> c_int { rust_uv_get_O_RDONLY() } -pub unsafe fn get_O_WRONLY() -> c_int { rust_uv_get_O_WRONLY() } -pub unsafe fn get_O_RDWR() -> c_int { rust_uv_get_O_RDWR() } -pub unsafe fn get_O_CREAT() -> c_int { rust_uv_get_O_CREAT() } -pub unsafe fn get_O_TRUNC() -> c_int { rust_uv_get_O_TRUNC() } -pub unsafe fn get_S_IWUSR() -> c_int { rust_uv_get_S_IWUSR() } -pub unsafe fn get_S_IRUSR() -> c_int { rust_uv_get_S_IRUSR() } -pub unsafe fn get_S_IRGRP() -> c_int { rust_uv_get_S_IRGRP() } -pub unsafe fn get_S_IROTH() -> c_int { rust_uv_get_S_IROTH() } pub unsafe fn get_result_from_fs_req(req: *uv_fs_t) -> c_int { rust_uv_get_result_from_fs_req(req) } @@ -835,15 +826,6 @@ extern { fn rust_uv_fs_close(loop_ptr: *c_void, req: *uv_fs_t, fd: c_int, cb: *u8) -> c_int; fn rust_uv_fs_req_cleanup(req: *uv_fs_t); - fn rust_uv_get_O_RDONLY() -> c_int; - fn rust_uv_get_O_WRONLY() -> c_int; - fn rust_uv_get_O_RDWR() -> c_int; - fn rust_uv_get_O_CREAT() -> c_int; - fn rust_uv_get_O_TRUNC() -> c_int; - fn rust_uv_get_S_IWUSR() -> c_int; - fn rust_uv_get_S_IRUSR() -> c_int; - fn rust_uv_get_S_IRGRP() -> c_int; - fn rust_uv_get_S_IROTH() -> c_int; fn rust_uv_get_result_from_fs_req(req: *uv_fs_t) -> c_int; fn rust_uv_get_loop_from_fs_req(req: *uv_fs_t) -> *uv_loop_t; diff --git a/src/rt/rust_uv.cpp b/src/rt/rust_uv.cpp index e08b4ac21eb..7ee68ff5304 100644 --- a/src/rt/rust_uv.cpp +++ b/src/rt/rust_uv.cpp @@ -538,42 +538,6 @@ rust_uv_fs_req_cleanup(uv_fs_t* req) { uv_fs_req_cleanup(req); } extern "C" int -rust_uv_get_O_RDONLY() { - return O_RDONLY; -} -extern "C" int -rust_uv_get_O_WRONLY() { - return O_WRONLY; -} -extern "C" int -rust_uv_get_O_RDWR() { - return O_RDWR; -} -extern "C" int -rust_uv_get_O_CREAT() { - return O_CREAT; -} -extern "C" int -rust_uv_get_O_TRUNC() { - return O_TRUNC; -} -extern "C" int -rust_uv_get_S_IWUSR() { - return S_IWUSR; -} -extern "C" int -rust_uv_get_S_IRUSR() { - return S_IRUSR; -} -extern "C" int -rust_uv_get_S_IRGRP() { - return S_IRGRP; -} -extern "C" int -rust_uv_get_S_IROTH() { - return S_IROTH; -} -extern "C" int rust_uv_get_result_from_fs_req(uv_fs_t* req) { return req->result; } diff --git a/src/rt/rustrt.def.in b/src/rt/rustrt.def.in index 824a3931771..5100e732308 100644 --- a/src/rt/rustrt.def.in +++ b/src/rt/rustrt.def.in @@ -113,15 +113,6 @@ rust_uv_fs_unlink rust_uv_fs_write rust_uv_fs_read rust_uv_fs_close -rust_uv_get_O_RDONLY -rust_uv_get_O_WRONLY -rust_uv_get_O_RDWR -rust_uv_get_O_CREAT -rust_uv_get_O_TRUNC -rust_uv_get_S_IRUSR -rust_uv_get_S_IWUSR -rust_uv_get_S_IROTH -rust_uv_get_S_IRGRP rust_uv_get_result_from_fs_req rust_uv_get_loop_from_fs_req rust_uv_fs_req_cleanup -- cgit 1.4.1-3-g733a5 From a7ee85b50b8c4b58dfb8640692d3b63f8b604b28 Mon Sep 17 00:00:00 2001 From: Jeff Olson Date: Tue, 20 Aug 2013 22:00:49 -0700 Subject: std: stripping unneeded fcntl.h include from rust_uv.cpp --- src/rt/rust_uv.cpp | 1 - 1 file changed, 1 deletion(-) (limited to 'src/rt/rust_uv.cpp') diff --git a/src/rt/rust_uv.cpp b/src/rt/rust_uv.cpp index 7ee68ff5304..43e6b97bc18 100644 --- a/src/rt/rust_uv.cpp +++ b/src/rt/rust_uv.cpp @@ -13,7 +13,6 @@ #include #endif -#include #include "uv.h" #include "rust_globals.h" -- cgit 1.4.1-3-g733a5 From c0fba3c4acc52dba67461e485a3798cfa236761b Mon Sep 17 00:00:00 2001 From: Jeff Olson Date: Wed, 21 Aug 2013 15:13:01 -0700 Subject: rt: re-adding lines erroneous stripped out in merge conflict --- src/rt/rust_uv.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'src/rt/rust_uv.cpp') diff --git a/src/rt/rust_uv.cpp b/src/rt/rust_uv.cpp index 43e6b97bc18..f90931dbea9 100644 --- a/src/rt/rust_uv.cpp +++ b/src/rt/rust_uv.cpp @@ -13,12 +13,21 @@ #include #endif +#ifndef __WIN32__ +// for signal +#include +#endif + #include "uv.h" #include "rust_globals.h" extern "C" void* rust_uv_loop_new() { +// XXX libuv doesn't always ignore SIGPIPE even though we don't need it. +#ifndef __WIN32__ + signal(SIGPIPE, SIG_IGN); +#endif return (void*)uv_loop_new(); } -- cgit 1.4.1-3-g733a5 From 744c46225e09bad815b7807ca660cbdbcf187443 Mon Sep 17 00:00:00 2001 From: Jeff Olson Date: Thu, 22 Aug 2013 16:33:59 -0700 Subject: make check appeasement --- src/libstd/rt/uv/file.rs | 3 ++- src/rt/rust_uv.cpp | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) (limited to 'src/rt/rust_uv.cpp') diff --git a/src/libstd/rt/uv/file.rs b/src/libstd/rt/uv/file.rs index 1a20ae82f17..405dfe0a7f0 100644 --- a/src/libstd/rt/uv/file.rs +++ b/src/libstd/rt/uv/file.rs @@ -59,7 +59,8 @@ impl FsRequest { FsRequest::open_common(loop_, path, flags, mode, Some(cb)); } - pub fn open_sync(loop_: &Loop, path: &P, flags: int, mode: int) -> Result { + pub fn open_sync(loop_: &Loop, path: &P, flags: int, mode: int) + -> Result { let result = FsRequest::open_common(loop_, path, flags, mode, None); sync_cleanup(loop_, result) } diff --git a/src/rt/rust_uv.cpp b/src/rt/rust_uv.cpp index f90931dbea9..8ef4572f810 100644 --- a/src/rt/rust_uv.cpp +++ b/src/rt/rust_uv.cpp @@ -27,7 +27,7 @@ rust_uv_loop_new() { // XXX libuv doesn't always ignore SIGPIPE even though we don't need it. #ifndef __WIN32__ signal(SIGPIPE, SIG_IGN); -#endif +#endif return (void*)uv_loop_new(); } -- cgit 1.4.1-3-g733a5