Skip to content
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

Aggressive remove_dir_all #1015

Merged
merged 3 commits into from
Apr 5, 2017
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/rustup-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ url = "1.1"
toml = "0.1.27"
semver = "0.4.0"
download = { path = "../download" }
lazy_static = "0.1.15"

[target."cfg(windows)".dependencies]
winapi = "0.2.8"
Expand Down
3 changes: 3 additions & 0 deletions src/rustup-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ extern crate url;
extern crate toml;
extern crate download;
extern crate semver;
#[macro_use]
extern crate lazy_static;

#[cfg(windows)]
extern crate winapi;
Expand All @@ -35,6 +37,7 @@ pub mod raw;
pub mod tty;
pub mod utils;
pub mod toml_utils;
mod remove_dir_all;

pub use errors::*;
pub use notifications::{Notification};
Expand Down
53 changes: 5 additions & 48 deletions src/rustup-utils/src/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,54 +320,11 @@ pub fn remove_dir(path: &Path) -> io::Result<()> {
fs::remove_file(path)
}
} else {
let mut result = Ok(());

// The implementation of `remove_dir_all` is broken on windows,
// so may need to try multiple times!
for _ in 0..5 {
result = rm_rf(path);
if !is_directory(path) {
return Ok(());
}
thread::sleep(Duration::from_millis(16));
}
result
}
}

// Again because remove_dir all doesn't delete write-only files on windows,
// this is a custom implementation, more-or-less copied from cargo.
// cc rust-lang/rust#31944
// cc https://github.com/rust-lang/cargo/blob/master/tests/support/paths.rs#L52-L80
fn rm_rf(path: &Path) -> io::Result<()> {
if path.exists() {
for file in fs::read_dir(path).unwrap() {
let file = try!(file);
let is_dir = try!(file.file_type()).is_dir();
let ref file = file.path();

if is_dir {
try!(rm_rf(file));
} else {
// On windows we can't remove a readonly file, and git will
// often clone files as readonly. As a result, we have some
// special logic to remove readonly files on windows.
match fs::remove_file(file) {
Ok(()) => {}
Err(ref e) if cfg!(windows) &&
e.kind() == io::ErrorKind::PermissionDenied => {
let mut p = file.metadata().unwrap().permissions();
p.set_readonly(false);
fs::set_permissions(file, p).unwrap();
try!(fs::remove_file(file));
}
Err(e) => return Err(e)
}
}
}
fs::remove_dir(path)
} else {
Ok(())
// Again because remove_dir all doesn't delete write-only files on windows,
// this is a custom implementation, more-or-less copied from cargo.
// cc rust-lang/rust#31944
// cc https://github.com/rust-lang/cargo/blob/master/tests/support/paths.rs#L52
::remove_dir_all::remove_dir_all(path)
}
}

Expand Down
Loading