Skip to content

Commit

Permalink
incr.comp.: Canonicalize path to session directory before deleteing it.
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelwoerister committed Aug 29, 2016
1 parent 50b008a commit bcd2f90
Showing 1 changed file with 29 additions and 5 deletions.
34 changes: 29 additions & 5 deletions src/librustc_incremental/persist/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ pub fn prepare_session_directory(tcx: TyCtxt) -> Result<bool, ()> {

// Try to remove the session directory we just allocated. We don't
// know if there's any garbage in it from the failed copy action.
if let Err(err) = std_fs::remove_dir_all(&session_dir) {
if let Err(err) = safe_remove_dir_all(&session_dir) {
tcx.sess.warn(&format!("Failed to delete partly initialized \
session dir `{}`: {}",
session_dir.display(),
Expand Down Expand Up @@ -282,7 +282,7 @@ pub fn finalize_session_directory(sess: &Session, svh: Svh) {
debug!("finalize_session_directory() - invalidating session directory: {}",
incr_comp_session_dir.display());

if let Err(err) = std_fs::remove_dir_all(&*incr_comp_session_dir) {
if let Err(err) = safe_remove_dir_all(&*incr_comp_session_dir) {
sess.warn(&format!("Error deleting incremental compilation \
session directory `{}`: {}",
incr_comp_session_dir.display(),
Expand Down Expand Up @@ -460,7 +460,7 @@ fn lock_directory(sess: &Session,

fn delete_session_dir_lock_file(sess: &Session,
lock_file_path: &Path) {
if let Err(err) = std_fs::remove_file(&lock_file_path) {
if let Err(err) = safe_remove_file(&lock_file_path) {
sess.warn(&format!("Error deleting lock file for incremental \
compilation session directory `{}`: {}",
lock_file_path.display(),
Expand Down Expand Up @@ -841,7 +841,7 @@ pub fn garbage_collect_session_directories(sess: &Session) -> io::Result<()> {
debug!("garbage_collect_session_directories() - deleting `{}`",
path.display());

if let Err(err) = std_fs::remove_dir_all(&path) {
if let Err(err) = safe_remove_dir_all(&path) {
sess.warn(&format!("Failed to garbage collect finalized incremental \
compilation session directory `{}`: {}",
path.display(),
Expand All @@ -860,7 +860,7 @@ pub fn garbage_collect_session_directories(sess: &Session) -> io::Result<()> {
debug!("garbage_collect_session_directories() - deleting `{}`",
path.display());

if let Err(err) = std_fs::remove_dir_all(&path) {
if let Err(err) = safe_remove_dir_all(&path) {
sess.warn(&format!("Failed to garbage collect incremental \
compilation session directory `{}`: {}",
path.display(),
Expand Down Expand Up @@ -893,6 +893,30 @@ fn all_except_most_recent(deletion_candidates: Vec<(SystemTime, PathBuf, Option<
}
}

/// Since paths of artifacts within session directories can get quite long, we
/// need to support deleting files with very long paths. The regular
/// WinApi functions only support paths up to 260 characters, however. In order
/// to circumvent this limitation, we canonicalize the path of the directory
/// before passing it to std::fs::remove_dir_all(). This will convert the path
/// into the '\\?\' format, which supports much longer paths.
fn safe_remove_dir_all(p: &Path) -> io::Result<()> {
if p.exists() {
let canonicalized = try!(p.canonicalize());
std_fs::remove_dir_all(canonicalized)
} else {
Ok(())
}
}

fn safe_remove_file(p: &Path) -> io::Result<()> {
if p.exists() {
let canonicalized = try!(p.canonicalize());
std_fs::remove_file(canonicalized)
} else {
Ok(())
}
}

#[test]
fn test_all_except_most_recent() {
assert_eq!(all_except_most_recent(
Expand Down

0 comments on commit bcd2f90

Please sign in to comment.