Skip to content

Commit 34ed4e2

Browse files
committed
std: Add a file-renaming function to std::os
1 parent 8464ee0 commit 34ed4e2

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

src/libstd/os.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,6 +1002,18 @@ pub fn remove_file(p: &Path) -> bool {
10021002
}
10031003
}
10041004
1005+
/// Renames an existing file or directory
1006+
pub fn rename_file(old: &Path, new: &Path) -> bool {
1007+
#[fixed_stack_segment]; #[inline(never)];
1008+
unsafe {
1009+
do old.with_c_str |old_buf| {
1010+
do new.with_c_str |new_buf| {
1011+
libc::rename(old_buf, new_buf) == (0 as c_int)
1012+
}
1013+
}
1014+
}
1015+
}
1016+
10051017
#[cfg(unix)]
10061018
/// Returns the platform-specific value of errno
10071019
pub fn errno() -> int {

src/test/run-pass/rename-directory.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// This test can't be a unit test in std,
12+
// because it needs mkdtemp, which is in extra
13+
14+
// xfail-fast
15+
extern mod extra;
16+
17+
use extra::tempfile::mkdtemp;
18+
use std::os;
19+
use std::libc;
20+
use std::libc::*;
21+
22+
fn rename_directory() {
23+
#[fixed_stack_segment];
24+
unsafe {
25+
static U_RWX: i32 = (S_IRUSR | S_IWUSR | S_IXUSR) as i32;
26+
27+
let tmpdir = mkdtemp(&os::tmpdir(), "rename_directory").expect("rename_directory failed");
28+
let old_path = tmpdir.push_many(["foo", "bar", "baz"]);
29+
assert!(os::mkdir_recursive(&old_path, U_RWX));
30+
let test_file = &old_path.push("temp.txt");
31+
32+
/* Write the temp input file */
33+
let ostream = do test_file.to_str().with_c_str |fromp| {
34+
do "w+b".with_c_str |modebuf| {
35+
libc::fopen(fromp, modebuf)
36+
}
37+
};
38+
assert!((ostream as uint != 0u));
39+
let s = ~"hello";
40+
do "hello".with_c_str |buf| {
41+
let write_len = libc::fwrite(buf as *c_void,
42+
1u as size_t,
43+
(s.len() + 1u) as size_t,
44+
ostream);
45+
assert_eq!(write_len, (s.len() + 1) as size_t)
46+
}
47+
assert_eq!(libc::fclose(ostream), (0u as c_int));
48+
49+
let new_path = tmpdir.push_many(["quux", "blat"]);
50+
assert!(os::mkdir_recursive(&new_path, U_RWX));
51+
assert!(os::rename_file(&old_path, &new_path.push("newdir")));
52+
assert!(os::path_is_dir(&new_path.push("newdir")));
53+
assert!(os::path_exists(&new_path.push_many(["newdir", "temp.txt"])));
54+
}
55+
}
56+
57+
fn main() { rename_directory() }

0 commit comments

Comments
 (0)