fn replace_dir<P: AsRef<Path>>(old: P, new: P, stage: P) -> Result<()> {
    let old = old.as_ref();
    let new = new.as_ref();
    let stage = stage.as_ref();

    if old.exists() && old.is_dir() {
        // sync the old directory to ensure our changes have been persisted
        let old_as_file = File::open(old)
            .chain_err(|| format!("failed to open old dir '{}' for syncing", old.display()))?;
        old_as_file
            .sync_all()
            .chain_err(|| format!("failed to sync old dir '{}'", old.display()))?;

        truncate_dir(stage)?;
        if new.exists() {
            fs::rename(new, stage)
                .chain_err(|| format!("failed to move '{:?}' to '{:?}'", new, stage))?;
        }
        fs::rename(old, new).chain_err(|| format!("failed to move '{:?}' to '{:?}'", old, new))?;

        let parent_path = new
            .parent()
            .ok_or_else(|| format!("failed to sync parent directory of '{}'", new.display()))?;
        let parent_dir = File::open(parent_path)
            .chain_err(|| format!("failed to open dir '{}' for syncing", parent_path.display()))?;
        parent_dir
            .sync_all()
            .chain_err(|| format!("failed to sync dir '{}'", parent_path.display()))?;

        truncate_dir(stage)?;
    }

    Ok(())
}
/// `replace_dir` moves old to new safely.
///
/// It takes the following steps to do so:
/// 1. Truncating stage, in case a stale staging directory is still around.
/// 1. Moving new to stage.
/// 1. Moving old to new.
/// 1. Truncating stage again to clean up.
/// If new doesn't exist, it simply renames old to new. if new is a file, it
/// deletes the file and moves the directory. If old doesn't exist, nothing
/// happens. If old is a file and not a directory, nothing happens.
`replace_dir` moves old to new safely.
It takes the following steps to do so:
1. Truncating stage, in case a stale staging directory is still around.
1. Moving new to stage.
1. Moving old to new.
1. Truncating stage again to clean up.
If new doesn't exist, it simply renames old to new. if new is a file, it
deletes the file and moves the directory. If old doesn't exist, nothing
happens. If old is a file and not a directory, nothing happens.
` replace_dir ` moves old to new safely . It takes the following steps to do so : 1 . Truncating stage in case a stale staging directory is still around . 1 . Moving new to stage . 1 . Moving old to new . 1 . Truncating stage again to clean up . If new doesn ' t exist it simply renames old to new . if new is a file it deletes the file and moves the directory . If old doesn ' t exist nothing happens . If old is a file and not a directory nothing happens .
fn replace_dir<P: AsRef<Path>>(old: P, new: P, stage: P) -> Result<()> {
    let old = old.as_ref();
    let new = new.as_ref();
    let stage = stage.as_ref();
    if old.exists() && old.is_dir() {
        // sync the old directory to ensure our changes have been persisted
        let old_as_file = File::open(old)
            .chain_err(|| format!("failed to open old dir '{}' for syncing", old.display()))?;
        old_as_file
            .sync_all()
            .chain_err(|| format!("failed to sync old dir '{}'", old.display()))?;
        truncate_dir(stage)?;
        if new.exists() {
            fs::rename(new, stage)
                .chain_err(|| format!("failed to move '{:?}' to '{:?}'", new, stage))?;
        }
        fs::rename(old, new).chain_err(|| format!("failed to move '{:?}' to '{:?}'", old, new))?;
        let parent_path = new
            .parent()
            .ok_or_else(|| format!("failed to sync parent directory of '{}'", new.display()))?;
        let parent_dir = File::open(parent_path)
            .chain_err(|| format!("failed to open dir '{}' for syncing", parent_path.display()))?;
        parent_dir
            .sync_all()
            .chain_err(|| format!("failed to sync dir '{}'", parent_path.display()))?;
        truncate_dir(stage)?;
    }
    Ok(())
}
fn replace_dir < P : AsRef < Path > > ( old : P , new : P , stage : P ) -> Result < ( ) > { let old = old . as_ref ( ) ; let new = new . as_ref ( ) ; let stage = stage . as_ref ( ) ; if old . exists ( ) && old . is_dir ( ) { // sync the old directory to ensure our changes have been persisted let old_as_file = File :: open ( old ) . chain_err ( | | format ! ( " " old display ( ) ) ) ? ; old_as_file . sync_all ( ) . chain_err ( | | format ! ( " " old display ( ) ) ) ? ; truncate_dir ( stage ) ? ; if new . exists ( ) { fs :: rename ( new , stage ) . chain_err ( | | format ! ( " " new stage ) ) ? ; } fs :: rename ( old , new ) . chain_err ( | | format ! ( " " old new ) ) ? ; let parent_path = new . parent ( ) . ok_or_else ( | | format ! ( " " new display ( ) ) ) ? ; let parent_dir = File :: open ( parent_path ) . chain_err ( | | format ! ( " " parent_path display ( ) ) ) ? ; parent_dir . sync_all ( ) . chain_err ( | | format ! ( " " parent_path display ( ) ) ) ? ; truncate_dir ( stage ) ? ; } Ok ( ( ) ) }
`replace_dir` moves old to new safely.
` replace_dir ` moves old to new safely .
old
P
new
P
stage
P
old
P
new
P
stage
P