|
| 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