2015-02-02 23:39:14 -06:00
|
|
|
// 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.
|
|
|
|
|
|
|
|
//! Filesystem manipulation operations
|
|
|
|
//!
|
|
|
|
//! This module contains basic methods to manipulate the contents of the local
|
|
|
|
//! filesystem. All methods in this module represent cross-platform filesystem
|
|
|
|
//! operations. Extra platform-specific functionality can be found in the
|
|
|
|
//! extension traits of `std::os::$platform`.
|
|
|
|
|
std: Stabilize the `fs` module
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]: https://github.com/rust-lang/rfcs/issues/939
[breaking-change]
2015-03-03 21:18:29 -06:00
|
|
|
#![stable(feature = "rust1", since = "1.0.0")]
|
2015-02-02 23:39:14 -06:00
|
|
|
|
2015-04-19 04:27:19 -05:00
|
|
|
use fmt;
|
2015-04-16 01:21:13 -05:00
|
|
|
use ffi::OsString;
|
2015-07-10 03:54:00 -05:00
|
|
|
use io::{self, SeekFrom, Seek, Read, Write};
|
2015-03-18 11:14:54 -05:00
|
|
|
use path::{Path, PathBuf};
|
2015-05-05 18:35:15 -05:00
|
|
|
use sys::fs as fs_imp;
|
2015-07-10 11:34:07 -05:00
|
|
|
use sys_common::io::read_to_end_uninitialized;
|
2015-07-16 01:31:24 -05:00
|
|
|
use sys_common::{AsInnerMut, FromInner, AsInner, IntoInner};
|
2015-02-02 23:39:14 -06:00
|
|
|
use vec::Vec;
|
|
|
|
|
|
|
|
/// A reference to an open file on the filesystem.
|
|
|
|
///
|
|
|
|
/// An instance of a `File` can be read and/or written depending on what options
|
|
|
|
/// it was opened with. Files also implement `Seek` to alter the logical cursor
|
|
|
|
/// that the file contains internally.
|
|
|
|
///
|
2015-03-11 20:11:40 -05:00
|
|
|
/// # Examples
|
2015-02-02 23:39:14 -06:00
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// use std::io::prelude::*;
|
|
|
|
/// use std::fs::File;
|
|
|
|
///
|
|
|
|
/// # fn foo() -> std::io::Result<()> {
|
|
|
|
/// let mut f = try!(File::create("foo.txt"));
|
|
|
|
/// try!(f.write_all(b"Hello, world!"));
|
|
|
|
///
|
|
|
|
/// let mut f = try!(File::open("foo.txt"));
|
|
|
|
/// let mut s = String::new();
|
|
|
|
/// try!(f.read_to_string(&mut s));
|
|
|
|
/// assert_eq!(s, "Hello, world!");
|
|
|
|
/// # Ok(())
|
|
|
|
/// # }
|
|
|
|
/// ```
|
std: Stabilize the `fs` module
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]: https://github.com/rust-lang/rfcs/issues/939
[breaking-change]
2015-03-03 21:18:29 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-02 23:39:14 -06:00
|
|
|
pub struct File {
|
|
|
|
inner: fs_imp::File,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Metadata information about a file.
|
|
|
|
///
|
|
|
|
/// This structure is returned from the `metadata` function or method and
|
|
|
|
/// represents known metadata about a file such as its permissions, size,
|
|
|
|
/// modification times, etc.
|
std: Stabilize the `fs` module
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]: https://github.com/rust-lang/rfcs/issues/939
[breaking-change]
2015-03-03 21:18:29 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-10-13 07:06:00 -05:00
|
|
|
#[derive(Clone)]
|
2015-02-02 23:39:14 -06:00
|
|
|
pub struct Metadata(fs_imp::FileAttr);
|
|
|
|
|
|
|
|
/// Iterator over the entries in a directory.
|
|
|
|
///
|
|
|
|
/// This iterator is returned from the `read_dir` function of this module and
|
|
|
|
/// will yield instances of `io::Result<DirEntry>`. Through a `DirEntry`
|
|
|
|
/// information like the entry's path and possibly other metadata can be
|
|
|
|
/// learned.
|
2015-03-19 15:53:06 -05:00
|
|
|
///
|
|
|
|
/// # Failure
|
|
|
|
///
|
|
|
|
/// This `io::Result` will be an `Err` if there's some sort of intermittent
|
|
|
|
/// IO error during iteration.
|
std: Stabilize the `fs` module
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]: https://github.com/rust-lang/rfcs/issues/939
[breaking-change]
2015-03-03 21:18:29 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-02 23:39:14 -06:00
|
|
|
pub struct ReadDir(fs_imp::ReadDir);
|
|
|
|
|
|
|
|
/// Entries returned by the `ReadDir` iterator.
|
|
|
|
///
|
|
|
|
/// An instance of `DirEntry` represents an entry inside of a directory on the
|
|
|
|
/// filesystem. Each entry can be inspected via methods to learn about the full
|
|
|
|
/// path or possibly other metadata through per-platform extension traits.
|
std: Stabilize the `fs` module
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]: https://github.com/rust-lang/rfcs/issues/939
[breaking-change]
2015-03-03 21:18:29 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-02 23:39:14 -06:00
|
|
|
pub struct DirEntry(fs_imp::DirEntry);
|
|
|
|
|
|
|
|
/// An iterator that recursively walks over the contents of a directory.
|
std: Stabilize the `fs` module
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]: https://github.com/rust-lang/rfcs/issues/939
[breaking-change]
2015-03-03 21:18:29 -06:00
|
|
|
#[unstable(feature = "fs_walk",
|
|
|
|
reason = "the precise semantics and defaults for a recursive walk \
|
|
|
|
may change and this may end up accounting for files such \
|
2015-08-13 12:12:38 -05:00
|
|
|
as symlinks differently",
|
|
|
|
issue = "27707")]
|
2015-12-02 19:31:49 -06:00
|
|
|
#[rustc_deprecated(reason = "superceded by the walkdir crate",
|
|
|
|
since = "1.6.0")]
|
2015-02-02 23:39:14 -06:00
|
|
|
pub struct WalkDir {
|
|
|
|
cur: Option<ReadDir>,
|
|
|
|
stack: Vec<io::Result<ReadDir>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Options and flags which can be used to configure how a file is opened.
|
|
|
|
///
|
2015-04-06 20:52:18 -05:00
|
|
|
/// This builder exposes the ability to configure how a `File` is opened and
|
|
|
|
/// what operations are permitted on the open file. The `File::open` and
|
|
|
|
/// `File::create` methods are aliases for commonly used options using this
|
|
|
|
/// builder.
|
2015-03-31 22:47:50 -05:00
|
|
|
///
|
2015-04-06 20:52:18 -05:00
|
|
|
/// Generally speaking, when using `OpenOptions`, you'll first call `new()`,
|
|
|
|
/// then chain calls to methods to set each option, then call `open()`, passing
|
|
|
|
/// the path of the file you're trying to open. This will give you a
|
|
|
|
/// [`io::Result`][result] with a [`File`][file] inside that you can further
|
|
|
|
/// operate on.
|
2015-03-31 22:47:50 -05:00
|
|
|
///
|
|
|
|
/// [result]: ../io/type.Result.html
|
|
|
|
/// [file]: struct.File.html
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// Opening a file to read:
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// use std::fs::OpenOptions;
|
|
|
|
///
|
|
|
|
/// let file = OpenOptions::new().read(true).open("foo.txt");
|
|
|
|
/// ```
|
|
|
|
///
|
2015-04-06 20:52:18 -05:00
|
|
|
/// Opening a file for both reading and writing, as well as creating it if it
|
|
|
|
/// doesn't exist:
|
2015-03-31 22:47:50 -05:00
|
|
|
///
|
2015-04-10 13:39:53 -05:00
|
|
|
/// ```no_run
|
2015-03-31 22:47:50 -05:00
|
|
|
/// use std::fs::OpenOptions;
|
|
|
|
///
|
|
|
|
/// let file = OpenOptions::new()
|
|
|
|
/// .read(true)
|
|
|
|
/// .write(true)
|
|
|
|
/// .create(true)
|
|
|
|
/// .open("foo.txt");
|
|
|
|
/// ```
|
2015-02-02 23:39:14 -06:00
|
|
|
#[derive(Clone)]
|
std: Stabilize the `fs` module
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]: https://github.com/rust-lang/rfcs/issues/939
[breaking-change]
2015-03-03 21:18:29 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-02 23:39:14 -06:00
|
|
|
pub struct OpenOptions(fs_imp::OpenOptions);
|
|
|
|
|
|
|
|
/// Representation of the various permissions on a file.
|
|
|
|
///
|
|
|
|
/// This module only currently provides one bit of information, `readonly`,
|
|
|
|
/// which is exposed on all currently supported platforms. Unix-specific
|
|
|
|
/// functionality, such as mode bits, is available through the
|
|
|
|
/// `os::unix::PermissionsExt` trait.
|
|
|
|
#[derive(Clone, PartialEq, Eq, Debug)]
|
std: Stabilize the `fs` module
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]: https://github.com/rust-lang/rfcs/issues/939
[breaking-change]
2015-03-03 21:18:29 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-02 23:39:14 -06:00
|
|
|
pub struct Permissions(fs_imp::FilePermissions);
|
|
|
|
|
2015-04-16 01:21:13 -05:00
|
|
|
/// An structure representing a type of file with accessors for each file type.
|
std: Stabilize a number of new fs features
This commit stabilizes the following APIs, slating them all to be cherry-picked
into the 1.1 release.
* fs::FileType (and transitively the derived trait implementations)
* fs::Metadata::file_type
* fs::FileType::is_dir
* fs::FileType::is_file
* fs::FileType::is_symlink
* fs::DirEntry::metadata
* fs::DirEntry::file_type
* fs::DirEntry::file_name
* fs::set_permissions
* fs::symlink_metadata
* os::raw::{self, *}
* os::{android, bitrig, linux, ...}::raw::{self, *}
* os::{android, bitrig, linux, ...}::fs::MetadataExt
* os::{android, bitrig, linux, ...}::fs::MetadataExt::as_raw_stat
* os::unix::fs::PermissionsExt
* os::unix::fs::PermissionsExt::mode
* os::unix::fs::PermissionsExt::set_mode
* os::unix::fs::PermissionsExt::from_mode
* os::unix::fs::OpenOptionsExt
* os::unix::fs::OpenOptionsExt::mode
* os::unix::fs::DirEntryExt
* os::unix::fs::DirEntryExt::ino
* os::windows::fs::MetadataExt
* os::windows::fs::MetadataExt::file_attributes
* os::windows::fs::MetadataExt::creation_time
* os::windows::fs::MetadataExt::last_access_time
* os::windows::fs::MetadataExt::last_write_time
* os::windows::fs::MetadataExt::file_size
The `os::unix::fs::Metadata` structure was also removed entirely, moving all of
its associated methods into the `os::unix::fs::MetadataExt` trait instead. The
methods are all marked as `#[stable]` still.
As some minor cleanup, some deprecated and unstable fs apis were also removed:
* File::path
* Metadata::accessed
* Metadata::modified
Features that were explicitly left unstable include:
* fs::WalkDir - the semantics of this were not considered in the recent fs
expansion RFC.
* fs::DirBuilder - it's still not 100% clear if the naming is right here and if
the set of functionality exposed is appropriate.
* fs::canonicalize - the implementation on Windows here is specifically in
question as it always returns a verbatim path. Additionally the Unix
implementation is susceptible to buffer overflows on long paths unfortunately.
* fs::PathExt - as this is just a convenience trait, it is not stabilized at
this time.
* fs::set_file_times - this funciton is still waiting on a time abstraction.
2015-05-27 18:29:55 -05:00
|
|
|
#[stable(feature = "file_type", since = "1.1.0")]
|
2015-04-16 01:21:13 -05:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
|
|
|
|
pub struct FileType(fs_imp::FileType);
|
|
|
|
|
2015-04-27 19:29:35 -05:00
|
|
|
/// A builder used to create directories in various manners.
|
|
|
|
///
|
|
|
|
/// This builder also supports platform-specific options.
|
2015-12-02 19:31:49 -06:00
|
|
|
#[stable(feature = "dir_builder", since = "1.6.0")]
|
2015-04-27 19:29:35 -05:00
|
|
|
pub struct DirBuilder {
|
|
|
|
inner: fs_imp::DirBuilder,
|
|
|
|
recursive: bool,
|
|
|
|
}
|
|
|
|
|
2015-02-02 23:39:14 -06:00
|
|
|
impl File {
|
|
|
|
/// Attempts to open a file in read-only mode.
|
|
|
|
///
|
|
|
|
/// See the `OpenOptions::open` method for more details.
|
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
///
|
|
|
|
/// This function will return an error if `path` does not already exist.
|
|
|
|
/// Other errors may also be returned according to `OpenOptions::open`.
|
2015-03-23 14:40:43 -05:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// use std::fs::File;
|
|
|
|
///
|
|
|
|
/// # fn foo() -> std::io::Result<()> {
|
|
|
|
/// let mut f = try!(File::open("foo.txt"));
|
|
|
|
/// # Ok(())
|
|
|
|
/// # }
|
|
|
|
/// ```
|
std: Stabilize the `fs` module
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]: https://github.com/rust-lang/rfcs/issues/939
[breaking-change]
2015-03-03 21:18:29 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-03-18 11:14:54 -05:00
|
|
|
pub fn open<P: AsRef<Path>>(path: P) -> io::Result<File> {
|
2015-09-09 14:37:59 -05:00
|
|
|
OpenOptions::new().read(true).open(path.as_ref())
|
2015-02-02 23:39:14 -06:00
|
|
|
}
|
|
|
|
|
2015-04-13 09:21:32 -05:00
|
|
|
/// Opens a file in write-only mode.
|
2015-02-02 23:39:14 -06:00
|
|
|
///
|
2015-02-17 04:22:27 -06:00
|
|
|
/// This function will create a file if it does not exist,
|
2015-02-14 19:52:17 -06:00
|
|
|
/// and will truncate it if it does.
|
2015-02-02 23:39:14 -06:00
|
|
|
///
|
|
|
|
/// See the `OpenOptions::open` function for more details.
|
2015-03-23 14:40:43 -05:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// use std::fs::File;
|
|
|
|
///
|
|
|
|
/// # fn foo() -> std::io::Result<()> {
|
|
|
|
/// let mut f = try!(File::create("foo.txt"));
|
|
|
|
/// # Ok(())
|
|
|
|
/// # }
|
|
|
|
/// ```
|
std: Stabilize the `fs` module
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]: https://github.com/rust-lang/rfcs/issues/939
[breaking-change]
2015-03-03 21:18:29 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-03-18 11:14:54 -05:00
|
|
|
pub fn create<P: AsRef<Path>>(path: P) -> io::Result<File> {
|
2015-09-09 14:37:59 -05:00
|
|
|
OpenOptions::new().write(true).create(true).truncate(true).open(path.as_ref())
|
2015-02-02 23:39:14 -06:00
|
|
|
}
|
|
|
|
|
2015-04-13 09:21:32 -05:00
|
|
|
/// Attempts to sync all OS-internal metadata to disk.
|
2015-02-02 23:39:14 -06:00
|
|
|
///
|
|
|
|
/// This function will attempt to ensure that all in-core data reaches the
|
|
|
|
/// filesystem before returning.
|
2015-03-23 14:40:43 -05:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// use std::fs::File;
|
2015-03-24 18:48:50 -05:00
|
|
|
/// use std::io::prelude::*;
|
2015-03-23 14:40:43 -05:00
|
|
|
///
|
|
|
|
/// # fn foo() -> std::io::Result<()> {
|
|
|
|
/// let mut f = try!(File::create("foo.txt"));
|
|
|
|
/// try!(f.write_all(b"Hello, world!"));
|
|
|
|
///
|
|
|
|
/// try!(f.sync_all());
|
|
|
|
/// # Ok(())
|
|
|
|
/// # }
|
|
|
|
/// ```
|
std: Stabilize the `fs` module
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]: https://github.com/rust-lang/rfcs/issues/939
[breaking-change]
2015-03-03 21:18:29 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-02 23:39:14 -06:00
|
|
|
pub fn sync_all(&self) -> io::Result<()> {
|
|
|
|
self.inner.fsync()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This function is similar to `sync_all`, except that it may not
|
|
|
|
/// synchronize file metadata to the filesystem.
|
|
|
|
///
|
|
|
|
/// This is intended for use cases that must synchronize content, but don't
|
|
|
|
/// need the metadata on disk. The goal of this method is to reduce disk
|
|
|
|
/// operations.
|
|
|
|
///
|
|
|
|
/// Note that some platforms may simply implement this in terms of
|
|
|
|
/// `sync_all`.
|
2015-03-23 14:40:43 -05:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// use std::fs::File;
|
2015-03-24 18:48:50 -05:00
|
|
|
/// use std::io::prelude::*;
|
2015-03-23 14:40:43 -05:00
|
|
|
///
|
|
|
|
/// # fn foo() -> std::io::Result<()> {
|
|
|
|
/// let mut f = try!(File::create("foo.txt"));
|
|
|
|
/// try!(f.write_all(b"Hello, world!"));
|
|
|
|
///
|
|
|
|
/// try!(f.sync_data());
|
|
|
|
/// # Ok(())
|
|
|
|
/// # }
|
|
|
|
/// ```
|
std: Stabilize the `fs` module
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]: https://github.com/rust-lang/rfcs/issues/939
[breaking-change]
2015-03-03 21:18:29 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-02 23:39:14 -06:00
|
|
|
pub fn sync_data(&self) -> io::Result<()> {
|
|
|
|
self.inner.datasync()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Truncates or extends the underlying file, updating the size of
|
|
|
|
/// this file to become `size`.
|
|
|
|
///
|
|
|
|
/// If the `size` is less than the current file's size, then the file will
|
|
|
|
/// be shrunk. If it is greater than the current file's size, then the file
|
|
|
|
/// will be extended to `size` and have all of the intermediate data filled
|
|
|
|
/// in with 0s.
|
2015-03-23 14:40:43 -05:00
|
|
|
///
|
2015-07-11 13:05:47 -05:00
|
|
|
/// # Errors
|
|
|
|
///
|
|
|
|
/// This function will return an error if the file is not opened for writing.
|
|
|
|
///
|
2015-03-23 14:40:43 -05:00
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// use std::fs::File;
|
|
|
|
///
|
|
|
|
/// # fn foo() -> std::io::Result<()> {
|
2015-07-11 13:05:47 -05:00
|
|
|
/// let mut f = try!(File::create("foo.txt"));
|
|
|
|
/// try!(f.set_len(10));
|
2015-03-23 14:40:43 -05:00
|
|
|
/// # Ok(())
|
|
|
|
/// # }
|
|
|
|
/// ```
|
std: Stabilize the `fs` module
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]: https://github.com/rust-lang/rfcs/issues/939
[breaking-change]
2015-03-03 21:18:29 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-02 23:39:14 -06:00
|
|
|
pub fn set_len(&self, size: u64) -> io::Result<()> {
|
|
|
|
self.inner.truncate(size)
|
|
|
|
}
|
|
|
|
|
std: Stabilize the `fs` module
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]: https://github.com/rust-lang/rfcs/issues/939
[breaking-change]
2015-03-03 21:18:29 -06:00
|
|
|
/// Queries metadata about the underlying file.
|
2015-03-23 14:40:43 -05:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// use std::fs::File;
|
|
|
|
///
|
|
|
|
/// # fn foo() -> std::io::Result<()> {
|
|
|
|
/// let mut f = try!(File::open("foo.txt"));
|
|
|
|
/// let metadata = try!(f.metadata());
|
|
|
|
/// # Ok(())
|
|
|
|
/// # }
|
|
|
|
/// ```
|
std: Stabilize the `fs` module
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]: https://github.com/rust-lang/rfcs/issues/939
[breaking-change]
2015-03-03 21:18:29 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-02 23:39:14 -06:00
|
|
|
pub fn metadata(&self) -> io::Result<Metadata> {
|
|
|
|
self.inner.file_attr().map(Metadata)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsInner<fs_imp::File> for File {
|
|
|
|
fn as_inner(&self) -> &fs_imp::File { &self.inner }
|
|
|
|
}
|
std: Stabilize parts of std::os::platform::io
This commit stabilizes the platform-specific `io` modules, specifically around
the traits having to do with the raw representation of each object on each
platform.
Specifically, the following material was stabilized:
* `AsRaw{Fd,Socket,Handle}`
* `RawFd` (renamed from `Fd`)
* `RawHandle` (renamed from `Handle`)
* `RawSocket` (renamed from `Socket`)
* `AsRaw{Fd,Socket,Handle}` implementations
* `std::os::{unix, windows}::io`
The following material was added as `#[unstable]`:
* `FromRaw{Fd,Socket,Handle}`
* Implementations for various primitives
There are a number of future improvements that are possible to make to this
module, but this should cover a good bit of functionality desired from these
modules for now. Some specific future additions may include:
* `IntoRawXXX` traits to consume the raw representation and cancel the
auto-destructor.
* `Fd`, `Socket`, and `Handle` abstractions that behave like Rust objects and
have nice methods for various syscalls.
At this time though, these are considered backwards-compatible extensions and
will not be stabilized at this time.
This commit is a breaking change due to the addition of `Raw` in from of the
type aliases in each of the platform-specific modules.
[breaking-change]
2015-03-26 18:18:29 -05:00
|
|
|
impl FromInner<fs_imp::File> for File {
|
|
|
|
fn from_inner(f: fs_imp::File) -> File {
|
2015-04-15 02:52:08 -05:00
|
|
|
File { inner: f }
|
std: Stabilize parts of std::os::platform::io
This commit stabilizes the platform-specific `io` modules, specifically around
the traits having to do with the raw representation of each object on each
platform.
Specifically, the following material was stabilized:
* `AsRaw{Fd,Socket,Handle}`
* `RawFd` (renamed from `Fd`)
* `RawHandle` (renamed from `Handle`)
* `RawSocket` (renamed from `Socket`)
* `AsRaw{Fd,Socket,Handle}` implementations
* `std::os::{unix, windows}::io`
The following material was added as `#[unstable]`:
* `FromRaw{Fd,Socket,Handle}`
* Implementations for various primitives
There are a number of future improvements that are possible to make to this
module, but this should cover a good bit of functionality desired from these
modules for now. Some specific future additions may include:
* `IntoRawXXX` traits to consume the raw representation and cancel the
auto-destructor.
* `Fd`, `Socket`, and `Handle` abstractions that behave like Rust objects and
have nice methods for various syscalls.
At this time though, these are considered backwards-compatible extensions and
will not be stabilized at this time.
This commit is a breaking change due to the addition of `Raw` in from of the
type aliases in each of the platform-specific modules.
[breaking-change]
2015-03-26 18:18:29 -05:00
|
|
|
}
|
|
|
|
}
|
2015-07-16 01:31:24 -05:00
|
|
|
impl IntoInner<fs_imp::File> for File {
|
|
|
|
fn into_inner(self) -> fs_imp::File {
|
|
|
|
self.inner
|
|
|
|
}
|
|
|
|
}
|
std: Stabilize parts of std::os::platform::io
This commit stabilizes the platform-specific `io` modules, specifically around
the traits having to do with the raw representation of each object on each
platform.
Specifically, the following material was stabilized:
* `AsRaw{Fd,Socket,Handle}`
* `RawFd` (renamed from `Fd`)
* `RawHandle` (renamed from `Handle`)
* `RawSocket` (renamed from `Socket`)
* `AsRaw{Fd,Socket,Handle}` implementations
* `std::os::{unix, windows}::io`
The following material was added as `#[unstable]`:
* `FromRaw{Fd,Socket,Handle}`
* Implementations for various primitives
There are a number of future improvements that are possible to make to this
module, but this should cover a good bit of functionality desired from these
modules for now. Some specific future additions may include:
* `IntoRawXXX` traits to consume the raw representation and cancel the
auto-destructor.
* `Fd`, `Socket`, and `Handle` abstractions that behave like Rust objects and
have nice methods for various syscalls.
At this time though, these are considered backwards-compatible extensions and
will not be stabilized at this time.
This commit is a breaking change due to the addition of `Raw` in from of the
type aliases in each of the platform-specific modules.
[breaking-change]
2015-03-26 18:18:29 -05:00
|
|
|
|
2015-11-16 10:54:28 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-04-19 04:27:19 -05:00
|
|
|
impl fmt::Debug for File {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
self.inner.fmt(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
std: Stabilize the `fs` module
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]: https://github.com/rust-lang/rfcs/issues/939
[breaking-change]
2015-03-03 21:18:29 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-02 23:39:14 -06:00
|
|
|
impl Read for File {
|
|
|
|
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
|
|
|
self.inner.read(buf)
|
|
|
|
}
|
2015-07-10 11:34:07 -05:00
|
|
|
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
|
|
|
|
unsafe { read_to_end_uninitialized(self, buf) }
|
|
|
|
}
|
2015-02-02 23:39:14 -06:00
|
|
|
}
|
std: Stabilize the `fs` module
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]: https://github.com/rust-lang/rfcs/issues/939
[breaking-change]
2015-03-03 21:18:29 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-02 23:39:14 -06:00
|
|
|
impl Write for File {
|
|
|
|
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
|
|
|
self.inner.write(buf)
|
|
|
|
}
|
|
|
|
fn flush(&mut self) -> io::Result<()> { self.inner.flush() }
|
|
|
|
}
|
std: Stabilize the `fs` module
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]: https://github.com/rust-lang/rfcs/issues/939
[breaking-change]
2015-03-03 21:18:29 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-02 23:39:14 -06:00
|
|
|
impl Seek for File {
|
|
|
|
fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
|
|
|
|
self.inner.seek(pos)
|
|
|
|
}
|
|
|
|
}
|
std: Stabilize the `fs` module
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]: https://github.com/rust-lang/rfcs/issues/939
[breaking-change]
2015-03-03 21:18:29 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-02 23:39:14 -06:00
|
|
|
impl<'a> Read for &'a File {
|
|
|
|
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
|
|
|
self.inner.read(buf)
|
|
|
|
}
|
|
|
|
}
|
std: Stabilize the `fs` module
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]: https://github.com/rust-lang/rfcs/issues/939
[breaking-change]
2015-03-03 21:18:29 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-02 23:39:14 -06:00
|
|
|
impl<'a> Write for &'a File {
|
|
|
|
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
|
|
|
self.inner.write(buf)
|
|
|
|
}
|
|
|
|
fn flush(&mut self) -> io::Result<()> { self.inner.flush() }
|
|
|
|
}
|
std: Stabilize the `fs` module
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]: https://github.com/rust-lang/rfcs/issues/939
[breaking-change]
2015-03-03 21:18:29 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-02 23:39:14 -06:00
|
|
|
impl<'a> Seek for &'a File {
|
|
|
|
fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
|
|
|
|
self.inner.seek(pos)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl OpenOptions {
|
2016-01-13 11:08:08 -06:00
|
|
|
/// Creates a blank new set of options ready for configuration.
|
2015-02-02 23:39:14 -06:00
|
|
|
///
|
|
|
|
/// All options are initially set to `false`.
|
2015-03-31 22:47:50 -05:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// use std::fs::OpenOptions;
|
|
|
|
///
|
2016-01-13 11:08:08 -06:00
|
|
|
/// let mut options = OpenOptions::new();
|
|
|
|
/// let file = options.read(true).open("foo.txt");
|
2015-03-31 22:47:50 -05:00
|
|
|
/// ```
|
std: Stabilize the `fs` module
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]: https://github.com/rust-lang/rfcs/issues/939
[breaking-change]
2015-03-03 21:18:29 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-02 23:39:14 -06:00
|
|
|
pub fn new() -> OpenOptions {
|
|
|
|
OpenOptions(fs_imp::OpenOptions::new())
|
|
|
|
}
|
|
|
|
|
2015-04-13 09:21:32 -05:00
|
|
|
/// Sets the option for read access.
|
2015-02-02 23:39:14 -06:00
|
|
|
///
|
|
|
|
/// This option, when true, will indicate that the file should be
|
|
|
|
/// `read`-able if opened.
|
2015-03-31 22:47:50 -05:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// use std::fs::OpenOptions;
|
|
|
|
///
|
|
|
|
/// let file = OpenOptions::new().read(true).open("foo.txt");
|
|
|
|
/// ```
|
std: Stabilize the `fs` module
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]: https://github.com/rust-lang/rfcs/issues/939
[breaking-change]
2015-03-03 21:18:29 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-02 23:39:14 -06:00
|
|
|
pub fn read(&mut self, read: bool) -> &mut OpenOptions {
|
|
|
|
self.0.read(read); self
|
|
|
|
}
|
|
|
|
|
2015-04-13 09:21:32 -05:00
|
|
|
/// Sets the option for write access.
|
2015-02-02 23:39:14 -06:00
|
|
|
///
|
|
|
|
/// This option, when true, will indicate that the file should be
|
|
|
|
/// `write`-able if opened.
|
2015-03-31 22:47:50 -05:00
|
|
|
///
|
2016-01-21 15:14:47 -06:00
|
|
|
/// If the file already exists, any write calls on it will overwrite its
|
2016-01-15 12:04:53 -06:00
|
|
|
/// contents, without truncating it.
|
2016-01-13 11:08:08 -06:00
|
|
|
///
|
2015-03-31 22:47:50 -05:00
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// use std::fs::OpenOptions;
|
|
|
|
///
|
|
|
|
/// let file = OpenOptions::new().write(true).open("foo.txt");
|
|
|
|
/// ```
|
std: Stabilize the `fs` module
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]: https://github.com/rust-lang/rfcs/issues/939
[breaking-change]
2015-03-03 21:18:29 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-02 23:39:14 -06:00
|
|
|
pub fn write(&mut self, write: bool) -> &mut OpenOptions {
|
|
|
|
self.0.write(write); self
|
|
|
|
}
|
|
|
|
|
2015-04-13 09:21:32 -05:00
|
|
|
/// Sets the option for the append mode.
|
2015-02-02 23:39:14 -06:00
|
|
|
///
|
|
|
|
/// This option, when true, means that writes will append to a file instead
|
|
|
|
/// of overwriting previous contents.
|
2016-01-13 11:08:08 -06:00
|
|
|
/// Note that setting `.write(true).append(true)` has the same effect as
|
|
|
|
/// setting only `.append(true)`.
|
|
|
|
///
|
2016-01-21 15:41:06 -06:00
|
|
|
/// For most filesystems, the operating system guarantees that all writes are
|
2016-01-15 12:04:53 -06:00
|
|
|
/// atomic: no writes get mangled because another process writes at the same
|
|
|
|
/// time.
|
2016-01-13 11:08:08 -06:00
|
|
|
///
|
2016-01-15 12:04:53 -06:00
|
|
|
/// One maybe obvious note when using append-mode: make sure that all data
|
2016-01-21 15:41:06 -06:00
|
|
|
/// that belongs together is written to the file in one operation. This
|
2016-01-15 12:04:53 -06:00
|
|
|
/// can be done by concatenating strings before passing them to `write()`,
|
2016-01-21 15:41:06 -06:00
|
|
|
/// or using a buffered writer (with a buffer of adequate size),
|
2016-01-15 12:04:53 -06:00
|
|
|
/// and calling `flush()` when the message is complete.
|
2016-01-13 11:08:08 -06:00
|
|
|
///
|
2016-01-15 12:04:53 -06:00
|
|
|
/// If a file is opened with both read and append access, beware that after
|
2016-01-21 15:41:06 -06:00
|
|
|
/// opening, and after every write, the position for reading may be set at the
|
|
|
|
/// end of the file. So, before writing, save the current position (using
|
2016-01-15 12:04:53 -06:00
|
|
|
/// `seek(SeekFrom::Current(0))`, and restore it before the next read.
|
2015-03-31 22:47:50 -05:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// use std::fs::OpenOptions;
|
|
|
|
///
|
2016-01-13 11:08:08 -06:00
|
|
|
/// let file = OpenOptions::new().append(true).open("foo.txt");
|
2015-03-31 22:47:50 -05:00
|
|
|
/// ```
|
std: Stabilize the `fs` module
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]: https://github.com/rust-lang/rfcs/issues/939
[breaking-change]
2015-03-03 21:18:29 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-02 23:39:14 -06:00
|
|
|
pub fn append(&mut self, append: bool) -> &mut OpenOptions {
|
|
|
|
self.0.append(append); self
|
|
|
|
}
|
|
|
|
|
2015-04-13 09:21:32 -05:00
|
|
|
/// Sets the option for truncating a previous file.
|
2015-02-02 23:39:14 -06:00
|
|
|
///
|
|
|
|
/// If a file is successfully opened with this option set it will truncate
|
|
|
|
/// the file to 0 length if it already exists.
|
2015-03-31 22:47:50 -05:00
|
|
|
///
|
2016-01-13 11:08:08 -06:00
|
|
|
/// The file must be opened with write access for truncate to work.
|
|
|
|
///
|
2015-03-31 22:47:50 -05:00
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// use std::fs::OpenOptions;
|
|
|
|
///
|
2015-06-28 16:23:53 -05:00
|
|
|
/// let file = OpenOptions::new().write(true).truncate(true).open("foo.txt");
|
2015-03-31 22:47:50 -05:00
|
|
|
/// ```
|
std: Stabilize the `fs` module
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]: https://github.com/rust-lang/rfcs/issues/939
[breaking-change]
2015-03-03 21:18:29 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-02 23:39:14 -06:00
|
|
|
pub fn truncate(&mut self, truncate: bool) -> &mut OpenOptions {
|
|
|
|
self.0.truncate(truncate); self
|
|
|
|
}
|
|
|
|
|
2015-04-13 09:21:32 -05:00
|
|
|
/// Sets the option for creating a new file.
|
2015-02-02 23:39:14 -06:00
|
|
|
///
|
|
|
|
/// This option indicates whether a new file will be created if the file
|
|
|
|
/// does not yet already exist.
|
2015-03-31 22:47:50 -05:00
|
|
|
///
|
2016-01-21 15:27:18 -06:00
|
|
|
/// In order for the file to be created, `write` or `append` access must
|
|
|
|
/// be used.
|
2016-01-13 11:08:08 -06:00
|
|
|
///
|
2015-03-31 22:47:50 -05:00
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// use std::fs::OpenOptions;
|
|
|
|
///
|
2016-01-13 11:08:08 -06:00
|
|
|
/// let file = OpenOptions::new().write(true).create(true).open("foo.txt");
|
2015-03-31 22:47:50 -05:00
|
|
|
/// ```
|
std: Stabilize the `fs` module
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]: https://github.com/rust-lang/rfcs/issues/939
[breaking-change]
2015-03-03 21:18:29 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-02 23:39:14 -06:00
|
|
|
pub fn create(&mut self, create: bool) -> &mut OpenOptions {
|
|
|
|
self.0.create(create); self
|
|
|
|
}
|
|
|
|
|
2016-01-13 11:08:08 -06:00
|
|
|
/// Sets the option to always create a new file.
|
|
|
|
///
|
|
|
|
/// This option indicates whether a new file will be created.
|
|
|
|
/// No file is allowed to exist at the target location, also no (dangling)
|
|
|
|
/// symlink.
|
|
|
|
///
|
2016-01-15 12:04:53 -06:00
|
|
|
/// This option is usefull because it as atomic. Otherwise between checking
|
|
|
|
/// whether a file exists and creating a new one, the file may have been
|
|
|
|
/// created by another process (a TOCTOU race condition / attack).
|
|
|
|
///
|
|
|
|
/// If `.create_new(true)` is set, `.create()` and `.truncate()` are
|
|
|
|
/// ignored.
|
2016-01-13 11:08:08 -06:00
|
|
|
///
|
|
|
|
/// The file must be opened with write or append access in order to create
|
|
|
|
/// a new file.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```no_run
|
2016-01-14 09:59:28 -06:00
|
|
|
/// #![feature(expand_open_options)]
|
2016-01-13 11:08:08 -06:00
|
|
|
/// use std::fs::OpenOptions;
|
|
|
|
///
|
2016-01-15 12:04:53 -06:00
|
|
|
/// let file = OpenOptions::new().write(true)
|
|
|
|
/// .create_new(true)
|
|
|
|
/// .open("foo.txt");
|
2016-01-13 11:08:08 -06:00
|
|
|
/// ```
|
2016-01-13 14:47:46 -06:00
|
|
|
#[unstable(feature = "expand_open_options",
|
|
|
|
reason = "recently added",
|
|
|
|
issue = "30014")]
|
2016-01-13 11:08:08 -06:00
|
|
|
pub fn create_new(&mut self, create_new: bool) -> &mut OpenOptions {
|
|
|
|
self.0.create_new(create_new); self
|
|
|
|
}
|
|
|
|
|
2015-04-13 09:21:32 -05:00
|
|
|
/// Opens a file at `path` with the options specified by `self`.
|
2015-02-02 23:39:14 -06:00
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
///
|
|
|
|
/// This function will return an error under a number of different
|
|
|
|
/// circumstances, to include but not limited to:
|
|
|
|
///
|
2016-01-15 12:04:53 -06:00
|
|
|
/// * Opening a file that does not exist without setting `create` or
|
|
|
|
/// `create_new`.
|
2015-02-02 23:39:14 -06:00
|
|
|
/// * Attempting to open a file with access that the user lacks
|
|
|
|
/// permissions for
|
|
|
|
/// * Filesystem-level errors (full disk, etc)
|
2016-01-13 11:08:08 -06:00
|
|
|
/// * Invalid combinations of open options (truncate without write access,
|
|
|
|
/// no access mode set, etc)
|
2015-03-31 22:47:50 -05:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// use std::fs::OpenOptions;
|
|
|
|
///
|
|
|
|
/// let file = OpenOptions::new().open("foo.txt");
|
|
|
|
/// ```
|
std: Stabilize the `fs` module
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]: https://github.com/rust-lang/rfcs/issues/939
[breaking-change]
2015-03-03 21:18:29 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-03-18 11:14:54 -05:00
|
|
|
pub fn open<P: AsRef<Path>>(&self, path: P) -> io::Result<File> {
|
2015-09-09 14:37:59 -05:00
|
|
|
self._open(path.as_ref())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn _open(&self, path: &Path) -> io::Result<File> {
|
2015-02-02 23:39:14 -06:00
|
|
|
let inner = try!(fs_imp::File::open(path, &self.0));
|
2015-04-15 02:52:08 -05:00
|
|
|
Ok(File { inner: inner })
|
2015-02-02 23:39:14 -06:00
|
|
|
}
|
|
|
|
}
|
std: Stabilize the `fs` module
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]: https://github.com/rust-lang/rfcs/issues/939
[breaking-change]
2015-03-03 21:18:29 -06:00
|
|
|
|
2015-02-02 23:39:14 -06:00
|
|
|
impl AsInnerMut<fs_imp::OpenOptions> for OpenOptions {
|
|
|
|
fn as_inner_mut(&mut self) -> &mut fs_imp::OpenOptions { &mut self.0 }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Metadata {
|
2015-04-16 01:21:13 -05:00
|
|
|
/// Returns the file type for this metadata.
|
std: Stabilize a number of new fs features
This commit stabilizes the following APIs, slating them all to be cherry-picked
into the 1.1 release.
* fs::FileType (and transitively the derived trait implementations)
* fs::Metadata::file_type
* fs::FileType::is_dir
* fs::FileType::is_file
* fs::FileType::is_symlink
* fs::DirEntry::metadata
* fs::DirEntry::file_type
* fs::DirEntry::file_name
* fs::set_permissions
* fs::symlink_metadata
* os::raw::{self, *}
* os::{android, bitrig, linux, ...}::raw::{self, *}
* os::{android, bitrig, linux, ...}::fs::MetadataExt
* os::{android, bitrig, linux, ...}::fs::MetadataExt::as_raw_stat
* os::unix::fs::PermissionsExt
* os::unix::fs::PermissionsExt::mode
* os::unix::fs::PermissionsExt::set_mode
* os::unix::fs::PermissionsExt::from_mode
* os::unix::fs::OpenOptionsExt
* os::unix::fs::OpenOptionsExt::mode
* os::unix::fs::DirEntryExt
* os::unix::fs::DirEntryExt::ino
* os::windows::fs::MetadataExt
* os::windows::fs::MetadataExt::file_attributes
* os::windows::fs::MetadataExt::creation_time
* os::windows::fs::MetadataExt::last_access_time
* os::windows::fs::MetadataExt::last_write_time
* os::windows::fs::MetadataExt::file_size
The `os::unix::fs::Metadata` structure was also removed entirely, moving all of
its associated methods into the `os::unix::fs::MetadataExt` trait instead. The
methods are all marked as `#[stable]` still.
As some minor cleanup, some deprecated and unstable fs apis were also removed:
* File::path
* Metadata::accessed
* Metadata::modified
Features that were explicitly left unstable include:
* fs::WalkDir - the semantics of this were not considered in the recent fs
expansion RFC.
* fs::DirBuilder - it's still not 100% clear if the naming is right here and if
the set of functionality exposed is appropriate.
* fs::canonicalize - the implementation on Windows here is specifically in
question as it always returns a verbatim path. Additionally the Unix
implementation is susceptible to buffer overflows on long paths unfortunately.
* fs::PathExt - as this is just a convenience trait, it is not stabilized at
this time.
* fs::set_file_times - this funciton is still waiting on a time abstraction.
2015-05-27 18:29:55 -05:00
|
|
|
#[stable(feature = "file_type", since = "1.1.0")]
|
2015-04-16 01:21:13 -05:00
|
|
|
pub fn file_type(&self) -> FileType {
|
|
|
|
FileType(self.0.file_type())
|
|
|
|
}
|
|
|
|
|
2015-02-02 23:39:14 -06:00
|
|
|
/// Returns whether this metadata is for a directory.
|
2015-03-31 22:47:50 -05:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// # fn foo() -> std::io::Result<()> {
|
|
|
|
/// use std::fs;
|
|
|
|
///
|
|
|
|
/// let metadata = try!(fs::metadata("foo.txt"));
|
|
|
|
///
|
|
|
|
/// assert!(!metadata.is_dir());
|
|
|
|
/// # Ok(())
|
|
|
|
/// # }
|
|
|
|
/// ```
|
std: Stabilize the `fs` module
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]: https://github.com/rust-lang/rfcs/issues/939
[breaking-change]
2015-03-03 21:18:29 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-04-16 01:21:13 -05:00
|
|
|
pub fn is_dir(&self) -> bool { self.file_type().is_dir() }
|
2015-02-02 23:39:14 -06:00
|
|
|
|
|
|
|
/// Returns whether this metadata is for a regular file.
|
2015-03-31 22:47:50 -05:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// # fn foo() -> std::io::Result<()> {
|
|
|
|
/// use std::fs;
|
|
|
|
///
|
|
|
|
/// let metadata = try!(fs::metadata("foo.txt"));
|
|
|
|
///
|
|
|
|
/// assert!(metadata.is_file());
|
|
|
|
/// # Ok(())
|
|
|
|
/// # }
|
|
|
|
/// ```
|
std: Stabilize the `fs` module
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]: https://github.com/rust-lang/rfcs/issues/939
[breaking-change]
2015-03-03 21:18:29 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-04-16 01:21:13 -05:00
|
|
|
pub fn is_file(&self) -> bool { self.file_type().is_file() }
|
2015-02-02 23:39:14 -06:00
|
|
|
|
|
|
|
/// Returns the size of the file, in bytes, this metadata is for.
|
2015-03-31 22:47:50 -05:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// # fn foo() -> std::io::Result<()> {
|
|
|
|
/// use std::fs;
|
|
|
|
///
|
|
|
|
/// let metadata = try!(fs::metadata("foo.txt"));
|
|
|
|
///
|
|
|
|
/// assert_eq!(0, metadata.len());
|
|
|
|
/// # Ok(())
|
|
|
|
/// # }
|
|
|
|
/// ```
|
std: Stabilize the `fs` module
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]: https://github.com/rust-lang/rfcs/issues/939
[breaking-change]
2015-03-03 21:18:29 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-02 23:39:14 -06:00
|
|
|
pub fn len(&self) -> u64 { self.0.size() }
|
|
|
|
|
|
|
|
/// Returns the permissions of the file this metadata is for.
|
2015-03-31 22:47:50 -05:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// # fn foo() -> std::io::Result<()> {
|
|
|
|
/// use std::fs;
|
|
|
|
///
|
|
|
|
/// let metadata = try!(fs::metadata("foo.txt"));
|
|
|
|
///
|
|
|
|
/// assert!(!metadata.permissions().readonly());
|
|
|
|
/// # Ok(())
|
|
|
|
/// # }
|
|
|
|
/// ```
|
std: Stabilize the `fs` module
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]: https://github.com/rust-lang/rfcs/issues/939
[breaking-change]
2015-03-03 21:18:29 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-02 23:39:14 -06:00
|
|
|
pub fn permissions(&self) -> Permissions {
|
|
|
|
Permissions(self.0.perm())
|
|
|
|
}
|
2015-04-16 01:21:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AsInner<fs_imp::FileAttr> for Metadata {
|
|
|
|
fn as_inner(&self) -> &fs_imp::FileAttr { &self.0 }
|
2015-02-02 23:39:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Permissions {
|
|
|
|
/// Returns whether these permissions describe a readonly file.
|
2015-03-31 22:47:50 -05:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::fs::File;
|
|
|
|
///
|
|
|
|
/// # fn foo() -> std::io::Result<()> {
|
|
|
|
/// let mut f = try!(File::create("foo.txt"));
|
|
|
|
/// let metadata = try!(f.metadata());
|
|
|
|
///
|
|
|
|
/// assert_eq!(false, metadata.permissions().readonly());
|
|
|
|
/// # Ok(())
|
|
|
|
/// # }
|
|
|
|
/// ```
|
std: Stabilize the `fs` module
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]: https://github.com/rust-lang/rfcs/issues/939
[breaking-change]
2015-03-03 21:18:29 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-02 23:39:14 -06:00
|
|
|
pub fn readonly(&self) -> bool { self.0.readonly() }
|
|
|
|
|
2015-04-13 09:21:32 -05:00
|
|
|
/// Modifies the readonly flag for this set of permissions.
|
2015-02-02 23:39:14 -06:00
|
|
|
///
|
|
|
|
/// This operation does **not** modify the filesystem. To modify the
|
|
|
|
/// filesystem use the `fs::set_permissions` function.
|
2015-03-31 22:47:50 -05:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::fs::File;
|
|
|
|
///
|
|
|
|
/// # fn foo() -> std::io::Result<()> {
|
2015-04-30 18:20:59 -05:00
|
|
|
/// let f = try!(File::create("foo.txt"));
|
2015-03-31 22:47:50 -05:00
|
|
|
/// let metadata = try!(f.metadata());
|
|
|
|
/// let mut permissions = metadata.permissions();
|
|
|
|
///
|
|
|
|
/// permissions.set_readonly(true);
|
|
|
|
///
|
|
|
|
/// // filesystem doesn't change
|
|
|
|
/// assert_eq!(false, metadata.permissions().readonly());
|
|
|
|
///
|
|
|
|
/// // just this particular `permissions`.
|
|
|
|
/// assert_eq!(true, permissions.readonly());
|
|
|
|
/// # Ok(())
|
|
|
|
/// # }
|
|
|
|
/// ```
|
std: Stabilize the `fs` module
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]: https://github.com/rust-lang/rfcs/issues/939
[breaking-change]
2015-03-03 21:18:29 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-02 23:39:14 -06:00
|
|
|
pub fn set_readonly(&mut self, readonly: bool) {
|
|
|
|
self.0.set_readonly(readonly)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-16 01:21:13 -05:00
|
|
|
impl FileType {
|
|
|
|
/// Test whether this file type represents a directory.
|
std: Stabilize a number of new fs features
This commit stabilizes the following APIs, slating them all to be cherry-picked
into the 1.1 release.
* fs::FileType (and transitively the derived trait implementations)
* fs::Metadata::file_type
* fs::FileType::is_dir
* fs::FileType::is_file
* fs::FileType::is_symlink
* fs::DirEntry::metadata
* fs::DirEntry::file_type
* fs::DirEntry::file_name
* fs::set_permissions
* fs::symlink_metadata
* os::raw::{self, *}
* os::{android, bitrig, linux, ...}::raw::{self, *}
* os::{android, bitrig, linux, ...}::fs::MetadataExt
* os::{android, bitrig, linux, ...}::fs::MetadataExt::as_raw_stat
* os::unix::fs::PermissionsExt
* os::unix::fs::PermissionsExt::mode
* os::unix::fs::PermissionsExt::set_mode
* os::unix::fs::PermissionsExt::from_mode
* os::unix::fs::OpenOptionsExt
* os::unix::fs::OpenOptionsExt::mode
* os::unix::fs::DirEntryExt
* os::unix::fs::DirEntryExt::ino
* os::windows::fs::MetadataExt
* os::windows::fs::MetadataExt::file_attributes
* os::windows::fs::MetadataExt::creation_time
* os::windows::fs::MetadataExt::last_access_time
* os::windows::fs::MetadataExt::last_write_time
* os::windows::fs::MetadataExt::file_size
The `os::unix::fs::Metadata` structure was also removed entirely, moving all of
its associated methods into the `os::unix::fs::MetadataExt` trait instead. The
methods are all marked as `#[stable]` still.
As some minor cleanup, some deprecated and unstable fs apis were also removed:
* File::path
* Metadata::accessed
* Metadata::modified
Features that were explicitly left unstable include:
* fs::WalkDir - the semantics of this were not considered in the recent fs
expansion RFC.
* fs::DirBuilder - it's still not 100% clear if the naming is right here and if
the set of functionality exposed is appropriate.
* fs::canonicalize - the implementation on Windows here is specifically in
question as it always returns a verbatim path. Additionally the Unix
implementation is susceptible to buffer overflows on long paths unfortunately.
* fs::PathExt - as this is just a convenience trait, it is not stabilized at
this time.
* fs::set_file_times - this funciton is still waiting on a time abstraction.
2015-05-27 18:29:55 -05:00
|
|
|
#[stable(feature = "file_type", since = "1.1.0")]
|
2015-04-16 01:21:13 -05:00
|
|
|
pub fn is_dir(&self) -> bool { self.0.is_dir() }
|
|
|
|
|
|
|
|
/// Test whether this file type represents a regular file.
|
std: Stabilize a number of new fs features
This commit stabilizes the following APIs, slating them all to be cherry-picked
into the 1.1 release.
* fs::FileType (and transitively the derived trait implementations)
* fs::Metadata::file_type
* fs::FileType::is_dir
* fs::FileType::is_file
* fs::FileType::is_symlink
* fs::DirEntry::metadata
* fs::DirEntry::file_type
* fs::DirEntry::file_name
* fs::set_permissions
* fs::symlink_metadata
* os::raw::{self, *}
* os::{android, bitrig, linux, ...}::raw::{self, *}
* os::{android, bitrig, linux, ...}::fs::MetadataExt
* os::{android, bitrig, linux, ...}::fs::MetadataExt::as_raw_stat
* os::unix::fs::PermissionsExt
* os::unix::fs::PermissionsExt::mode
* os::unix::fs::PermissionsExt::set_mode
* os::unix::fs::PermissionsExt::from_mode
* os::unix::fs::OpenOptionsExt
* os::unix::fs::OpenOptionsExt::mode
* os::unix::fs::DirEntryExt
* os::unix::fs::DirEntryExt::ino
* os::windows::fs::MetadataExt
* os::windows::fs::MetadataExt::file_attributes
* os::windows::fs::MetadataExt::creation_time
* os::windows::fs::MetadataExt::last_access_time
* os::windows::fs::MetadataExt::last_write_time
* os::windows::fs::MetadataExt::file_size
The `os::unix::fs::Metadata` structure was also removed entirely, moving all of
its associated methods into the `os::unix::fs::MetadataExt` trait instead. The
methods are all marked as `#[stable]` still.
As some minor cleanup, some deprecated and unstable fs apis were also removed:
* File::path
* Metadata::accessed
* Metadata::modified
Features that were explicitly left unstable include:
* fs::WalkDir - the semantics of this were not considered in the recent fs
expansion RFC.
* fs::DirBuilder - it's still not 100% clear if the naming is right here and if
the set of functionality exposed is appropriate.
* fs::canonicalize - the implementation on Windows here is specifically in
question as it always returns a verbatim path. Additionally the Unix
implementation is susceptible to buffer overflows on long paths unfortunately.
* fs::PathExt - as this is just a convenience trait, it is not stabilized at
this time.
* fs::set_file_times - this funciton is still waiting on a time abstraction.
2015-05-27 18:29:55 -05:00
|
|
|
#[stable(feature = "file_type", since = "1.1.0")]
|
2015-04-16 01:21:13 -05:00
|
|
|
pub fn is_file(&self) -> bool { self.0.is_file() }
|
|
|
|
|
|
|
|
/// Test whether this file type represents a symbolic link.
|
std: Stabilize a number of new fs features
This commit stabilizes the following APIs, slating them all to be cherry-picked
into the 1.1 release.
* fs::FileType (and transitively the derived trait implementations)
* fs::Metadata::file_type
* fs::FileType::is_dir
* fs::FileType::is_file
* fs::FileType::is_symlink
* fs::DirEntry::metadata
* fs::DirEntry::file_type
* fs::DirEntry::file_name
* fs::set_permissions
* fs::symlink_metadata
* os::raw::{self, *}
* os::{android, bitrig, linux, ...}::raw::{self, *}
* os::{android, bitrig, linux, ...}::fs::MetadataExt
* os::{android, bitrig, linux, ...}::fs::MetadataExt::as_raw_stat
* os::unix::fs::PermissionsExt
* os::unix::fs::PermissionsExt::mode
* os::unix::fs::PermissionsExt::set_mode
* os::unix::fs::PermissionsExt::from_mode
* os::unix::fs::OpenOptionsExt
* os::unix::fs::OpenOptionsExt::mode
* os::unix::fs::DirEntryExt
* os::unix::fs::DirEntryExt::ino
* os::windows::fs::MetadataExt
* os::windows::fs::MetadataExt::file_attributes
* os::windows::fs::MetadataExt::creation_time
* os::windows::fs::MetadataExt::last_access_time
* os::windows::fs::MetadataExt::last_write_time
* os::windows::fs::MetadataExt::file_size
The `os::unix::fs::Metadata` structure was also removed entirely, moving all of
its associated methods into the `os::unix::fs::MetadataExt` trait instead. The
methods are all marked as `#[stable]` still.
As some minor cleanup, some deprecated and unstable fs apis were also removed:
* File::path
* Metadata::accessed
* Metadata::modified
Features that were explicitly left unstable include:
* fs::WalkDir - the semantics of this were not considered in the recent fs
expansion RFC.
* fs::DirBuilder - it's still not 100% clear if the naming is right here and if
the set of functionality exposed is appropriate.
* fs::canonicalize - the implementation on Windows here is specifically in
question as it always returns a verbatim path. Additionally the Unix
implementation is susceptible to buffer overflows on long paths unfortunately.
* fs::PathExt - as this is just a convenience trait, it is not stabilized at
this time.
* fs::set_file_times - this funciton is still waiting on a time abstraction.
2015-05-27 18:29:55 -05:00
|
|
|
#[stable(feature = "file_type", since = "1.1.0")]
|
2015-04-16 01:21:13 -05:00
|
|
|
pub fn is_symlink(&self) -> bool { self.0.is_symlink() }
|
|
|
|
}
|
|
|
|
|
2015-07-05 16:16:25 -05:00
|
|
|
impl AsInner<fs_imp::FileType> for FileType {
|
|
|
|
fn as_inner(&self) -> &fs_imp::FileType { &self.0 }
|
|
|
|
}
|
|
|
|
|
2015-02-02 23:39:14 -06:00
|
|
|
impl FromInner<fs_imp::FilePermissions> for Permissions {
|
|
|
|
fn from_inner(f: fs_imp::FilePermissions) -> Permissions {
|
|
|
|
Permissions(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-23 17:26:18 -06:00
|
|
|
impl AsInner<fs_imp::FilePermissions> for Permissions {
|
|
|
|
fn as_inner(&self) -> &fs_imp::FilePermissions { &self.0 }
|
|
|
|
}
|
|
|
|
|
std: Stabilize the `fs` module
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]: https://github.com/rust-lang/rfcs/issues/939
[breaking-change]
2015-03-03 21:18:29 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-02 23:39:14 -06:00
|
|
|
impl Iterator for ReadDir {
|
|
|
|
type Item = io::Result<DirEntry>;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<io::Result<DirEntry>> {
|
|
|
|
self.0.next().map(|entry| entry.map(DirEntry))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DirEntry {
|
|
|
|
/// Returns the full path to the file that this entry represents.
|
|
|
|
///
|
|
|
|
/// The full path is created by joining the original path to `read_dir` or
|
|
|
|
/// `walk_dir` with the filename of this entry.
|
2015-03-31 22:47:50 -05:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::fs;
|
|
|
|
/// # fn foo() -> std::io::Result<()> {
|
|
|
|
/// for entry in try!(fs::read_dir(".")) {
|
|
|
|
/// let dir = try!(entry);
|
|
|
|
/// println!("{:?}", dir.path());
|
|
|
|
/// }
|
|
|
|
/// # Ok(())
|
|
|
|
/// # }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// This prints output like:
|
|
|
|
///
|
|
|
|
/// ```text
|
|
|
|
/// "./whatever.txt"
|
|
|
|
/// "./foo.html"
|
|
|
|
/// "./hello_world.rs"
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// The exact text, of course, depends on what files you have in `.`.
|
std: Stabilize the `fs` module
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]: https://github.com/rust-lang/rfcs/issues/939
[breaking-change]
2015-03-03 21:18:29 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-02 23:39:14 -06:00
|
|
|
pub fn path(&self) -> PathBuf { self.0.path() }
|
2015-04-16 01:21:13 -05:00
|
|
|
|
|
|
|
/// Return the metadata for the file that this entry points at.
|
|
|
|
///
|
|
|
|
/// This function will not traverse symlinks if this entry points at a
|
|
|
|
/// symlink.
|
|
|
|
///
|
2015-11-30 20:23:19 -06:00
|
|
|
/// # Platform-specific behavior
|
2015-04-16 01:21:13 -05:00
|
|
|
///
|
|
|
|
/// On Windows this function is cheap to call (no extra system calls
|
|
|
|
/// needed), but on Unix platforms this function is the equivalent of
|
|
|
|
/// calling `symlink_metadata` on the path.
|
std: Stabilize a number of new fs features
This commit stabilizes the following APIs, slating them all to be cherry-picked
into the 1.1 release.
* fs::FileType (and transitively the derived trait implementations)
* fs::Metadata::file_type
* fs::FileType::is_dir
* fs::FileType::is_file
* fs::FileType::is_symlink
* fs::DirEntry::metadata
* fs::DirEntry::file_type
* fs::DirEntry::file_name
* fs::set_permissions
* fs::symlink_metadata
* os::raw::{self, *}
* os::{android, bitrig, linux, ...}::raw::{self, *}
* os::{android, bitrig, linux, ...}::fs::MetadataExt
* os::{android, bitrig, linux, ...}::fs::MetadataExt::as_raw_stat
* os::unix::fs::PermissionsExt
* os::unix::fs::PermissionsExt::mode
* os::unix::fs::PermissionsExt::set_mode
* os::unix::fs::PermissionsExt::from_mode
* os::unix::fs::OpenOptionsExt
* os::unix::fs::OpenOptionsExt::mode
* os::unix::fs::DirEntryExt
* os::unix::fs::DirEntryExt::ino
* os::windows::fs::MetadataExt
* os::windows::fs::MetadataExt::file_attributes
* os::windows::fs::MetadataExt::creation_time
* os::windows::fs::MetadataExt::last_access_time
* os::windows::fs::MetadataExt::last_write_time
* os::windows::fs::MetadataExt::file_size
The `os::unix::fs::Metadata` structure was also removed entirely, moving all of
its associated methods into the `os::unix::fs::MetadataExt` trait instead. The
methods are all marked as `#[stable]` still.
As some minor cleanup, some deprecated and unstable fs apis were also removed:
* File::path
* Metadata::accessed
* Metadata::modified
Features that were explicitly left unstable include:
* fs::WalkDir - the semantics of this were not considered in the recent fs
expansion RFC.
* fs::DirBuilder - it's still not 100% clear if the naming is right here and if
the set of functionality exposed is appropriate.
* fs::canonicalize - the implementation on Windows here is specifically in
question as it always returns a verbatim path. Additionally the Unix
implementation is susceptible to buffer overflows on long paths unfortunately.
* fs::PathExt - as this is just a convenience trait, it is not stabilized at
this time.
* fs::set_file_times - this funciton is still waiting on a time abstraction.
2015-05-27 18:29:55 -05:00
|
|
|
#[stable(feature = "dir_entry_ext", since = "1.1.0")]
|
2015-04-16 01:21:13 -05:00
|
|
|
pub fn metadata(&self) -> io::Result<Metadata> {
|
|
|
|
self.0.metadata().map(Metadata)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the file type for the file that this entry points at.
|
|
|
|
///
|
|
|
|
/// This function will not traverse symlinks if this entry points at a
|
|
|
|
/// symlink.
|
|
|
|
///
|
2015-11-30 20:23:19 -06:00
|
|
|
/// # Platform-specific behavior
|
2015-04-16 01:21:13 -05:00
|
|
|
///
|
|
|
|
/// On Windows and most Unix platforms this function is free (no extra
|
|
|
|
/// system calls needed), but some Unix platforms may require the equivalent
|
|
|
|
/// call to `symlink_metadata` to learn about the target file type.
|
std: Stabilize a number of new fs features
This commit stabilizes the following APIs, slating them all to be cherry-picked
into the 1.1 release.
* fs::FileType (and transitively the derived trait implementations)
* fs::Metadata::file_type
* fs::FileType::is_dir
* fs::FileType::is_file
* fs::FileType::is_symlink
* fs::DirEntry::metadata
* fs::DirEntry::file_type
* fs::DirEntry::file_name
* fs::set_permissions
* fs::symlink_metadata
* os::raw::{self, *}
* os::{android, bitrig, linux, ...}::raw::{self, *}
* os::{android, bitrig, linux, ...}::fs::MetadataExt
* os::{android, bitrig, linux, ...}::fs::MetadataExt::as_raw_stat
* os::unix::fs::PermissionsExt
* os::unix::fs::PermissionsExt::mode
* os::unix::fs::PermissionsExt::set_mode
* os::unix::fs::PermissionsExt::from_mode
* os::unix::fs::OpenOptionsExt
* os::unix::fs::OpenOptionsExt::mode
* os::unix::fs::DirEntryExt
* os::unix::fs::DirEntryExt::ino
* os::windows::fs::MetadataExt
* os::windows::fs::MetadataExt::file_attributes
* os::windows::fs::MetadataExt::creation_time
* os::windows::fs::MetadataExt::last_access_time
* os::windows::fs::MetadataExt::last_write_time
* os::windows::fs::MetadataExt::file_size
The `os::unix::fs::Metadata` structure was also removed entirely, moving all of
its associated methods into the `os::unix::fs::MetadataExt` trait instead. The
methods are all marked as `#[stable]` still.
As some minor cleanup, some deprecated and unstable fs apis were also removed:
* File::path
* Metadata::accessed
* Metadata::modified
Features that were explicitly left unstable include:
* fs::WalkDir - the semantics of this were not considered in the recent fs
expansion RFC.
* fs::DirBuilder - it's still not 100% clear if the naming is right here and if
the set of functionality exposed is appropriate.
* fs::canonicalize - the implementation on Windows here is specifically in
question as it always returns a verbatim path. Additionally the Unix
implementation is susceptible to buffer overflows on long paths unfortunately.
* fs::PathExt - as this is just a convenience trait, it is not stabilized at
this time.
* fs::set_file_times - this funciton is still waiting on a time abstraction.
2015-05-27 18:29:55 -05:00
|
|
|
#[stable(feature = "dir_entry_ext", since = "1.1.0")]
|
2015-04-16 01:21:13 -05:00
|
|
|
pub fn file_type(&self) -> io::Result<FileType> {
|
|
|
|
self.0.file_type().map(FileType)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the bare file name of this directory entry without any other
|
|
|
|
/// leading path component.
|
std: Stabilize a number of new fs features
This commit stabilizes the following APIs, slating them all to be cherry-picked
into the 1.1 release.
* fs::FileType (and transitively the derived trait implementations)
* fs::Metadata::file_type
* fs::FileType::is_dir
* fs::FileType::is_file
* fs::FileType::is_symlink
* fs::DirEntry::metadata
* fs::DirEntry::file_type
* fs::DirEntry::file_name
* fs::set_permissions
* fs::symlink_metadata
* os::raw::{self, *}
* os::{android, bitrig, linux, ...}::raw::{self, *}
* os::{android, bitrig, linux, ...}::fs::MetadataExt
* os::{android, bitrig, linux, ...}::fs::MetadataExt::as_raw_stat
* os::unix::fs::PermissionsExt
* os::unix::fs::PermissionsExt::mode
* os::unix::fs::PermissionsExt::set_mode
* os::unix::fs::PermissionsExt::from_mode
* os::unix::fs::OpenOptionsExt
* os::unix::fs::OpenOptionsExt::mode
* os::unix::fs::DirEntryExt
* os::unix::fs::DirEntryExt::ino
* os::windows::fs::MetadataExt
* os::windows::fs::MetadataExt::file_attributes
* os::windows::fs::MetadataExt::creation_time
* os::windows::fs::MetadataExt::last_access_time
* os::windows::fs::MetadataExt::last_write_time
* os::windows::fs::MetadataExt::file_size
The `os::unix::fs::Metadata` structure was also removed entirely, moving all of
its associated methods into the `os::unix::fs::MetadataExt` trait instead. The
methods are all marked as `#[stable]` still.
As some minor cleanup, some deprecated and unstable fs apis were also removed:
* File::path
* Metadata::accessed
* Metadata::modified
Features that were explicitly left unstable include:
* fs::WalkDir - the semantics of this were not considered in the recent fs
expansion RFC.
* fs::DirBuilder - it's still not 100% clear if the naming is right here and if
the set of functionality exposed is appropriate.
* fs::canonicalize - the implementation on Windows here is specifically in
question as it always returns a verbatim path. Additionally the Unix
implementation is susceptible to buffer overflows on long paths unfortunately.
* fs::PathExt - as this is just a convenience trait, it is not stabilized at
this time.
* fs::set_file_times - this funciton is still waiting on a time abstraction.
2015-05-27 18:29:55 -05:00
|
|
|
#[stable(feature = "dir_entry_ext", since = "1.1.0")]
|
2015-04-16 01:21:13 -05:00
|
|
|
pub fn file_name(&self) -> OsString {
|
|
|
|
self.0.file_name()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsInner<fs_imp::DirEntry> for DirEntry {
|
|
|
|
fn as_inner(&self) -> &fs_imp::DirEntry { &self.0 }
|
2015-02-02 23:39:14 -06:00
|
|
|
}
|
|
|
|
|
2015-08-10 18:13:15 -05:00
|
|
|
/// Removes a file from the filesystem.
|
2015-02-02 23:39:14 -06:00
|
|
|
///
|
2015-08-10 16:52:18 -05:00
|
|
|
/// Note that there is no
|
|
|
|
/// guarantee that the file is immediately deleted (e.g. depending on
|
2015-02-02 23:39:14 -06:00
|
|
|
/// platform, other open file descriptors may prevent immediate removal).
|
|
|
|
///
|
2015-11-30 20:23:19 -06:00
|
|
|
/// # Platform-specific behavior
|
2015-11-09 18:43:12 -06:00
|
|
|
///
|
|
|
|
/// This function currently corresponds to the `unlink` function on Unix
|
2015-11-09 19:13:10 -06:00
|
|
|
/// and the `DeleteFile` function on Windows.
|
|
|
|
/// Note that, this [may change in the future][changes].
|
2015-11-30 20:23:19 -06:00
|
|
|
/// [changes]: ../io/index.html#platform-specific-behavior
|
2015-11-09 18:43:12 -06:00
|
|
|
///
|
2015-02-02 23:39:14 -06:00
|
|
|
/// # Errors
|
|
|
|
///
|
2015-11-09 18:43:12 -06:00
|
|
|
/// This function will return an error in the following situations, but is not
|
|
|
|
/// limited to just these cases:
|
|
|
|
///
|
2015-11-09 19:13:10 -06:00
|
|
|
/// * `path` points to a directory.
|
|
|
|
/// * The user lacks permissions to remove the file.
|
2015-03-31 22:47:50 -05:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::fs;
|
|
|
|
///
|
|
|
|
/// # fn foo() -> std::io::Result<()> {
|
|
|
|
/// try!(fs::remove_file("a.txt"));
|
|
|
|
/// # Ok(())
|
|
|
|
/// # }
|
|
|
|
/// ```
|
std: Stabilize the `fs` module
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]: https://github.com/rust-lang/rfcs/issues/939
[breaking-change]
2015-03-03 21:18:29 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-03-18 11:14:54 -05:00
|
|
|
pub fn remove_file<P: AsRef<Path>>(path: P) -> io::Result<()> {
|
|
|
|
fs_imp::unlink(path.as_ref())
|
2015-02-02 23:39:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Given a path, query the file system to get information about a file,
|
|
|
|
/// directory, etc.
|
|
|
|
///
|
Deprecate std::fs::soft_link in favor of platform-specific versions
On Windows, when you create a symbolic link you must specify whether it
points to a directory or a file, even if it is created dangling, while
on Unix, the same symbolic link could point to a directory, a file, or
nothing at all. Furthermore, on Windows special privilege is necessary
to use a symbolic link, while on Unix, you can generally create a
symbolic link in any directory you have write privileges to.
This means that it is unlikely to be able to use symbolic links purely
portably; anyone who uses them will need to think about the cross
platform implications. This means that using platform-specific APIs
will make it easier to see where code will need to differ between the
platforms, rather than trying to provide some kind of compatibility
wrapper.
Furthermore, `soft_link` has no precedence in any other API, so to avoid
confusion, move back to the more standard `symlink` terminology.
Create a `std::os::unix::symlink` for the Unix version that is
destination type agnostic, as well as `std::os::windows::{symlink_file,
symlink_dir}` for Windows.
Because this is a stable API, leave a compatibility wrapper in
`std::fs::soft_link`, which calls `symlink` on Unix and `symlink_file`
on Windows, preserving the existing behavior of `soft_link`.
2015-04-09 02:22:44 -05:00
|
|
|
/// This function will traverse symbolic links to query information about the
|
2015-02-02 23:39:14 -06:00
|
|
|
/// destination file.
|
|
|
|
///
|
2015-11-30 20:23:19 -06:00
|
|
|
/// # Platform-specific behavior
|
2015-11-09 18:43:12 -06:00
|
|
|
///
|
|
|
|
/// This function currently corresponds to the `stat` function on Unix
|
2015-11-09 19:13:10 -06:00
|
|
|
/// and the `GetFileAttributesEx` function on Windows.
|
|
|
|
/// Note that, this [may change in the future][changes].
|
2015-11-30 20:23:19 -06:00
|
|
|
/// [changes]: ../io/index.html#platform-specific-behavior
|
2015-11-09 18:43:12 -06:00
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
///
|
|
|
|
/// This function will return an error in the following situations, but is not
|
|
|
|
/// limited to just these cases:
|
|
|
|
///
|
2015-11-09 19:13:10 -06:00
|
|
|
/// * The user lacks permissions to perform `metadata` call on `path`.
|
|
|
|
/// * `path` does not exist.
|
2015-11-09 18:43:12 -06:00
|
|
|
///
|
2015-03-11 20:11:40 -05:00
|
|
|
/// # Examples
|
2015-02-02 23:39:14 -06:00
|
|
|
///
|
2015-03-31 22:47:50 -05:00
|
|
|
/// ```rust
|
2015-02-02 23:39:14 -06:00
|
|
|
/// # fn foo() -> std::io::Result<()> {
|
|
|
|
/// use std::fs;
|
|
|
|
///
|
|
|
|
/// let attr = try!(fs::metadata("/some/file/path.txt"));
|
|
|
|
/// // inspect attr ...
|
|
|
|
/// # Ok(())
|
|
|
|
/// # }
|
|
|
|
/// ```
|
std: Stabilize the `fs` module
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]: https://github.com/rust-lang/rfcs/issues/939
[breaking-change]
2015-03-03 21:18:29 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-03-18 11:14:54 -05:00
|
|
|
pub fn metadata<P: AsRef<Path>>(path: P) -> io::Result<Metadata> {
|
|
|
|
fs_imp::stat(path.as_ref()).map(Metadata)
|
2015-02-02 23:39:14 -06:00
|
|
|
}
|
|
|
|
|
2015-04-16 01:21:13 -05:00
|
|
|
/// Query the metadata about a file without following symlinks.
|
|
|
|
///
|
2015-11-30 20:23:19 -06:00
|
|
|
/// # Platform-specific behavior
|
2015-11-09 18:43:12 -06:00
|
|
|
///
|
|
|
|
/// This function currently corresponds to the `lstat` function on Unix
|
2015-11-09 19:13:10 -06:00
|
|
|
/// and the `GetFileAttributesEx` function on Windows.
|
|
|
|
/// Note that, this [may change in the future][changes].
|
2015-11-30 20:23:19 -06:00
|
|
|
/// [changes]: ../io/index.html#platform-specific-behavior
|
2015-11-09 18:43:12 -06:00
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
///
|
|
|
|
/// This function will return an error in the following situations, but is not
|
|
|
|
/// limited to just these cases:
|
|
|
|
///
|
2015-11-09 19:13:10 -06:00
|
|
|
/// * The user lacks permissions to perform `metadata` call on `path`.
|
|
|
|
/// * `path` does not exist.
|
2015-11-09 18:43:12 -06:00
|
|
|
///
|
2015-04-16 01:21:13 -05:00
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # fn foo() -> std::io::Result<()> {
|
|
|
|
/// use std::fs;
|
|
|
|
///
|
|
|
|
/// let attr = try!(fs::symlink_metadata("/some/file/path.txt"));
|
|
|
|
/// // inspect attr ...
|
|
|
|
/// # Ok(())
|
|
|
|
/// # }
|
|
|
|
/// ```
|
std: Stabilize a number of new fs features
This commit stabilizes the following APIs, slating them all to be cherry-picked
into the 1.1 release.
* fs::FileType (and transitively the derived trait implementations)
* fs::Metadata::file_type
* fs::FileType::is_dir
* fs::FileType::is_file
* fs::FileType::is_symlink
* fs::DirEntry::metadata
* fs::DirEntry::file_type
* fs::DirEntry::file_name
* fs::set_permissions
* fs::symlink_metadata
* os::raw::{self, *}
* os::{android, bitrig, linux, ...}::raw::{self, *}
* os::{android, bitrig, linux, ...}::fs::MetadataExt
* os::{android, bitrig, linux, ...}::fs::MetadataExt::as_raw_stat
* os::unix::fs::PermissionsExt
* os::unix::fs::PermissionsExt::mode
* os::unix::fs::PermissionsExt::set_mode
* os::unix::fs::PermissionsExt::from_mode
* os::unix::fs::OpenOptionsExt
* os::unix::fs::OpenOptionsExt::mode
* os::unix::fs::DirEntryExt
* os::unix::fs::DirEntryExt::ino
* os::windows::fs::MetadataExt
* os::windows::fs::MetadataExt::file_attributes
* os::windows::fs::MetadataExt::creation_time
* os::windows::fs::MetadataExt::last_access_time
* os::windows::fs::MetadataExt::last_write_time
* os::windows::fs::MetadataExt::file_size
The `os::unix::fs::Metadata` structure was also removed entirely, moving all of
its associated methods into the `os::unix::fs::MetadataExt` trait instead. The
methods are all marked as `#[stable]` still.
As some minor cleanup, some deprecated and unstable fs apis were also removed:
* File::path
* Metadata::accessed
* Metadata::modified
Features that were explicitly left unstable include:
* fs::WalkDir - the semantics of this were not considered in the recent fs
expansion RFC.
* fs::DirBuilder - it's still not 100% clear if the naming is right here and if
the set of functionality exposed is appropriate.
* fs::canonicalize - the implementation on Windows here is specifically in
question as it always returns a verbatim path. Additionally the Unix
implementation is susceptible to buffer overflows on long paths unfortunately.
* fs::PathExt - as this is just a convenience trait, it is not stabilized at
this time.
* fs::set_file_times - this funciton is still waiting on a time abstraction.
2015-05-27 18:29:55 -05:00
|
|
|
#[stable(feature = "symlink_metadata", since = "1.1.0")]
|
2015-04-16 01:21:13 -05:00
|
|
|
pub fn symlink_metadata<P: AsRef<Path>>(path: P) -> io::Result<Metadata> {
|
|
|
|
fs_imp::lstat(path.as_ref()).map(Metadata)
|
|
|
|
}
|
|
|
|
|
2015-02-02 23:39:14 -06:00
|
|
|
/// Rename a file or directory to a new name.
|
|
|
|
///
|
2015-05-05 15:30:13 -05:00
|
|
|
/// This will not work if the new name is on a different mount point.
|
|
|
|
///
|
2015-11-30 20:23:19 -06:00
|
|
|
/// # Platform-specific behavior
|
2015-11-09 18:43:12 -06:00
|
|
|
///
|
|
|
|
/// This function currently corresponds to the `rename` function on Unix
|
|
|
|
/// and the `MoveFileEx` function with the `MOVEFILE_REPLACE_EXISTING` flag on Windows.
|
2015-11-09 19:13:10 -06:00
|
|
|
/// Note that, this [may change in the future][changes].
|
2015-11-30 20:23:19 -06:00
|
|
|
/// [changes]: ../io/index.html#platform-specific-behavior
|
2015-11-09 18:43:12 -06:00
|
|
|
///
|
2015-02-02 23:39:14 -06:00
|
|
|
/// # Errors
|
|
|
|
///
|
2015-11-09 18:43:12 -06:00
|
|
|
/// This function will return an error in the following situations, but is not
|
|
|
|
/// limited to just these cases:
|
|
|
|
///
|
2015-11-09 19:13:10 -06:00
|
|
|
/// * `from` does not exist.
|
|
|
|
/// * The user lacks permissions to view contents.
|
|
|
|
/// * `from` and `to` are on separate filesystems.
|
2015-03-31 22:47:50 -05:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::fs;
|
|
|
|
///
|
|
|
|
/// # fn foo() -> std::io::Result<()> {
|
2016-01-01 03:09:24 -06:00
|
|
|
/// try!(fs::rename("a.txt", "b.txt")); // Rename a.txt to b.txt
|
2015-03-31 22:47:50 -05:00
|
|
|
/// # Ok(())
|
|
|
|
/// # }
|
|
|
|
/// ```
|
std: Stabilize the `fs` module
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]: https://github.com/rust-lang/rfcs/issues/939
[breaking-change]
2015-03-03 21:18:29 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-03-18 11:14:54 -05:00
|
|
|
pub fn rename<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> io::Result<()> {
|
|
|
|
fs_imp::rename(from.as_ref(), to.as_ref())
|
2015-02-02 23:39:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Copies the contents of one file to another. This function will also
|
|
|
|
/// copy the permission bits of the original file to the destination file.
|
|
|
|
///
|
|
|
|
/// This function will **overwrite** the contents of `to`.
|
|
|
|
///
|
|
|
|
/// Note that if `from` and `to` both point to the same file, then the file
|
|
|
|
/// will likely get truncated by this operation.
|
|
|
|
///
|
2015-08-05 17:48:57 -05:00
|
|
|
/// On success, the total number of bytes copied is returned.
|
|
|
|
///
|
2015-11-30 20:23:19 -06:00
|
|
|
/// # Platform-specific behavior
|
2015-11-09 18:43:12 -06:00
|
|
|
///
|
|
|
|
/// This function currently corresponds to the `open` function in Unix
|
|
|
|
/// with `O_RDONLY` for `from` and `O_WRONLY`, `O_CREAT`, and `O_TRUNC` for `to`.
|
|
|
|
/// `O_CLOEXEC` is set for returned file descriptors.
|
|
|
|
/// On Windows, this function currently corresponds to `CopyFileEx`.
|
2015-11-09 19:13:10 -06:00
|
|
|
/// Note that, this [may change in the future][changes].
|
2015-11-30 20:23:19 -06:00
|
|
|
/// [changes]: ../io/index.html#platform-specific-behavior
|
2015-11-09 18:43:12 -06:00
|
|
|
///
|
2015-02-02 23:39:14 -06:00
|
|
|
/// # Errors
|
|
|
|
///
|
|
|
|
/// This function will return an error in the following situations, but is not
|
|
|
|
/// limited to just these cases:
|
|
|
|
///
|
2015-11-09 19:13:10 -06:00
|
|
|
/// * The `from` path is not a file.
|
|
|
|
/// * The `from` file does not exist.
|
2015-02-02 23:39:14 -06:00
|
|
|
/// * The current process does not have the permission rights to access
|
2015-11-09 19:13:10 -06:00
|
|
|
/// `from` or write `to`.
|
2015-03-31 22:47:50 -05:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// use std::fs;
|
|
|
|
///
|
2015-04-06 20:52:18 -05:00
|
|
|
/// # fn foo() -> std::io::Result<()> {
|
2016-01-01 03:09:24 -06:00
|
|
|
/// try!(fs::copy("foo.txt", "bar.txt")); // Copy foo.txt to bar.txt
|
2015-04-06 20:52:18 -05:00
|
|
|
/// # Ok(()) }
|
2015-03-31 22:47:50 -05:00
|
|
|
/// ```
|
std: Stabilize the `fs` module
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]: https://github.com/rust-lang/rfcs/issues/939
[breaking-change]
2015-03-03 21:18:29 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-03-18 11:14:54 -05:00
|
|
|
pub fn copy<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> io::Result<u64> {
|
2015-07-10 03:54:00 -05:00
|
|
|
fs_imp::copy(from.as_ref(), to.as_ref())
|
2015-02-02 23:39:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a new hard link on the filesystem.
|
|
|
|
///
|
|
|
|
/// The `dst` path will be a link pointing to the `src` path. Note that systems
|
|
|
|
/// often require these two paths to both be located on the same filesystem.
|
2015-03-31 22:47:50 -05:00
|
|
|
///
|
2015-11-30 20:23:19 -06:00
|
|
|
/// # Platform-specific behavior
|
2015-11-09 18:43:12 -06:00
|
|
|
///
|
|
|
|
/// This function currently corresponds to the `link` function on Unix
|
|
|
|
/// and the `CreateHardLink` function on Windows.
|
2015-11-09 19:13:10 -06:00
|
|
|
/// Note that, this [may change in the future][changes].
|
2015-11-30 20:23:19 -06:00
|
|
|
/// [changes]: ../io/index.html#platform-specific-behavior
|
2015-11-09 18:43:12 -06:00
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
///
|
|
|
|
/// This function will return an error in the following situations, but is not
|
|
|
|
/// limited to just these cases:
|
|
|
|
///
|
2015-11-09 19:13:10 -06:00
|
|
|
/// * The `src` path is not a file or doesn't exist.
|
2015-11-09 18:43:12 -06:00
|
|
|
///
|
2015-03-31 22:47:50 -05:00
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::fs;
|
|
|
|
///
|
|
|
|
/// # fn foo() -> std::io::Result<()> {
|
2016-01-01 03:09:24 -06:00
|
|
|
/// try!(fs::hard_link("a.txt", "b.txt")); // Hard link a.txt to b.txt
|
2015-03-31 22:47:50 -05:00
|
|
|
/// # Ok(())
|
|
|
|
/// # }
|
|
|
|
/// ```
|
std: Stabilize the `fs` module
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]: https://github.com/rust-lang/rfcs/issues/939
[breaking-change]
2015-03-03 21:18:29 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-03-18 11:14:54 -05:00
|
|
|
pub fn hard_link<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q) -> io::Result<()> {
|
|
|
|
fs_imp::link(src.as_ref(), dst.as_ref())
|
2015-02-02 23:39:14 -06:00
|
|
|
}
|
|
|
|
|
Deprecate std::fs::soft_link in favor of platform-specific versions
On Windows, when you create a symbolic link you must specify whether it
points to a directory or a file, even if it is created dangling, while
on Unix, the same symbolic link could point to a directory, a file, or
nothing at all. Furthermore, on Windows special privilege is necessary
to use a symbolic link, while on Unix, you can generally create a
symbolic link in any directory you have write privileges to.
This means that it is unlikely to be able to use symbolic links purely
portably; anyone who uses them will need to think about the cross
platform implications. This means that using platform-specific APIs
will make it easier to see where code will need to differ between the
platforms, rather than trying to provide some kind of compatibility
wrapper.
Furthermore, `soft_link` has no precedence in any other API, so to avoid
confusion, move back to the more standard `symlink` terminology.
Create a `std::os::unix::symlink` for the Unix version that is
destination type agnostic, as well as `std::os::windows::{symlink_file,
symlink_dir}` for Windows.
Because this is a stable API, leave a compatibility wrapper in
`std::fs::soft_link`, which calls `symlink` on Unix and `symlink_file`
on Windows, preserving the existing behavior of `soft_link`.
2015-04-09 02:22:44 -05:00
|
|
|
/// Creates a new symbolic link on the filesystem.
|
2015-02-02 23:39:14 -06:00
|
|
|
///
|
Deprecate std::fs::soft_link in favor of platform-specific versions
On Windows, when you create a symbolic link you must specify whether it
points to a directory or a file, even if it is created dangling, while
on Unix, the same symbolic link could point to a directory, a file, or
nothing at all. Furthermore, on Windows special privilege is necessary
to use a symbolic link, while on Unix, you can generally create a
symbolic link in any directory you have write privileges to.
This means that it is unlikely to be able to use symbolic links purely
portably; anyone who uses them will need to think about the cross
platform implications. This means that using platform-specific APIs
will make it easier to see where code will need to differ between the
platforms, rather than trying to provide some kind of compatibility
wrapper.
Furthermore, `soft_link` has no precedence in any other API, so to avoid
confusion, move back to the more standard `symlink` terminology.
Create a `std::os::unix::symlink` for the Unix version that is
destination type agnostic, as well as `std::os::windows::{symlink_file,
symlink_dir}` for Windows.
Because this is a stable API, leave a compatibility wrapper in
`std::fs::soft_link`, which calls `symlink` on Unix and `symlink_file`
on Windows, preserving the existing behavior of `soft_link`.
2015-04-09 02:22:44 -05:00
|
|
|
/// The `dst` path will be a symbolic link pointing to the `src` path.
|
|
|
|
/// On Windows, this will be a file symlink, not a directory symlink;
|
|
|
|
/// for this reason, the platform-specific `std::os::unix::fs::symlink`
|
|
|
|
/// and `std::os::windows::fs::{symlink_file, symlink_dir}` should be
|
|
|
|
/// used instead to make the intent explicit.
|
2015-03-31 22:47:50 -05:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::fs;
|
|
|
|
///
|
|
|
|
/// # fn foo() -> std::io::Result<()> {
|
|
|
|
/// try!(fs::soft_link("a.txt", "b.txt"));
|
|
|
|
/// # Ok(())
|
|
|
|
/// # }
|
|
|
|
/// ```
|
2015-12-12 06:55:28 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-11-20 07:11:20 -06:00
|
|
|
#[rustc_deprecated(since = "1.1.0",
|
Deprecate std::fs::soft_link in favor of platform-specific versions
On Windows, when you create a symbolic link you must specify whether it
points to a directory or a file, even if it is created dangling, while
on Unix, the same symbolic link could point to a directory, a file, or
nothing at all. Furthermore, on Windows special privilege is necessary
to use a symbolic link, while on Unix, you can generally create a
symbolic link in any directory you have write privileges to.
This means that it is unlikely to be able to use symbolic links purely
portably; anyone who uses them will need to think about the cross
platform implications. This means that using platform-specific APIs
will make it easier to see where code will need to differ between the
platforms, rather than trying to provide some kind of compatibility
wrapper.
Furthermore, `soft_link` has no precedence in any other API, so to avoid
confusion, move back to the more standard `symlink` terminology.
Create a `std::os::unix::symlink` for the Unix version that is
destination type agnostic, as well as `std::os::windows::{symlink_file,
symlink_dir}` for Windows.
Because this is a stable API, leave a compatibility wrapper in
`std::fs::soft_link`, which calls `symlink` on Unix and `symlink_file`
on Windows, preserving the existing behavior of `soft_link`.
2015-04-09 02:22:44 -05:00
|
|
|
reason = "replaced with std::os::unix::fs::symlink and \
|
|
|
|
std::os::windows::fs::{symlink_file, symlink_dir}")]
|
2015-03-18 11:14:54 -05:00
|
|
|
pub fn soft_link<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q) -> io::Result<()> {
|
|
|
|
fs_imp::symlink(src.as_ref(), dst.as_ref())
|
2015-02-02 23:39:14 -06:00
|
|
|
}
|
|
|
|
|
Deprecate std::fs::soft_link in favor of platform-specific versions
On Windows, when you create a symbolic link you must specify whether it
points to a directory or a file, even if it is created dangling, while
on Unix, the same symbolic link could point to a directory, a file, or
nothing at all. Furthermore, on Windows special privilege is necessary
to use a symbolic link, while on Unix, you can generally create a
symbolic link in any directory you have write privileges to.
This means that it is unlikely to be able to use symbolic links purely
portably; anyone who uses them will need to think about the cross
platform implications. This means that using platform-specific APIs
will make it easier to see where code will need to differ between the
platforms, rather than trying to provide some kind of compatibility
wrapper.
Furthermore, `soft_link` has no precedence in any other API, so to avoid
confusion, move back to the more standard `symlink` terminology.
Create a `std::os::unix::symlink` for the Unix version that is
destination type agnostic, as well as `std::os::windows::{symlink_file,
symlink_dir}` for Windows.
Because this is a stable API, leave a compatibility wrapper in
`std::fs::soft_link`, which calls `symlink` on Unix and `symlink_file`
on Windows, preserving the existing behavior of `soft_link`.
2015-04-09 02:22:44 -05:00
|
|
|
/// Reads a symbolic link, returning the file that the link points to.
|
2015-02-02 23:39:14 -06:00
|
|
|
///
|
2015-11-30 20:23:19 -06:00
|
|
|
/// # Platform-specific behavior
|
2015-11-09 18:43:12 -06:00
|
|
|
///
|
|
|
|
/// This function currently corresponds to the `readlink` function on Unix
|
|
|
|
/// and the `CreateFile` function with `FILE_FLAG_OPEN_REPARSE_POINT` and
|
|
|
|
/// `FILE_FLAG_BACKUP_SEMANTICS` flags on Windows.
|
2015-11-09 19:13:10 -06:00
|
|
|
/// Note that, this [may change in the future][changes].
|
2015-11-30 20:23:19 -06:00
|
|
|
/// [changes]: ../io/index.html#platform-specific-behavior
|
2015-11-09 18:43:12 -06:00
|
|
|
///
|
2015-02-02 23:39:14 -06:00
|
|
|
/// # Errors
|
|
|
|
///
|
2015-11-09 18:43:12 -06:00
|
|
|
/// This function will return an error in the following situations, but is not
|
|
|
|
/// limited to just these cases:
|
|
|
|
///
|
2015-11-09 19:13:10 -06:00
|
|
|
/// * `path` is not a symbolic link.
|
|
|
|
/// * `path` does not exist.
|
2015-03-31 22:47:50 -05:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::fs;
|
|
|
|
///
|
|
|
|
/// # fn foo() -> std::io::Result<()> {
|
|
|
|
/// let path = try!(fs::read_link("a.txt"));
|
|
|
|
/// # Ok(())
|
|
|
|
/// # }
|
|
|
|
/// ```
|
std: Stabilize the `fs` module
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]: https://github.com/rust-lang/rfcs/issues/939
[breaking-change]
2015-03-03 21:18:29 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-03-18 11:14:54 -05:00
|
|
|
pub fn read_link<P: AsRef<Path>>(path: P) -> io::Result<PathBuf> {
|
|
|
|
fs_imp::readlink(path.as_ref())
|
2015-02-02 23:39:14 -06:00
|
|
|
}
|
|
|
|
|
2015-04-16 01:21:13 -05:00
|
|
|
/// Returns the canonical form of a path with all intermediate components
|
|
|
|
/// normalized and symbolic links resolved.
|
std: Stabilize library APIs for 1.5
This commit stabilizes and deprecates library APIs whose FCP has closed in the
last cycle, specifically:
Stabilized APIs:
* `fs::canonicalize`
* `Path::{metadata, symlink_metadata, canonicalize, read_link, read_dir, exists,
is_file, is_dir}` - all moved to inherent methods from the `PathExt` trait.
* `Formatter::fill`
* `Formatter::width`
* `Formatter::precision`
* `Formatter::sign_plus`
* `Formatter::sign_minus`
* `Formatter::alternate`
* `Formatter::sign_aware_zero_pad`
* `string::ParseError`
* `Utf8Error::valid_up_to`
* `Iterator::{cmp, partial_cmp, eq, ne, lt, le, gt, ge}`
* `<[T]>::split_{first,last}{,_mut}`
* `Condvar::wait_timeout` - note that `wait_timeout_ms` is not yet deprecated
but will be once 1.5 is released.
* `str::{R,}MatchIndices`
* `str::{r,}match_indices`
* `char::from_u32_unchecked`
* `VecDeque::insert`
* `VecDeque::shrink_to_fit`
* `VecDeque::as_slices`
* `VecDeque::as_mut_slices`
* `VecDeque::swap_remove_front` - (renamed from `swap_front_remove`)
* `VecDeque::swap_remove_back` - (renamed from `swap_back_remove`)
* `Vec::resize`
* `str::slice_mut_unchecked`
* `FileTypeExt`
* `FileTypeExt::{is_block_device, is_char_device, is_fifo, is_socket}`
* `BinaryHeap::from` - `from_vec` deprecated in favor of this
* `BinaryHeap::into_vec` - plus a `Into` impl
* `BinaryHeap::into_sorted_vec`
Deprecated APIs
* `slice::ref_slice`
* `slice::mut_ref_slice`
* `iter::{range_inclusive, RangeInclusive}`
* `std::dynamic_lib`
Closes #27706
Closes #27725
cc #27726 (align not stabilized yet)
Closes #27734
Closes #27737
Closes #27742
Closes #27743
Closes #27772
Closes #27774
Closes #27777
Closes #27781
cc #27788 (a few remaining methods though)
Closes #27790
Closes #27793
Closes #27796
Closes #27810
cc #28147 (not all parts stabilized)
2015-10-22 18:28:45 -05:00
|
|
|
///
|
2015-11-30 20:23:19 -06:00
|
|
|
/// # Platform-specific behavior
|
2015-11-09 18:43:12 -06:00
|
|
|
///
|
|
|
|
/// This function currently corresponds to the `realpath` function on Unix
|
|
|
|
/// and the `CreateFile` and `GetFinalPathNameByHandle` functions on Windows.
|
2015-11-09 19:13:10 -06:00
|
|
|
/// Note that, this [may change in the future][changes].
|
2015-11-30 20:23:19 -06:00
|
|
|
/// [changes]: ../io/index.html#platform-specific-behavior
|
2015-11-09 18:43:12 -06:00
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
///
|
|
|
|
/// This function will return an error in the following situations, but is not
|
|
|
|
/// limited to just these cases:
|
|
|
|
///
|
2015-11-09 19:13:10 -06:00
|
|
|
/// * `path` does not exist.
|
|
|
|
/// * A component in path is not a directory.
|
std: Stabilize library APIs for 1.5
This commit stabilizes and deprecates library APIs whose FCP has closed in the
last cycle, specifically:
Stabilized APIs:
* `fs::canonicalize`
* `Path::{metadata, symlink_metadata, canonicalize, read_link, read_dir, exists,
is_file, is_dir}` - all moved to inherent methods from the `PathExt` trait.
* `Formatter::fill`
* `Formatter::width`
* `Formatter::precision`
* `Formatter::sign_plus`
* `Formatter::sign_minus`
* `Formatter::alternate`
* `Formatter::sign_aware_zero_pad`
* `string::ParseError`
* `Utf8Error::valid_up_to`
* `Iterator::{cmp, partial_cmp, eq, ne, lt, le, gt, ge}`
* `<[T]>::split_{first,last}{,_mut}`
* `Condvar::wait_timeout` - note that `wait_timeout_ms` is not yet deprecated
but will be once 1.5 is released.
* `str::{R,}MatchIndices`
* `str::{r,}match_indices`
* `char::from_u32_unchecked`
* `VecDeque::insert`
* `VecDeque::shrink_to_fit`
* `VecDeque::as_slices`
* `VecDeque::as_mut_slices`
* `VecDeque::swap_remove_front` - (renamed from `swap_front_remove`)
* `VecDeque::swap_remove_back` - (renamed from `swap_back_remove`)
* `Vec::resize`
* `str::slice_mut_unchecked`
* `FileTypeExt`
* `FileTypeExt::{is_block_device, is_char_device, is_fifo, is_socket}`
* `BinaryHeap::from` - `from_vec` deprecated in favor of this
* `BinaryHeap::into_vec` - plus a `Into` impl
* `BinaryHeap::into_sorted_vec`
Deprecated APIs
* `slice::ref_slice`
* `slice::mut_ref_slice`
* `iter::{range_inclusive, RangeInclusive}`
* `std::dynamic_lib`
Closes #27706
Closes #27725
cc #27726 (align not stabilized yet)
Closes #27734
Closes #27737
Closes #27742
Closes #27743
Closes #27772
Closes #27774
Closes #27777
Closes #27781
cc #27788 (a few remaining methods though)
Closes #27790
Closes #27793
Closes #27796
Closes #27810
cc #28147 (not all parts stabilized)
2015-10-22 18:28:45 -05:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::fs;
|
|
|
|
///
|
|
|
|
/// # fn foo() -> std::io::Result<()> {
|
|
|
|
/// let path = try!(fs::canonicalize("../a/../foo.txt"));
|
|
|
|
/// # Ok(())
|
|
|
|
/// # }
|
|
|
|
/// ```
|
|
|
|
#[stable(feature = "fs_canonicalize", since = "1.5.0")]
|
2015-04-16 01:21:13 -05:00
|
|
|
pub fn canonicalize<P: AsRef<Path>>(path: P) -> io::Result<PathBuf> {
|
|
|
|
fs_imp::canonicalize(path.as_ref())
|
|
|
|
}
|
|
|
|
|
2015-04-13 09:21:32 -05:00
|
|
|
/// Creates a new, empty directory at the provided path
|
2015-02-02 23:39:14 -06:00
|
|
|
///
|
2015-11-30 20:23:19 -06:00
|
|
|
/// # Platform-specific behavior
|
2015-11-09 18:43:12 -06:00
|
|
|
///
|
|
|
|
/// This function currently corresponds to the `mkdir` function on Unix
|
|
|
|
/// and the `CreateDirectory` function on Windows.
|
2015-11-09 19:13:10 -06:00
|
|
|
/// Note that, this [may change in the future][changes].
|
2015-11-30 20:23:19 -06:00
|
|
|
/// [changes]: ../io/index.html#platform-specific-behavior
|
2015-11-09 18:43:12 -06:00
|
|
|
///
|
2015-03-31 22:47:50 -05:00
|
|
|
/// # Errors
|
|
|
|
///
|
2015-11-09 18:43:12 -06:00
|
|
|
/// This function will return an error in the following situations, but is not
|
|
|
|
/// limited to just these cases:
|
|
|
|
///
|
2015-11-09 19:13:10 -06:00
|
|
|
/// * User lacks permissions to create directory at `path`.
|
|
|
|
/// * `path` already exists.
|
2015-03-31 22:47:50 -05:00
|
|
|
///
|
2015-03-11 20:11:40 -05:00
|
|
|
/// # Examples
|
2015-02-02 23:39:14 -06:00
|
|
|
///
|
2015-03-12 21:42:38 -05:00
|
|
|
/// ```
|
2015-02-02 23:39:14 -06:00
|
|
|
/// use std::fs;
|
|
|
|
///
|
2015-03-31 22:47:50 -05:00
|
|
|
/// # fn foo() -> std::io::Result<()> {
|
|
|
|
/// try!(fs::create_dir("/some/dir"));
|
|
|
|
/// # Ok(())
|
|
|
|
/// # }
|
2015-02-02 23:39:14 -06:00
|
|
|
/// ```
|
std: Stabilize the `fs` module
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]: https://github.com/rust-lang/rfcs/issues/939
[breaking-change]
2015-03-03 21:18:29 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-03-18 11:14:54 -05:00
|
|
|
pub fn create_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
|
2015-04-27 19:29:35 -05:00
|
|
|
DirBuilder::new().create(path.as_ref())
|
2015-02-02 23:39:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Recursively create a directory and all of its parent components if they
|
|
|
|
/// are missing.
|
|
|
|
///
|
2015-11-30 20:23:19 -06:00
|
|
|
/// # Platform-specific behavior
|
2015-11-09 18:43:12 -06:00
|
|
|
///
|
|
|
|
/// This function currently corresponds to the `mkdir` function on Unix
|
|
|
|
/// and the `CreateDirectory` function on Windows.
|
2015-11-09 19:13:10 -06:00
|
|
|
/// Note that, this [may change in the future][changes].
|
2015-11-30 20:23:19 -06:00
|
|
|
/// [changes]: ../io/index.html#platform-specific-behavior
|
2015-11-09 18:43:12 -06:00
|
|
|
///
|
2015-02-02 23:39:14 -06:00
|
|
|
/// # Errors
|
|
|
|
///
|
2015-11-09 18:43:12 -06:00
|
|
|
/// This function will return an error in the following situations, but is not
|
|
|
|
/// limited to just these cases:
|
|
|
|
///
|
|
|
|
/// * If any directory in the path specified by `path`
|
2015-02-02 23:39:14 -06:00
|
|
|
/// does not already exist and it could not be created otherwise. The specific
|
|
|
|
/// error conditions for when a directory is being created (after it is
|
|
|
|
/// determined to not exist) are outlined by `fs::create_dir`.
|
2015-03-31 22:47:50 -05:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::fs;
|
|
|
|
///
|
|
|
|
/// # fn foo() -> std::io::Result<()> {
|
|
|
|
/// try!(fs::create_dir_all("/some/dir"));
|
|
|
|
/// # Ok(())
|
|
|
|
/// # }
|
|
|
|
/// ```
|
std: Stabilize the `fs` module
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]: https://github.com/rust-lang/rfcs/issues/939
[breaking-change]
2015-03-03 21:18:29 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-03-18 11:14:54 -05:00
|
|
|
pub fn create_dir_all<P: AsRef<Path>>(path: P) -> io::Result<()> {
|
2015-04-27 19:29:35 -05:00
|
|
|
DirBuilder::new().recursive(true).create(path.as_ref())
|
2015-02-02 23:39:14 -06:00
|
|
|
}
|
|
|
|
|
2015-04-13 09:21:32 -05:00
|
|
|
/// Removes an existing, empty directory.
|
2015-02-02 23:39:14 -06:00
|
|
|
///
|
2015-11-30 20:23:19 -06:00
|
|
|
/// # Platform-specific behavior
|
2015-11-09 18:43:12 -06:00
|
|
|
///
|
|
|
|
/// This function currently corresponds to the `rmdir` function on Unix
|
|
|
|
/// and the `RemoveDirectory` function on Windows.
|
2015-11-09 19:13:10 -06:00
|
|
|
/// Note that, this [may change in the future][changes].
|
2015-11-30 20:23:19 -06:00
|
|
|
/// [changes]: ../io/index.html#platform-specific-behavior
|
2015-11-09 18:43:12 -06:00
|
|
|
///
|
2015-03-31 22:47:50 -05:00
|
|
|
/// # Errors
|
|
|
|
///
|
2015-11-09 18:43:12 -06:00
|
|
|
/// This function will return an error in the following situations, but is not
|
|
|
|
/// limited to just these cases:
|
|
|
|
///
|
2015-11-09 19:13:10 -06:00
|
|
|
/// * The user lacks permissions to remove the directory at the provided `path`.
|
|
|
|
/// * The directory isn't empty.
|
2015-03-31 22:47:50 -05:00
|
|
|
///
|
2015-03-11 20:11:40 -05:00
|
|
|
/// # Examples
|
2015-02-02 23:39:14 -06:00
|
|
|
///
|
2015-03-12 21:42:38 -05:00
|
|
|
/// ```
|
2015-02-02 23:39:14 -06:00
|
|
|
/// use std::fs;
|
|
|
|
///
|
2015-03-31 22:47:50 -05:00
|
|
|
/// # fn foo() -> std::io::Result<()> {
|
|
|
|
/// try!(fs::remove_dir("/some/dir"));
|
|
|
|
/// # Ok(())
|
|
|
|
/// # }
|
2015-02-02 23:39:14 -06:00
|
|
|
/// ```
|
std: Stabilize the `fs` module
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]: https://github.com/rust-lang/rfcs/issues/939
[breaking-change]
2015-03-03 21:18:29 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-03-18 11:14:54 -05:00
|
|
|
pub fn remove_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
|
|
|
|
fs_imp::rmdir(path.as_ref())
|
2015-02-02 23:39:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Removes a directory at this path, after removing all its contents. Use
|
|
|
|
/// carefully!
|
|
|
|
///
|
Deprecate std::fs::soft_link in favor of platform-specific versions
On Windows, when you create a symbolic link you must specify whether it
points to a directory or a file, even if it is created dangling, while
on Unix, the same symbolic link could point to a directory, a file, or
nothing at all. Furthermore, on Windows special privilege is necessary
to use a symbolic link, while on Unix, you can generally create a
symbolic link in any directory you have write privileges to.
This means that it is unlikely to be able to use symbolic links purely
portably; anyone who uses them will need to think about the cross
platform implications. This means that using platform-specific APIs
will make it easier to see where code will need to differ between the
platforms, rather than trying to provide some kind of compatibility
wrapper.
Furthermore, `soft_link` has no precedence in any other API, so to avoid
confusion, move back to the more standard `symlink` terminology.
Create a `std::os::unix::symlink` for the Unix version that is
destination type agnostic, as well as `std::os::windows::{symlink_file,
symlink_dir}` for Windows.
Because this is a stable API, leave a compatibility wrapper in
`std::fs::soft_link`, which calls `symlink` on Unix and `symlink_file`
on Windows, preserving the existing behavior of `soft_link`.
2015-04-09 02:22:44 -05:00
|
|
|
/// This function does **not** follow symbolic links and it will simply remove the
|
|
|
|
/// symbolic link itself.
|
2015-02-02 23:39:14 -06:00
|
|
|
///
|
2015-11-30 20:23:19 -06:00
|
|
|
/// # Platform-specific behavior
|
2015-11-09 18:43:12 -06:00
|
|
|
///
|
|
|
|
/// This function currently corresponds to `opendir`, `lstat`, `rm` and `rmdir` functions on Unix
|
|
|
|
/// and the `FindFirstFile`, `GetFileAttributesEx`, `DeleteFile`, and `RemoveDirectory` functions
|
|
|
|
/// on Windows.
|
2015-11-09 19:13:10 -06:00
|
|
|
/// Note that, this [may change in the future][changes].
|
2015-11-30 20:23:19 -06:00
|
|
|
/// [changes]: ../io/index.html#platform-specific-behavior
|
2015-11-09 18:43:12 -06:00
|
|
|
///
|
2015-02-02 23:39:14 -06:00
|
|
|
/// # Errors
|
|
|
|
///
|
2015-03-31 22:47:50 -05:00
|
|
|
/// See `file::remove_file` and `fs::remove_dir`.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::fs;
|
|
|
|
///
|
|
|
|
/// # fn foo() -> std::io::Result<()> {
|
|
|
|
/// try!(fs::remove_dir_all("/some/dir"));
|
|
|
|
/// # Ok(())
|
|
|
|
/// # }
|
|
|
|
/// ```
|
std: Stabilize the `fs` module
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]: https://github.com/rust-lang/rfcs/issues/939
[breaking-change]
2015-03-03 21:18:29 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-03-18 11:14:54 -05:00
|
|
|
pub fn remove_dir_all<P: AsRef<Path>>(path: P) -> io::Result<()> {
|
2016-02-02 00:04:55 -06:00
|
|
|
fs_imp::remove_dir_all(path.as_ref())
|
2015-02-02 23:39:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns an iterator over the entries within a directory.
|
|
|
|
///
|
|
|
|
/// The iterator will yield instances of `io::Result<DirEntry>`. New errors may
|
|
|
|
/// be encountered after an iterator is initially constructed.
|
|
|
|
///
|
2015-11-30 20:23:19 -06:00
|
|
|
/// # Platform-specific behavior
|
2015-11-09 18:43:12 -06:00
|
|
|
///
|
|
|
|
/// This function currently corresponds to the `opendir` function on Unix
|
|
|
|
/// and the `FindFirstFile` function on Windows.
|
2015-11-09 19:13:10 -06:00
|
|
|
/// Note that, this [may change in the future][changes].
|
2015-11-30 20:23:19 -06:00
|
|
|
/// [changes]: ../io/index.html#platform-specific-behavior
|
2015-11-09 18:43:12 -06:00
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
///
|
|
|
|
/// This function will return an error in the following situations, but is not
|
|
|
|
/// limited to just these cases:
|
|
|
|
///
|
2015-11-09 19:13:10 -06:00
|
|
|
/// * The provided `path` doesn't exist.
|
|
|
|
/// * The process lacks permissions to view the contents.
|
|
|
|
/// * The `path` points at a non-directory file.
|
2015-11-09 18:43:12 -06:00
|
|
|
///
|
2015-03-11 20:11:40 -05:00
|
|
|
/// # Examples
|
2015-02-02 23:39:14 -06:00
|
|
|
///
|
2015-03-12 21:42:38 -05:00
|
|
|
/// ```
|
2015-02-02 23:39:14 -06:00
|
|
|
/// use std::io;
|
2015-05-16 18:07:52 -05:00
|
|
|
/// use std::fs::{self, DirEntry};
|
2015-02-02 23:39:14 -06:00
|
|
|
/// use std::path::Path;
|
|
|
|
///
|
|
|
|
/// // one possible implementation of fs::walk_dir only visiting files
|
2015-05-16 18:07:52 -05:00
|
|
|
/// fn visit_dirs(dir: &Path, cb: &Fn(&DirEntry)) -> io::Result<()> {
|
|
|
|
/// if try!(fs::metadata(dir)).is_dir() {
|
2015-02-02 23:39:14 -06:00
|
|
|
/// for entry in try!(fs::read_dir(dir)) {
|
|
|
|
/// let entry = try!(entry);
|
2015-05-16 18:07:52 -05:00
|
|
|
/// if try!(fs::metadata(entry.path())).is_dir() {
|
2015-02-02 23:39:14 -06:00
|
|
|
/// try!(visit_dirs(&entry.path(), cb));
|
|
|
|
/// } else {
|
2015-05-16 18:07:52 -05:00
|
|
|
/// cb(&entry);
|
2015-02-02 23:39:14 -06:00
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// Ok(())
|
|
|
|
/// }
|
|
|
|
/// ```
|
std: Stabilize the `fs` module
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]: https://github.com/rust-lang/rfcs/issues/939
[breaking-change]
2015-03-03 21:18:29 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-03-18 11:14:54 -05:00
|
|
|
pub fn read_dir<P: AsRef<Path>>(path: P) -> io::Result<ReadDir> {
|
|
|
|
fs_imp::readdir(path.as_ref()).map(ReadDir)
|
2015-02-02 23:39:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns an iterator that will recursively walk the directory structure
|
|
|
|
/// rooted at `path`.
|
|
|
|
///
|
|
|
|
/// The path given will not be iterated over, and this will perform iteration in
|
|
|
|
/// some top-down order. The contents of unreadable subdirectories are ignored.
|
|
|
|
///
|
|
|
|
/// The iterator will yield instances of `io::Result<DirEntry>`. New errors may
|
|
|
|
/// be encountered after an iterator is initially constructed.
|
std: Stabilize the `fs` module
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]: https://github.com/rust-lang/rfcs/issues/939
[breaking-change]
2015-03-03 21:18:29 -06:00
|
|
|
#[unstable(feature = "fs_walk",
|
|
|
|
reason = "the precise semantics and defaults for a recursive walk \
|
|
|
|
may change and this may end up accounting for files such \
|
2015-08-13 12:12:38 -05:00
|
|
|
as symlinks differently",
|
|
|
|
issue = "27707")]
|
2015-12-02 19:31:49 -06:00
|
|
|
#[rustc_deprecated(reason = "superceded by the walkdir crate",
|
|
|
|
since = "1.6.0")]
|
|
|
|
#[allow(deprecated)]
|
2015-03-18 11:14:54 -05:00
|
|
|
pub fn walk_dir<P: AsRef<Path>>(path: P) -> io::Result<WalkDir> {
|
2015-09-09 14:37:59 -05:00
|
|
|
_walk_dir(path.as_ref())
|
|
|
|
}
|
|
|
|
|
2015-12-02 19:31:49 -06:00
|
|
|
#[allow(deprecated)]
|
2015-09-09 14:37:59 -05:00
|
|
|
fn _walk_dir(path: &Path) -> io::Result<WalkDir> {
|
2015-02-02 23:39:14 -06:00
|
|
|
let start = try!(read_dir(path));
|
|
|
|
Ok(WalkDir { cur: Some(start), stack: Vec::new() })
|
|
|
|
}
|
|
|
|
|
2015-08-13 12:12:38 -05:00
|
|
|
#[unstable(feature = "fs_walk", issue = "27707")]
|
2015-12-02 19:31:49 -06:00
|
|
|
#[rustc_deprecated(reason = "superceded by the walkdir crate",
|
|
|
|
since = "1.6.0")]
|
|
|
|
#[allow(deprecated)]
|
2015-02-02 23:39:14 -06:00
|
|
|
impl Iterator for WalkDir {
|
|
|
|
type Item = io::Result<DirEntry>;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<io::Result<DirEntry>> {
|
|
|
|
loop {
|
|
|
|
if let Some(ref mut cur) = self.cur {
|
|
|
|
match cur.next() {
|
|
|
|
Some(Err(e)) => return Some(Err(e)),
|
|
|
|
Some(Ok(next)) => {
|
|
|
|
let path = next.path();
|
|
|
|
if path.is_dir() {
|
|
|
|
self.stack.push(read_dir(&*path));
|
|
|
|
}
|
|
|
|
return Some(Ok(next))
|
|
|
|
}
|
|
|
|
None => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.cur = None;
|
|
|
|
match self.stack.pop() {
|
|
|
|
Some(Err(e)) => return Some(Err(e)),
|
|
|
|
Some(Ok(next)) => self.cur = Some(next),
|
|
|
|
None => return None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Changes the permissions found on a file or a directory.
|
|
|
|
///
|
2015-11-30 20:23:19 -06:00
|
|
|
/// # Platform-specific behavior
|
2015-11-09 18:43:12 -06:00
|
|
|
///
|
|
|
|
/// This function currently corresponds to the `chmod` function on Unix
|
|
|
|
/// and the `SetFileAttributes` function on Windows.
|
2015-11-09 19:13:10 -06:00
|
|
|
/// Note that, this [may change in the future][changes].
|
2015-11-30 20:23:19 -06:00
|
|
|
/// [changes]: ../io/index.html#platform-specific-behavior
|
2015-11-09 18:43:12 -06:00
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
///
|
|
|
|
/// This function will return an error in the following situations, but is not
|
|
|
|
/// limited to just these cases:
|
|
|
|
///
|
2015-11-09 19:13:10 -06:00
|
|
|
/// * `path` does not exist.
|
|
|
|
/// * The user lacks the permission to change attributes of the file.
|
2015-11-09 18:43:12 -06:00
|
|
|
///
|
2015-03-11 20:11:40 -05:00
|
|
|
/// # Examples
|
2015-02-02 23:39:14 -06:00
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// # fn foo() -> std::io::Result<()> {
|
|
|
|
/// use std::fs;
|
|
|
|
///
|
|
|
|
/// let mut perms = try!(fs::metadata("foo.txt")).permissions();
|
|
|
|
/// perms.set_readonly(true);
|
|
|
|
/// try!(fs::set_permissions("foo.txt", perms));
|
|
|
|
/// # Ok(())
|
|
|
|
/// # }
|
|
|
|
/// ```
|
std: Stabilize a number of new fs features
This commit stabilizes the following APIs, slating them all to be cherry-picked
into the 1.1 release.
* fs::FileType (and transitively the derived trait implementations)
* fs::Metadata::file_type
* fs::FileType::is_dir
* fs::FileType::is_file
* fs::FileType::is_symlink
* fs::DirEntry::metadata
* fs::DirEntry::file_type
* fs::DirEntry::file_name
* fs::set_permissions
* fs::symlink_metadata
* os::raw::{self, *}
* os::{android, bitrig, linux, ...}::raw::{self, *}
* os::{android, bitrig, linux, ...}::fs::MetadataExt
* os::{android, bitrig, linux, ...}::fs::MetadataExt::as_raw_stat
* os::unix::fs::PermissionsExt
* os::unix::fs::PermissionsExt::mode
* os::unix::fs::PermissionsExt::set_mode
* os::unix::fs::PermissionsExt::from_mode
* os::unix::fs::OpenOptionsExt
* os::unix::fs::OpenOptionsExt::mode
* os::unix::fs::DirEntryExt
* os::unix::fs::DirEntryExt::ino
* os::windows::fs::MetadataExt
* os::windows::fs::MetadataExt::file_attributes
* os::windows::fs::MetadataExt::creation_time
* os::windows::fs::MetadataExt::last_access_time
* os::windows::fs::MetadataExt::last_write_time
* os::windows::fs::MetadataExt::file_size
The `os::unix::fs::Metadata` structure was also removed entirely, moving all of
its associated methods into the `os::unix::fs::MetadataExt` trait instead. The
methods are all marked as `#[stable]` still.
As some minor cleanup, some deprecated and unstable fs apis were also removed:
* File::path
* Metadata::accessed
* Metadata::modified
Features that were explicitly left unstable include:
* fs::WalkDir - the semantics of this were not considered in the recent fs
expansion RFC.
* fs::DirBuilder - it's still not 100% clear if the naming is right here and if
the set of functionality exposed is appropriate.
* fs::canonicalize - the implementation on Windows here is specifically in
question as it always returns a verbatim path. Additionally the Unix
implementation is susceptible to buffer overflows on long paths unfortunately.
* fs::PathExt - as this is just a convenience trait, it is not stabilized at
this time.
* fs::set_file_times - this funciton is still waiting on a time abstraction.
2015-05-27 18:29:55 -05:00
|
|
|
#[stable(feature = "set_permissions", since = "1.1.0")]
|
|
|
|
pub fn set_permissions<P: AsRef<Path>>(path: P, perm: Permissions)
|
|
|
|
-> io::Result<()> {
|
2015-03-18 11:14:54 -05:00
|
|
|
fs_imp::set_perm(path.as_ref(), perm.0)
|
2015-02-02 23:39:14 -06:00
|
|
|
}
|
|
|
|
|
2015-04-27 19:29:35 -05:00
|
|
|
impl DirBuilder {
|
|
|
|
/// Creates a new set of options with default mode/security settings for all
|
|
|
|
/// platforms and also non-recursive.
|
2015-12-02 19:31:49 -06:00
|
|
|
#[stable(feature = "dir_builder", since = "1.6.0")]
|
2015-04-27 19:29:35 -05:00
|
|
|
pub fn new() -> DirBuilder {
|
|
|
|
DirBuilder {
|
|
|
|
inner: fs_imp::DirBuilder::new(),
|
|
|
|
recursive: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Indicate that directories create should be created recursively, creating
|
|
|
|
/// all parent directories if they do not exist with the same security and
|
|
|
|
/// permissions settings.
|
|
|
|
///
|
|
|
|
/// This option defaults to `false`
|
2015-12-02 19:31:49 -06:00
|
|
|
#[stable(feature = "dir_builder", since = "1.6.0")]
|
2015-04-27 19:29:35 -05:00
|
|
|
pub fn recursive(&mut self, recursive: bool) -> &mut Self {
|
|
|
|
self.recursive = recursive;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create the specified directory with the options configured in this
|
|
|
|
/// builder.
|
2015-12-03 13:50:09 -06:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// use std::fs::{self, DirBuilder};
|
|
|
|
///
|
|
|
|
/// let path = "/tmp/foo/bar/baz";
|
|
|
|
/// DirBuilder::new()
|
|
|
|
/// .recursive(true)
|
|
|
|
/// .create(path).unwrap();
|
|
|
|
///
|
|
|
|
/// assert!(fs::metadata(path).unwrap().is_dir());
|
|
|
|
/// ```
|
2015-12-02 19:31:49 -06:00
|
|
|
#[stable(feature = "dir_builder", since = "1.6.0")]
|
2015-04-27 19:29:35 -05:00
|
|
|
pub fn create<P: AsRef<Path>>(&self, path: P) -> io::Result<()> {
|
2015-09-09 14:37:59 -05:00
|
|
|
self._create(path.as_ref())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn _create(&self, path: &Path) -> io::Result<()> {
|
2015-04-27 19:29:35 -05:00
|
|
|
if self.recursive {
|
|
|
|
self.create_dir_all(path)
|
|
|
|
} else {
|
|
|
|
self.inner.mkdir(path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_dir_all(&self, path: &Path) -> io::Result<()> {
|
|
|
|
if path == Path::new("") || path.is_dir() { return Ok(()) }
|
|
|
|
if let Some(p) = path.parent() {
|
|
|
|
try!(self.create_dir_all(p))
|
|
|
|
}
|
|
|
|
self.inner.mkdir(path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsInnerMut<fs_imp::DirBuilder> for DirBuilder {
|
|
|
|
fn as_inner_mut(&mut self) -> &mut fs_imp::DirBuilder {
|
|
|
|
&mut self.inner
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-02 23:39:14 -06:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use prelude::v1::*;
|
|
|
|
use io::prelude::*;
|
|
|
|
|
2015-03-17 15:33:26 -05:00
|
|
|
use env;
|
2015-02-02 23:39:14 -06:00
|
|
|
use fs::{self, File, OpenOptions};
|
|
|
|
use io::{ErrorKind, SeekFrom};
|
2016-02-02 00:04:55 -06:00
|
|
|
use path::{Path, PathBuf};
|
2015-02-02 23:39:14 -06:00
|
|
|
use rand::{self, StdRng, Rng};
|
|
|
|
use str;
|
|
|
|
|
2016-02-02 00:04:55 -06:00
|
|
|
#[cfg(windows)]
|
2016-02-03 11:23:33 -06:00
|
|
|
use os::windows::fs::{symlink_dir, symlink_file};
|
|
|
|
#[cfg(windows)]
|
|
|
|
use sys::fs::symlink_junction;
|
2016-02-02 00:04:55 -06:00
|
|
|
#[cfg(unix)]
|
|
|
|
use os::unix::fs::symlink as symlink_dir;
|
|
|
|
#[cfg(unix)]
|
|
|
|
use os::unix::fs::symlink as symlink_file;
|
|
|
|
#[cfg(unix)]
|
|
|
|
use os::unix::fs::symlink as symlink_junction;
|
|
|
|
|
2015-02-02 23:39:14 -06:00
|
|
|
macro_rules! check { ($e:expr) => (
|
|
|
|
match $e {
|
|
|
|
Ok(t) => t,
|
|
|
|
Err(e) => panic!("{} failed with: {}", stringify!($e), e),
|
|
|
|
}
|
|
|
|
) }
|
|
|
|
|
|
|
|
macro_rules! error { ($e:expr, $s:expr) => (
|
|
|
|
match $e {
|
|
|
|
Ok(_) => panic!("Unexpected success. Should've been: {:?}", $s),
|
2015-03-07 20:08:48 -06:00
|
|
|
Err(ref err) => assert!(err.to_string().contains($s),
|
2015-02-02 23:39:14 -06:00
|
|
|
format!("`{}` did not contain `{}`", err, $s))
|
|
|
|
}
|
|
|
|
) }
|
|
|
|
|
|
|
|
pub struct TempDir(PathBuf);
|
|
|
|
|
|
|
|
impl TempDir {
|
|
|
|
fn join(&self, path: &str) -> PathBuf {
|
|
|
|
let TempDir(ref p) = *self;
|
|
|
|
p.join(path)
|
|
|
|
}
|
|
|
|
|
2016-02-02 00:04:55 -06:00
|
|
|
fn path<'a>(&'a self) -> &'a Path {
|
2015-02-02 23:39:14 -06:00
|
|
|
let TempDir(ref p) = *self;
|
|
|
|
p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for TempDir {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
// Gee, seeing how we're testing the fs module I sure hope that we
|
|
|
|
// at least implement this correctly!
|
|
|
|
let TempDir(ref p) = *self;
|
|
|
|
check!(fs::remove_dir_all(p));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn tmpdir() -> TempDir {
|
2015-03-17 15:33:26 -05:00
|
|
|
let p = env::temp_dir();
|
2015-04-10 13:39:53 -05:00
|
|
|
let mut r = rand::thread_rng();
|
|
|
|
let ret = p.join(&format!("rust-{}", r.next_u32()));
|
2015-02-02 23:39:14 -06:00
|
|
|
check!(fs::create_dir(&ret));
|
|
|
|
TempDir(ret)
|
|
|
|
}
|
|
|
|
|
2016-02-03 11:50:57 -06:00
|
|
|
// Several test fail on windows if the user does not have permission to
|
|
|
|
// create symlinks (the `SeCreateSymbolicLinkPrivilege`). Instead of
|
|
|
|
// disabling these test on Windows, use this function to test whether we
|
|
|
|
// have permission, and return otherwise. This way, we still don't run these
|
|
|
|
// tests most of the time, but at least we do if the user has the right
|
|
|
|
// permissions.
|
2016-02-02 00:04:55 -06:00
|
|
|
pub fn got_symlink_permission(tmpdir: &TempDir) -> bool {
|
2016-02-03 11:50:57 -06:00
|
|
|
if cfg!(unix) { return true }
|
2016-02-02 00:04:55 -06:00
|
|
|
let link = tmpdir.join("some_hopefully_unique_link_name");
|
|
|
|
|
|
|
|
match symlink_file(r"nonexisting_target", link) {
|
|
|
|
Ok(_) => true,
|
|
|
|
Err(ref err) =>
|
|
|
|
if err.to_string().contains("A required privilege is not held by the client.") {
|
|
|
|
false
|
|
|
|
} else {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-02 23:39:14 -06:00
|
|
|
#[test]
|
|
|
|
fn file_test_io_smoke_test() {
|
|
|
|
let message = "it's alright. have a good time";
|
|
|
|
let tmpdir = tmpdir();
|
|
|
|
let filename = &tmpdir.join("file_rt_io_file_test.txt");
|
|
|
|
{
|
|
|
|
let mut write_stream = check!(File::create(filename));
|
|
|
|
check!(write_stream.write(message.as_bytes()));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
let mut read_stream = check!(File::open(filename));
|
|
|
|
let mut read_buf = [0; 1028];
|
|
|
|
let read_str = match check!(read_stream.read(&mut read_buf)) {
|
2015-04-01 18:34:15 -05:00
|
|
|
0 => panic!("shouldn't happen"),
|
2015-02-02 23:39:14 -06:00
|
|
|
n => str::from_utf8(&read_buf[..n]).unwrap().to_string()
|
|
|
|
};
|
2015-03-07 20:08:48 -06:00
|
|
|
assert_eq!(read_str, message);
|
2015-02-02 23:39:14 -06:00
|
|
|
}
|
|
|
|
check!(fs::remove_file(filename));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn invalid_path_raises() {
|
|
|
|
let tmpdir = tmpdir();
|
|
|
|
let filename = &tmpdir.join("file_that_does_not_exist.txt");
|
|
|
|
let result = File::open(filename);
|
|
|
|
|
|
|
|
if cfg!(unix) {
|
|
|
|
error!(result, "o such file or directory");
|
|
|
|
}
|
2016-02-02 00:04:55 -06:00
|
|
|
if cfg!(windows) {
|
|
|
|
error!(result, "The system cannot find the file specified");
|
|
|
|
}
|
2015-02-02 23:39:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn file_test_iounlinking_invalid_path_should_raise_condition() {
|
|
|
|
let tmpdir = tmpdir();
|
|
|
|
let filename = &tmpdir.join("file_another_file_that_does_not_exist.txt");
|
|
|
|
|
|
|
|
let result = fs::remove_file(filename);
|
|
|
|
|
|
|
|
if cfg!(unix) {
|
|
|
|
error!(result, "o such file or directory");
|
|
|
|
}
|
2016-02-02 00:04:55 -06:00
|
|
|
if cfg!(windows) {
|
|
|
|
error!(result, "The system cannot find the file specified");
|
|
|
|
}
|
2015-02-02 23:39:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn file_test_io_non_positional_read() {
|
|
|
|
let message: &str = "ten-four";
|
|
|
|
let mut read_mem = [0; 8];
|
|
|
|
let tmpdir = tmpdir();
|
|
|
|
let filename = &tmpdir.join("file_rt_io_file_test_positional.txt");
|
|
|
|
{
|
|
|
|
let mut rw_stream = check!(File::create(filename));
|
|
|
|
check!(rw_stream.write(message.as_bytes()));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
let mut read_stream = check!(File::open(filename));
|
|
|
|
{
|
|
|
|
let read_buf = &mut read_mem[0..4];
|
|
|
|
check!(read_stream.read(read_buf));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
let read_buf = &mut read_mem[4..8];
|
|
|
|
check!(read_stream.read(read_buf));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
check!(fs::remove_file(filename));
|
|
|
|
let read_str = str::from_utf8(&read_mem).unwrap();
|
|
|
|
assert_eq!(read_str, message);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn file_test_io_seek_and_tell_smoke_test() {
|
|
|
|
let message = "ten-four";
|
|
|
|
let mut read_mem = [0; 4];
|
|
|
|
let set_cursor = 4 as u64;
|
2015-12-18 06:29:49 -06:00
|
|
|
let tell_pos_pre_read;
|
|
|
|
let tell_pos_post_read;
|
2015-02-02 23:39:14 -06:00
|
|
|
let tmpdir = tmpdir();
|
|
|
|
let filename = &tmpdir.join("file_rt_io_file_test_seeking.txt");
|
|
|
|
{
|
|
|
|
let mut rw_stream = check!(File::create(filename));
|
|
|
|
check!(rw_stream.write(message.as_bytes()));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
let mut read_stream = check!(File::open(filename));
|
|
|
|
check!(read_stream.seek(SeekFrom::Start(set_cursor)));
|
|
|
|
tell_pos_pre_read = check!(read_stream.seek(SeekFrom::Current(0)));
|
|
|
|
check!(read_stream.read(&mut read_mem));
|
|
|
|
tell_pos_post_read = check!(read_stream.seek(SeekFrom::Current(0)));
|
|
|
|
}
|
|
|
|
check!(fs::remove_file(filename));
|
|
|
|
let read_str = str::from_utf8(&read_mem).unwrap();
|
|
|
|
assert_eq!(read_str, &message[4..8]);
|
|
|
|
assert_eq!(tell_pos_pre_read, set_cursor);
|
|
|
|
assert_eq!(tell_pos_post_read, message.len() as u64);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn file_test_io_seek_and_write() {
|
|
|
|
let initial_msg = "food-is-yummy";
|
|
|
|
let overwrite_msg = "-the-bar!!";
|
|
|
|
let final_msg = "foo-the-bar!!";
|
|
|
|
let seek_idx = 3;
|
|
|
|
let mut read_mem = [0; 13];
|
|
|
|
let tmpdir = tmpdir();
|
|
|
|
let filename = &tmpdir.join("file_rt_io_file_test_seek_and_write.txt");
|
|
|
|
{
|
|
|
|
let mut rw_stream = check!(File::create(filename));
|
|
|
|
check!(rw_stream.write(initial_msg.as_bytes()));
|
|
|
|
check!(rw_stream.seek(SeekFrom::Start(seek_idx)));
|
|
|
|
check!(rw_stream.write(overwrite_msg.as_bytes()));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
let mut read_stream = check!(File::open(filename));
|
|
|
|
check!(read_stream.read(&mut read_mem));
|
|
|
|
}
|
|
|
|
check!(fs::remove_file(filename));
|
|
|
|
let read_str = str::from_utf8(&read_mem).unwrap();
|
|
|
|
assert!(read_str == final_msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn file_test_io_seek_shakedown() {
|
|
|
|
// 01234567890123
|
|
|
|
let initial_msg = "qwer-asdf-zxcv";
|
|
|
|
let chunk_one: &str = "qwer";
|
|
|
|
let chunk_two: &str = "asdf";
|
|
|
|
let chunk_three: &str = "zxcv";
|
|
|
|
let mut read_mem = [0; 4];
|
|
|
|
let tmpdir = tmpdir();
|
|
|
|
let filename = &tmpdir.join("file_rt_io_file_test_seek_shakedown.txt");
|
|
|
|
{
|
|
|
|
let mut rw_stream = check!(File::create(filename));
|
|
|
|
check!(rw_stream.write(initial_msg.as_bytes()));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
let mut read_stream = check!(File::open(filename));
|
|
|
|
|
|
|
|
check!(read_stream.seek(SeekFrom::End(-4)));
|
|
|
|
check!(read_stream.read(&mut read_mem));
|
|
|
|
assert_eq!(str::from_utf8(&read_mem).unwrap(), chunk_three);
|
|
|
|
|
|
|
|
check!(read_stream.seek(SeekFrom::Current(-9)));
|
|
|
|
check!(read_stream.read(&mut read_mem));
|
|
|
|
assert_eq!(str::from_utf8(&read_mem).unwrap(), chunk_two);
|
|
|
|
|
|
|
|
check!(read_stream.seek(SeekFrom::Start(0)));
|
|
|
|
check!(read_stream.read(&mut read_mem));
|
|
|
|
assert_eq!(str::from_utf8(&read_mem).unwrap(), chunk_one);
|
|
|
|
}
|
|
|
|
check!(fs::remove_file(filename));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn file_test_stat_is_correct_on_is_file() {
|
|
|
|
let tmpdir = tmpdir();
|
|
|
|
let filename = &tmpdir.join("file_stat_correct_on_is_file.txt");
|
|
|
|
{
|
|
|
|
let mut opts = OpenOptions::new();
|
|
|
|
let mut fs = check!(opts.read(true).write(true)
|
|
|
|
.create(true).open(filename));
|
|
|
|
let msg = "hw";
|
|
|
|
fs.write(msg.as_bytes()).unwrap();
|
|
|
|
|
|
|
|
let fstat_res = check!(fs.metadata());
|
|
|
|
assert!(fstat_res.is_file());
|
|
|
|
}
|
|
|
|
let stat_res_fn = check!(fs::metadata(filename));
|
|
|
|
assert!(stat_res_fn.is_file());
|
|
|
|
let stat_res_meth = check!(filename.metadata());
|
|
|
|
assert!(stat_res_meth.is_file());
|
|
|
|
check!(fs::remove_file(filename));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn file_test_stat_is_correct_on_is_dir() {
|
|
|
|
let tmpdir = tmpdir();
|
|
|
|
let filename = &tmpdir.join("file_stat_correct_on_is_dir");
|
|
|
|
check!(fs::create_dir(filename));
|
|
|
|
let stat_res_fn = check!(fs::metadata(filename));
|
|
|
|
assert!(stat_res_fn.is_dir());
|
|
|
|
let stat_res_meth = check!(filename.metadata());
|
|
|
|
assert!(stat_res_meth.is_dir());
|
|
|
|
check!(fs::remove_dir(filename));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn file_test_fileinfo_false_when_checking_is_file_on_a_directory() {
|
|
|
|
let tmpdir = tmpdir();
|
|
|
|
let dir = &tmpdir.join("fileinfo_false_on_dir");
|
|
|
|
check!(fs::create_dir(dir));
|
|
|
|
assert!(dir.is_file() == false);
|
|
|
|
check!(fs::remove_dir(dir));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn file_test_fileinfo_check_exists_before_and_after_file_creation() {
|
|
|
|
let tmpdir = tmpdir();
|
|
|
|
let file = &tmpdir.join("fileinfo_check_exists_b_and_a.txt");
|
|
|
|
check!(check!(File::create(file)).write(b"foo"));
|
|
|
|
assert!(file.exists());
|
|
|
|
check!(fs::remove_file(file));
|
|
|
|
assert!(!file.exists());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn file_test_directoryinfo_check_exists_before_and_after_mkdir() {
|
|
|
|
let tmpdir = tmpdir();
|
|
|
|
let dir = &tmpdir.join("before_and_after_dir");
|
|
|
|
assert!(!dir.exists());
|
|
|
|
check!(fs::create_dir(dir));
|
|
|
|
assert!(dir.exists());
|
|
|
|
assert!(dir.is_dir());
|
|
|
|
check!(fs::remove_dir(dir));
|
|
|
|
assert!(!dir.exists());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn file_test_directoryinfo_readdir() {
|
|
|
|
let tmpdir = tmpdir();
|
|
|
|
let dir = &tmpdir.join("di_readdir");
|
|
|
|
check!(fs::create_dir(dir));
|
|
|
|
let prefix = "foo";
|
2015-03-17 15:33:26 -05:00
|
|
|
for n in 0..3 {
|
2015-02-02 23:39:14 -06:00
|
|
|
let f = dir.join(&format!("{}.txt", n));
|
|
|
|
let mut w = check!(File::create(&f));
|
|
|
|
let msg_str = format!("{}{}", prefix, n.to_string());
|
|
|
|
let msg = msg_str.as_bytes();
|
|
|
|
check!(w.write(msg));
|
|
|
|
}
|
2015-02-19 11:57:25 -06:00
|
|
|
let files = check!(fs::read_dir(dir));
|
2015-03-03 02:42:26 -06:00
|
|
|
let mut mem = [0; 4];
|
2015-02-02 23:39:14 -06:00
|
|
|
for f in files {
|
|
|
|
let f = f.unwrap().path();
|
|
|
|
{
|
|
|
|
let n = f.file_stem().unwrap();
|
|
|
|
check!(check!(File::open(&f)).read(&mut mem));
|
|
|
|
let read_str = str::from_utf8(&mem).unwrap();
|
|
|
|
let expected = format!("{}{}", prefix, n.to_str().unwrap());
|
2015-03-07 20:08:48 -06:00
|
|
|
assert_eq!(expected, read_str);
|
2015-02-02 23:39:14 -06:00
|
|
|
}
|
|
|
|
check!(fs::remove_file(&f));
|
|
|
|
}
|
|
|
|
check!(fs::remove_dir(dir));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-02-02 00:04:55 -06:00
|
|
|
#[allow(deprecated)]
|
2015-02-02 23:39:14 -06:00
|
|
|
fn file_test_walk_dir() {
|
|
|
|
let tmpdir = tmpdir();
|
|
|
|
let dir = &tmpdir.join("walk_dir");
|
|
|
|
check!(fs::create_dir(dir));
|
|
|
|
|
|
|
|
let dir1 = &dir.join("01/02/03");
|
|
|
|
check!(fs::create_dir_all(dir1));
|
|
|
|
check!(File::create(&dir1.join("04")));
|
|
|
|
|
|
|
|
let dir2 = &dir.join("11/12/13");
|
|
|
|
check!(fs::create_dir_all(dir2));
|
|
|
|
check!(File::create(&dir2.join("14")));
|
|
|
|
|
2015-02-19 11:57:25 -06:00
|
|
|
let files = check!(fs::walk_dir(dir));
|
2015-03-03 02:42:26 -06:00
|
|
|
let mut cur = [0; 2];
|
2015-02-02 23:39:14 -06:00
|
|
|
for f in files {
|
|
|
|
let f = f.unwrap().path();
|
|
|
|
let stem = f.file_stem().unwrap().to_str().unwrap();
|
|
|
|
let root = stem.as_bytes()[0] - b'0';
|
|
|
|
let name = stem.as_bytes()[1] - b'0';
|
|
|
|
assert!(cur[root as usize] < name);
|
|
|
|
cur[root as usize] = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
check!(fs::remove_dir_all(dir));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn mkdir_path_already_exists_error() {
|
|
|
|
let tmpdir = tmpdir();
|
|
|
|
let dir = &tmpdir.join("mkdir_error_twice");
|
|
|
|
check!(fs::create_dir(dir));
|
|
|
|
let e = fs::create_dir(dir).err().unwrap();
|
2015-03-16 20:08:57 -05:00
|
|
|
assert_eq!(e.kind(), ErrorKind::AlreadyExists);
|
2015-02-02 23:39:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn recursive_mkdir() {
|
|
|
|
let tmpdir = tmpdir();
|
|
|
|
let dir = tmpdir.join("d1/d2");
|
|
|
|
check!(fs::create_dir_all(&dir));
|
|
|
|
assert!(dir.is_dir())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn recursive_mkdir_failure() {
|
|
|
|
let tmpdir = tmpdir();
|
|
|
|
let dir = tmpdir.join("d1");
|
|
|
|
let file = dir.join("f1");
|
|
|
|
|
|
|
|
check!(fs::create_dir_all(&dir));
|
|
|
|
check!(File::create(&file));
|
|
|
|
|
|
|
|
let result = fs::create_dir_all(&file);
|
|
|
|
|
|
|
|
assert!(result.is_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn recursive_mkdir_slash() {
|
2016-02-02 00:04:55 -06:00
|
|
|
check!(fs::create_dir_all(&Path::new("/")));
|
2015-02-02 23:39:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn recursive_rmdir() {
|
|
|
|
let tmpdir = tmpdir();
|
|
|
|
let d1 = tmpdir.join("d1");
|
|
|
|
let dt = d1.join("t");
|
|
|
|
let dtt = dt.join("t");
|
|
|
|
let d2 = tmpdir.join("d2");
|
|
|
|
let canary = d2.join("do_not_delete");
|
|
|
|
check!(fs::create_dir_all(&dtt));
|
|
|
|
check!(fs::create_dir_all(&d2));
|
|
|
|
check!(check!(File::create(&canary)).write(b"foo"));
|
2016-02-02 00:04:55 -06:00
|
|
|
check!(symlink_junction(&d2, &dt.join("d2")));
|
2015-02-02 23:39:14 -06:00
|
|
|
check!(fs::remove_dir_all(&d1));
|
|
|
|
|
|
|
|
assert!(!d1.is_dir());
|
|
|
|
assert!(canary.exists());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn unicode_path_is_dir() {
|
2016-02-02 00:04:55 -06:00
|
|
|
assert!(Path::new(".").is_dir());
|
|
|
|
assert!(!Path::new("test/stdtest/fs.rs").is_dir());
|
2015-02-02 23:39:14 -06:00
|
|
|
|
|
|
|
let tmpdir = tmpdir();
|
|
|
|
|
|
|
|
let mut dirpath = tmpdir.path().to_path_buf();
|
2015-11-05 08:30:32 -06:00
|
|
|
dirpath.push("test-가一ー你好");
|
2015-02-02 23:39:14 -06:00
|
|
|
check!(fs::create_dir(&dirpath));
|
|
|
|
assert!(dirpath.is_dir());
|
|
|
|
|
|
|
|
let mut filepath = dirpath;
|
|
|
|
filepath.push("unicode-file-\u{ac00}\u{4e00}\u{30fc}\u{4f60}\u{597d}.rs");
|
|
|
|
check!(File::create(&filepath)); // ignore return; touch only
|
|
|
|
assert!(!filepath.is_dir());
|
|
|
|
assert!(filepath.exists());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn unicode_path_exists() {
|
2016-02-02 00:04:55 -06:00
|
|
|
assert!(Path::new(".").exists());
|
|
|
|
assert!(!Path::new("test/nonexistent-bogus-path").exists());
|
2015-02-02 23:39:14 -06:00
|
|
|
|
|
|
|
let tmpdir = tmpdir();
|
|
|
|
let unicode = tmpdir.path();
|
|
|
|
let unicode = unicode.join(&format!("test-각丁ー再见"));
|
|
|
|
check!(fs::create_dir(&unicode));
|
|
|
|
assert!(unicode.exists());
|
2016-02-02 00:04:55 -06:00
|
|
|
assert!(!Path::new("test/unicode-bogus-path-각丁ー再见").exists());
|
2015-02-02 23:39:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn copy_file_does_not_exist() {
|
2016-02-02 00:04:55 -06:00
|
|
|
let from = Path::new("test/nonexistent-bogus-path");
|
|
|
|
let to = Path::new("test/other-bogus-path");
|
2015-02-02 23:39:14 -06:00
|
|
|
|
|
|
|
match fs::copy(&from, &to) {
|
|
|
|
Ok(..) => panic!(),
|
|
|
|
Err(..) => {
|
|
|
|
assert!(!from.exists());
|
|
|
|
assert!(!to.exists());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-10 03:54:00 -05:00
|
|
|
#[test]
|
|
|
|
fn copy_src_does_not_exist() {
|
|
|
|
let tmpdir = tmpdir();
|
2016-02-02 00:04:55 -06:00
|
|
|
let from = Path::new("test/nonexistent-bogus-path");
|
2015-07-10 03:54:00 -05:00
|
|
|
let to = tmpdir.join("out.txt");
|
|
|
|
check!(check!(File::create(&to)).write(b"hello"));
|
|
|
|
assert!(fs::copy(&from, &to).is_err());
|
|
|
|
assert!(!from.exists());
|
|
|
|
let mut v = Vec::new();
|
|
|
|
check!(check!(File::open(&to)).read_to_end(&mut v));
|
|
|
|
assert_eq!(v, b"hello");
|
|
|
|
}
|
|
|
|
|
2015-02-02 23:39:14 -06:00
|
|
|
#[test]
|
|
|
|
fn copy_file_ok() {
|
|
|
|
let tmpdir = tmpdir();
|
|
|
|
let input = tmpdir.join("in.txt");
|
|
|
|
let out = tmpdir.join("out.txt");
|
|
|
|
|
|
|
|
check!(check!(File::create(&input)).write(b"hello"));
|
|
|
|
check!(fs::copy(&input, &out));
|
|
|
|
let mut v = Vec::new();
|
|
|
|
check!(check!(File::open(&out)).read_to_end(&mut v));
|
2015-03-30 13:00:05 -05:00
|
|
|
assert_eq!(v, b"hello");
|
2015-02-02 23:39:14 -06:00
|
|
|
|
|
|
|
assert_eq!(check!(input.metadata()).permissions(),
|
|
|
|
check!(out.metadata()).permissions());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn copy_file_dst_dir() {
|
|
|
|
let tmpdir = tmpdir();
|
|
|
|
let out = tmpdir.join("out");
|
|
|
|
|
|
|
|
check!(File::create(&out));
|
|
|
|
match fs::copy(&*out, tmpdir.path()) {
|
|
|
|
Ok(..) => panic!(), Err(..) => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn copy_file_dst_exists() {
|
|
|
|
let tmpdir = tmpdir();
|
|
|
|
let input = tmpdir.join("in");
|
|
|
|
let output = tmpdir.join("out");
|
|
|
|
|
|
|
|
check!(check!(File::create(&input)).write("foo".as_bytes()));
|
|
|
|
check!(check!(File::create(&output)).write("bar".as_bytes()));
|
|
|
|
check!(fs::copy(&input, &output));
|
|
|
|
|
|
|
|
let mut v = Vec::new();
|
|
|
|
check!(check!(File::open(&output)).read_to_end(&mut v));
|
|
|
|
assert_eq!(v, b"foo".to_vec());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn copy_file_src_dir() {
|
|
|
|
let tmpdir = tmpdir();
|
|
|
|
let out = tmpdir.join("out");
|
|
|
|
|
|
|
|
match fs::copy(tmpdir.path(), &out) {
|
|
|
|
Ok(..) => panic!(), Err(..) => {}
|
|
|
|
}
|
|
|
|
assert!(!out.exists());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn copy_file_preserves_perm_bits() {
|
|
|
|
let tmpdir = tmpdir();
|
|
|
|
let input = tmpdir.join("in.txt");
|
|
|
|
let out = tmpdir.join("out.txt");
|
|
|
|
|
|
|
|
let attr = check!(check!(File::create(&input)).metadata());
|
|
|
|
let mut p = attr.permissions();
|
|
|
|
p.set_readonly(true);
|
|
|
|
check!(fs::set_permissions(&input, p));
|
|
|
|
check!(fs::copy(&input, &out));
|
|
|
|
assert!(check!(out.metadata()).permissions().readonly());
|
2015-03-06 17:53:32 -06:00
|
|
|
check!(fs::set_permissions(&input, attr.permissions()));
|
|
|
|
check!(fs::set_permissions(&out, attr.permissions()));
|
2015-02-02 23:39:14 -06:00
|
|
|
}
|
|
|
|
|
2015-07-10 03:54:00 -05:00
|
|
|
#[cfg(windows)]
|
|
|
|
#[test]
|
|
|
|
fn copy_file_preserves_streams() {
|
|
|
|
let tmp = tmpdir();
|
|
|
|
check!(check!(File::create(tmp.join("in.txt:bunny"))).write("carrot".as_bytes()));
|
|
|
|
assert_eq!(check!(fs::copy(tmp.join("in.txt"), tmp.join("out.txt"))), 6);
|
|
|
|
assert_eq!(check!(tmp.join("out.txt").metadata()).len(), 0);
|
|
|
|
let mut v = Vec::new();
|
|
|
|
check!(check!(File::open(tmp.join("out.txt:bunny"))).read_to_end(&mut v));
|
|
|
|
assert_eq!(v, b"carrot".to_vec());
|
|
|
|
}
|
|
|
|
|
2015-02-02 23:39:14 -06:00
|
|
|
#[test]
|
|
|
|
fn symlinks_work() {
|
|
|
|
let tmpdir = tmpdir();
|
2016-02-02 00:04:55 -06:00
|
|
|
if !got_symlink_permission(&tmpdir) { return };
|
|
|
|
|
2015-02-02 23:39:14 -06:00
|
|
|
let input = tmpdir.join("in.txt");
|
|
|
|
let out = tmpdir.join("out.txt");
|
|
|
|
|
|
|
|
check!(check!(File::create(&input)).write("foobar".as_bytes()));
|
2016-02-02 00:04:55 -06:00
|
|
|
check!(symlink_file(&input, &out));
|
|
|
|
assert!(check!(out.symlink_metadata()).file_type().is_symlink());
|
2015-02-02 23:39:14 -06:00
|
|
|
assert_eq!(check!(fs::metadata(&out)).len(),
|
|
|
|
check!(fs::metadata(&input)).len());
|
|
|
|
let mut v = Vec::new();
|
|
|
|
check!(check!(File::open(&out)).read_to_end(&mut v));
|
|
|
|
assert_eq!(v, b"foobar".to_vec());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn symlink_noexist() {
|
2016-02-02 00:04:55 -06:00
|
|
|
// Symlinks can point to things that don't exist
|
2015-02-02 23:39:14 -06:00
|
|
|
let tmpdir = tmpdir();
|
2016-02-02 00:04:55 -06:00
|
|
|
if !got_symlink_permission(&tmpdir) { return };
|
|
|
|
|
2016-02-03 11:50:57 -06:00
|
|
|
// Use a relative path for testing. Symlinks get normalized by Windows,
|
|
|
|
// so we may not get the same path back for absolute paths
|
2016-02-02 00:04:55 -06:00
|
|
|
check!(symlink_file(&"foo", &tmpdir.join("bar")));
|
2016-02-03 11:50:57 -06:00
|
|
|
assert_eq!(check!(fs::read_link(&tmpdir.join("bar"))).to_str().unwrap(),
|
|
|
|
"foo");
|
2015-02-02 23:39:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn readlink_not_symlink() {
|
|
|
|
let tmpdir = tmpdir();
|
|
|
|
match fs::read_link(tmpdir.path()) {
|
|
|
|
Ok(..) => panic!("wanted a failure"),
|
|
|
|
Err(..) => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn links_work() {
|
|
|
|
let tmpdir = tmpdir();
|
|
|
|
let input = tmpdir.join("in.txt");
|
|
|
|
let out = tmpdir.join("out.txt");
|
|
|
|
|
|
|
|
check!(check!(File::create(&input)).write("foobar".as_bytes()));
|
|
|
|
check!(fs::hard_link(&input, &out));
|
|
|
|
assert_eq!(check!(fs::metadata(&out)).len(),
|
|
|
|
check!(fs::metadata(&input)).len());
|
|
|
|
assert_eq!(check!(fs::metadata(&out)).len(),
|
|
|
|
check!(input.metadata()).len());
|
|
|
|
let mut v = Vec::new();
|
|
|
|
check!(check!(File::open(&out)).read_to_end(&mut v));
|
|
|
|
assert_eq!(v, b"foobar".to_vec());
|
|
|
|
|
|
|
|
// can't link to yourself
|
|
|
|
match fs::hard_link(&input, &input) {
|
|
|
|
Ok(..) => panic!("wanted a failure"),
|
|
|
|
Err(..) => {}
|
|
|
|
}
|
|
|
|
// can't link to something that doesn't exist
|
|
|
|
match fs::hard_link(&tmpdir.join("foo"), &tmpdir.join("bar")) {
|
|
|
|
Ok(..) => panic!("wanted a failure"),
|
|
|
|
Err(..) => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn chmod_works() {
|
|
|
|
let tmpdir = tmpdir();
|
|
|
|
let file = tmpdir.join("in.txt");
|
|
|
|
|
|
|
|
check!(File::create(&file));
|
|
|
|
let attr = check!(fs::metadata(&file));
|
|
|
|
assert!(!attr.permissions().readonly());
|
|
|
|
let mut p = attr.permissions();
|
|
|
|
p.set_readonly(true);
|
|
|
|
check!(fs::set_permissions(&file, p.clone()));
|
|
|
|
let attr = check!(fs::metadata(&file));
|
|
|
|
assert!(attr.permissions().readonly());
|
|
|
|
|
2015-03-06 17:53:32 -06:00
|
|
|
match fs::set_permissions(&tmpdir.join("foo"), p.clone()) {
|
|
|
|
Ok(..) => panic!("wanted an error"),
|
2015-02-02 23:39:14 -06:00
|
|
|
Err(..) => {}
|
|
|
|
}
|
2015-03-06 17:53:32 -06:00
|
|
|
|
|
|
|
p.set_readonly(false);
|
|
|
|
check!(fs::set_permissions(&file, p));
|
2015-02-02 23:39:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn sync_doesnt_kill_anything() {
|
|
|
|
let tmpdir = tmpdir();
|
|
|
|
let path = tmpdir.join("in.txt");
|
|
|
|
|
|
|
|
let mut file = check!(File::create(&path));
|
|
|
|
check!(file.sync_all());
|
|
|
|
check!(file.sync_data());
|
|
|
|
check!(file.write(b"foo"));
|
|
|
|
check!(file.sync_all());
|
|
|
|
check!(file.sync_data());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn truncate_works() {
|
|
|
|
let tmpdir = tmpdir();
|
|
|
|
let path = tmpdir.join("in.txt");
|
|
|
|
|
|
|
|
let mut file = check!(File::create(&path));
|
|
|
|
check!(file.write(b"foo"));
|
|
|
|
check!(file.sync_all());
|
|
|
|
|
|
|
|
// Do some simple things with truncation
|
|
|
|
assert_eq!(check!(file.metadata()).len(), 3);
|
|
|
|
check!(file.set_len(10));
|
|
|
|
assert_eq!(check!(file.metadata()).len(), 10);
|
|
|
|
check!(file.write(b"bar"));
|
|
|
|
check!(file.sync_all());
|
|
|
|
assert_eq!(check!(file.metadata()).len(), 10);
|
|
|
|
|
|
|
|
let mut v = Vec::new();
|
|
|
|
check!(check!(File::open(&path)).read_to_end(&mut v));
|
|
|
|
assert_eq!(v, b"foobar\0\0\0\0".to_vec());
|
|
|
|
|
|
|
|
// Truncate to a smaller length, don't seek, and then write something.
|
2015-03-28 10:09:51 -05:00
|
|
|
// Ensure that the intermediate zeroes are all filled in (we have `seek`ed
|
2015-02-02 23:39:14 -06:00
|
|
|
// past the end of the file).
|
|
|
|
check!(file.set_len(2));
|
|
|
|
assert_eq!(check!(file.metadata()).len(), 2);
|
|
|
|
check!(file.write(b"wut"));
|
|
|
|
check!(file.sync_all());
|
|
|
|
assert_eq!(check!(file.metadata()).len(), 9);
|
|
|
|
let mut v = Vec::new();
|
|
|
|
check!(check!(File::open(&path)).read_to_end(&mut v));
|
|
|
|
assert_eq!(v, b"fo\0\0\0\0wut".to_vec());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn open_flavors() {
|
|
|
|
use fs::OpenOptions as OO;
|
|
|
|
fn c<T: Clone>(t: &T) -> T { t.clone() }
|
|
|
|
|
|
|
|
let tmpdir = tmpdir();
|
|
|
|
|
|
|
|
let mut r = OO::new(); r.read(true);
|
|
|
|
let mut w = OO::new(); w.write(true);
|
2016-01-13 11:08:08 -06:00
|
|
|
let mut rw = OO::new(); rw.read(true).write(true);
|
|
|
|
let mut a = OO::new(); a.append(true);
|
|
|
|
let mut ra = OO::new(); ra.read(true).append(true);
|
|
|
|
|
|
|
|
let invalid_options = if cfg!(windows) { "The parameter is incorrect" }
|
|
|
|
else { "Invalid argument" };
|
|
|
|
|
|
|
|
// Test various combinations of creation modes and access modes.
|
|
|
|
//
|
|
|
|
// Allowed:
|
|
|
|
// creation mode | read | write | read-write | append | read-append |
|
|
|
|
// :-----------------------|:-----:|:-----:|:----------:|:------:|:-----------:|
|
|
|
|
// not set (open existing) | X | X | X | X | X |
|
|
|
|
// create | | X | X | X | X |
|
|
|
|
// truncate | | X | X | | |
|
|
|
|
// create and truncate | | X | X | | |
|
|
|
|
// create_new | | X | X | X | X |
|
|
|
|
//
|
|
|
|
// tested in reverse order, so 'create_new' creates the file, and 'open existing' opens it.
|
|
|
|
|
|
|
|
// write-only
|
|
|
|
check!(c(&w).create_new(true).open(&tmpdir.join("a")));
|
|
|
|
check!(c(&w).create(true).truncate(true).open(&tmpdir.join("a")));
|
|
|
|
check!(c(&w).truncate(true).open(&tmpdir.join("a")));
|
|
|
|
check!(c(&w).create(true).open(&tmpdir.join("a")));
|
|
|
|
check!(c(&w).open(&tmpdir.join("a")));
|
|
|
|
|
|
|
|
// read-only
|
|
|
|
error!(c(&r).create_new(true).open(&tmpdir.join("b")), invalid_options);
|
|
|
|
error!(c(&r).create(true).truncate(true).open(&tmpdir.join("b")), invalid_options);
|
|
|
|
error!(c(&r).truncate(true).open(&tmpdir.join("b")), invalid_options);
|
|
|
|
error!(c(&r).create(true).open(&tmpdir.join("b")), invalid_options);
|
|
|
|
check!(c(&r).open(&tmpdir.join("a"))); // try opening the file created with write_only
|
|
|
|
|
|
|
|
// read-write
|
|
|
|
check!(c(&rw).create_new(true).open(&tmpdir.join("c")));
|
|
|
|
check!(c(&rw).create(true).truncate(true).open(&tmpdir.join("c")));
|
|
|
|
check!(c(&rw).truncate(true).open(&tmpdir.join("c")));
|
2015-02-02 23:39:14 -06:00
|
|
|
check!(c(&rw).create(true).open(&tmpdir.join("c")));
|
2016-01-13 11:08:08 -06:00
|
|
|
check!(c(&rw).open(&tmpdir.join("c")));
|
|
|
|
|
|
|
|
// append
|
|
|
|
check!(c(&a).create_new(true).open(&tmpdir.join("d")));
|
|
|
|
error!(c(&a).create(true).truncate(true).open(&tmpdir.join("d")), invalid_options);
|
|
|
|
error!(c(&a).truncate(true).open(&tmpdir.join("d")), invalid_options);
|
|
|
|
check!(c(&a).create(true).open(&tmpdir.join("d")));
|
|
|
|
check!(c(&a).open(&tmpdir.join("d")));
|
|
|
|
|
|
|
|
// read-append
|
|
|
|
check!(c(&ra).create_new(true).open(&tmpdir.join("e")));
|
|
|
|
error!(c(&ra).create(true).truncate(true).open(&tmpdir.join("e")), invalid_options);
|
|
|
|
error!(c(&ra).truncate(true).open(&tmpdir.join("e")), invalid_options);
|
|
|
|
check!(c(&ra).create(true).open(&tmpdir.join("e")));
|
|
|
|
check!(c(&ra).open(&tmpdir.join("e")));
|
|
|
|
|
|
|
|
// Test opening a file without setting an access mode
|
|
|
|
let mut blank = OO::new();
|
|
|
|
error!(blank.create(true).open(&tmpdir.join("f")), invalid_options);
|
|
|
|
|
|
|
|
// Test write works
|
|
|
|
check!(check!(File::create(&tmpdir.join("h"))).write("foobar".as_bytes()));
|
|
|
|
|
|
|
|
// Test write fails for read-only
|
2015-02-02 23:39:14 -06:00
|
|
|
check!(r.open(&tmpdir.join("h")));
|
|
|
|
{
|
|
|
|
let mut f = check!(r.open(&tmpdir.join("h")));
|
|
|
|
assert!(f.write("wut".as_bytes()).is_err());
|
|
|
|
}
|
2016-01-13 11:08:08 -06:00
|
|
|
|
|
|
|
// Test write overwrites
|
|
|
|
{
|
|
|
|
let mut f = check!(c(&w).open(&tmpdir.join("h")));
|
|
|
|
check!(f.write("baz".as_bytes()));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
let mut f = check!(c(&r).open(&tmpdir.join("h")));
|
|
|
|
let mut b = vec![0; 6];
|
|
|
|
check!(f.read(&mut b));
|
|
|
|
assert_eq!(b, "bazbar".as_bytes());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test truncate works
|
|
|
|
{
|
|
|
|
let mut f = check!(c(&w).truncate(true).open(&tmpdir.join("h")));
|
|
|
|
check!(f.write("foo".as_bytes()));
|
|
|
|
}
|
|
|
|
assert_eq!(check!(fs::metadata(&tmpdir.join("h"))).len(), 3);
|
|
|
|
|
|
|
|
// Test append works
|
2015-02-02 23:39:14 -06:00
|
|
|
assert_eq!(check!(fs::metadata(&tmpdir.join("h"))).len(), 3);
|
|
|
|
{
|
2016-01-13 11:08:08 -06:00
|
|
|
let mut f = check!(c(&a).open(&tmpdir.join("h")));
|
2015-02-02 23:39:14 -06:00
|
|
|
check!(f.write("bar".as_bytes()));
|
|
|
|
}
|
|
|
|
assert_eq!(check!(fs::metadata(&tmpdir.join("h"))).len(), 6);
|
2016-01-13 11:08:08 -06:00
|
|
|
|
|
|
|
// Test .append(true) equals .write(true).append(true)
|
2015-02-02 23:39:14 -06:00
|
|
|
{
|
2016-01-13 11:08:08 -06:00
|
|
|
let mut f = check!(c(&w).append(true).open(&tmpdir.join("h")));
|
|
|
|
check!(f.write("baz".as_bytes()));
|
2015-02-02 23:39:14 -06:00
|
|
|
}
|
2016-01-13 11:08:08 -06:00
|
|
|
assert_eq!(check!(fs::metadata(&tmpdir.join("h"))).len(), 9);
|
2015-02-02 23:39:14 -06:00
|
|
|
}
|
|
|
|
|
2016-01-20 01:41:20 -06:00
|
|
|
#[test]
|
|
|
|
fn _assert_send_sync() {
|
|
|
|
fn _assert_send_sync<T: Send + Sync>() {}
|
|
|
|
_assert_send_sync::<OpenOptions>();
|
|
|
|
}
|
|
|
|
|
2015-02-02 23:39:14 -06:00
|
|
|
#[test]
|
|
|
|
fn binary_file() {
|
|
|
|
let mut bytes = [0; 1024];
|
2015-03-20 02:19:13 -05:00
|
|
|
StdRng::new().unwrap().fill_bytes(&mut bytes);
|
2015-02-02 23:39:14 -06:00
|
|
|
|
|
|
|
let tmpdir = tmpdir();
|
|
|
|
|
|
|
|
check!(check!(File::create(&tmpdir.join("test"))).write(&bytes));
|
|
|
|
let mut v = Vec::new();
|
|
|
|
check!(check!(File::open(&tmpdir.join("test"))).read_to_end(&mut v));
|
2015-03-30 11:22:46 -05:00
|
|
|
assert!(v == &bytes[..]);
|
2015-02-02 23:39:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2015-03-06 15:54:42 -06:00
|
|
|
#[cfg(not(windows))]
|
2015-02-02 23:39:14 -06:00
|
|
|
fn unlink_readonly() {
|
|
|
|
let tmpdir = tmpdir();
|
|
|
|
let path = tmpdir.join("file");
|
|
|
|
check!(File::create(&path));
|
|
|
|
let mut perm = check!(fs::metadata(&path)).permissions();
|
|
|
|
perm.set_readonly(true);
|
|
|
|
check!(fs::set_permissions(&path, perm));
|
|
|
|
check!(fs::remove_file(&path));
|
|
|
|
}
|
2015-02-23 17:07:10 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn mkdir_trailing_slash() {
|
|
|
|
let tmpdir = tmpdir();
|
|
|
|
let path = tmpdir.join("file");
|
|
|
|
check!(fs::create_dir_all(&path.join("a/")));
|
|
|
|
}
|
2015-04-16 01:21:13 -05:00
|
|
|
|
2015-09-29 13:21:39 -05:00
|
|
|
#[test]
|
|
|
|
fn canonicalize_works_simple() {
|
|
|
|
let tmpdir = tmpdir();
|
|
|
|
let tmpdir = fs::canonicalize(tmpdir.path()).unwrap();
|
|
|
|
let file = tmpdir.join("test");
|
|
|
|
File::create(&file).unwrap();
|
|
|
|
assert_eq!(fs::canonicalize(&file).unwrap(), file);
|
|
|
|
}
|
|
|
|
|
2015-04-16 01:21:13 -05:00
|
|
|
#[test]
|
|
|
|
fn realpath_works() {
|
|
|
|
let tmpdir = tmpdir();
|
2016-02-02 00:04:55 -06:00
|
|
|
if !got_symlink_permission(&tmpdir) { return };
|
|
|
|
|
2015-04-16 01:21:13 -05:00
|
|
|
let tmpdir = fs::canonicalize(tmpdir.path()).unwrap();
|
|
|
|
let file = tmpdir.join("test");
|
|
|
|
let dir = tmpdir.join("test2");
|
|
|
|
let link = dir.join("link");
|
|
|
|
let linkdir = tmpdir.join("test3");
|
|
|
|
|
|
|
|
File::create(&file).unwrap();
|
|
|
|
fs::create_dir(&dir).unwrap();
|
2016-02-02 00:04:55 -06:00
|
|
|
symlink_file(&file, &link).unwrap();
|
|
|
|
symlink_dir(&dir, &linkdir).unwrap();
|
2015-04-16 01:21:13 -05:00
|
|
|
|
|
|
|
assert!(link.symlink_metadata().unwrap().file_type().is_symlink());
|
|
|
|
|
|
|
|
assert_eq!(fs::canonicalize(&tmpdir).unwrap(), tmpdir);
|
|
|
|
assert_eq!(fs::canonicalize(&file).unwrap(), file);
|
|
|
|
assert_eq!(fs::canonicalize(&link).unwrap(), file);
|
|
|
|
assert_eq!(fs::canonicalize(&linkdir).unwrap(), dir);
|
|
|
|
assert_eq!(fs::canonicalize(&linkdir.join("link")).unwrap(), file);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn realpath_works_tricky() {
|
|
|
|
let tmpdir = tmpdir();
|
2016-02-02 00:04:55 -06:00
|
|
|
if !got_symlink_permission(&tmpdir) { return };
|
2015-04-16 01:21:13 -05:00
|
|
|
|
2016-02-02 00:04:55 -06:00
|
|
|
let tmpdir = fs::canonicalize(tmpdir.path()).unwrap();
|
2015-04-16 01:21:13 -05:00
|
|
|
let a = tmpdir.join("a");
|
|
|
|
let b = a.join("b");
|
|
|
|
let c = b.join("c");
|
|
|
|
let d = a.join("d");
|
|
|
|
let e = d.join("e");
|
|
|
|
let f = a.join("f");
|
|
|
|
|
|
|
|
fs::create_dir_all(&b).unwrap();
|
|
|
|
fs::create_dir_all(&d).unwrap();
|
|
|
|
File::create(&f).unwrap();
|
2016-02-02 00:04:55 -06:00
|
|
|
if cfg!(not(windows)) {
|
|
|
|
symlink_dir("../d/e", &c).unwrap();
|
|
|
|
symlink_file("../f", &e).unwrap();
|
|
|
|
}
|
|
|
|
if cfg!(windows) {
|
|
|
|
symlink_dir(r"..\d\e", &c).unwrap();
|
|
|
|
symlink_file(r"..\f", &e).unwrap();
|
|
|
|
}
|
2015-04-16 01:21:13 -05:00
|
|
|
|
|
|
|
assert_eq!(fs::canonicalize(&c).unwrap(), f);
|
|
|
|
assert_eq!(fs::canonicalize(&e).unwrap(), f);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn dir_entry_methods() {
|
|
|
|
let tmpdir = tmpdir();
|
|
|
|
|
|
|
|
fs::create_dir_all(&tmpdir.join("a")).unwrap();
|
|
|
|
File::create(&tmpdir.join("b")).unwrap();
|
|
|
|
|
|
|
|
for file in tmpdir.path().read_dir().unwrap().map(|f| f.unwrap()) {
|
|
|
|
let fname = file.file_name();
|
|
|
|
match fname.to_str() {
|
|
|
|
Some("a") => {
|
|
|
|
assert!(file.file_type().unwrap().is_dir());
|
|
|
|
assert!(file.metadata().unwrap().is_dir());
|
|
|
|
}
|
|
|
|
Some("b") => {
|
|
|
|
assert!(file.file_type().unwrap().is_file());
|
|
|
|
assert!(file.metadata().unwrap().is_file());
|
|
|
|
}
|
|
|
|
f => panic!("unknown file name: {:?}", f),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-10-18 23:54:51 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn read_dir_not_found() {
|
|
|
|
let res = fs::read_dir("/path/that/does/not/exist");
|
|
|
|
assert_eq!(res.err().unwrap().kind(), ErrorKind::NotFound);
|
|
|
|
}
|
2016-02-03 11:23:33 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn create_dir_all_with_junctions() {
|
|
|
|
let tmpdir = tmpdir();
|
|
|
|
let target = tmpdir.join("target");
|
|
|
|
|
|
|
|
let junction = tmpdir.join("junction");
|
|
|
|
let b = junction.join("a/b");
|
|
|
|
|
|
|
|
let link = tmpdir.join("link");
|
|
|
|
let d = link.join("c/d");
|
|
|
|
|
|
|
|
fs::create_dir(&target).unwrap();
|
|
|
|
|
|
|
|
check!(symlink_junction(&target, &junction));
|
|
|
|
check!(fs::create_dir_all(&b));
|
2016-02-03 11:50:57 -06:00
|
|
|
// the junction itself is not a directory, but `is_dir()` on a Path
|
|
|
|
// follows links
|
2016-02-03 11:23:33 -06:00
|
|
|
assert!(junction.is_dir());
|
|
|
|
assert!(b.exists());
|
|
|
|
|
|
|
|
if !got_symlink_permission(&tmpdir) { return };
|
|
|
|
check!(symlink_dir(&target, &link));
|
|
|
|
check!(fs::create_dir_all(&d));
|
|
|
|
assert!(link.is_dir());
|
|
|
|
assert!(d.exists());
|
|
|
|
}
|
2015-02-02 23:39:14 -06:00
|
|
|
}
|