Skip to content

Commit 6ef5829

Browse files
committed
---
yaml --- r: 144271 b: refs/heads/try2 c: f60bd75 h: refs/heads/master i: 144269: 0fcf43d 144267: 3360991 144263: d6da62f 144255: 8945699 v: v3
1 parent 533f979 commit 6ef5829

File tree

5 files changed

+69
-188
lines changed

5 files changed

+69
-188
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/snap-stage3: 78a7676898d9f80ab540c6df5d4c9ce35bb50463
55
refs/heads/try: 519addf6277dbafccbb4159db4b710c37eaa2ec5
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8-
refs/heads/try2: e0a80ee332dc347d588bfcea66c11896c04263bb
8+
refs/heads/try2: f60bd75f4d8b776cf1e777b59d2d2262d03818a7
99
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/try2/src/libstd/rt/uv/file.rs

Lines changed: 68 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -12,60 +12,19 @@ use prelude::*;
1212
use ptr::null;
1313
use libc::c_void;
1414
use rt::uv::{Request, NativeHandle, Loop, FsCallback, Buf,
15-
status_to_maybe_uv_error_with_loop,
16-
vec_to_uv_buf, vec_from_uv_buf};
15+
status_to_maybe_uv_error_with_loop};
1716
use rt::uv::uvll;
1817
use rt::uv::uvll::*;
1918
use path::Path;
2019
use cast::transmute;
2120
use libc::{c_int};
2221
use option::{None, Some, Option};
23-
use vec;
2422

2523
pub struct FsRequest(*uvll::uv_fs_t);
2624
impl Request for FsRequest;
2725

28-
#[allow(non_camel_case_types)]
29-
pub enum UvFileFlag {
30-
O_RDONLY,
31-
O_WRONLY,
32-
O_RDWR,
33-
O_CREAT,
34-
O_TRUNC
35-
}
36-
// just want enough to get 0644
37-
#[allow(non_camel_case_types)]
38-
pub enum UvFileMode {
39-
S_IWUSR,
40-
S_IRUSR,
41-
S_IRGRP,
42-
S_IROTH
43-
}
44-
pub fn map_flag(v: UvFileFlag) -> int {
45-
unsafe {
46-
match v {
47-
O_RDONLY => uvll::get_O_RDONLY() as int,
48-
O_WRONLY => uvll::get_O_WRONLY() as int,
49-
O_RDWR => uvll::get_O_RDWR() as int,
50-
O_CREAT => uvll::get_O_CREAT() as int,
51-
O_TRUNC => uvll::get_O_TRUNC() as int
52-
}
53-
}
54-
}
55-
pub fn map_mode(v: UvFileMode) -> int {
56-
unsafe {
57-
match v {
58-
S_IWUSR => uvll::get_S_IWUSR() as int,
59-
S_IRUSR => uvll::get_S_IRUSR() as int,
60-
S_IRGRP => uvll::get_S_IRGRP() as int,
61-
S_IROTH => uvll::get_S_IROTH() as int
62-
}
63-
}
64-
}
65-
6626
pub struct RequestData {
6727
complete_cb: Option<FsCallback>,
68-
buf: Option<Buf>,
6928
raw_fd: Option<c_int>
7029
}
7130

