From 7bf58c2baaac3f7cb3c8e8d735b27ac9e7d3cd78 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 25 Oct 2013 16:50:08 -0700 Subject: [PATCH] Modify IoFactory's fs_mkdir, and add fs_rename The invocation for making a directory should be able to specify a mode to make the directory with (instead of defaulting to one particular mode). Additionally, libuv and various OSes implement efficient versions of renaming files, so this operation is exposed as an IoFactory call. --- src/librustuv/file.rs | 15 +++++++++++++++ src/librustuv/uvio.rs | 14 +++++++++++--- src/librustuv/uvll.rs | 8 ++++++++ src/libstd/rt/rtio.rs | 3 ++- src/rt/rust_uv.cpp | 5 +++++ 5 files changed, 41 insertions(+), 4 deletions(-) diff --git a/src/librustuv/file.rs b/src/librustuv/file.rs index 575226f79028..2303721986c4 100644 --- a/src/librustuv/file.rs +++ b/src/librustuv/file.rs @@ -206,6 +206,21 @@ impl FsRequest { assert_eq!(ret, 0); } + pub fn rename(self, loop_: &Loop, path: &CString, to: &CString, cb: FsCallback) { + let complete_cb_ptr = { + let mut me = self; + me.req_boilerplate(Some(cb)) + }; + let ret = unsafe { + uvll::fs_rename(loop_.native_handle(), + self.native_handle(), + path.with_ref(|p| p), + to.with_ref(|p| p), + complete_cb_ptr) + }; + assert_eq!(ret, 0); + } + pub fn readdir(self, loop_: &Loop, path: &CString, flags: c_int, cb: FsCallback) { let complete_cb_ptr = { diff --git a/src/librustuv/uvio.rs b/src/librustuv/uvio.rs index c34d20ed4f50..7ecb51bb0d6e 100644 --- a/src/librustuv/uvio.rs +++ b/src/librustuv/uvio.rs @@ -699,10 +699,9 @@ impl IoFactory for UvIoFactory { assert!(!result_cell.is_empty()); return result_cell.take(); } - fn fs_mkdir(&mut self, path: &CString) -> Result<(), IoError> { - let mode = S_IRWXU as int; + fn fs_mkdir(&mut self, path: &CString, mode: int) -> Result<(), IoError> { do uv_fs_helper(self.uv_loop(), path) |mkdir_req, l, p, cb| { - do mkdir_req.mkdir(l, p, mode as int) |req, err| { + do mkdir_req.mkdir(l, p, mode) |req, err| { cb(req, err) }; } @@ -714,6 +713,15 @@ impl IoFactory for UvIoFactory { }; } } + fn fs_rename(&mut self, path: &CString, to: &CString) -> Result<(), IoError> { + let to = to.with_ref(|p| p); + do uv_fs_helper(self.uv_loop(), path) |rename_req, l, p, cb| { + let to = unsafe { CString::new(to, false) }; + do rename_req.rename(l, p, &to) |req, err| { + cb(req, err) + }; + } + } fn fs_readdir(&mut self, path: &CString, flags: c_int) -> Result<~[Path], IoError> { use str::StrSlice; diff --git a/src/librustuv/uvll.rs b/src/librustuv/uvll.rs index 9e86ab11286e..9f26f9506a08 100644 --- a/src/librustuv/uvll.rs +++ b/src/librustuv/uvll.rs @@ -807,6 +807,12 @@ pub unsafe fn fs_rmdir(loop_ptr: *uv_loop_t, req: *uv_fs_t, path: *c_char, rust_uv_fs_rmdir(loop_ptr, req, path, cb) } +pub unsafe fn fs_rename(loop_ptr: *uv_loop_t, req: *uv_fs_t, path: *c_char, + to: *c_char, cb: *u8) -> c_int { + #[fixed_stack_segment]; #[inline(never)]; + + rust_uv_fs_rename(loop_ptr, req, path, to, cb) +} pub unsafe fn fs_readdir(loop_ptr: *uv_loop_t, req: *uv_fs_t, path: *c_char, flags: c_int, cb: *u8) -> c_int { #[fixed_stack_segment]; #[inline(never)]; @@ -1107,6 +1113,8 @@ extern { mode: c_int, cb: *u8) -> c_int; fn rust_uv_fs_rmdir(loop_ptr: *c_void, req: *uv_fs_t, path: *c_char, cb: *u8) -> c_int; + fn rust_uv_fs_rename(loop_ptr: *c_void, req: *uv_fs_t, path: *c_char, + to: *c_char, cb: *u8) -> c_int; fn rust_uv_fs_readdir(loop_ptr: *c_void, req: *uv_fs_t, path: *c_char, flags: c_int, cb: *u8) -> c_int; fn rust_uv_fs_req_cleanup(req: *uv_fs_t); diff --git a/src/libstd/rt/rtio.rs b/src/libstd/rt/rtio.rs index 82ff8071896f..44d9f59c4106 100644 --- a/src/libstd/rt/rtio.rs +++ b/src/libstd/rt/rtio.rs @@ -102,8 +102,9 @@ pub trait IoFactory { -> Result<~RtioFileStream, IoError>; fn fs_unlink(&mut self, path: &CString) -> Result<(), IoError>; fn fs_stat(&mut self, path: &CString) -> Result; - fn fs_mkdir(&mut self, path: &CString) -> Result<(), IoError>; + fn fs_mkdir(&mut self, path: &CString, mode: int) -> Result<(), IoError>; fn fs_rmdir(&mut self, path: &CString) -> Result<(), IoError>; + fn fs_rename(&mut self, path: &CString, to: &CString) -> Result<(), IoError>; fn fs_readdir(&mut self, path: &CString, flags: c_int) -> Result<~[Path], IoError>; fn spawn(&mut self, config: ProcessConfig) diff --git a/src/rt/rust_uv.cpp b/src/rt/rust_uv.cpp index c59dacab8899..70602100f2e0 100644 --- a/src/rt/rust_uv.cpp +++ b/src/rt/rust_uv.cpp @@ -592,6 +592,11 @@ extern "C" int rust_uv_fs_readdir(uv_loop_t* loop, uv_fs_t* req, const char* path, int flags, uv_fs_cb cb) { return uv_fs_readdir(loop, req, path, flags, cb); } +extern "C" int +rust_uv_fs_rename(uv_loop_t *loop, uv_fs_t* req, const char *path, + const char *to, uv_fs_cb cb) { + return uv_fs_rename(loop, req, path, to, cb); +} extern "C" int rust_uv_spawn(uv_loop_t *loop, uv_process_t *p, uv_process_options_t options) {