| 96 | } |
| 97 | |
| 98 | fn normalize_path(path: &Path) -> PathBuf { |
| 99 | let mut result = PathBuf::new(); |
| 100 | let mut go_back = 0; |
| 101 | for component in path.components() { |
| 102 | match component.as_os_str().to_str() { |
| 103 | Some(".") => {} |
| 104 | Some("..") => { |
| 105 | if !result.pop() { |
| 106 | go_back += 1; |
| 107 | } |
| 108 | } |
| 109 | _ => result.push(component), |
| 110 | } |
| 111 | } |
| 112 | if go_back > 0 { |
| 113 | let mut prefix = PathBuf::new(); |
| 114 | for _ in 0..go_back { |
| 115 | prefix.push(".."); |
| 116 | } |
| 117 | prefix.push(result); |
| 118 | result = prefix; |
| 119 | } |
| 120 | result |
| 121 | } |
| 122 | |
| 123 | /// Create a tree based on a sequence of lines describing the tree structure |
| 124 | /// inside the given directory |