Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d234f433 authored by Marybeth Fair's avatar Marybeth Fair Committed by Android (Google) Code Review
Browse files

Merge "aconfig: add redact param to PackageTableNode" into main

parents b3d989c0 dd6506b9
Loading
Loading
Loading
Loading
+3 −4
Original line number Original line Diff line number Diff line
@@ -119,10 +119,9 @@ define generate-partition-aconfig-storage-file
$(eval $(strip $(1)): PRIVATE_OUT := $(strip $(1)))
$(eval $(strip $(1)): PRIVATE_OUT := $(strip $(1)))
$(eval $(strip $(1)): PRIVATE_IN := $(strip $(9)))
$(eval $(strip $(1)): PRIVATE_IN := $(strip $(9)))


ifneq (,$(RELEASE_FINGERPRINT_ACONFIG_PACKAGES))
STORAGE_FILE_VERSION := $(RELEASE_ACONFIG_STORAGE_VERSION)
STORAGE_FILE_VERSION := 2
ifeq (,$(STORAGE_FILE_VERSION))
else
STORAGE_FILE_VERSION := "2"
STORAGE_FILE_VERSION := 1
endif
endif


$(strip $(1)): $(ACONFIG) $(strip $(9))
$(strip $(1)): $(ACONFIG) $(strip $(9))
+7 −2
Original line number Original line Diff line number Diff line
@@ -35,6 +35,7 @@ pub struct FlagPackage<'a> {
    pub package_name: &'a str,
    pub package_name: &'a str,
    pub package_id: u32,
    pub package_id: u32,
    pub fingerprint: u64,
    pub fingerprint: u64,
    pub redact_exported_reads: bool,
    pub flag_names: HashSet<&'a str>,
    pub flag_names: HashSet<&'a str>,
    pub boolean_flags: Vec<&'a ProtoParsedFlag>,
    pub boolean_flags: Vec<&'a ProtoParsedFlag>,
    // The index of the first boolean flag in this aconfig package among all boolean
    // The index of the first boolean flag in this aconfig package among all boolean
@@ -48,6 +49,7 @@ impl<'a> FlagPackage<'a> {
            package_name,
            package_name,
            package_id,
            package_id,
            fingerprint: 0,
            fingerprint: 0,
            redact_exported_reads: false,
            flag_names: HashSet::new(),
            flag_names: HashSet::new(),
            boolean_flags: vec![],
            boolean_flags: vec![],
            boolean_start_index: 0,
            boolean_start_index: 0,
@@ -95,6 +97,8 @@ where
            let fingerprint = compute_flags_fingerprint(&mut flag_names_vec);
            let fingerprint = compute_flags_fingerprint(&mut flag_names_vec);
            p.fingerprint = fingerprint;
            p.fingerprint = fingerprint;
        }
        }

        // TODO - b/377311211: Set redact_exported_reads if the build flag is enabled.
    }
    }


    packages
    packages
@@ -133,7 +137,6 @@ where


#[cfg(test)]
#[cfg(test)]
mod tests {
mod tests {
    use aconfig_storage_file::DEFAULT_FILE_VERSION;


    use super::*;
    use super::*;
    use crate::commands::Input;
    use crate::commands::Input;
@@ -185,10 +188,11 @@ mod tests {
            .collect()
            .collect()
    }
    }


