Merge "aflags: read protos from all containers" into main am: ac407794b9 am: 91c54cf72f

Original change: https://android-review.googlesource.com/c/platform/build/+/3001064

Change-Id: Ifd21198de67ebfe880a5bff7b90b9c8adadfdc10
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
Treehugger Robot 2024-04-18 18:00:10 +00:00 committed by Automerger Merge Worker
commit e1af7846fc
11 changed files with 180 additions and 70 deletions

View file

@ -2,6 +2,7 @@
members = [ members = [
"aconfig", "aconfig",
"aconfig_device_paths",
"aconfig_protos", "aconfig_protos",
"aconfig_storage_file", "aconfig_storage_file",
"aconfig_storage_read_api", "aconfig_storage_read_api",

View file

@ -0,0 +1,38 @@
// Copyright (C) 2024 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package {
default_applicable_licenses: ["Android-Apache-2.0"],
}
rust_defaults {
name: "libaconfig_device_paths.defaults",
edition: "2021",
clippy_lints: "android",
lints: "android",
srcs: ["src/lib.rs"],
rustlibs: [
"libaconfig_protos",
"libanyhow",
"libprotobuf",
"libregex",
],
}
rust_library {
name: "libaconfig_device_paths",
crate_name: "aconfig_device_paths",
host_supported: true,
defaults: ["libaconfig_device_paths.defaults"],
}

View file

@ -0,0 +1,9 @@
[package]
name = "aconfig_device_paths"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
anyhow = "1.0.82"

View file

@ -0,0 +1,6 @@
[
"/system/etc/aconfig_flags.pb",
"/system_ext/etc/aconfig_flags.pb",
"/product/etc/aconfig_flags.pb",
"/vendor/etc/aconfig_flags.pb",
]

View file

@ -0,0 +1,47 @@
/*
* Copyright (C) 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//! Library for finding all aconfig on-device protobuf file paths.
use anyhow::Result;
use std::path::PathBuf;
use std::fs;
/// Determine all paths that contain an aconfig protobuf file.
pub fn parsed_flags_proto_paths() -> Result<Vec<PathBuf>> {
let mut result: Vec<PathBuf> = include!("../partition_aconfig_flags_paths.txt")
.map(|s| PathBuf::from(s.to_string()))
.to_vec();
for dir in fs::read_dir("/apex")? {
let dir = dir?;
// Only scan the currently active version of each mainline module; skip the @version dirs.
if dir.file_name().as_encoded_bytes().iter().any(|&b| b == b'@') {
continue;
}
let mut path = PathBuf::from("/apex");
path.push(dir.path());
path.push("etc");
path.push("aconfig_flags.pb");
if path.exists() {
result.push(path);
}
}
Ok(result)
}

View file

@ -443,10 +443,12 @@ files {{
.unwrap(); .unwrap();
for i in 0..8 { for i in 0..8 {
set_flag_is_sticky(&mut file, FlagValueType::Boolean, i, true).unwrap(); set_flag_is_sticky(&mut file, FlagValueType::Boolean, i, true).unwrap();
let attribute = get_flag_attribute_at_offset(&flag_info_path, FlagValueType::Boolean, i); let attribute =
get_flag_attribute_at_offset(&flag_info_path, FlagValueType::Boolean, i);
assert!((attribute & (FlagInfoBit::IsSticky as u8)) != 0); assert!((attribute & (FlagInfoBit::IsSticky as u8)) != 0);
set_flag_is_sticky(&mut file, FlagValueType::Boolean, i, false).unwrap(); set_flag_is_sticky(&mut file, FlagValueType::Boolean, i, false).unwrap();
let attribute = get_flag_attribute_at_offset(&flag_info_path, FlagValueType::Boolean, i); let attribute =
get_flag_attribute_at_offset(&flag_info_path, FlagValueType::Boolean, i);
assert!((attribute & (FlagInfoBit::IsSticky as u8)) == 0); assert!((attribute & (FlagInfoBit::IsSticky as u8)) == 0);
} }
} }
@ -485,10 +487,12 @@ files {{
.unwrap(); .unwrap();
for i in 0..8 { for i in 0..8 {
set_flag_has_override(&mut file, FlagValueType::Boolean, i, true).unwrap(); set_flag_has_override(&mut file, FlagValueType::Boolean, i, true).unwrap();
let attribute = get_flag_attribute_at_offset(&flag_info_path, FlagValueType::Boolean, i); let attribute =
get_flag_attribute_at_offset(&flag_info_path, FlagValueType::Boolean, i);
assert!((attribute & (FlagInfoBit::HasOverride as u8)) != 0); assert!((attribute & (FlagInfoBit::HasOverride as u8)) != 0);
set_flag_has_override(&mut file, FlagValueType::Boolean, i, false).unwrap(); set_flag_has_override(&mut file, FlagValueType::Boolean, i, false).unwrap();
let attribute = get_flag_attribute_at_offset(&flag_info_path, FlagValueType::Boolean, i); let attribute =
get_flag_attribute_at_offset(&flag_info_path, FlagValueType::Boolean, i);
assert!((attribute & (FlagInfoBit::HasOverride as u8)) == 0); assert!((attribute & (FlagInfoBit::HasOverride as u8)) == 0);
} }
} }

View file

@ -9,6 +9,7 @@ rust_defaults {
lints: "android", lints: "android",
srcs: ["src/main.rs"], srcs: ["src/main.rs"],
rustlibs: [ rustlibs: [
"libaconfig_device_paths",
"libaconfig_protos", "libaconfig_protos",
"libaconfig_storage_read_api", "libaconfig_storage_read_api",
"libaconfig_storage_file", "libaconfig_storage_file",

View file

@ -13,3 +13,4 @@ nix = { version = "0.28.0", features = ["user"] }
aconfig_storage_file = { version = "0.1.0", path = "../aconfig_storage_file" } aconfig_storage_file = { version = "0.1.0", path = "../aconfig_storage_file" }
aconfig_storage_read_api = { version = "0.1.0", path = "../aconfig_storage_read_api" } aconfig_storage_read_api = { version = "0.1.0", path = "../aconfig_storage_read_api" }
clap = {version = "4.5.2" } clap = {version = "4.5.2" }
aconfig_device_paths = { version = "0.1.0", path = "../aconfig_device_paths" }

View file

@ -14,78 +14,17 @@
* limitations under the License. * limitations under the License.
*/ */
use crate::{Flag, FlagPermission, FlagSource, FlagValue, ValuePickedFrom}; use crate::load_protos;
use aconfig_protos::ProtoFlagPermission as ProtoPermission; use crate::{Flag, FlagSource, FlagValue, ValuePickedFrom};
use aconfig_protos::ProtoFlagState as ProtoState;
use aconfig_protos::ProtoParsedFlag;
use aconfig_protos::ProtoParsedFlags;
use anyhow::{anyhow, bail, Result}; use anyhow::{anyhow, bail, Result};
use regex::Regex; use regex::Regex;
use std::collections::BTreeMap;
use std::collections::HashMap; use std::collections::HashMap;
use std::process::Command; use std::process::Command;
use std::{fs, str}; use std::str;
pub struct DeviceConfigSource {} pub struct DeviceConfigSource {}
fn convert_parsed_flag(flag: &ProtoParsedFlag) -> Flag {
let namespace = flag.namespace().to_string();
let package = flag.package().to_string();
let name = flag.name().to_string();
let container = if flag.container().is_empty() {
"system".to_string()
} else {
flag.container().to_string()
};
let value = match flag.state() {
ProtoState::ENABLED => FlagValue::Enabled,
ProtoState::DISABLED => FlagValue::Disabled,
};
let permission = match flag.permission() {
ProtoPermission::READ_ONLY => FlagPermission::ReadOnly,
ProtoPermission::READ_WRITE => FlagPermission::ReadWrite,
};
Flag {
namespace,
package,
name,
container,
value,
staged_value: None,
permission,
value_picked_from: ValuePickedFrom::Default,
}
}
fn read_pb_files() -> Result<Vec<Flag>> {
let mut flags: BTreeMap<String, Flag> = BTreeMap::new();
for partition in ["system", "system_ext", "product", "vendor"] {
let path = format!("/{partition}/etc/aconfig_flags.pb");
let Ok(bytes) = fs::read(&path) else {
eprintln!("warning: failed to read {}", path);
continue;
};
let parsed_flags: ProtoParsedFlags = protobuf::Message::parse_from_bytes(&bytes)?;
for flag in parsed_flags.parsed_flag {
let key = format!("{}.{}", flag.package(), flag.name());
let container = if flag.container().is_empty() {
"system".to_string()
} else {
flag.container().to_string()
};
if container.eq(partition) {
flags.insert(key, convert_parsed_flag(&flag));
}
}
}
Ok(flags.values().cloned().collect())
}
fn parse_device_config(raw: &str) -> Result<HashMap<String, FlagValue>> { fn parse_device_config(raw: &str) -> Result<HashMap<String, FlagValue>> {
let mut flags = HashMap::new(); let mut flags = HashMap::new();
let regex = Regex::new(r"(?m)^([[[:alnum:]]_]+/[[[:alnum:]]_\.]+)=(true|false)$")?; let regex = Regex::new(r"(?m)^([[[:alnum:]]_]+/[[[:alnum:]]_\.]+)=(true|false)$")?;
@ -180,7 +119,7 @@ fn reconcile(
impl FlagSource for DeviceConfigSource { impl FlagSource for DeviceConfigSource {
fn list_flags() -> Result<Vec<Flag>> { fn list_flags() -> Result<Vec<Flag>> {
let pb_flags = read_pb_files()?; let pb_flags = load_protos::load()?;
let dc_flags = read_device_config_flags()?; let dc_flags = read_device_config_flags()?;
let staged_flags = read_staged_flags()?; let staged_flags = read_staged_flags()?;

View file

@ -0,0 +1,62 @@
use crate::{Flag, FlagPermission, FlagValue, ValuePickedFrom};
use aconfig_protos::ProtoFlagPermission as ProtoPermission;
use aconfig_protos::ProtoFlagState as ProtoState;
use aconfig_protos::ProtoParsedFlag;
use aconfig_protos::ProtoParsedFlags;
use anyhow::Result;
use std::fs;
use std::path::Path;
// TODO(b/329875578): use container field directly instead of inferring.
fn infer_container(path: &Path) -> String {
let path_str = path.to_string_lossy();
path_str
.strip_prefix("/apex/")
.or_else(|| path_str.strip_prefix('/'))
.unwrap_or(&path_str)
.strip_suffix("/etc/aconfig_flags.pb")
.unwrap_or(&path_str)
.to_string()
}
fn convert_parsed_flag(path: &Path, flag: &ProtoParsedFlag) -> Flag {
let namespace = flag.namespace().to_string();
let package = flag.package().to_string();
let name = flag.name().to_string();
let value = match flag.state() {
ProtoState::ENABLED => FlagValue::Enabled,
ProtoState::DISABLED => FlagValue::Disabled,
};
let permission = match flag.permission() {
ProtoPermission::READ_ONLY => FlagPermission::ReadOnly,
ProtoPermission::READ_WRITE => FlagPermission::ReadWrite,
};
Flag {
namespace,
package,
name,
container: infer_container(path),
value,
staged_value: None,
permission,
value_picked_from: ValuePickedFrom::Default,
}
}
pub(crate) fn load() -> Result<Vec<Flag>> {
let mut result = Vec::new();
let paths = aconfig_device_paths::parsed_flags_proto_paths()?;
for path in paths {
let bytes = fs::read(path.clone())?;
let parsed_flags: ProtoParsedFlags = protobuf::Message::parse_from_bytes(&bytes)?;
for flag in parsed_flags.parsed_flag {
// TODO(b/334954748): enforce one-container-per-flag invariant.
result.push(convert_parsed_flag(&path, &flag));
}
}
Ok(result)
}

View file

@ -25,6 +25,8 @@ use device_config_source::DeviceConfigSource;
mod aconfig_storage_source; mod aconfig_storage_source;
use aconfig_storage_source::AconfigStorageSource; use aconfig_storage_source::AconfigStorageSource;
mod load_protos;
#[derive(Clone, PartialEq, Debug)] #[derive(Clone, PartialEq, Debug)]
enum FlagPermission { enum FlagPermission {
ReadOnly, ReadOnly,