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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
use crate::{
common::{util, AbsolutePath, Item},
config::Config,
verbose_println,
};
use derive_more::From;
use failure::Fail;
use std::{
collections::HashSet,
ffi::OsString,
io, iter,
path::{Path, PathBuf},
};
use walkdir::WalkDir;
fn make_hidden(path: &Path) -> PathBuf {
let path_str = OsString::from(path.as_os_str());
let hidden_path = {
let mut hidden_path = OsString::from(".");
hidden_path.push(path_str);
hidden_path
};
PathBuf::from(hidden_path)
}
fn link_dir_contents(
dir: &AbsolutePath,
excludes: &HashSet<&AbsolutePath>,
) -> Result<Vec<Item>, Error> {
let mut res = vec![];
for entry in WalkDir::new(dir)
.into_iter()
.filter_entry(|entry| !util::is_hidden(entry.file_name()))
{
let entry = entry?;
let path = AbsolutePath::from(entry.path());
if excludes.contains(&path) {
verbose_println!("Excluded {}", path);
}
if !util::is_hidden(entry.file_name())
&& entry.file_type().is_file()
&& !excludes.contains(&path)
{
let dest = {
let dest_tail = match dir.parent() {
None => path.as_path(),
Some(parent) => path
.strip_prefix(parent)
.expect("dir must be a prefix of entry"),
};
AbsolutePath::from(util::home_dir().join(make_hidden(dest_tail)))
};
let source = path;
res.push(Item::new(source, dest));
}
}
Ok(res)
}
fn find_items(
root: AbsolutePath,
is_prefixed: &impl Fn(&Path) -> bool,
active_prefixed_dirs: &HashSet<&Path>,
excludes: &HashSet<&AbsolutePath>,
res: &mut Vec<Item>,
) -> Result<(), Error> {
for entry in root.read_dir()? {
let entry = entry?;
let path = AbsolutePath::from(entry.path());
let entry_name = entry.file_name();
let entry_name = Path::new(&entry_name);
let excluded = excludes.contains(&path);
if util::is_hidden(entry_name.as_os_str()) || excluded {
if excluded {
verbose_println!("Excluded {}", path);
}
continue;
}
if is_prefixed(&entry_name) {
if active_prefixed_dirs.contains(entry_name) {
find_items(path, is_prefixed, active_prefixed_dirs, excludes, res)?;
}
} else {
let contents = link_dir_contents(&AbsolutePath::from(entry.path()), excludes)?;
res.extend(contents);
}
}
Ok(())
}
pub fn get(config: &Config) -> Result<Vec<Item>, Error> {
let hostname_prefix = "host-";
let tag_prefix = "tag-";
let prefixes = [hostname_prefix, tag_prefix];
let is_prefixed = |filename: &Path| -> bool {
for prefix in &prefixes {
match filename.to_str() {
Some(s) if s.starts_with(prefix) => return true,
_ => (),
}
}
false
};
let hostname_dir = PathBuf::from([hostname_prefix, config.hostname()].concat());
let tag_dirs: Vec<PathBuf> = config
.tags()
.iter()
.map(|tag| PathBuf::from([tag_prefix, tag].concat()))
.collect();
let active_prefixed_dirs: HashSet<&Path> = iter::once(&hostname_dir)
.chain(tag_dirs.iter())
.map(|p| p.as_path())
.collect();
let excludes = config.excludes().iter().collect();
let mut res = vec![];
find_items(
config.dotfiles_path().clone(),
&is_prefixed,
&active_prefixed_dirs,
&excludes,
&mut res,
)?;
let mut seen = HashSet::new();
for item in &res {
let dest = item.dest().clone();
if seen.contains(&dest) {
return Err(DuplicateFiles { dest });
} else {
seen.insert(dest);
}
}
Ok(res)
}
#[derive(Debug, From, Fail)]
pub enum Error {
#[fail(display = "multiple source files for destination {}", dest)]
DuplicateFiles { dest: AbsolutePath },
#[fail(display = "error reading from dotfiles directory ({})", _0)]
IoError(#[fail(cause)] io::Error),
#[fail(display = "error reading from dotfiles directory ({})", _0)]
WalkdirError(#[fail(cause)] walkdir::Error),
}
use self::Error::*;