blob: 7480b3002cad5279d3f2d7fcbefa0422cf1595a3 [file] [log] [blame]
Ted Bauer206d44a2024-03-15 17:04:06 +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//! Library for finding all aconfig on-device protobuf file paths.
18
19use anyhow::Result;
20use std::path::PathBuf;
21
22use std::fs;
23
Ted Bauer1f9d55d2024-05-14 15:12:10 -040024fn read_partition_paths() -> Vec<PathBuf> {
25 include_str!("../partition_aconfig_flags_paths.txt")
26 .split(',')
27 .map(|s| s.trim().trim_matches('"'))
28 .filter(|s| !s.is_empty())
29 .map(|s| PathBuf::from(s.to_string()))
30 .collect()
31}
32
Ted Bauer206d44a2024-03-15 17:04:06 +000033/// Determine all paths that contain an aconfig protobuf file.
34pub fn parsed_flags_proto_paths() -> Result<Vec<PathBuf>> {
Ted Bauer1f9d55d2024-05-14 15:12:10 -040035 let mut result: Vec<PathBuf> = read_partition_paths();
36
Ted Bauer206d44a2024-03-15 17:04:06 +000037 for dir in fs::read_dir("/apex")? {
38 let dir = dir?;
39
40 // Only scan the currently active version of each mainline module; skip the @version dirs.
41 if dir.file_name().as_encoded_bytes().iter().any(|&b| b == b'@') {
42 continue;
43 }
44
45 let mut path = PathBuf::from("/apex");
46 path.push(dir.path());
47 path.push("etc");
48 path.push("aconfig_flags.pb");
49 if path.exists() {
50 result.push(path);
51 }
52 }
53
54 Ok(result)
55}
Ted Bauer1f9d55d2024-05-14 15:12:10 -040056
57#[cfg(test)]
58mod tests {
59 use super::*;
60
61 #[test]
62 fn test_read_partition_paths() {
63 assert_eq!(read_partition_paths().len(), 4);
64
65 assert_eq!(
66 read_partition_paths(),
67 vec![
68 PathBuf::from("/system/etc/aconfig_flags.pb"),
69 PathBuf::from("/system_ext/etc/aconfig_flags.pb"),
70 PathBuf::from("/product/etc/aconfig_flags.pb"),
71 PathBuf::from("/vendor/etc/aconfig_flags.pb")
72 ]
73 );
74 }
75}