forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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]
- Loading branch information
Showing
18 changed files
with
229 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT | ||
// 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. | ||
|
||
use std::env; | ||
use std::io::{self, Error, ErrorKind}; | ||
use std::fs; | ||
use std::path::{self, PathBuf, AsPath}; | ||
use std::rand::{thread_rng, Rng}; | ||
|
||
/// A wrapper for a path to temporary directory implementing automatic | ||
/// scope-based deletion. | ||
pub struct TempDir { | ||
path: Option<PathBuf>, | ||
} | ||
|
||
// How many times should we (re)try finding an unused random name? It should be | ||
// enough that an attacker will run out of luck before we run out of patience. | ||
const NUM_RETRIES: u32 = 1 << 31; | ||
// How many characters should we include in a random file name? It needs to | ||
// be enough to dissuade an attacker from trying to preemptively create names | ||
// of that length, but not so huge that we unnecessarily drain the random number | ||
// generator of entropy. | ||
const NUM_RAND_CHARS: uint = 12; | ||
|
||
impl TempDir { | ||
/// Attempts to make a temporary directory inside of `tmpdir` whose name | ||
/// will have the prefix `prefix`. The directory will be automatically | ||
/// deleted once the returned wrapper is destroyed. | ||
/// | ||
/// If no directory can be created, `Err` is returned. | ||
#[allow(deprecated)] // rand usage | ||
pub fn new_in<P: AsPath + ?Sized>(tmpdir: &P, prefix: &str) | ||
-> io::Result<TempDir> { | ||
let storage; | ||
let mut tmpdir = tmpdir.as_path(); | ||
if !tmpdir.is_absolute() { | ||
let cur_dir = try!(env::current_dir()); | ||
storage = cur_dir.join(tmpdir); | ||
tmpdir = &storage; | ||
// return TempDir::new_in(&cur_dir.join(tmpdir), prefix); | ||
} | ||
|
||
let mut rng = thread_rng(); | ||
for _ in 0..NUM_RETRIES { | ||
let suffix: String = rng.gen_ascii_chars().take(NUM_RAND_CHARS).collect(); | ||
let leaf = if prefix.len() > 0 { | ||
format!("{}.{}", prefix, suffix) | ||
} else { | ||
// If we're given an empty string for a prefix, then creating a | ||
// directory starting with "." would lead to it being | ||
// semi-invisible on some systems. | ||
suffix | ||
}; | ||
let path = tmpdir.join(&leaf); | ||
match fs::create_dir(&path) { | ||
Ok(_) => return Ok(TempDir { path: Some(path) }), | ||
Err(ref e) if e.kind() == ErrorKind::PathAlreadyExists => {} | ||
Err(e) => return Err(e) | ||
} | ||
} | ||
|
||
Err(Error::new(ErrorKind::PathAlreadyExists, | ||
"too many temporary directories already exist", | ||
None)) | ||
} | ||
|
||
/// Attempts to make a temporary directory inside of `env::temp_dir()` whose | ||
/// name will have the prefix `prefix`. The directory will be automatically | ||
/// deleted once the returned wrapper is destroyed. | ||
/// | ||
/// If no directory can be created, `Err` is returned. | ||
#[allow(deprecated)] | ||
pub fn new(prefix: &str) -> io::Result<TempDir> { | ||
TempDir::new_in(&env::temp_dir(), prefix) | ||
} | ||
|
||
/// Unwrap the wrapped `std::path::Path` from the `TempDir` wrapper. | ||
/// This discards the wrapper so that the automatic deletion of the | ||
/// temporary directory is prevented. | ||
pub fn into_path(mut self) -> PathBuf { | ||
self.path.take().unwrap() | ||
} | ||
|
||
/// Access the wrapped `std::path::Path` to the temporary directory. | ||
pub fn path(&self) -> &path::Path { | ||
self.path.as_ref().unwrap() | ||
} | ||
|
||
/// Close and remove the temporary directory | ||
/// | ||
/// Although `TempDir` removes the directory on drop, in the destructor | ||
/// any errors are ignored. To detect errors cleaning up the temporary | ||
/// directory, call `close` instead. | ||
pub fn close(mut self) -> io::Result<()> { | ||
self.cleanup_dir() | ||
} | ||
|
||
fn cleanup_dir(&mut self) -> io::Result<()> { | ||
match self.path { | ||
Some(ref p) => fs::remove_dir_all(p), | ||
None => Ok(()) | ||
} | ||
} | ||
} | ||
|
||
impl Drop for TempDir { | ||
fn drop(&mut self) { | ||
let _ = self.cleanup_dir(); | ||
} | ||
} | ||
|
||
// the tests for this module need to change the path using change_dir, | ||
// and this doesn't play nicely with other tests so these unit tests are located | ||
// in src/test/run-pass/tempfile.rs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.