1 //
2 // Copyright (C) 2021 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 //! Pack profiles into reports.
18
19 use anyhow::{anyhow, Result};
20 use lazy_static::lazy_static;
21 use macaddr::MacAddr6;
22 use std::fs::{self, File, Permissions};
23 use std::io::{Read, Write};
24 use std::os::unix::fs::PermissionsExt;
25 use std::path::{Path, PathBuf};
26 use std::time::{Duration, SystemTime};
27 use uuid::v1::{Context, Timestamp};
28 use uuid::Uuid;
29 use zip::write::FileOptions;
30 use zip::CompressionMethod::Deflated;
31 use zip::ZipWriter;
32
33 use crate::config::Config;
34
35 lazy_static! {
36 pub static ref UUID_CONTEXT: Context = Context::new(0);
37 }
38
pack_report(profile: &Path, report: &Path, config: &Config) -> Result<String>39 pub fn pack_report(profile: &Path, report: &Path, config: &Config) -> Result<String> {
40 let mut report = PathBuf::from(report);
41 let report_filename = get_report_filename(&config.node_id)?;
42 report.push(&report_filename);
43 report.set_extension("zip");
44
45 // Remove the current report file if exists.
46 fs::remove_file(&report).ok();
47
48 let report_file = fs::OpenOptions::new().create_new(true).write(true).open(&report)?;
49
50 // Set report file ACL bits to 644, so that this can be shared to uploaders.
51 // Who has permission to actually read the file is protected by SELinux policy.
52 fs::set_permissions(&report, Permissions::from_mode(0o644))?;
53
54 let options = FileOptions::default().compression_method(Deflated);
55 let mut zip = ZipWriter::new(report_file);
56
57 fs::read_dir(profile)?
58 .filter_map(|e| e.ok())
59 .map(|e| e.path())
60 .filter(|e| e.is_file())
61 .try_for_each(|e| -> Result<()> {
62 let filename = e
63 .file_name()
64 .and_then(|f| f.to_str())
65 .ok_or_else(|| anyhow!("Malformed profile path: {}", e.display()))?;
66 zip.start_file(filename, options)?;
67 let mut f = File::open(e)?;
68 let mut buffer = Vec::new();
69 f.read_to_end(&mut buffer)?;
70 zip.write_all(&*buffer)?;
71 Ok(())
72 })?;
73 zip.finish()?;
74
75 Ok(report_filename)
76 }
77
get_report_filename(node_id: &MacAddr6) -> Result<String>78 fn get_report_filename(node_id: &MacAddr6) -> Result<String> {
79 let since_epoch = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)?;
80 let ts =
81 Timestamp::from_unix(&*UUID_CONTEXT, since_epoch.as_secs(), since_epoch.subsec_nanos());
82 let uuid = Uuid::new_v1(ts, &node_id.as_bytes())?;
83 Ok(uuid.to_string())
84 }
85
86 /// Get report creation timestamp through its filename (version 1 UUID).
get_report_ts(filename: &str) -> Result<SystemTime>87 pub fn get_report_ts(filename: &str) -> Result<SystemTime> {
88 let uuid_ts = Uuid::parse_str(filename)?
89 .to_timestamp()
90 .ok_or_else(|| anyhow!("filename is not a valid V1 UUID."))?
91 .to_unix();
92 Ok(SystemTime::UNIX_EPOCH + Duration::new(uuid_ts.0, uuid_ts.1))
93 }
94