summaryrefslogtreecommitdiff
path: root/profcollectd/libprofcollectd/config.rs
diff options
context:
space:
mode:
authorYi Kong <yikong@google.com>2021-11-29 19:57:55 +0800
committerYi Kong <yikong@google.com>2021-11-30 08:00:03 +0000
commit970ba98ee22d24d42c5f1ee05aa8ec25fe90ea36 (patch)
tree65239b8e4ece24724d798a698406673fffb3e88a /profcollectd/libprofcollectd/config.rs
parentc0e6b3f2b147fb889f7e8af2db21924622d8b621 (diff)
downloadextras-970ba98ee22d24d42c5f1ee05aa8ec25fe90ea36.tar.gz
profcollectd: Remove local files once disabled
This saves storage space on users' devices if profcollect is no longer enabled. Test: manual Bug: 207426233 Change-Id: I04f19ba7ddbf9d9f977bb40ac4bc74a09369350f Merged-In: I04f19ba7ddbf9d9f977bb40ac4bc74a09369350f
Diffstat (limited to 'profcollectd/libprofcollectd/config.rs')
-rw-r--r--profcollectd/libprofcollectd/config.rs19
1 files changed, 19 insertions, 0 deletions
diff --git a/profcollectd/libprofcollectd/config.rs b/profcollectd/libprofcollectd/config.rs
index ea1a67c8..902b5b5e 100644
--- a/profcollectd/libprofcollectd/config.rs
+++ b/profcollectd/libprofcollectd/config.rs
@@ -22,6 +22,7 @@ use macaddr::MacAddr6;
use rand::Rng;
use serde::{Deserialize, Serialize};
use std::error::Error;
+use std::fs::{read_dir, remove_file};
use std::path::Path;
use std::str::FromStr;
use std::time::Duration;
@@ -31,6 +32,7 @@ const PROFCOLLECT_NODE_ID_PROPERTY: &str = "persist.profcollectd.node_id";
pub const REPORT_RETENTION_SECS: u64 = 14 * 24 * 60 * 60; // 14 days.
+// Static configs that cannot be changed.
lazy_static! {
pub static ref TRACE_OUTPUT_DIR: &'static Path = Path::new("/data/misc/profcollectd/trace/");
pub static ref PROFILE_OUTPUT_DIR: &'static Path = Path::new("/data/misc/profcollectd/output/");
@@ -42,6 +44,7 @@ lazy_static! {
Path::new("/data/misc/profcollectd/output/config.json");
}
+/// Dynamic configs, stored in config.json.
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize, Debug)]
pub struct Config {
/// Version of config file scheme, always equals to 1.
@@ -144,3 +147,19 @@ fn generate_random_node_id() -> MacAddr6 {
node_id[0] |= 0x1;
MacAddr6::from(node_id)
}
+
+pub fn clear_data() -> Result<()> {
+ fn remove_files(path: &Path) -> Result<()> {
+ read_dir(path)?
+ .filter_map(|e| e.ok())
+ .map(|e| e.path())
+ .filter(|e| e.is_file())
+ .try_for_each(remove_file)?;
+ Ok(())
+ }
+
+ remove_files(&TRACE_OUTPUT_DIR)?;
+ remove_files(&PROFILE_OUTPUT_DIR)?;
+ remove_files(&REPORT_OUTPUT_DIR)?;
+ Ok(())
+}