-
Notifications
You must be signed in to change notification settings - Fork 0
/
filelist.rs
65 lines (60 loc) · 2.06 KB
/
filelist.rs
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
#![deny(unused_results)]
use std::{fs::OpenOptions, io::{self, Write}, path::PathBuf};
use foundations::fs::*;
fn main() {
let mut args = std::env::args_os();
let _ = args.next();
let src_root: PathBuf = args.next().map(Into::into).unwrap_or_else(|| PathBuf::from("."));
// In fact, normalize-then-sort is more reasonable, and the behavior of the `iter_path`'s `normalized`
// is also normalize-then-sort. But since the previous behavior was sort-then-normalize, `iter_path`'s
// `normalized` is not used, and the same behavior as before is maintained. Sorting before normalizing
// could lead to inconsistencies in the order under different conditions! The impact of this issue
// needs further investigation.
let src_list = iter_path(&src_root, None, true, false).unwrap();
let dst = args.next();
let ffmpeg_concat = args.next().is_some();
let mut dst_h: Box<dyn Write> = if let Some(dst_path) = dst {
Box::new(OpenOptions::new().create_new(true).write(true).open(&dst_path).unwrap())
} else {
Box::new(io::stdout().lock())
};
macro_rules! ws {
($buf:expr) => {
dst_h.write_all($buf.as_bytes()).unwrap();
};
}
macro_rules! wn {
() => {
dst_h.write_all(b"\n").unwrap();
};
}
if !ffmpeg_concat {
ws!("Berylsoft File Fragment Hash Standard Version 2.2");
wn!();
wn!();
ws!("type=filelist");
ws!("writer=filelist@");
ws!(env!("GIT_HASH"));
wn!();
wn!();
}
for (src_path, is_dir) in src_list {
if !is_dir {
// see `iter_path` call above
let src_path = normalize(&src_path);
let name = src_path.to_str().unwrap();
#[cfg(windows)]
let name = name.replace("\\", "/");
assert!(!name.contains("\n"));
if ffmpeg_concat {
assert!(!name.contains("'"));
ws!("file '");
}
ws!(name);
if ffmpeg_concat {
ws!("'");
}
wn!();
}
}
}