Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions library/std/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2446,14 +2446,12 @@ pub fn symlink_metadata<P: AsRef<Path>>(path: P) -> io::Result<Metadata> {
/// # Platform-specific behavior
///
/// This function currently corresponds to the `rename` function on Unix
/// and the `SetFileInformationByHandle` function on Windows.
/// and the `MoveFileEx` function with the `MOVEFILE_REPLACE_EXISTING` flag on Windows.
///
/// Because of this, the behavior when both `from` and `to` exist differs. On
/// Unix, if `from` is a directory, `to` must also be an (empty) directory. If
/// `from` is not a directory, `to` must also be not a directory. The behavior
/// on Windows is the same on Windows 10 1607 and higher if `FileRenameInfoEx`
/// is supported by the filesystem; otherwise, `from` can be anything, but
/// `to` must *not* be a directory.
/// `from` is not a directory, `to` must also be not a directory. In contrast,
/// on Windows, `from` can be anything, but `to` must *not* be a directory.
///
/// Note that, this [may change in the future][changes].
///
Expand Down
74 changes: 0 additions & 74 deletions library/std/src/fs/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1913,77 +1913,3 @@ fn test_hidden_file_truncation() {
let metadata = file.metadata().unwrap();
assert_eq!(metadata.len(), 0);
}

// See https://github.com/rust-lang/rust/pull/131072 for more details about why
// these two tests are disabled under Windows 7 here.
#[cfg(windows)]
#[test]
#[cfg_attr(target_vendor = "win7", ignore = "Unsupported under Windows 7.")]
fn test_rename_file_over_open_file() {
// Make sure that std::fs::rename works if the target file is already opened with FILE_SHARE_DELETE. See #123985.
let tmpdir = tmpdir();

// Create source with test data to read.
let source_path = tmpdir.join("source_file.txt");
fs::write(&source_path, b"source hello world").unwrap();

// Create target file with test data to read;
let target_path = tmpdir.join("target_file.txt");
fs::write(&target_path, b"target hello world").unwrap();

// Open target file
let target_file = fs::File::open(&target_path).unwrap();

// Rename source
fs::rename(source_path, &target_path).unwrap();

core::mem::drop(target_file);
assert_eq!(fs::read(target_path).unwrap(), b"source hello world");
}

#[test]
#[cfg(windows)]
#[cfg_attr(target_vendor = "win7", ignore = "Unsupported under Windows 7.")]
fn test_rename_directory_to_non_empty_directory() {
// Renaming a directory over a non-empty existing directory should fail on Windows.
let tmpdir: TempDir = tmpdir();

let source_path = tmpdir.join("source_directory");
let target_path = tmpdir.join("target_directory");

fs::create_dir(&source_path).unwrap();
fs::create_dir(&target_path).unwrap();

fs::write(target_path.join("target_file.txt"), b"target hello world").unwrap();

error!(fs::rename(source_path, target_path), 145); // ERROR_DIR_NOT_EMPTY
}

#[test]
fn test_rename_symlink() {
let tmpdir = tmpdir();
let original = tmpdir.join("original");
let dest = tmpdir.join("dest");
let not_exist = Path::new("does not exist");

symlink_file(not_exist, &original).unwrap();
fs::rename(&original, &dest).unwrap();
// Make sure that renaming `original` to `dest` preserves the symlink.
assert_eq!(fs::read_link(&dest).unwrap().as_path(), not_exist);
}

#[test]
#[cfg(windows)]
fn test_rename_junction() {
let tmpdir = tmpdir();
let original = tmpdir.join("original");
let dest = tmpdir.join("dest");
let not_exist = Path::new("does not exist");

junction_point(&not_exist, &original).unwrap();
fs::rename(&original, &dest).unwrap();

// Make sure that renaming `original` to `dest` preserves the junction point.
// Junction links are always absolute so we just check the file name is correct.
assert_eq!(fs::read_link(&dest).unwrap().file_name(), Some(not_exist.as_os_str()));
}
4 changes: 3 additions & 1 deletion library/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,9 @@
//!
//! - after-main use of thread-locals, which also affects additional features:
//! - [`thread::current()`]
//! - before-main stdio file descriptors are not guaranteed to be open on unix platforms
//! - under UNIX, before main, file descriptors 0, 1, and 2 may be unchanged
//! (they are guaranteed to be open during main,
//! and are opened to /dev/null O_RDWR if they weren't open on program start)
//!
//!
//! [I/O]: io
Expand Down
Loading
Loading