xref: /aosp_15_r20/system/sepolicy/compat/libgenfslabelsversion/src/lib.rs (revision e4a36f4174b17bbab9dc043f4a65dc8d87377290)
1 // Copyright 2024 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 //! Client library to read genfs labels version of the vendor.
16 
17 use std::fs;
18 
19 const GENFS_LABELS_VERSION_TXT_PATH: &str = "/vendor/etc/selinux/genfs_labels_version.txt";
20 const DEFAULT_GENFS_LABELS_VERSION: i32 = 202404;
21 
22 /// Get genfs labels version from the vendor partition.
23 ///
24 /// This function reads the genfs labels version from the file
25 /// `/vendor/etc/selinux/genfs_labels_version.txt`. If the file does not exist or
26 /// cannot be parsed, it returns a default version of 202404.
27 ///
28 /// # Returns
29 ///
30 /// The genfs labels version as an integer.
31 #[no_mangle]
get_genfs_labels_version() -> i3232 pub extern "C" fn get_genfs_labels_version() -> i32 {
33     match fs::read_to_string(GENFS_LABELS_VERSION_TXT_PATH) {
34         Ok(contents) => match contents.trim().parse::<i32>() {
35             Ok(version) => version,
36             Err(_) => DEFAULT_GENFS_LABELS_VERSION,
37         },
38         Err(_) => DEFAULT_GENFS_LABELS_VERSION,
39     }
40 }
41