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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
use std::ffi::OsString;
use std::io;
use std::path::{Component, Path, PathBuf};
use crate::index::normalize_path;
use crate::prelude::*;
use crate::store::Entries;
#[derive(Clone, Debug)]
pub struct CaselessFs<S> {
inner: S,
}
impl<S: Store> CaselessFs<S> {
pub fn new(inner: S) -> Self {
Self { inner }
}
pub fn into_inner(self) -> S {
self.inner
}
pub fn get_ref(&self) -> &S {
&self.inner
}
pub fn get_mut(&mut self) -> &mut S {
&mut self.inner
}
pub fn find<P: AsRef<Path>>(&self, path: P) -> Vec<PathBuf> {
let path = normalize_path(path.as_ref());
let mut paths = vec![PathBuf::new()];
for component in path.components() {
paths = find_next_ascii_lowercase(&self.inner, &component, paths);
if paths.len() == 0 {
return paths;
}
}
paths
}
}
impl<S: Store> Store for CaselessFs<S> {
type File = S::File;
fn open_path(&self, path: &Path) -> io::Result<Self::File> {
if let Ok(file) = self.inner.open_path(path) {
return Ok(file);
}
for path in self.find(path) {
return self.inner.open_path(&path);
}
Err(io::ErrorKind::NotFound.into())
}
fn entries_path(&self, path: &Path) -> io::Result<Entries> {
self.inner.entries_path(path)
}
}
fn find_next_ascii_lowercase<S: Store>(
fs: &S,
component: &Component,
paths: Vec<PathBuf>,
) -> Vec<PathBuf> {
let mut next = Vec::new();
let target: OsString = match component {
Component::Normal(os_s) => (*os_s).to_owned(),
Component::RootDir => {
next.push(Path::new("/").to_owned());
return next;
}
_ => {
panic!(format!("unexpected path component {:?}", component));
}
};
if let Some(t_s) = target.to_str() {
for path in paths {
if let Ok(entries) = fs.entries(&path) {
for e in entries {
if let Ok(entry) = e {
if let Some(e_s) = entry.name.to_str() {
if t_s.to_ascii_lowercase() == e_s.to_ascii_lowercase() {
let mut path = path.to_owned();
path.push(&entry.name);
next.push(path);
}
}
}
}
}
}
} else {
for path in paths {
if let Ok(entries) = fs.entries(&path) {
for e in entries {
if let Ok(entry) = e {
if &entry.name == &target {
let mut path = path.to_owned();
path.push(&entry.name);
next.push(path);
}
}
}
}
}
}
next
}