    // Storage file v1.
    #[test]
    #[test]
    fn test_flag_package() {
    fn test_flag_package() {
        let caches = parse_all_test_flags();
        let caches = parse_all_test_flags();
        let packages = group_flags_by_package(caches.iter(), DEFAULT_FILE_VERSION);
        let packages = group_flags_by_package(caches.iter(), 1);


        for pkg in packages.iter() {
        for pkg in packages.iter() {
            let pkg_name = pkg.package_name;
            let pkg_name = pkg.package_name;
@@ -228,6 +232,7 @@ mod tests {
        assert_eq!(packages[2].fingerprint, 0);
        assert_eq!(packages[2].fingerprint, 0);
    }
    }


    // Storage file v2.
    #[test]
    #[test]
    fn test_flag_package_with_fingerprint() {
    fn test_flag_package_with_fingerprint() {
        let caches = parse_all_test_flags();
        let caches = parse_all_test_flags();
+1 −0
Original line number Original line Diff line number Diff line
@@ -48,6 +48,7 @@ impl PackageTableNodeWrapper {
            package_name: String::from(package.package_name),
            package_name: String::from(package.package_name),
            package_id: package.package_id,
            package_id: package.package_id,
            fingerprint: package.fingerprint,
            fingerprint: package.fingerprint,
            redact_exported_reads: package.redact_exported_reads,
            boolean_start_index: package.boolean_start_index,
            boolean_start_index: package.boolean_start_index,
            next_offset: None,
            next_offset: None,
        };
        };
+2 −2
Original line number Original line Diff line number Diff line
@@ -60,11 +60,11 @@ use crate::AconfigStorageError::{


/// The max storage file version from which we can safely read/write. May be
/// The max storage file version from which we can safely read/write. May be
/// experimental.
/// experimental.
pub const MAX_SUPPORTED_FILE_VERSION: u32 = 2;
pub const MAX_SUPPORTED_FILE_VERSION: u32 = 3;


/// The newest fully-released version. Unless otherwise specified, this is the
/// The newest fully-released version. Unless otherwise specified, this is the
/// version we will write.
/// version we will write.
pub const DEFAULT_FILE_VERSION: u32 = 1;
pub const DEFAULT_FILE_VERSION: u32 = 2;


/// Good hash table prime number
/// Good hash table prime number
pub(crate) const HASH_PRIMES: [u32; 29] = [
pub(crate) const HASH_PRIMES: [u32; 29] = [
+58 −3
Original line number Original line Diff line number Diff line
@@ -101,6 +101,7 @@ pub struct PackageTableNode {
    pub package_name: String,
    pub package_name: String,
    pub package_id: u32,
    pub package_id: u32,
    pub fingerprint: u64,
    pub fingerprint: u64,
    pub redact_exported_reads: bool,
    // The index of the first boolean flag in this aconfig package among all boolean
    // The index of the first boolean flag in this aconfig package among all boolean
    // flags in this container.
    // flags in this container.
    pub boolean_start_index: u32,
    pub boolean_start_index: u32,
@@ -112,10 +113,11 @@ impl fmt::Debug for PackageTableNode {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        writeln!(
        writeln!(
            f,
            f,
            "Package: {}, Id: {}, Fingerprint: {}, Boolean flag start index: {}, Next: {:?}",
            "Package: {}, Id: {}, Fingerprint: {}, Redact Exported Reads: {}, Boolean flag start index: {}, Next: {:?}",
            self.package_name,
            self.package_name,
            self.package_id,
            self.package_id,
            self.fingerprint,
            self.fingerprint,
            self.redact_exported_reads,
            self.boolean_start_index,
            self.boolean_start_index,
            self.next_offset
            self.next_offset
        )?;
        )?;
@@ -129,6 +131,7 @@ impl PackageTableNode {
        match version {
        match version {
            1 => Self::into_bytes_v1(self),
            1 => Self::into_bytes_v1(self),
            2 => Self::into_bytes_v2(self),
            2 => Self::into_bytes_v2(self),
            3 => Self::into_bytes_v3(self),
            // TODO(b/316357686): into_bytes should return a Result.
            // TODO(b/316357686): into_bytes should return a Result.
            _ => Self::into_bytes_v2(&self),
            _ => Self::into_bytes_v2(&self),
        }
        }
@@ -157,11 +160,25 @@ impl PackageTableNode {
        result
        result
    }
    }


    fn into_bytes_v3(&self) -> Vec<u8> {
        let mut result = Vec::new();
        let name_bytes = self.package_name.as_bytes();
        result.extend_from_slice(&(name_bytes.len() as u32).to_le_bytes());
        result.extend_from_slice(name_bytes);
        result.extend_from_slice(&self.package_id.to_le_bytes());
        result.extend_from_slice(&self.fingerprint.to_le_bytes());
        result.extend_from_slice(&u8::from(self.redact_exported_reads).to_le_bytes());
        result.extend_from_slice(&self.boolean_start_index.to_le_bytes());
        result.extend_from_slice(&self.next_offset.unwrap_or(0).to_le_bytes());
        result
    }

    /// Deserialize from bytes based on file version.
    /// Deserialize from bytes based on file version.
    pub fn from_bytes(bytes: &[u8], version: u32) -> Result<Self, AconfigStorageError> {
    pub fn from_bytes(bytes: &[u8], version: u32) -> Result<Self, AconfigStorageError> {
        match version {
        match version {
            1 => Self::from_bytes_v1(bytes),
            1 => Self::from_bytes_v1(bytes),
            2 => Self::from_bytes_v2(bytes),
            2 => Self::from_bytes_v2(bytes),
            3 => Self::from_bytes_v3(bytes),
            _ => {
            _ => {
                return Err(AconfigStorageError::BytesParseFail(anyhow!(
                return Err(AconfigStorageError::BytesParseFail(anyhow!(
                    "Binary file is an unsupported version: {}",
                    "Binary file is an unsupported version: {}",
@@ -183,7 +200,14 @@ impl PackageTableNode {
            val => Some(val),
            val => Some(val),
        };
        };


        let node = Self { package_name, package_id, fingerprint, boolean_start_index, next_offset };
        let node = Self {
            package_name,
            package_id,
            fingerprint,
            redact_exported_reads: false,
            boolean_start_index,
            next_offset,
        };
        Ok(node)
        Ok(node)
    }
    }


@@ -198,7 +222,38 @@ impl PackageTableNode {
            val => Some(val),
            val => Some(val),
        };
        };


        let node = Self { package_name, package_id, fingerprint, boolean_start_index, next_offset };
        let node = Self {
            package_name,
            package_id,
            fingerprint,
            redact_exported_reads: false,
            boolean_start_index,
            next_offset,
        };
        Ok(node)
    }

    fn from_bytes_v3(bytes: &[u8]) -> Result<Self, AconfigStorageError> {
        let mut head = 0;
        let package_name = read_str_from_bytes(bytes, &mut head)?;
        let package_id = read_u32_from_bytes(bytes, &mut head)?;
        let fingerprint = read_u64_from_bytes(bytes, &mut head)?;
        let redact_exported_reads_bytes = read_u8_from_bytes(bytes, &mut head)?;
        let redact_exported_reads = redact_exported_reads_bytes == 1;
        let boolean_start_index = read_u32_from_bytes(bytes, &mut head)?;
        let next_offset = match read_u32_from_bytes(bytes, &mut head)? {
            0 => None,
            val => Some(val),
        };

        let node = Self {
            package_name,
            package_id,
            fingerprint,
            redact_exported_reads,
            boolean_start_index,
            next_offset,
        };
        Ok(node)
        Ok(node)
    }
    }


Loading