blob: 1c453c52c351d3274a0b9c6ce6197500f22f50aa [file] [log] [blame]
Ted Bauer4dbf58a2024-02-08 18:46:52 +00001/*
2 * Copyright (C) 2024 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//! `aflags` is a device binary to read and write aconfig flags.
18
Ted Bauera98448f2024-03-06 14:01:19 -050019use anyhow::{anyhow, ensure, Result};
Ted Bauer4dbf58a2024-02-08 18:46:52 +000020use clap::Parser;
21
22mod device_config_source;
23use device_config_source::DeviceConfigSource;
24
Ted Bauer6d4db662024-03-06 18:08:32 -050025mod aconfig_storage_source;
26use aconfig_storage_source::AconfigStorageSource;
27
Ted Bauer46d758b2024-03-12 19:28:58 +000028#[derive(Clone, PartialEq, Debug)]
Ted Bauer4dbf58a2024-02-08 18:46:52 +000029enum FlagPermission {
30 ReadOnly,
31 ReadWrite,
32}
33
34impl ToString for FlagPermission {
35 fn to_string(&self) -> String {
36 match &self {
37 Self::ReadOnly => "read-only".into(),
38 Self::ReadWrite => "read-write".into(),
39 }
40 }
41}
42
Ted Bauer46d758b2024-03-12 19:28:58 +000043#[derive(Clone, Debug)]
Ted Bauer4dbf58a2024-02-08 18:46:52 +000044enum ValuePickedFrom {
45 Default,
46 Server,
47}
48
49impl ToString for ValuePickedFrom {
50 fn to_string(&self) -> String {
51 match &self {
52 Self::Default => "default".into(),
53 Self::Server => "server".into(),
54 }
55 }
56}
57
Mårten Kongstadf0c594d2024-03-08 10:20:32 +010058#[derive(Clone, Copy, PartialEq, Eq, Debug)]
59enum FlagValue {
60 Enabled,
61 Disabled,
62}
63
64impl TryFrom<&str> for FlagValue {
65 type Error = anyhow::Error;
66
67 fn try_from(value: &str) -> std::result::Result<Self, Self::Error> {
68 match value {
69 "true" | "enabled" => Ok(Self::Enabled),
70 "false" | "disabled" => Ok(Self::Disabled),
71 _ => Err(anyhow!("cannot convert string '{}' to FlagValue", value)),
72 }
73 }
74}
75
76impl ToString for FlagValue {
77 fn to_string(&self) -> String {
78 match &self {
79 Self::Enabled => "enabled".into(),
80 Self::Disabled => "disabled".into(),
81 }
82 }
83}
84
Ted Bauer46d758b2024-03-12 19:28:58 +000085#[derive(Clone, Debug)]
Ted Bauer4dbf58a2024-02-08 18:46:52 +000086struct Flag {
87 namespace: String,
88 name: String,
89 package: String,
90 container: String,
Mårten Kongstadf0c594d2024-03-08 10:20:32 +010091 value: FlagValue,
Ted Bauer46d758b2024-03-12 19:28:58 +000092 staged_value: Option<FlagValue>,
Ted Bauer4dbf58a2024-02-08 18:46:52 +000093 permission: FlagPermission,
94 value_picked_from: ValuePickedFrom,
95}
96
Ted Bauer84883bd2024-03-04 22:45:29 +000097impl Flag {
98 fn qualified_name(&self) -> String {
99 format!("{}.{}", self.package, self.name)
100 }
Ted Bauer46d758b2024-03-12 19:28:58 +0000101
102 fn display_staged_value(&self) -> String {
103 match self.staged_value {
104 Some(v) => format!("(->{})", v.to_string()),
105 None => "-".to_string(),
106 }
107 }
Ted Bauer84883bd2024-03-04 22:45:29 +0000108}
109
Ted Bauer4dbf58a2024-02-08 18:46:52 +0000110trait FlagSource {
111 fn list_flags() -> Result<Vec<Flag>>;
Ted Bauer84883bd2024-03-04 22:45:29 +0000112 fn override_flag(namespace: &str, qualified_name: &str, value: &str) -> Result<()>;
Ted Bauer4dbf58a2024-02-08 18:46:52 +0000113}
114
Ted Bauer6d4db662024-03-06 18:08:32 -0500115enum FlagSourceType {
116 DeviceConfig,
117 AconfigStorage,
118}
119
Ted Bauer4dbf58a2024-02-08 18:46:52 +0000120const ABOUT_TEXT: &str = "Tool for reading and writing flags.
121
122Rows in the table from the `list` command follow this format:
123
124 package flag_name value provenance permission container
125
126 * `package`: package set for this flag in its .aconfig definition.
127 * `flag_name`: flag name, also set in definition.
128 * `value`: the value read from the flag.
Ted Bauer46d758b2024-03-12 19:28:58 +0000129 * `staged_value`: the value on next boot:
130 + `-`: same as current value
131 + `(->enabled) flipped to enabled on boot.
132 + `(->disabled) flipped to disabled on boot.
Ted Bauer4dbf58a2024-02-08 18:46:52 +0000133 * `provenance`: one of:
134 + `default`: the flag value comes from its build-time default.
135 + `server`: the flag value comes from a server override.
136 * `permission`: read-write or read-only.
137 * `container`: the container for the flag, configured in its definition.
138";
139
140#[derive(Parser, Debug)]
141#[clap(long_about=ABOUT_TEXT)]
142struct Cli {
143 #[clap(subcommand)]
144 command: Command,
145}
146
147#[derive(Parser, Debug)]
148enum Command {
149 /// List all aconfig flags on this device.
Ted Bauer6d4db662024-03-06 18:08:32 -0500150 List {
151 /// Read from the new flag storage.
152 #[clap(long)]
153 use_new_storage: bool,
154 },
Ted Bauer84883bd2024-03-04 22:45:29 +0000155
156 /// Enable an aconfig flag on this device, on the next boot.
157 Enable {
158 /// <package>.<flag_name>
159 qualified_name: String,
160 },
161
162 /// Disable an aconfig flag on this device, on the next boot.
163 Disable {
164 /// <package>.<flag_name>
165 qualified_name: String,
166 },
Ted Bauer4dbf58a2024-02-08 18:46:52 +0000167}
168
169struct PaddingInfo {
Mårten Kongstadd408e962024-03-07 13:56:30 +0100170 longest_flag_col: usize,
Ted Bauer4dbf58a2024-02-08 18:46:52 +0000171 longest_val_col: usize,
Ted Bauer46d758b2024-03-12 19:28:58 +0000172 longest_staged_val_col: usize,
Ted Bauer4dbf58a2024-02-08 18:46:52 +0000173 longest_value_picked_from_col: usize,
174 longest_permission_col: usize,
175}
176
177fn format_flag_row(flag: &Flag, info: &PaddingInfo) -> String {
Mårten Kongstadd408e962024-03-07 13:56:30 +0100178 let full_name = flag.qualified_name();
179 let p0 = info.longest_flag_col + 1;
Ted Bauer4dbf58a2024-02-08 18:46:52 +0000180
Mårten Kongstadf0c594d2024-03-08 10:20:32 +0100181 let val = flag.value.to_string();
Mårten Kongstadd408e962024-03-07 13:56:30 +0100182 let p1 = info.longest_val_col + 1;
Ted Bauer4dbf58a2024-02-08 18:46:52 +0000183
Ted Bauer46d758b2024-03-12 19:28:58 +0000184 let staged_val = flag.display_staged_value();
185 let p2 = info.longest_staged_val_col + 1;
186
Ted Bauer4dbf58a2024-02-08 18:46:52 +0000187 let value_picked_from = flag.value_picked_from.to_string();
Ted Bauer46d758b2024-03-12 19:28:58 +0000188 let p3 = info.longest_value_picked_from_col + 1;
Ted Bauer4dbf58a2024-02-08 18:46:52 +0000189
190 let perm = flag.permission.to_string();
Ted Bauer46d758b2024-03-12 19:28:58 +0000191 let p4 = info.longest_permission_col + 1;
Ted Bauer4dbf58a2024-02-08 18:46:52 +0000192
193 let container = &flag.container;
194
Ted Bauer46d758b2024-03-12 19:28:58 +0000195 format!(
196 "{full_name:p0$}{val:p1$}{staged_val:p2$}{value_picked_from:p3$}{perm:p4$}{container}\n"
197 )
Ted Bauer4dbf58a2024-02-08 18:46:52 +0000198}
199
Ted Bauer84883bd2024-03-04 22:45:29 +0000200fn set_flag(qualified_name: &str, value: &str) -> Result<()> {
Ted Bauera98448f2024-03-06 14:01:19 -0500201 ensure!(nix::unistd::Uid::current().is_root(), "must be root to mutate flags");
202
Ted Bauer84883bd2024-03-04 22:45:29 +0000203 let flags_binding = DeviceConfigSource::list_flags()?;
204 let flag = flags_binding.iter().find(|f| f.qualified_name() == qualified_name).ok_or(
205 anyhow!("no aconfig flag '{qualified_name}'. Does the flag have an .aconfig definition?"),
206 )?;
207
Ted Bauera98448f2024-03-06 14:01:19 -0500208 ensure!(flag.permission == FlagPermission::ReadWrite,
209 format!("could not write flag '{qualified_name}', it is read-only for the current release configuration."));
Ted Bauer84883bd2024-03-04 22:45:29 +0000210
211 DeviceConfigSource::override_flag(&flag.namespace, qualified_name, value)?;
212
213 Ok(())
214}
215
Ted Bauer6d4db662024-03-06 18:08:32 -0500216fn list(source_type: FlagSourceType) -> Result<String> {
217 let flags = match source_type {
218 FlagSourceType::DeviceConfig => DeviceConfigSource::list_flags()?,
219 FlagSourceType::AconfigStorage => AconfigStorageSource::list_flags()?,
220 };
Ted Bauer4dbf58a2024-02-08 18:46:52 +0000221 let padding_info = PaddingInfo {
Mårten Kongstadd408e962024-03-07 13:56:30 +0100222 longest_flag_col: flags.iter().map(|f| f.qualified_name().len()).max().unwrap_or(0),
Mårten Kongstadf0c594d2024-03-08 10:20:32 +0100223 longest_val_col: flags.iter().map(|f| f.value.to_string().len()).max().unwrap_or(0),
Ted Bauer46d758b2024-03-12 19:28:58 +0000224 longest_staged_val_col: flags
225 .iter()
226 .map(|f| f.display_staged_value().len())
227 .max()
228 .unwrap_or(0),
Ted Bauer4dbf58a2024-02-08 18:46:52 +0000229 longest_value_picked_from_col: flags
230 .iter()
231 .map(|f| f.value_picked_from.to_string().len())
232 .max()
233 .unwrap_or(0),
234 longest_permission_col: flags
235 .iter()
236 .map(|f| f.permission.to_string().len())
237 .max()
238 .unwrap_or(0),
239 };
240
241 let mut result = String::from("");
242 for flag in flags {
243 let row = format_flag_row(&flag, &padding_info);
244 result.push_str(&row);
245 }
246 Ok(result)
247}
248
249fn main() {
250 let cli = Cli::parse();
251 let output = match cli.command {
Ted Bauer6d4db662024-03-06 18:08:32 -0500252 Command::List { use_new_storage: true } => list(FlagSourceType::AconfigStorage).map(Some),
253 Command::List { use_new_storage: false } => list(FlagSourceType::DeviceConfig).map(Some),
Ted Bauer84883bd2024-03-04 22:45:29 +0000254 Command::Enable { qualified_name } => set_flag(&qualified_name, "true").map(|_| None),
255 Command::Disable { qualified_name } => set_flag(&qualified_name, "false").map(|_| None),
Ted Bauer4dbf58a2024-02-08 18:46:52 +0000256 };
257 match output {
Ted Bauer84883bd2024-03-04 22:45:29 +0000258 Ok(Some(text)) => println!("{text}"),
259 Ok(None) => (),
260 Err(message) => println!("Error: {message}"),
Ted Bauer4dbf58a2024-02-08 18:46:52 +0000261 }
262}