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 macaddr::MacAddr6;
21 use std::fs::{self, File, Permissions};
22 use std::io::{Read, Write};
23 use std::os::unix::fs::PermissionsExt;
24 use std::path::{Path, PathBuf};
25 use std::time::{Duration, SystemTime};
26 use uuid::v1::{Context, Timestamp};
27 use uuid::Uuid;
28 use zip::write::FileOptions;
29 use zip::CompressionMethod::Deflated;
30 use zip::ZipWriter;
31
32 use crate::config::{clear_processed_files, Config};
33
34 pub const NO_USAGE_SETTING: i32 = -1;
35
36 pub static UUID_CONTEXT: Context = Context::new(0);
37
pack_report( profile: &Path, report: &Path, config: &Config, usage_setting: i32, ) -> Result<String>38 pub fn pack_report(
39 profile: &Path,
40 report: &Path,
41 config: &Config,
42 usage_setting: i32,
43 ) -> Result<String> {
44 let mut report = PathBuf::from(report);
45 let report_filename = get_report_filename(&config.node_id)?;
46 report.push(&report_filename);
47 report.set_extension("zip");
48
49 // Remove the current report file if exists.
50 fs::remove_file(&report).ok();
51
52 let report_file = fs::OpenOptions::new().create_new(true).write(true).open(&report)?;
53
54 // Set report file ACL bits to 644, so that this can be shared to uploaders.
55 // Who has permission to actually read the file is protected by SELinux policy.
56 fs::set_permissions(&report, Permissions::from_mode(0o644))?;
57
58 let options = FileOptions::default().compression_method(Deflated);
59 let mut zip = ZipWriter::new(report_file);
60
61 fs::read_dir(profile)?
62 .filter_map(|e| e.ok())
63 .map(|e| e.path())
64 .filter(|e| e.is_file())
65 .try_for_each(|e| -> Result<()> {
66 let filename = e
67 .file_name()
68 .and_then(|f| f.to_str())
69 .ok_or_else(|| anyhow!("Malformed profile path: {}", e.display()))?;
70 zip.start_file(filename, options)?;
71 let mut f = File::open(e)?;
72 let mut buffer = Vec::new();
73 f.read_to_end(&mut buffer)?;
74 zip.write_all(&buffer)?;
75 Ok(())
76 })?;
77
78 if usage_setting != NO_USAGE_SETTING {
79 zip.start_file("usage_setting", options)?;
80 zip.write_all(usage_setting.to_string().as_bytes())?;
81 }
82 zip.finish()?;
83 clear_processed_files()?;
84
85 Ok(report_filename)
86 }
87
get_report_filename(node_id: &MacAddr6) -> Result<String>88 fn get_report_filename(node_id: &MacAddr6) -> Result<String> {
89 let since_epoch = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)?;
90 let ts = Timestamp::from_unix(&UUID_CONTEXT, since_epoch.as_secs(), since_epoch.subsec_nanos());
91 let uuid = Uuid::new_v1(
92 ts,
93 node_id.as_bytes().try_into().expect("Invalid number of bytes in V1 UUID"),
94 );
95 Ok(uuid.to_string())
96 }
97
98 /// Get report creation timestamp through its filename (version 1 UUID).
get_report_ts(filename: &str) -> Result<SystemTime>99 pub fn get_report_ts(filename: &str) -> Result<SystemTime> {
100 let uuid_ts = Uuid::parse_str(filename)?
101 .get_timestamp()
102 .ok_or_else(|| anyhow!("filename is not a valid V1 UUID."))?
103 .to_unix();
104 Ok(SystemTime::UNIX_EPOCH + Duration::new(uuid_ts.0, uuid_ts.1))
105 }
106