From 24a0b6a20ce342957bd1bcbab7c0bdef1ad3c592 Mon Sep 17 00:00:00 2001 From: Zhi Dou Date: Thu, 10 Aug 2023 21:39:59 +0000 Subject: [PATCH] aconfig: add default-permission argument for create-cache command Aconfig set flag default permission as READ_WRITE regardless of the release configuration. This change enables the caller of create-cache to set the default permission for all flags, if the flag doesn't have a value. Test: atest aconfig.test Bug: 294417368 Change-Id: I1ba19e1ba793cd2ae59923b136b9b50a92315ece --- core/soong_config.mk | 1 + tools/aconfig/src/commands.rs | 45 +++++++++++++++++++++++++++++++---- tools/aconfig/src/main.rs | 12 +++++++++- tools/aconfig/src/protos.rs | 20 ++++++++++++++++ tools/aconfig/src/test.rs | 1 + 5 files changed, 73 insertions(+), 6 deletions(-) diff --git a/core/soong_config.mk b/core/soong_config.mk index bd6cfbb9e6..37049878cd 100644 --- a/core/soong_config.mk +++ b/core/soong_config.mk @@ -320,6 +320,7 @@ $(call add_json_list, BuildVersionTags, $(BUILD_VERSION_TAGS)) $(call add_json_str, ReleaseVersion, $(_RELEASE_VERSION)) $(call add_json_list, ReleaseAconfigValueSets, $(RELEASE_ACONFIG_VALUE_SETS)) +$(call add_json_str, ReleaseAconfigFlagDefaultPermission, $(RELEASE_ACONFIG_FLAG_DEFAULT_PERMISSION)) $(call add_json_bool, KeepVndk, $(filter true,$(KEEP_VNDK))) diff --git a/tools/aconfig/src/commands.rs b/tools/aconfig/src/commands.rs index bd09e24ef6..ab5b0f2e25 100644 --- a/tools/aconfig/src/commands.rs +++ b/tools/aconfig/src/commands.rs @@ -52,10 +52,15 @@ pub struct OutputFile { pub contents: Vec, } -const DEFAULT_FLAG_STATE: ProtoFlagState = ProtoFlagState::DISABLED; -const DEFAULT_FLAG_PERMISSION: ProtoFlagPermission = ProtoFlagPermission::READ_WRITE; +pub const DEFAULT_FLAG_STATE: ProtoFlagState = ProtoFlagState::DISABLED; +pub const DEFAULT_FLAG_PERMISSION: ProtoFlagPermission = ProtoFlagPermission::READ_WRITE; -pub fn parse_flags(package: &str, declarations: Vec, values: Vec) -> Result> { +pub fn parse_flags( + package: &str, + declarations: Vec, + values: Vec, + default_permission: ProtoFlagPermission, +) -> Result> { let mut parsed_flags = ProtoParsedFlags::new(); for mut input in declarations { @@ -86,11 +91,11 @@ pub fn parse_flags(package: &str, declarations: Vec, values: Vec) parsed_flag.set_description(flag_declaration.take_description()); parsed_flag.bug.append(&mut flag_declaration.bug); parsed_flag.set_state(DEFAULT_FLAG_STATE); - parsed_flag.set_permission(DEFAULT_FLAG_PERMISSION); + parsed_flag.set_permission(default_permission); let mut tracepoint = ProtoTracepoint::new(); tracepoint.set_source(input.source.clone()); tracepoint.set_state(DEFAULT_FLAG_STATE); - tracepoint.set_permission(DEFAULT_FLAG_PERMISSION); + tracepoint.set_permission(default_permission); parsed_flag.trace.push(tracepoint); // verify ParsedFlag looks reasonable @@ -327,6 +332,36 @@ mod tests { } } + #[test] + fn test_parse_flags_setting_default() { + let first_flag = r#" + package: "com.first" + flag { + name: "first" + namespace: "first_ns" + description: "This is the description of the first flag." + bug: "123" + } + "#; + let declaration = + vec![Input { source: "momery".to_string(), reader: Box::new(first_flag.as_bytes()) }]; + let value: Vec = vec![]; + + let flags_bytes = crate::commands::parse_flags( + "com.first", + declaration, + value, + ProtoFlagPermission::READ_ONLY, + ) + .unwrap(); + let parsed_flags = + crate::protos::parsed_flags::try_from_binary_proto(&flags_bytes).unwrap(); + assert_eq!(1, parsed_flags.parsed_flag.len()); + let parsed_flag = parsed_flags.parsed_flag.first().unwrap(); + assert_eq!(ProtoFlagState::DISABLED, parsed_flag.state()); + assert_eq!(ProtoFlagPermission::READ_ONLY, parsed_flag.permission()); + } + #[test] fn test_create_device_config_defaults() { let input = parse_test_flags_as_input(); diff --git a/tools/aconfig/src/main.rs b/tools/aconfig/src/main.rs index 920b7610da..84073f7ce0 100644 --- a/tools/aconfig/src/main.rs +++ b/tools/aconfig/src/main.rs @@ -44,6 +44,14 @@ fn cli() -> Command { .arg(Arg::new("package").long("package").required(true)) .arg(Arg::new("declarations").long("declarations").action(ArgAction::Append)) .arg(Arg::new("values").long("values").action(ArgAction::Append)) + .arg( + Arg::new("default-permission") + .long("default-permission") + .value_parser(protos::flag_permission::parse_from_str) + .default_value(protos::flag_permission::to_string( + &commands::DEFAULT_FLAG_PERMISSION, + )), + ) .arg(Arg::new("cache").long("cache").required(true)), ) .subcommand( @@ -161,7 +169,9 @@ fn main() -> Result<()> { let package = get_required_arg::(sub_matches, "package")?; let declarations = open_zero_or_more_files(sub_matches, "declarations")?; let values = open_zero_or_more_files(sub_matches, "values")?; - let output = commands::parse_flags(package, declarations, values) + let default_permission = + get_required_arg::(sub_matches, "default-permission")?; + let output = commands::parse_flags(package, declarations, values, *default_permission) .context("failed to create cache")?; let path = get_required_arg::(sub_matches, "cache")?; write_output_to_file_or_stdout(path, &output)?; diff --git a/tools/aconfig/src/protos.rs b/tools/aconfig/src/protos.rs index 2ab6e05b31..c3911e52ac 100644 --- a/tools/aconfig/src/protos.rs +++ b/tools/aconfig/src/protos.rs @@ -156,6 +156,26 @@ pub mod flag_values { } } +pub mod flag_permission { + use super::*; + use anyhow::bail; + + pub fn parse_from_str(permission: &str) -> Result { + match permission.to_ascii_lowercase().as_str() { + "read_write" => Ok(ProtoFlagPermission::READ_WRITE), + "read_only" => Ok(ProtoFlagPermission::READ_ONLY), + _ => bail!("Permission needs to be read_only or read_write."), + } + } + + pub fn to_string(permission: &ProtoFlagPermission) -> &str { + match permission { + ProtoFlagPermission::READ_WRITE => "read_write", + ProtoFlagPermission::READ_ONLY => "read_only", + } + } +} + pub mod tracepoint { use super::*; use anyhow::ensure; diff --git a/tools/aconfig/src/test.rs b/tools/aconfig/src/test.rs index 14beb9328e..6c278850d4 100644 --- a/tools/aconfig/src/test.rs +++ b/tools/aconfig/src/test.rs @@ -118,6 +118,7 @@ parsed_flag { reader: Box::new(include_bytes!("../tests/second.values").as_slice()), }, ], + crate::commands::DEFAULT_FLAG_PERMISSION, ) .unwrap(); crate::protos::parsed_flags::try_from_binary_proto(&bytes).unwrap()