@@ -82,7 +41,6 @@ impl FsRequest {
8241
let fs_req = (self.native_handle()) as *uvll::uv_write_t;
8342
let data = ~RequestData {
8443
complete_cb: cb,
85-
buf: None,
8644
raw_fd: None
8745
};
8846
unsafe {
@@ -112,15 +70,7 @@ impl FsRequest {
11270
fn cleanup_and_delete(self) {
11371
unsafe {
11472
let data = uvll::get_data_for_req(self.native_handle());
115-
let mut _data = transmute::<*c_void, ~RequestData>(data);
116-
// if set we're going to convert the buf param back into
117-
// a rust vec, as that's the mechanism by which the raw
118-
// uv_buf_t's .base field gets freed. We immediately discard
119-
// the result
120-
if _data.buf.is_some() {
121-
let buf = _data.buf.take_unwrap();
122-
vec_from_uv_buf(buf);
123-
}
73+
let _data = transmute::<*c_void, ~RequestData>(data);
12474
uvll::set_data_for_req(self.native_handle(), null::<()>());
12575
uvll::fs_req_cleanup(self.native_handle());
12676
free_req(self.native_handle() as *c_void)
@@ -192,18 +142,16 @@ impl FileDescriptor {
192142
}
193143

194144
// as per bnoordhuis in #libuv: offset >= 0 uses prwrite instead of write
195-
fn write_common(&self, loop_: Loop, buf: ~[u8], offset: i64, cb: Option<FsCallback>)
145+
fn write_common(&self, loop_: Loop, buf: Buf, offset: i64, cb: Option<FsCallback>)
196146
-> int {
197147
let complete_cb_ptr = match cb {
198148
Some(_) => compl_cb,
199149
None => 0 as *u8
200150
};
201151
let is_sync = cb.is_none();
202152
let mut req = FsRequest::new(cb);
203-
let len = buf.len();
204-
let buf = vec_to_uv_buf(buf);
205153
let base_ptr = buf.base as *c_void;
206-
req.get_req_data().buf = Some(buf);
154+
let len = buf.len as uint;
207155
req.get_req_data().raw_fd = Some(self.native_handle());
208156
let result = unsafe {
209157
uvll::fs_write(loop_.native_handle(), req.native_handle(),
@@ -213,17 +161,17 @@ impl FileDescriptor {
213161
if is_sync { req.cleanup_and_delete(); }
214162
result
215163
}
216-
pub fn write(&self, loop_: Loop, buf: ~[u8], offset: i64, cb: FsCallback)
164+
pub fn write(&self, loop_: Loop, buf: Buf, offset: i64, cb: FsCallback)
217165
-> int {
218166
self.write_common(loop_, buf, offset, Some(cb))
219167
}
220-
pub fn write_sync(&self, loop_: Loop, buf: ~[u8], offset: i64)
168+
pub fn write_sync(&self, loop_: Loop, buf: Buf, offset: i64)
221169
-> int {
222170
self.write_common(loop_, buf, offset, None)
223171
}
224172

225-
fn read_common(&self, loop_: Loop, buf_ptr: Option<*c_void>,
226-
len: uint, offset: i64, cb: Option<FsCallback>)
173+
fn read_common(&self, loop_: Loop, buf: Buf,
174+
offset: i64, cb: Option<FsCallback>)
227175
-> int {
228176
let complete_cb_ptr = match cb {
229177
Some(_) => compl_cb,
@@ -232,31 +180,22 @@ impl FileDescriptor {
232180
let is_sync = cb.is_none();
233181
let mut req = FsRequest::new(cb);
234182
req.get_req_data().raw_fd = Some(self.native_handle());
183+
let buf_ptr = buf.base as *c_void;
235184
let result = unsafe {
236-
let buf_ptr = match buf_ptr {
237-
Some(ptr) => ptr,
238-
None => {
239-
let buf = vec::from_elem(len, 0u8);
240-
let buf = vec_to_uv_buf(buf);
241-
req.get_req_data().buf = Some(buf);
242-
buf.base as *c_void
243-
}
244-
};
245185
uvll::fs_read(loop_.native_handle(), req.native_handle(),
246186
self.native_handle(), buf_ptr,
247-
len, offset, complete_cb_ptr) as int
187+
buf.len as uint, offset, complete_cb_ptr) as int
248188
};
249189
if is_sync { req.cleanup_and_delete(); }
250190
result
251191
}
252-
pub fn read(&self, loop_: Loop, buf_ptr: Option<*c_void>,
253-
len: uint, offset: i64, cb: FsCallback)
192+
pub fn read(&self, loop_: Loop, buf: Buf, offset: i64, cb: FsCallback)
254193
-> int {
255-
self.read_common(loop_, buf_ptr, len, offset, Some(cb))
194+
self.read_common(loop_, buf, offset, Some(cb))
256195
}
257-
pub fn read_sync(&self, loop_: Loop, buf_ptr: Option<*c_void>, len: uint, offset: i64)
196+
pub fn read_sync(&self, loop_: Loop, buf: Buf, offset: i64)
258197
-> int {
259-
self.read_common(loop_, buf_ptr, len, offset, None)
198+
self.read_common(loop_, buf, offset, None)
260199
}
261200

262201
fn close_common(self, loop_: Loop, cb: Option<FsCallback>) -> int {
@@ -316,61 +255,67 @@ impl NativeHandle<c_int> for FileDescriptor {
316255
mod test {
317256
use super::*;
318257
//use rt::test::*;
319-
use option::{Some};
320-
use libc::{STDOUT_FILENO, c_void};
258+
use libc::{STDOUT_FILENO};
321259
use vec;
322260
use str;
323261
use unstable::run_in_bare_thread;
324262
use path::Path;
325-
use rt::uv::{Loop, vec_to_uv_buf, vec_from_uv_buf,
263+
use rt::uv::{Loop, Buf, slice_to_uv_buf,
326264
status_to_maybe_uv_error_with_loop};
327-
use option::{None};
265+
use libc::{O_CREAT, O_RDWR, O_RDONLY,
266+
S_IWUSR, S_IRUSR}; //NOTE: need defs for S_**GRP|S_**OTH in libc:: ...
267+
//S_IRGRP, S_IROTH};
328268

329269
fn file_test_full_simple_impl() {
330270
do run_in_bare_thread {
331271
let mut loop_ = Loop::new();
332-
let create_flags = map_flag(O_RDWR) |
333-
map_flag(O_CREAT);
334-
let read_flags = map_flag(O_RDONLY);
335-
// 0644
336-
let mode = map_mode(S_IWUSR) |
337-
map_mode(S_IRUSR) |
338-
map_mode(S_IRGRP) |
339-
map_mode(S_IROTH);
272+
let create_flags = O_RDWR | O_CREAT;
273+
let read_flags = O_RDONLY;
274+
// 0644 BZZT! WRONG! 0600! See below.
275+
let mode = S_IWUSR |S_IRUSR;
276+
// these aren't defined in std::libc :(
277+
//map_mode(S_IRGRP) |
278+
//map_mode(S_IROTH);
340279
let path_str = "./file_full_simple.txt";
341-
let write_val = "hello";
342-
do FileDescriptor::open(loop_, Path(path_str), create_flags, mode)
280+
let write_val = "hello".as_bytes().to_owned();
281+
let write_buf = slice_to_uv_buf(write_val);
282+
let write_buf_ptr: *Buf = &write_buf;
283+
let read_buf_len = 1028;
284+
let read_mem = vec::from_elem(read_buf_len, 0u8);
285+
let read_buf = slice_to_uv_buf(read_mem);
286+
let read_buf_ptr: *Buf = &read_buf;
287+
do FileDescriptor::open(loop_, Path(path_str), create_flags as int, mode as int)
343288
|req, uverr| {
344289
let loop_ = req.get_loop();
345290
assert!(uverr.is_none());
346291
let fd = FileDescriptor::from_open_req(req);
347-
let msg: ~[u8] = write_val.as_bytes().to_owned();
348292
let raw_fd = fd.native_handle();
349-
do fd.write(loop_, msg, -1) |_, uverr| {
293+
let buf = unsafe { *write_buf_ptr };
294+
do fd.write(loop_, buf, -1) |_, uverr| {
350295
let fd = FileDescriptor(raw_fd);
351296
do fd.close(loop_) |req, _| {
352297
let loop_ = req.get_loop();
353298
assert!(uverr.is_none());
354-
do FileDescriptor::open(loop_, Path(path_str), read_flags,0)
299+
do FileDescriptor::open(loop_, Path(path_str), read_flags as int,0)
355300
|req, uverr| {
356301
assert!(uverr.is_none());
357302
let loop_ = req.get_loop();
358-
let len = 1028;
359303
let fd = FileDescriptor::from_open_req(req);
360304
let raw_fd = fd.native_handle();
361-
do fd.read(loop_, None, len, 0) |req, uverr| {
305+
let read_buf = unsafe { *read_buf_ptr };
306+
do fd.read(loop_, read_buf, 0) |req, uverr| {
362307
assert!(uverr.is_none());
363308
let loop_ = req.get_loop();
364309
// we know nread >=0 because uverr is none..
365310
let nread = req.get_result() as uint;
366311
// nread == 0 would be EOF
367312
if nread > 0 {
368-
let buf = vec_from_uv_buf(
369-
req.get_req_data().buf.take_unwrap())
370-
.take_unwrap();
371-
let read_str = str::from_bytes(
372-
buf.slice(0,
373-
nread));
313+
let read_str = unsafe {
314+
let read_buf = *read_buf_ptr;
315+
str::from_bytes(
316+
vec::from_buf(
317+
read_buf.base, nread))
318+
};
374319
assert!(read_str == ~"hello");
375320
do FileDescriptor(raw_fd).close(loop_) |_,uverr| {
376321
assert!(uverr.is_none());
@@ -393,50 +338,50 @@ mod test {
393338
do run_in_bare_thread {
394339
// setup
395340
let mut loop_ = Loop::new();
396-
let create_flags = map_flag(O_RDWR) |
397-
map_flag(O_CREAT);
398-
let read_flags = map_flag(O_RDONLY);
341+
let create_flags = O_RDWR |
342+
O_CREAT;
343+
let read_flags = O_RDONLY;
399344
// 0644
400-
let mode = map_mode(S_IWUSR) |
401-
map_mode(S_IRUSR) |
402-
map_mode(S_IRGRP) |
403-
map_mode(S_IROTH);
345+
let mode = S_IWUSR |
346+
S_IRUSR;
347+
//S_IRGRP |
348+
//S_IROTH;
404349
let path_str = "./file_full_simple_sync.txt";
405-
let write_val = "hello";
350+
let write_val = "hello".as_bytes().to_owned();
351+
let write_buf = slice_to_uv_buf(write_val);
406352
// open/create
407-
let result = FileDescriptor::open_sync(loop_, Path(path_str), create_flags, mode);
353+
let result = FileDescriptor::open_sync(loop_, Path(path_str),
354+
create_flags as int, mode as int);
408355
assert!(status_to_maybe_uv_error_with_loop(
409356
loop_.native_handle(), result as i32).is_none());
410357
let fd = FileDescriptor(result as i32);
411-
let msg: ~[u8] = write_val.as_bytes().to_owned();
412358
// write
413-
let result = fd.write_sync(loop_, msg, -1);
359+
let result = fd.write_sync(loop_, write_buf, -1);
414360
assert!(status_to_maybe_uv_error_with_loop(
415361
loop_.native_handle(), result as i32).is_none());
416362
// close
417363
let result = fd.close_sync(loop_);
418364
assert!(status_to_maybe_uv_error_with_loop(
419365
loop_.native_handle(), result as i32).is_none());
420366
// re-open
421-
let result = FileDescriptor::open_sync(loop_, Path(path_str), read_flags,0);
367+
let result = FileDescriptor::open_sync(loop_, Path(path_str),
368+
read_flags as int,0);
422369
assert!(status_to_maybe_uv_error_with_loop(
423370
loop_.native_handle(), result as i32).is_none());
424371
let len = 1028;
425372
let fd = FileDescriptor(result as i32);
426373
// read
427-
let buf: ~[u8] = vec::from_elem(len, 0u8);
428-
let buf = vec_to_uv_buf(buf);
429-
let buf_ptr = buf.base as *c_void;
430-
let result = fd.read_sync(loop_, Some(buf_ptr), len, 0);
374+
let read_mem: ~[u8] = vec::from_elem(len, 0u8);
375+
let buf = slice_to_uv_buf(read_mem);
376+
let result = fd.read_sync(loop_, buf, 0);
431377
assert!(status_to_maybe_uv_error_with_loop(
432378
loop_.native_handle(), result as i32).is_none());
433379
let nread = result;
434380
// nread == 0 would be EOF.. we know it's >= zero because otherwise
435381
// the above assert would fail
436382
if nread > 0 {
437-
let buf = vec_from_uv_buf(buf).take_unwrap();
438383
let read_str = str::from_bytes(
439-
buf.slice(0, nread as uint));
384+
read_mem.slice(0, nread as uint));
440385
assert!(read_str == ~"hello");
441386
// close
442387
let result = fd.close_sync(loop_);
@@ -461,19 +406,18 @@ mod test {
461406
file_test_full_simple_impl_sync();
462407
}
463408
464-
fn naive_print(loop_: Loop, input: ~str) {
409+
fn naive_print(loop_: Loop, input: &str) {
465410
let stdout = FileDescriptor(STDOUT_FILENO);
466-
let msg = input.as_bytes().to_owned();
467-
do stdout.write(loop_, msg, -1) |_, uverr| {
468-
assert!(uverr.is_none());
469-
};
411+
let write_val = input.as_bytes();
412+
let write_buf = slice_to_uv_buf(write_val);
413+
stdout.write_sync(loop_, write_buf, 0);
470414
}
471415
472416
#[test]
473417
fn file_test_write_to_stdout() {
474418
do run_in_bare_thread {
475419
let mut loop_ = Loop::new();
476-
naive_print(loop_, ~"zanzibar!\n");
420+
naive_print(loop_, "zanzibar!\n");
477421
loop_.run();
478422
loop_.close();
479423
};

branches/try2/src/libstd/rt/uv/uvll.rs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -643,15 +643,6 @@ pub unsafe fn fs_req_cleanup(req: *uv_fs_t) {
643643
}
644644

645645
// data access helpers
646-
pub unsafe fn get_O_RDONLY() -> c_int { rust_uv_get_O_RDONLY() }
647-
pub unsafe fn get_O_WRONLY() -> c_int { rust_uv_get_O_WRONLY() }
648-
pub unsafe fn get_O_RDWR() -> c_int { rust_uv_get_O_RDWR() }
649-
pub unsafe fn get_O_CREAT() -> c_int { rust_uv_get_O_CREAT() }
650-
pub unsafe fn get_O_TRUNC() -> c_int { rust_uv_get_O_TRUNC() }
651-
pub unsafe fn get_S_IWUSR() -> c_int { rust_uv_get_S_IWUSR() }
652-
pub unsafe fn get_S_IRUSR() -> c_int { rust_uv_get_S_IRUSR() }
653-
pub unsafe fn get_S_IRGRP() -> c_int { rust_uv_get_S_IRGRP() }
654-
pub unsafe fn get_S_IROTH() -> c_int { rust_uv_get_S_IROTH() }
655646
pub unsafe fn get_result_from_fs_req(req: *uv_fs_t) -> c_int {
656647
rust_uv_get_result_from_fs_req(req)
657648
}
@@ -835,15 +826,6 @@ extern {
835826
fn rust_uv_fs_close(loop_ptr: *c_void, req: *uv_fs_t, fd: c_int,
836827
cb: *u8) -> c_int;
837828
fn rust_uv_fs_req_cleanup(req: *uv_fs_t);
838-
fn rust_uv_get_O_RDONLY() -> c_int;
839-
fn rust_uv_get_O_WRONLY() -> c_int;
840-
fn rust_uv_get_O_RDWR() -> c_int;
841-
fn rust_uv_get_O_CREAT() -> c_int;
842-
fn rust_uv_get_O_TRUNC() -> c_int;
843-
fn rust_uv_get_S_IWUSR() -> c_int;
844-
fn rust_uv_get_S_IRUSR() -> c_int;
845-
fn rust_uv_get_S_IRGRP() -> c_int;
846-
fn rust_uv_get_S_IROTH() -> c_int;
847829
fn rust_uv_get_result_from_fs_req(req: *uv_fs_t) -> c_int;
848830
fn rust_uv_get_loop_from_fs_req(req: *uv_fs_t) -> *uv_loop_t;
849831

0 commit comments

Comments
 (0)