team-ljyzipelo

공개
76줄2.3 KB
1use super::*;
2
3pub(super) fn worker_catalog(
4 bytes: &[u8],
5 limits: Limits,
6 decode: bool,
7 password: Option<&[u8]>,
8) -> Result<archive::Catalog, ErrorCode> {
9 let scratch = Scratch::new();
10 fs::write(scratch.0.join("input"), bytes).unwrap();
11 let events = execute(Start {
12 action: Action::List {
13 archive: scratch.path("input"),
14 },
15 limits,
16 });
17 if terminal(&events).outcome != Outcome::Completed {
18 return Err(terminal(&events).error);
19 }
20 let list_id = events
21 .iter()
22 .find_map(|event| match event {
23 Message::ListEnd { list_id, .. } => Some(list_id.clone()),
24 _ => None,
25 })
26 .expect("작업자의 목록 식별자가 필요함");
27 let entries: Vec<_> = events
28 .into_iter()
29 .filter_map(|event| match event {
30 Message::Entry(entry) => Some(entry),
31 _ => None,
32 })
33 .collect();
34 if decode {
35 let builder = fs::DirBuilder::new();
36 #[cfg(unix)]
37 let mut builder = builder;
38 #[cfg(unix)]
39 {
40 use std::os::unix::fs::DirBuilderExt;
41 builder.mode(0o700);
42 }
43 builder.create(scratch.0.join("output")).unwrap();
44 let action = Action::Extract {
45 archive: scratch.path("input"),
46 destination: scratch.path("output"),
47 selection: Selection::All,
48 gzip_output_name: (list_id.format == Format::Gzip).then(|| entries[0].name.clone()),
49 list_id: Some(list_id.clone()),
50 };
51 let start = Start { action, limits };
52 let extracted = if let Some(password) = password {
53 interactive(start, |id, _, _| password_reply(id, password))
54 } else {
55 execute(start)
56 };
57 if terminal(&extracted).outcome != Outcome::Completed {
58 return Err(terminal(&extracted).error);
59 }
60 }
61 let members = entries
62 .into_iter()
63 .map(|entry| {
64 let data = if decode && entry.kind == EntryKind::File {
65 fs::read(scratch.0.join("output").join(&entry.name)).unwrap()
66 } else {
67 Vec::new()
68 };
69 archive::Member { entry, data }
70 })
71 .collect();
72 Ok(archive::Catalog {
73 format: list_id.format,
74 members,
75 })
76}