xref: /aosp_15_r20/build/make/tools/aconfig/aconfig_device_paths/src/lib.rs (revision 9e94795a3d4ef5c1d47486f9a02bb378756cea8a)
1 /*
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 
19 use anyhow::Result;
20 use std::path::PathBuf;
21 
22 use std::fs;
23 
read_partition_paths() -> Vec<PathBuf>24 fn 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 
33 /// Determines all paths that contain an aconfig protobuf file,
34 /// filtering out nonexistent partition protobuf files.
parsed_flags_proto_paths() -> Result<Vec<PathBuf>>35 pub fn parsed_flags_proto_paths() -> Result<Vec<PathBuf>> {
36     let mut result: Vec<PathBuf> =
37         read_partition_paths().into_iter().filter(|s| s.exists()).collect();
38 
39     for dir in fs::read_dir("/apex")? {
40         let dir = dir?;
41 
42         // Only scan the currently active version of each mainline module; skip the @version dirs.
43         if dir.file_name().as_encoded_bytes().iter().any(|&b| b == b'@') {
44             continue;
45         }
46 
47         let mut path = PathBuf::from("/apex");
48         path.push(dir.path());
49         path.push("etc");
50         path.push("aconfig_flags.pb");
51         if path.exists() {
52             result.push(path);
53         }
54     }
55 
56     Ok(result)
57 }
58 
59 #[cfg(test)]
60 mod tests {
61     use super::*;
62 
63     #[test]
test_read_partition_paths()64     fn test_read_partition_paths() {
65         assert_eq!(read_partition_paths().len(), 3);
66 
67         assert_eq!(
68             read_partition_paths(),
69             vec![
70                 PathBuf::from("/system/etc/aconfig_flags.pb"),
71                 PathBuf::from("/product/etc/aconfig_flags.pb"),
72                 PathBuf::from("/vendor/etc/aconfig_flags.pb")
73             ]
74         );
75     }
76 }
77