Skip to content

Commit

Permalink
Auto merge of #1015 - brson:remove_dir_all, r=brson
Browse files Browse the repository at this point in the history
Aggressive remove_dir_all

Let's see if this helps the flakiness.

cc @Diggsey @alexcrichton

cc @Aaronepower this PR contains a self-contained remove_dir_all module based on winapi.
  • Loading branch information
bors committed Mar 31, 2017
2 parents f9077d2 + b61370a commit 0acd9dd
Show file tree
Hide file tree
Showing 5 changed files with 845 additions and 48 deletions.
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

0 comments on commit 0acd9dd

Please sign in to comment.