Skip to content

Commit fe41c93

Browse files
committed
Rollup merge of rust-lang#23081 - alexcrichton:stabilize-fs, r=aturon
This commit performs a stabilization pass over the `std::fs` module now that it's had some time to bake. The change was largely just adding `#[stable]` tags, but there are a few APIs that remain `#[unstable]`. The following apis are now marked `#[stable]`: * `std::fs` (the name) * `File` * `Metadata` * `ReadDir` * `DirEntry` * `OpenOptions` * `Permissions` * `File::{open, create}` * `File::{sync_all, sync_data}` * `File::set_len` * `File::metadata` * Trait implementations for `File` and `&File` * `OpenOptions::new` * `OpenOptions::{read, write, append, truncate, create}` * `OpenOptions::open` - this function was modified, however, to not attempt to reject cross-platform openings of directories. This means that some platforms will succeed in opening a directory and others will fail. * `Metadata::{is_dir, is_file, len, permissions}` * `Permissions::{readonly, set_readonly}` * `Iterator for ReadDir` * `DirEntry::path` * `remove_file` - like with `OpenOptions::open`, the extra windows code to remove a readonly file has been removed. This means that removing a readonly file will succeed on some platforms but fail on others. * `metadata` * `rename` * `copy` * `hard_link` * `soft_link` * `read_link` * `create_dir` * `create_dir_all` * `remove_dir` * `remove_dir_all` * `read_dir` The following apis remain `#[unstable]`. * `WalkDir` and `walk` - there are many methods by which a directory walk can be constructed, and it's unclear whether the current semantics are the right ones. For example symlinks are not handled super well currently. This is now behind a new `fs_walk` feature. * `File::path` - this is an extra abstraction which the standard library provides on top of what the system offers and it's unclear whether we should be doing so. This is now behind a new `file_path` feature. * `Metadata::{accessed, modified}` - we do not currently have a good abstraction for a moment in time which is what these APIs should likely be returning, so these remain `#[unstable]` for now. These are now behind a new `fs_time` feature * `set_file_times` - like with `Metadata::accessed`, we do not currently have the appropriate abstraction for the arguments here so this API remains unstable behind the `fs_time` feature gate. * `PathExt` - the precise set of methods on this trait may change over time and some methods may be removed. This API remains unstable behind the `path_ext` feature gate. * `set_permissions` - we may wish to expose a more granular ability to set the permissions on a file instead of just a blanket \"set all permissions\" method. This function remains behind the `fs` feature. The following apis are now `#[deprecated]` * The `TempDir` type is now entirely deprecated and is [located on crates.io][tempdir] as the `tempdir` crate with [its source][github] at rust-lang/tempdir. [tempdir]: https://crates.io/crates/tempdir [github]: https://github.com/rust-lang/tempdir The stability of some of these APIs has been questioned over the past few weeks in using these APIs, and it is intentional that the majority of APIs here are marked `#[stable]`. The `std::fs` module has a lot of room to grow and the material is [being tracked in a RFC issue][rfc-issue]. [rfc-issue]: rust-lang/rfcs#939 Closes rust-lang#22879 [breaking-change]
2 parents c9063e0 + 73b0b25 commit fe41c93

File tree

18 files changed

+229
-56
lines changed

18 files changed

+229
-56
lines changed

src/compiletest/compiletest.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
#![feature(path)]
2525
#![feature(os)]
2626
#![feature(io)]
27-
#![feature(fs)]
2827
#![feature(net)]
28+
#![feature(path_ext)]
2929

3030
#![deny(warnings)]
3131

src/libgetopts/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@
9292
#![feature(collections)]
9393
#![feature(int_uint)]
9494
#![feature(staged_api)]
95-
#![feature(str_words)]
9695
#![feature(core)]
96+
#![feature(str_words)]
9797
#![cfg_attr(test, feature(rustc_private))]
9898

9999
#[cfg(test)] #[macro_use] extern crate log;

src/librustc/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@
4040
#![feature(std_misc)]
4141
#![feature(os)]
4242
#![feature(path)]
43-
#![feature(fs)]
4443
#![feature(io)]
44+
#![feature(path_ext)]
4545
#![cfg_attr(test, feature(test))]
4646

4747
extern crate arena;

