Skip to content

Commit 7bf58c2

Browse files
committed
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.
1 parent d7b6502 commit 7bf58c2

File tree

5 files changed

+41
-4
lines changed

5 files changed

+41
-4
lines changed

src/librustuv/file.rs

+15
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,21 @@ impl FsRequest {
206206
assert_eq!(ret, 0);
207207
}
208208

209+
pub fn rename(self, loop_: &Loop, path: &CString, to: &CString, cb: FsCallback) {
210+
let complete_cb_ptr = {
211+
let mut me = self;
212+
me.req_boilerplate(Some(cb))
213+
};
214+
let ret = unsafe {
215+
uvll::fs_rename(loop_.native_handle(),
216+
self.native_handle(),
217+
path.with_ref(|p| p),
218+
to.with_ref(|p| p),
219+
complete_cb_ptr)
220+
};
221+
assert_eq!(ret, 0);
222+
}
223+
209224
pub fn readdir(self, loop_: &Loop, path: &CString,
210225
flags: c_int, cb: FsCallback) {
211226
let complete_cb_ptr = {

src/librustuv/uvio.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -699,10 +699,9 @@ impl IoFactory for UvIoFactory {
699699
assert!(!result_cell.is_empty());
700700
return result_cell.take();
701701
}
702-
fn fs_mkdir(&mut self, path: &CString) -> Result<(), IoError> {
703-
let mode = S_IRWXU as int;
702+
fn fs_mkdir(&mut self, path: &CString, mode: int) -> Result<(), IoError> {
704703
do uv_fs_helper(self.uv_loop(), path) |mkdir_req, l, p, cb| {
705-
do mkdir_req.mkdir(l, p, mode as int) |req, err| {
704+
do mkdir_req.mkdir(l, p, mode) |req, err| {
706705
cb(req, err)
707706
};
708707
}
@@ -714,6 +713,15 @@ impl IoFactory for UvIoFactory {
714713
};
715714
}
716715
}
716+
fn fs_rename(&mut self, path: &CString, to: &CString) -> Result<(), IoError> {
717+
let to = to.with_ref(|p| p);
718+
do uv_fs_helper(self.uv_loop(), path) |rename_req, l, p, cb| {
719+
let to = unsafe { CString::new(to, false) };
720+
do rename_req.rename(l, p, &to) |req, err| {
721+
cb(req, err)
722+
};
723+
}
724+
}
717725
fn fs_readdir(&mut self, path: &CString, flags: c_int) ->
718726
Result<~[Path], IoError> {
719727
use str::StrSlice;

src/librustuv/uvll.rs

+8
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,12 @@ pub unsafe fn fs_rmdir(loop_ptr: *uv_loop_t, req: *uv_fs_t, path: *c_char,
807807

808808
rust_uv_fs_rmdir(loop_ptr, req, path, cb)
809809
}
810+
pub unsafe fn fs_rename(loop_ptr: *uv_loop_t, req: *uv_fs_t, path: *c_char,
811+
to: *c_char, cb: *u8) -> c_int {
812+
#[fixed_stack_segment]; #[inline(never)];
813+
814+
rust_uv_fs_rename(loop_ptr, req, path, to, cb)
815+
}
810816
pub unsafe fn fs_readdir(loop_ptr: *uv_loop_t, req: *uv_fs_t, path: *c_char,
811817
flags: c_int, cb: *u8) -> c_int {
812818
#[fixed_stack_segment]; #[inline(never)];
@@ -1107,6 +1113,8 @@ extern {
11071113
mode: c_int, cb: *u8) -> c_int;
11081114
fn rust_uv_fs_rmdir(loop_ptr: *c_void, req: *uv_fs_t, path: *c_char,
11091115
cb: *u8) -> c_int;
1116+
fn rust_uv_fs_rename(loop_ptr: *c_void, req: *uv_fs_t, path: *c_char,
1117+
to: *c_char, cb: *u8) -> c_int;
11101118
fn rust_uv_fs_readdir(loop_ptr: *c_void, req: *uv_fs_t, path: *c_char,
11111119
flags: c_int, cb: *u8) -> c_int;
11121120
fn rust_uv_fs_req_cleanup(req: *uv_fs_t);

src/libstd/rt/rtio.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,9 @@ pub trait IoFactory {
102102
-> Result<~RtioFileStream, IoError>;
103103
fn fs_unlink(&mut self, path: &CString) -> Result<(), IoError>;
104104
fn fs_stat(&mut self, path: &CString) -> Result<FileStat, IoError>;
105-
fn fs_mkdir(&mut self, path: &CString) -> Result<(), IoError>;
105+
fn fs_mkdir(&mut self, path: &CString, mode: int) -> Result<(), IoError>;
106106
fn fs_rmdir(&mut self, path: &CString) -> Result<(), IoError>;
107+
fn fs_rename(&mut self, path: &CString, to: &CString) -> Result<(), IoError>;
107108
fn fs_readdir(&mut self, path: &CString, flags: c_int) ->
108109
Result<~[Path], IoError>;
109110
fn spawn(&mut self, config: ProcessConfig)

src/rt/rust_uv.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,11 @@ extern "C" int
592592
rust_uv_fs_readdir(uv_loop_t* loop, uv_fs_t* req, const char* path, int flags, uv_fs_cb cb) {
593593
return uv_fs_readdir(loop, req, path, flags, cb);
594594
}
595+
extern "C" int
596+
rust_uv_fs_rename(uv_loop_t *loop, uv_fs_t* req, const char *path,
597+
const char *to, uv_fs_cb cb) {
598+
return uv_fs_rename(loop, req, path, to, cb);
599+
}
595600

596601
extern "C" int
597602
rust_uv_spawn(uv_loop_t *loop, uv_process_t *p, uv_process_options_t options) {

0 commit comments

Comments
 (0)