Ted Bauer | 206d44a | 2024-03-15 17:04:06 +0000 | [diff] [blame] | 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 | |
Ted Bauer | 1f9d55d | 2024-05-14 15:12:10 -0400 | [diff] [blame] | 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 | |
Ted Bauer | 206d44a | 2024-03-15 17:04:06 +0000 | [diff] [blame] | 33 | /// Determine all paths that contain an aconfig protobuf file. |
| 34 | pub fn parsed_flags_proto_paths() -> Result<Vec<PathBuf>> { |
Ted Bauer | 1f9d55d | 2024-05-14 15:12:10 -0400 | [diff] [blame] | 35 | let mut result: Vec<PathBuf> = read_partition_paths(); |
| 36 | |
Ted Bauer | 206d44a | 2024-03-15 17:04:06 +0000 | [diff] [blame] | 37 | 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 Bauer | 1f9d55d | 2024-05-14 15:12:10 -0400 | [diff] [blame] | 56 | |
| 57 | #[cfg(test)] |
| 58 | mod 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 | } |