Skip to content

Don't let remove_dir_all recursively remove a symlink #31468

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 8, 2016
Merged
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
34 changes: 34 additions & 0 deletions src/libstd/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1881,12 +1881,46 @@ mod tests {
check!(fs::create_dir_all(&d2));
check!(check!(File::create(&canary)).write(b"foo"));
check!(symlink_junction(&d2, &dt.join("d2")));
let _ = symlink_file(&canary, &d1.join("canary"));
check!(fs::remove_dir_all(&d1));

assert!(!d1.is_dir());
assert!(canary.exists());
}

#[test]
fn recursive_rmdir_of_symlink() {
// test we do not recursively delete a symlink but only dirs.
let tmpdir = tmpdir();
let link = tmpdir.join("d1");
let dir = tmpdir.join("d2");
let canary = dir.join("do_not_delete");
check!(fs::create_dir_all(&dir));
check!(check!(File::create(&canary)).write(b"foo"));
check!(symlink_junction(&dir, &link));
check!(fs::remove_dir_all(&link));

assert!(!link.is_dir());
assert!(canary.exists());
}

#[test]
// only Windows makes a distinction between file and directory symlinks.
#[cfg(windows)]
fn recursive_rmdir_of_file_symlink() {
let tmpdir = tmpdir();
if !got_symlink_permission(&tmpdir) { return };

let f1 = tmpdir.join("f1");
let f2 = tmpdir.join("f2");
check!(check!(File::create(&f1)).write(b"foo"));
check!(symlink_file(&f1, &f2));
match fs::remove_dir_all(&f2) {
Ok(..) => panic!("wanted a failure"),
Err(..) => {}
}
}

#[test]
fn unicode_path_is_dir() {
assert!(Path::new(".").is_dir());
Expand Down
11 changes: 10 additions & 1 deletion src/libstd/sys/unix/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,10 +511,19 @@ pub fn rmdir(p: &Path) -> io::Result<()> {
}

pub fn remove_dir_all(path: &Path) -> io::Result<()> {
let filetype = try!(lstat(path)).file_type();
if filetype.is_symlink() {
unlink(path)
} else {
remove_dir_all_recursive(path)
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic looks pretty similar between here and windows, perhaps it could be lifted to std::fs and leave the recursive bits to the platform-specific modules?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Windows version is similar but a bit tricky. It uses rmdir() so it can only remove directory symlinks and not file symlinks. Unix just uses unlink(). But I don't mind if we make the distinction here or in std::fs.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right, yeah, good point!


fn remove_dir_all_recursive(path: &Path) -> io::Result<()> {
for child in try!(readdir(path)) {
let child = try!(child);
if try!(child.file_type()).is_dir() {
try!(remove_dir_all(&child.path()));
try!(remove_dir_all_recursive(&child.path()));
} else {
try!(unlink(&child.path()));
}
Expand Down
13 changes: 12 additions & 1 deletion src/libstd/sys/windows/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -525,11 +525,22 @@ pub fn rmdir(p: &Path) -> io::Result<()> {
}

pub fn remove_dir_all(path: &Path) -> io::Result<()> {
let filetype = try!(lstat(path)).file_type();
if filetype.is_symlink() {
// On Windows symlinks to files and directories are removed differently.
// rmdir only deletes dir symlinks and junctions, not file symlinks.
rmdir(path)
} else {
remove_dir_all_recursive(path)
}
}

fn remove_dir_all_recursive(path: &Path) -> io::Result<()> {
for child in try!(readdir(path)) {
let child = try!(child);
let child_type = try!(child.file_type());
if child_type.is_dir() {
try!(remove_dir_all(&child.path()));
try!(remove_dir_all_recursive(&child.path()));
} else if child_type.is_symlink_dir() {
try!(rmdir(&child.path()));
} else {
Expand Down