1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//! Platform-agnostic path normalization compatible with tar behavior.

use std::path;
use std::fmt;

/// Normalize paths, removing CurDir components and resolving ParentDir when possible.
pub fn normalize<P: AsRef<path::Path>>(inpath: &P) -> path::PathBuf where P: fmt::Debug {
    let mut outpath = path::PathBuf::new();
    
    for component in inpath.as_ref().components() {
        match component {
            path::Component::CurDir => {},
            path::Component::ParentDir => {
                outpath.pop();
            },
            path::Component::RootDir => {
                outpath.push(component);
            },
            path::Component::Prefix(_) => {
                outpath.push(component);
            },
            path::Component::Normal(_) => {
                outpath.push(component);
            }
        }
    }
    
    outpath
}