2014-02-07 20:08:32 +01:00
|
|
|
// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
|
2013-08-28 12:27:10 -07:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
// This test can't be a unit test in std,
|
2013-10-11 15:55:37 +02:00
|
|
|
// because it needs TempDir, which is in extra
|
2013-08-28 12:27:10 -07:00
|
|
|
|
2014-02-26 12:58:41 -05:00
|
|
|
extern crate libc;
|
|
|
|
|
2014-03-14 11:16:10 -07:00
|
|
|
use std::io::TempDir;
|
2014-09-10 22:26:41 -07:00
|
|
|
use std::io::fs::PathExtensions;
|
2013-11-10 22:46:32 -08:00
|
|
|
use std::io::fs;
|
2014-09-10 22:26:41 -07:00
|
|
|
use std::io;
|
|
|
|
use std::os;
|
2013-08-28 12:27:10 -07:00
|
|
|
|
|
|
|
fn rename_directory() {
|
|
|
|
unsafe {
|
2013-10-02 20:00:54 -07:00
|
|
|
static U_RWX: i32 = (libc::S_IRUSR | libc::S_IWUSR | libc::S_IXUSR) as i32;
|
2013-08-28 12:27:10 -07:00
|
|
|
|
2014-08-30 13:12:47 +01:00
|
|
|
let tmpdir = TempDir::new("rename_directory").ok().expect("rename_directory failed");
|
2013-10-11 15:55:37 +02:00
|
|
|
let tmpdir = tmpdir.path();
|
2013-10-05 19:49:32 -07:00
|
|
|
let old_path = tmpdir.join_many(["foo", "bar", "baz"]);
|
2014-10-06 16:08:46 +13:00
|
|
|
fs::mkdir_recursive(&old_path, io::USER_RWX);
|
2013-10-05 19:49:32 -07:00
|
|
|
let test_file = &old_path.join("temp.txt");
|
2013-08-28 12:27:10 -07:00
|
|
|
|
|
|
|
/* Write the temp input file */
|
2013-11-21 17:23:21 -08:00
|
|
|
let ostream = test_file.with_c_str(|fromp| {
|
|
|
|
"w+b".with_c_str(|modebuf| {
|
2013-08-28 12:27:10 -07:00
|
|
|
libc::fopen(fromp, modebuf)
|
2013-11-21 17:23:21 -08:00
|
|
|
})
|
|
|
|
});
|
2013-08-28 12:27:10 -07:00
|
|
|
assert!((ostream as uint != 0u));
|
2014-05-25 03:10:11 -07:00
|
|
|
let s = "hello".to_string();
|
2013-11-21 17:23:21 -08:00
|
|
|
"hello".with_c_str(|buf| {
|
2014-06-25 12:47:34 -07:00
|
|
|
let write_len = libc::fwrite(buf as *const libc::c_void,
|
2013-10-02 20:00:54 -07:00
|
|
|
1u as libc::size_t,
|
|
|
|
(s.len() + 1u) as libc::size_t,
|
2013-08-28 12:27:10 -07:00
|
|
|
ostream);
|
2013-10-02 20:00:54 -07:00
|
|
|
assert_eq!(write_len, (s.len() + 1) as libc::size_t)
|
2013-11-21 17:23:21 -08:00
|
|
|
});
|
2013-10-02 20:00:54 -07:00
|
|
|
assert_eq!(libc::fclose(ostream), (0u as libc::c_int));
|
2013-08-28 12:27:10 -07:00
|
|
|
|
2013-10-05 19:49:32 -07:00
|
|
|
let new_path = tmpdir.join_many(["quux", "blat"]);
|
2014-10-06 16:08:46 +13:00
|
|
|
fs::mkdir_recursive(&new_path, io::USER_RWX);
|
2013-10-31 15:15:30 -07:00
|
|
|
fs::rename(&old_path, &new_path.join("newdir"));
|
2013-10-25 17:04:37 -07:00
|
|
|
assert!(new_path.join("newdir").is_dir());
|
|
|
|
assert!(new_path.join_many(["newdir", "temp.txt"]).exists());
|
2013-08-28 12:27:10 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-03 15:30:54 -08:00
|
|
|
pub fn main() { rename_directory() }
|