Vendor things
This commit is contained in:
parent
5deceec006
commit
977e3c17e5
19434 changed files with 10682014 additions and 0 deletions
111
third-party/vendor/android-properties/src/android/mod.rs
vendored
Normal file
111
third-party/vendor/android-properties/src/android/mod.rs
vendored
Normal file
|
|
@ -0,0 +1,111 @@
|
|||
use crate::AndroidProperty;
|
||||
|
||||
use std::{
|
||||
ffi::{CStr, CString},
|
||||
os::raw::{c_char, c_int, c_void},
|
||||
};
|
||||
|
||||
type Callback = unsafe fn(*mut ValuePair, *const c_char, *const c_char, u32);
|
||||
type ForEachCallback = unsafe fn(*const c_void, *mut Vec<AndroidProperty>);
|
||||
|
||||
struct ValuePair {
|
||||
name: String,
|
||||
value: String,
|
||||
}
|
||||
|
||||
unsafe fn property_callback(cookie: *mut ValuePair, name: *const c_char, value: *const c_char, _serial: u32) {
|
||||
let cname = CStr::from_ptr(name);
|
||||
let cvalue = CStr::from_ptr(value);
|
||||
(*cookie).name = cname.to_str().unwrap().to_string();
|
||||
(*cookie).value = cvalue.to_str().unwrap().to_string();
|
||||
}
|
||||
|
||||
unsafe fn foreach_property_callback(pi: *const c_void, cookie: *mut Vec<AndroidProperty>) {
|
||||
let mut result = Box::new(ValuePair {
|
||||
name: String::new(),
|
||||
value: String::new(),
|
||||
});
|
||||
__system_property_read_callback(pi, property_callback, &mut *result);
|
||||
(*cookie).push(AndroidProperty {
|
||||
name: (*result).name,
|
||||
property_info: pi,
|
||||
});
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
fn __system_property_set(name: *const c_char, value: *const c_char) -> c_int;
|
||||
fn __system_property_find(name: *const c_char) -> *const c_void;
|
||||
fn __system_property_read_callback(pi: *const c_void, callback: Callback, cookie: *mut ValuePair);
|
||||
fn __system_property_foreach(callback: ForEachCallback, cookie: *mut Vec<AndroidProperty>) -> c_int;
|
||||
}
|
||||
|
||||
#[cfg(feature = "bionic-deprecated")]
|
||||
extern "C" {
|
||||
/* Deprecated. Use __system_property_read_callback instead. */
|
||||
fn __system_property_get(name: *const c_char, value: *mut c_char) -> c_int;
|
||||
}
|
||||
|
||||
/// Set system property `name` to `value`, creating the system property if it doesn't already exist
|
||||
pub fn plat_setprop(name: &str, value: &str) -> Result<(), String> {
|
||||
let cname = CString::new(name).unwrap();
|
||||
let cvalue = CString::new(value).unwrap();
|
||||
let ret = unsafe { __system_property_set(cname.as_ptr(), cvalue.as_ptr()) };
|
||||
if ret >= 0 {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(format!("Failed to set Android property \"{}\" to \"{}\"", name, value))
|
||||
}
|
||||
}
|
||||
|
||||
/// Retrieve a property with name `name`. Returns None if the operation fails.
|
||||
#[cfg(not(feature = "bionic-deprecated"))]
|
||||
pub fn plat_getprop(_: &str, property_info: *const c_void) -> Option<String> {
|
||||
let mut result = Box::new(ValuePair {
|
||||
name: String::new(),
|
||||
value: String::new(),
|
||||
});
|
||||
if !property_info.is_null() {
|
||||
unsafe { __system_property_read_callback(property_info, property_callback, &mut *result) };
|
||||
}
|
||||
Some((*result).value)
|
||||
}
|
||||
|
||||
/// Retrieve a property with name `name`. Returns None if the operation fails.
|
||||
#[cfg(feature = "bionic-deprecated")]
|
||||
pub fn plat_getprop(name: &str, _: *const c_void) -> Option<String> {
|
||||
const PROPERTY_VALUE_MAX: usize = 92;
|
||||
let cname = CString::new(name).unwrap();
|
||||
let cvalue = CString::new(Vec::with_capacity(PROPERTY_VALUE_MAX)).unwrap();
|
||||
let raw = cvalue.into_raw();
|
||||
let ret = unsafe { __system_property_get(cname.as_ptr(), raw) };
|
||||
match ret {
|
||||
len if len > 0 => unsafe { Some(String::from_raw_parts(raw as *mut u8, len as usize, PROPERTY_VALUE_MAX)) },
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns an iterator to vector, which contains all properties present in a system
|
||||
pub fn plat_prop_values() -> impl Iterator<Item = AndroidProperty> {
|
||||
let mut properties: Box<Vec<AndroidProperty>> = Box::new(Vec::new());
|
||||
unsafe {
|
||||
__system_property_foreach(foreach_property_callback, &mut *properties);
|
||||
}
|
||||
properties.into_iter()
|
||||
}
|
||||
|
||||
/// Find property_info pointer using bionic syscall
|
||||
///
|
||||
/// returns nullptr if not found, otherwise valid pointer
|
||||
#[cfg(not(feature = "bionic-deprecated"))]
|
||||
pub fn plat_get_property_info(name: &str) -> *const c_void {
|
||||
let cname = CString::new(name).unwrap();
|
||||
unsafe { __system_property_find(cname.as_ptr()) }
|
||||
}
|
||||
|
||||
/// Deprecated version to find property_info pointer
|
||||
///
|
||||
/// Always returns nullptr
|
||||
#[cfg(feature = "bionic-deprecated")]
|
||||
pub fn plat_get_property_info(_name: &str) -> *const c_void {
|
||||
std::ptr::null()
|
||||
}
|
||||
92
third-party/vendor/android-properties/src/lib.rs
vendored
Normal file
92
third-party/vendor/android-properties/src/lib.rs
vendored
Normal file
|
|
@ -0,0 +1,92 @@
|
|||
//! android-properties is a rust wrapper for bionic property-related syscalls
|
||||
|
||||
#![deny(missing_docs, missing_debug_implementations, unused)]
|
||||
|
||||
use std::{fmt, os::raw::c_void};
|
||||
|
||||
#[cfg(target_os = "android")]
|
||||
use crate::android::*;
|
||||
#[cfg(not(target_os = "android"))]
|
||||
use crate::mock::*;
|
||||
|
||||
#[cfg(target_os = "android")]
|
||||
/// The implementation of property API for Android bionic-based systems
|
||||
pub mod android;
|
||||
|
||||
#[cfg(not(target_os = "android"))]
|
||||
/// The mock implementation of property API for non-Android based systems
|
||||
pub mod mock;
|
||||
|
||||
/// A struct representing android properties
|
||||
///
|
||||
/// This struct consists from a name-value pair
|
||||
#[derive(Debug)]
|
||||
pub struct AndroidProperty {
|
||||
/// Property name
|
||||
name: String,
|
||||
/// Property info pointer
|
||||
property_info: *const c_void,
|
||||
}
|
||||
|
||||
impl AndroidProperty {
|
||||
/// Initializes and returns struct representing android properties
|
||||
pub fn new(name: &str) -> Self {
|
||||
AndroidProperty {
|
||||
name: name.to_string(),
|
||||
property_info: std::ptr::null(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Return property name
|
||||
pub fn name(&self) -> String {
|
||||
self.name.clone()
|
||||
}
|
||||
|
||||
/// Return property value
|
||||
pub fn value(&mut self) -> Option<String> {
|
||||
if self.property_info.is_null() {
|
||||
self.property_info = plat_get_property_info(&self.name);
|
||||
}
|
||||
plat_getprop(&self.name, self.property_info)
|
||||
}
|
||||
|
||||
/// Set property value
|
||||
pub fn set_value(&self, value: &str) -> Result<(), String> {
|
||||
plat_setprop(&self.name, value)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for AndroidProperty {
|
||||
// Output in format [<name>]: [<value>]
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
let mut property_info = self.property_info;
|
||||
if property_info.is_null() {
|
||||
property_info = plat_get_property_info(&self.name);
|
||||
}
|
||||
write!(
|
||||
f,
|
||||
"[{}]: [{}]",
|
||||
self.name,
|
||||
plat_getprop(&self.name, property_info).unwrap_or_else(|| "".into())
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the property value if it exists
|
||||
pub fn getprop(name: &str) -> AndroidProperty {
|
||||
AndroidProperty::new(name)
|
||||
}
|
||||
|
||||
/// Sets the property value if it exists or creates new one with specified value
|
||||
pub fn setprop(name: &str, value: &str) -> Result<(), String> {
|
||||
AndroidProperty::new(name).set_value(value)
|
||||
}
|
||||
|
||||
/// Returns an iterator to vector, which contains all properties present in a system
|
||||
pub fn prop_values() -> impl Iterator<Item = AndroidProperty> {
|
||||
#[cfg(target_os = "android")]
|
||||
return crate::android::plat_prop_values();
|
||||
|
||||
#[cfg(not(target_os = "android"))]
|
||||
return crate::mock::plat_prop_values();
|
||||
}
|
||||
31
third-party/vendor/android-properties/src/mock/mod.rs
vendored
Normal file
31
third-party/vendor/android-properties/src/mock/mod.rs
vendored
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
use crate::AndroidProperty;
|
||||
use std::os::raw::c_void;
|
||||
|
||||
/// Mock implementation for getprop
|
||||
///
|
||||
/// Always returns None
|
||||
pub fn plat_getprop(_: &str, _: *const c_void) -> Option<String> {
|
||||
None
|
||||
}
|
||||
|
||||
/// Mock implementation for setprop
|
||||
///
|
||||
/// Always returns Err
|
||||
pub fn plat_setprop(_name: &str, _value: &str) -> Result<(), String> {
|
||||
Err("Failed to set android property (OS not supported)".to_string())
|
||||
}
|
||||
|
||||
/// Mock implementation for prop_values
|
||||
///
|
||||
/// Always returns iterator to empty vector
|
||||
pub fn plat_prop_values() -> impl Iterator<Item = AndroidProperty> {
|
||||
let properties: Box<Vec<AndroidProperty>> = Box::new(Vec::new());
|
||||
properties.into_iter()
|
||||
}
|
||||
|
||||
/// Mock implementation to find property_info pointer
|
||||
///
|
||||
/// Always returns nullptr
|
||||
pub fn plat_get_property_info(_name: &str) -> *const c_void {
|
||||
std::ptr::null()
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue