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
177
178
179
180
181
182
183
184
185
186
187
188
|
mod encoded_string;
mod error;
mod flags;
mod main_result;
use std::{
collections::HashSet,
env,
fs,
hash::Hash,
io::{self, BufRead, BufReader, Write},
path::Path,
process::{Command, Stdio}
};
use {
encoded_string::EncodedString,
error::Error,
main_result::MainResult
};
use {
flags::Flags,
getopt::{Opt, Parser},
itertools::Itertools,
tempfile::{Builder, NamedTempFile, TempDir}
};
fn main() -> MainResult {
work().into()
}
fn work() -> Result<(), Error> {
let mut argv = env::args().collect::<Vec<String>>();
let mut flags = Flags { ..Default::default() };
let mut opts = Parser::new(&argv, ":0ei");
loop {
match opts.next().transpose() {
Ok(v) => match v {
None => break,
Some(opt) => match opt {
Opt('0', None) => flags.nul = true,
Opt('e', None) => flags.encode = true,
Opt('i', None) => flags.individual = true,
_ => { return Err(Error::BadArgs); }
}
},
Err(_) => { return Err(Error::BadArgs); }
}
}
let rest = argv.split_off(opts.index());
let cmd = rest.get(0).ok_or(Error::BadArgs)?;
let args = &rest[1..];
let mut old_files: Vec<_> = io::stdin()
.lines()
.collect::<Result<_, _>>()
.unwrap();
if old_files.iter().any(|s| s.is_empty()) {
return Err(Error::BadArgs);
}
if flags.encode {
old_files = old_files.iter().map(move |s| encode_string(s)).collect();
}
let dups = duplicate_elements(old_files.clone());
if !dups.is_empty() {
return Err(Error::DupInputElems(dups));
}
let mut child = Command::new(cmd)
.args(args)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()
.map_err(|e| Error::SpawnFailed(cmd.to_owned(), e))?;
{
let mut stdin = child.stdin.take().expect("Failed to open stdin");
old_files.iter().try_for_each(|f| writeln!(stdin, "{f}"))?;
}
let output = child.wait_with_output()?;
if !output.status.success() {
return Err(Error::Nop);
}
let new_files = String::from_utf8(output.stdout)?
.lines()
.map(|s| if flags.encode {
decode_string(s)
} else {
s.to_string()
})
.collect::<Vec<String>>();
if old_files.len() != new_files.len() {
return Err(Error::BadLengths);
}
let dups = duplicate_elements(new_files.iter().cloned().collect::<Vec<_>>());
if !dups.is_empty() {
return Err(Error::DupOutputElems(dups));
}
let tmpdir = Builder::new().prefix("mmv").tempdir()?;
let mut conflicts = Vec::<&str>::new();
old_files
.iter()
.zip(new_files.iter())
.filter(|(x, y)| x != y)
.try_for_each(|(x, y)| try_move(&mut conflicts, &tmpdir, x, y))?;
conflicts
.iter()
.try_for_each(|c| do_move(&tmpdir, c))?;
Ok(())
}
fn duplicate_elements<T>(iter: T) -> Vec<T::Item>
where
T: IntoIterator,
T::Item: Clone + Eq + Hash
{
let mut uniq = HashSet::new();
iter
.into_iter()
.filter(|x| !uniq.insert(x.clone()))
.unique()
.collect::<Vec<_>>()
}
fn encode_string(s: &str) -> String {
let mut n = String::new();
s.chars().for_each(|c| match c {
'\\' => n.push_str("\\\\"),
'\n' => n.push_str("\\n"),
_ => n.push(c)
});
n
}
fn decode_string(s: &str) -> String {
(EncodedString { s: s.bytes() }).decode()
}
fn decode_from_file(tmpfile: &NamedTempFile) -> Result<Vec<String>, io::Error> {
BufReader::new(tmpfile)
.lines()
.map(|r| match r {
Ok(s) => {
let es = EncodedString { s: s.bytes() };
Ok(es.decode())
},
Err(_) => r
})
.filter(|r| match r {
Ok(s) => !s.is_empty(),
_ => true
})
.collect::<Result<Vec<String>, _>>()
}
fn try_move<'a>(
conflicts: &mut Vec<&'a str>,
tmpdir: &TempDir,
old: &str,
new: &'a str
) -> Result<(), io::Error> {
if Path::new(new).exists() {
let new_loc = tmpdir.path().to_str().unwrap().to_owned() + "/" + new;
fs::rename(old, new_loc)?;
conflicts.push(new);
} else {
fs::rename(old, new)?;
}
Ok(())
}
fn do_move(tmpdir: &TempDir, new: &str) -> Result<(), io::Error> {
let old = tmpdir.path().to_str().unwrap().to_owned() + "/" + new;
fs::rename(old, new)?;
Ok(())
}
|