src/librustc_back/archive.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@
1111
//! A helper class for dealing with static archives
1212
1313
use std::env;
14-
use std::fs::{self, TempDir};
14+
use std::fs;
1515
use std::io::prelude::*;
1616
use std::io;
1717
use std::path::{Path, PathBuf};
1818
use std::process::{Command, Output, Stdio};
1919
use std::str;
2020
use syntax::diagnostic::Handler as ErrorHandler;
2121

22+
use tempdir::TempDir;
23+
2224
pub const METADATA_FILENAME: &'static str = "rust.metadata.bin";
2325

2426
pub struct ArchiveConfig<'a> {

src/librustc_back/lib.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
#![feature(collections)]
3535
#![feature(core)]
3636
#![feature(old_fs)]
37-
#![feature(fs)]
3837
#![feature(hash)]
3938
#![feature(int_uint)]
4039
#![feature(io)]
@@ -44,14 +43,16 @@
4443
#![feature(path)]
4544
#![feature(rustc_private)]
4645
#![feature(staged_api)]
47-
#![feature(tempdir)]
46+
#![feature(rand)]
47+
#![feature(path_ext)]
4848

4949
extern crate syntax;
5050
extern crate serialize;
5151
#[macro_use] extern crate log;
5252

5353
pub mod abi;
5454
pub mod archive;
55+
pub mod tempdir;
5556
pub mod arm;
5657
pub mod fs;
5758
pub mod mips;

src/librustc_back/tempdir.rs

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
// Copyright 2015 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+
use std::env;
12+
use std::io::{self, Error, ErrorKind};
13+
use std::fs;
14+
use std::path::{self, PathBuf, AsPath};
15+
use std::rand::{thread_rng, Rng};
16+
17+
/// A wrapper for a path to temporary directory implementing automatic
18+
/// scope-based deletion.
19+
pub struct TempDir {
20+
path: Option<PathBuf>,
21+
}
22+
23+
// How many times should we (re)try finding an unused random name? It should be
24+
// enough that an attacker will run out of luck before we run out of patience.
25+
const NUM_RETRIES: u32 = 1 << 31;
26+
// How many characters should we include in a random file name? It needs to
27+
// be enough to dissuade an attacker from trying to preemptively create names
28+
// of that length, but not so huge that we unnecessarily drain the random number
29+
// generator of entropy.
30+
const NUM_RAND_CHARS: uint = 12;
31+
32+
impl TempDir {
33+
/// Attempts to make a temporary directory inside of `tmpdir` whose name
34+
/// will have the prefix `prefix`. The directory will be automatically
35+
/// deleted once the returned wrapper is destroyed.
36+
///
37+
/// If no directory can be created, `Err` is returned.
38+
#[allow(deprecated)] // rand usage
39+
pub fn new_in<P: AsPath + ?Sized>(tmpdir: &P, prefix: &str)
40+
-> io::Result<TempDir> {
41+
let storage;
42+
let mut tmpdir = tmpdir.as_path();
43+
if !tmpdir.is_absolute() {
44+
let cur_dir = try!(env::current_dir());
45+
storage = cur_dir.join(tmpdir);
46+
tmpdir = &storage;
47+
// return TempDir::new_in(&cur_dir.join(tmpdir), prefix);
48+
}
49+
50+
let mut rng = thread_rng();
51+
for _ in 0..NUM_RETRIES {
52+
let suffix: String = rng.gen_ascii_chars().take(NUM_RAND_CHARS).collect();
53+
let leaf = if prefix.len() > 0 {
54+
format!("{}.{}", prefix, suffix)
55+
} else {
56+
// If we're given an empty string for a prefix, then creating a
57+
// directory starting with "." would lead to it being
58+
// semi-invisible on some systems.
59+
suffix
60+
};
61+
let path = tmpdir.join(&leaf);
62+
match fs::create_dir(&path) {
63+
Ok(_) => return Ok(TempDir { path: Some(path) }),
64+
Err(ref e) if e.kind() == ErrorKind::PathAlreadyExists => {}
65+
Err(e) => return Err(e)
66+
}
67+
}
68+
69+
Err(Error::new(ErrorKind::PathAlreadyExists,
70+
"too many temporary directories already exist",
71+
None))
72+
}
73+
74+
/// Attempts to make a temporary directory inside of `env::temp_dir()` whose
75+
/// name will have the prefix `prefix`. The directory will be automatically
76+
/// deleted once the returned wrapper is destroyed.
77+
///
78+
/// If no directory can be created, `Err` is returned.
79+
#[allow(deprecated)]
80+
pub fn new(prefix: &str) -> io::Result<TempDir> {
81+
TempDir::new_in(&env::temp_dir(), prefix)
82+
}
83+
84+
/// Unwrap the wrapped `std::path::Path` from the `TempDir` wrapper.
85+
/// This discards the wrapper so that the automatic deletion of the
86+
/// temporary directory is prevented.
87+
pub fn into_path(mut self) -> PathBuf {
88+
self.path.take().unwrap()
89+
}
90+
91+
/// Access the wrapped `std::path::Path` to the temporary directory.
92+
pub fn path(&self) -> &path::Path {
93+
self.path.as_ref().unwrap()
94+
}
95+
96+
/// Close and remove the temporary directory
97+
///
98+
/// Although `TempDir` removes the directory on drop, in the destructor
99+
/// any errors are ignored. To detect errors cleaning up the temporary
100+
/// directory, call `close` instead.
101+
pub fn close(mut self) -> io::Result<()> {
102+
self.cleanup_dir()
103+
}
104+
105+
fn cleanup_dir(&mut self) -> io::Result<()> {
106+
match self.path {
107+
Some(ref p) => fs::remove_dir_all(p),
108+
None => Ok(())
109+
}
110+
}
111+
}
112+
113+
impl Drop for TempDir {
114+
fn drop(&mut self) {
115+
let _ = self.cleanup_dir();
116+
}
117+
}
118+
119+
// the tests for this module need to change the path using change_dir,
120+
// and this doesn't play nicely with other tests so these unit tests are located
121+
// in src/test/run-pass/tempfile.rs

