1 // Copyright 2013-2014 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.
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.
11 // This test can't be a unit test in std,
12 // because it needs TempDir, which is in extra
16 use std::ffi::CString;
17 use std::old_io::TempDir;
18 use std::old_io::fs::PathExtensions;
23 fn rename_directory() {
25 static U_RWX: i32 = (libc::S_IRUSR | libc::S_IWUSR | libc::S_IXUSR) as i32;
27 let tmpdir = TempDir::new("rename_directory").ok().expect("rename_directory failed");
28 let tmpdir = tmpdir.path();
29 let old_path = tmpdir.join_many(&["foo", "bar", "baz"]);
30 fs::mkdir_recursive(&old_path, old_io::USER_RWX);
31 let test_file = &old_path.join("temp.txt");
33 /* Write the temp input file */
34 let fromp = CString::from_slice(test_file.as_vec());
35 let modebuf = CString::from_slice(b"w+b");
36 let ostream = libc::fopen(fromp.as_ptr(), modebuf.as_ptr());
37 assert!((ostream as uint != 0_usize));
38 let s = "hello".to_string();
39 let buf = CString::from_slice(b"hello");
40 let write_len = libc::fwrite(buf.as_ptr() as *mut _,
41 1_usize as libc::size_t,
42 (s.len() + 1_usize) as libc::size_t,
44 assert_eq!(write_len, (s.len() + 1) as libc::size_t);
45 assert_eq!(libc::fclose(ostream), (0_usize as libc::c_int));
47 let new_path = tmpdir.join_many(&["quux", "blat"]);
48 fs::mkdir_recursive(&new_path, old_io::USER_RWX);
49 fs::rename(&old_path, &new_path.join("newdir"));
50 assert!(new_path.join("newdir").is_dir());
51 assert!(new_path.join_many(&["newdir", "temp.txt"]).exists());
55 pub fn main() { rename_directory() }