2013-01-16 04:28:39 -06:00
|
|
|
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 18:48:01 -06: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.
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
//! Temporary files and directories
|
2011-11-08 22:35:15 -06:00
|
|
|
|
2013-05-17 17:28:44 -05:00
|
|
|
|
2013-06-28 17:32:26 -05:00
|
|
|
use std::os;
|
|
|
|
use std::rand::RngUtil;
|
|
|
|
use std::rand;
|
2011-11-08 22:35:15 -06:00
|
|
|
|
2013-05-28 22:11:41 -05:00
|
|
|
/// Attempts to make a temporary directory inside of `tmpdir` whose name will
|
|
|
|
/// have the suffix `suffix`. If no directory can be created, None is returned.
|
2012-09-27 20:03:00 -05:00
|
|
|
pub fn mkdtemp(tmpdir: &Path, suffix: &str) -> Option<Path> {
|
2013-05-03 01:09:50 -05:00
|
|
|
let mut r = rand::rng();
|
2013-01-16 04:28:39 -06:00
|
|
|
for 1000.times {
|
|
|
|
let p = tmpdir.push(r.gen_str(16) + suffix);
|
|
|
|
if os::make_dir(&p, 0x1c0) { // 700
|
2012-08-20 14:23:37 -05:00
|
|
|
return Some(p);
|
2011-11-08 22:35:15 -06:00
|
|
|
}
|
|
|
|
}
|
2013-01-16 04:28:39 -06:00
|
|
|
None
|
2011-11-08 22:35:15 -06:00
|
|
|
}
|
2012-01-17 21:05:07 -06:00
|
|
|
|
2013-04-28 23:25:35 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2013-05-21 19:24:31 -05:00
|
|
|
|
2013-04-28 23:25:35 -05:00
|
|
|
use tempfile::mkdtemp;
|
2013-05-24 21:35:29 -05:00
|
|
|
|
2013-06-28 17:32:26 -05:00
|
|
|
use std::os;
|
2013-05-02 16:14:25 -05:00
|
|
|
|
2013-04-28 23:25:35 -05:00
|
|
|
#[test]
|
|
|
|
fn test_mkdtemp() {
|
|
|
|
let p = mkdtemp(&Path("."), "foobar").unwrap();
|
|
|
|
os::remove_dir(&p);
|
2013-06-10 10:03:16 -05:00
|
|
|
assert!(p.to_str().ends_with("foobar"));
|
2013-04-28 23:25:35 -05:00
|
|
|
}
|
|
|
|
|
2013-06-28 17:32:26 -05:00
|
|
|
// Ideally these would be in std::os but then core would need
|
2013-04-28 23:25:35 -05:00
|
|
|
// to depend on std
|
|
|
|
#[test]
|
|
|
|
fn recursive_mkdir_rel() {
|
2013-06-28 17:32:26 -05:00
|
|
|
use std::libc::consts::os::posix88::{S_IRUSR, S_IWUSR, S_IXUSR};
|
|
|
|
use std::os;
|
2013-04-28 23:25:35 -05:00
|
|
|
|
2013-04-30 19:58:24 -05:00
|
|
|
let root = mkdtemp(&os::tmpdir(), "recursive_mkdir_rel").
|
|
|
|
expect("recursive_mkdir_rel");
|
|
|
|
assert!(do os::change_dir_locked(&root) {
|
|
|
|
let path = Path("frob");
|
|
|
|
debug!("recursive_mkdir_rel: Making: %s in cwd %s [%?]", path.to_str(),
|
|
|
|
os::getcwd().to_str(),
|
|
|
|
os::path_exists(&path));
|
|
|
|
assert!(os::mkdir_recursive(&path, (S_IRUSR | S_IWUSR | S_IXUSR) as i32));
|
|
|
|
assert!(os::path_is_dir(&path));
|
|
|
|
assert!(os::mkdir_recursive(&path, (S_IRUSR | S_IWUSR | S_IXUSR) as i32));
|
|
|
|
assert!(os::path_is_dir(&path));
|
|
|
|
});
|
2013-04-28 23:25:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn recursive_mkdir_dot() {
|
2013-06-28 17:32:26 -05:00
|
|
|
use std::libc::consts::os::posix88::{S_IRUSR, S_IWUSR, S_IXUSR};
|
|
|
|
use std::os;
|
2013-04-28 23:25:35 -05:00
|
|
|
|
|
|
|
let dot = Path(".");
|
|
|
|
assert!(os::mkdir_recursive(&dot, (S_IRUSR | S_IWUSR | S_IXUSR) as i32));
|
|
|
|
let dotdot = Path("..");
|
|
|
|
assert!(os::mkdir_recursive(&dotdot, (S_IRUSR | S_IWUSR | S_IXUSR) as i32));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn recursive_mkdir_rel_2() {
|
2013-06-28 17:32:26 -05:00
|
|
|
use std::libc::consts::os::posix88::{S_IRUSR, S_IWUSR, S_IXUSR};
|
|
|
|
use std::os;
|
2013-04-28 23:25:35 -05:00
|
|
|
|
2013-04-30 19:58:24 -05:00
|
|
|
let root = mkdtemp(&os::tmpdir(), "recursive_mkdir_rel_2").
|
|
|
|
expect("recursive_mkdir_rel_2");
|
|
|
|
assert!(do os::change_dir_locked(&root) {
|
|
|
|
let path = Path("./frob/baz");
|
|
|
|
debug!("recursive_mkdir_rel_2: Making: %s in cwd %s [%?]", path.to_str(),
|
|
|
|
os::getcwd().to_str(), os::path_exists(&path));
|
|
|
|
assert!(os::mkdir_recursive(&path, (S_IRUSR | S_IWUSR | S_IXUSR) as i32));
|
|
|
|
assert!(os::path_is_dir(&path));
|
|
|
|
assert!(os::path_is_dir(&path.pop()));
|
|
|
|
let path2 = Path("quux/blat");
|
|
|
|
debug!("recursive_mkdir_rel_2: Making: %s in cwd %s", path2.to_str(),
|
|
|
|
os::getcwd().to_str());
|
|
|
|
assert!(os::mkdir_recursive(&path2, (S_IRUSR | S_IWUSR | S_IXUSR) as i32));
|
|
|
|
assert!(os::path_is_dir(&path2));
|
|
|
|
assert!(os::path_is_dir(&path2.pop()));
|
|
|
|
});
|
2013-04-28 23:25:35 -05:00
|
|
|
}
|
2013-05-02 15:09:28 -05:00
|
|
|
|
|
|
|
// Ideally this would be in core, but needs mkdtemp
|
|
|
|
#[test]
|
|
|
|
pub fn test_rmdir_recursive_ok() {
|
2013-06-28 17:32:26 -05:00
|
|
|
use std::libc::consts::os::posix88::{S_IRUSR, S_IWUSR, S_IXUSR};
|
|
|
|
use std::os;
|
2013-05-02 15:09:28 -05:00
|
|
|
|
|
|
|
let rwx = (S_IRUSR | S_IWUSR | S_IXUSR) as i32;
|
|
|
|
|
|
|
|
let tmpdir = mkdtemp(&os::tmpdir(), "test").expect("test_rmdir_recursive_ok: \
|
|
|
|
couldn't create temp dir");
|
|
|
|
let root = tmpdir.push("foo");
|
|
|
|
|
|
|
|
debug!("making %s", root.to_str());
|
|
|
|
assert!(os::make_dir(&root, rwx));
|
|
|
|
assert!(os::make_dir(&root.push("foo"), rwx));
|
|
|
|
assert!(os::make_dir(&root.push("foo").push("bar"), rwx));
|
|
|
|
assert!(os::make_dir(&root.push("foo").push("bar").push("blat"), rwx));
|
|
|
|
assert!(os::remove_dir_recursive(&root));
|
|
|
|
assert!(!os::path_exists(&root));
|
|
|
|
assert!(!os::path_exists(&root.push("bar")));
|
|
|
|
assert!(!os::path_exists(&root.push("bar").push("blat")));
|
|
|
|
}
|
2013-04-30 19:58:24 -05:00
|
|
|
}
|