src/librustc_driver/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
#![feature(exit_status)]
4040
#![feature(path)]
4141
#![feature(io)]
42-
#![feature(fs)]
4342

4443
extern crate arena;
4544
extern crate flate;

src/librustc_trans/back/link.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ use middle::ty::{self, Ty};
2626
use util::common::time;
2727
use util::ppaux;
2828
use util::sha2::{Digest, Sha256};
29+
use rustc_back::tempdir::TempDir;
2930

3031
use std::ffi::{AsOsStr, OsString};
31-
use std::fs::{self, TempDir, PathExt};
32+
use std::fs::{self, PathExt};
3233
use std::io::{self, Read, Write};
3334
use std::mem;
3435
use std::path::{Path, PathBuf};

src/librustc_trans/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@
3838
#![feature(std_misc)]
3939
#![feature(unicode)]
4040
#![feature(io)]
41-
#![feature(fs)]
4241
#![feature(path)]
4342
#![feature(os)]
44-
#![feature(tempdir)]
43+
#![feature(path_ext)]
44+
#![feature(fs)]
4545

4646
extern crate arena;
4747
extern crate flate;

src/librustdoc/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,8 @@
3535
#![feature(unicode)]
3636
#![feature(str_words)]
3737
#![feature(io)]
38-
#![feature(fs)]
3938
#![feature(path)]
40-
#![feature(tempdir)]
39+
#![feature(path_ext)]
4140

4241
extern crate arena;
4342
extern crate getopts;
@@ -47,6 +46,7 @@ extern crate rustc_trans;
4746
extern crate rustc_driver;
4847
extern crate rustc_resolve;
4948
extern crate rustc_lint;
49+
extern crate rustc_back;
5050
extern crate serialize;
5151
extern crate syntax;
5252
extern crate "test" as testing;

src/librustdoc/test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use std::collections::{HashSet, HashMap};
1313
use std::dynamic_lib::DynamicLibrary;
1414
use std::env;
1515
use std::ffi::OsString;
16-
use std::fs::TempDir;
1716
use std::old_io;
1817
use std::io;
1918
use std::path::PathBuf;
@@ -28,6 +27,7 @@ use rustc_lint;
2827
use rustc::session::{self, config};
2928
use rustc::session::config::get_unstable_features_setting;
3029
use rustc::session::search_paths::{SearchPaths, PathKind};
30+
use rustc_back::tempdir::TempDir;
3131
use rustc_driver::{driver, Compilation};
3232
use syntax::codemap::CodeMap;
3333
use syntax::diagnostic;

0 commit comments

Comments
 (0)