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

Commit d4a60ea9 authored by Zhi Dou's avatar Zhi Dou Committed by Gerrit Code Review
Browse files

Merge changes from topics "634d4559-873d-43f0-ba3c-76a20380ae8f",...

Merge changes from topics "634d4559-873d-43f0-ba3c-76a20380ae8f", "fc3d5ff8-8b95-47be-92b2-a628b6ea8b83" into main

* changes:
  Throw AconfigStorageException instead of AconfigStorageReadException
  remove StorageInternalReader
parents 8382f29f 64909f38
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
@@ -154,7 +154,6 @@ java_library {
java_library {
    name: "aconfig_storage_reader_java",
    srcs: [
        "srcs/android/aconfig/storage/StorageInternalReader.java",
        "srcs/android/os/flagging/PlatformAconfigPackageInternal.java",
    ],
    libs: [
+0 −94
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.aconfig.storage;

import android.compat.annotation.UnsupportedAppUsage;
import android.os.StrictMode;

import java.io.Closeable;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

/** @hide */
public class StorageInternalReader {

    private static final String MAP_PATH = "/metadata/aconfig/maps/";
    private static final String BOOT_PATH = "/metadata/aconfig/boot/";

    private PackageTable mPackageTable;
    private FlagValueList mFlagValueList;

    private int mPackageBooleanStartOffset;

    @UnsupportedAppUsage
    public StorageInternalReader(String container, String packageName) {
        this(packageName, MAP_PATH + container + ".package.map", BOOT_PATH + container + ".val");
    }

    @UnsupportedAppUsage
    public StorageInternalReader(String packageName, String packageMapFile, String flagValueFile) {
        StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads();
        mPackageTable = PackageTable.fromBytes(mapStorageFile(packageMapFile));
        mFlagValueList = FlagValueList.fromBytes(mapStorageFile(flagValueFile));
        StrictMode.setThreadPolicy(oldPolicy);
        mPackageBooleanStartOffset = getPackageBooleanStartOffset(packageName);
    }

    @UnsupportedAppUsage
    public boolean getBooleanFlagValue(int index) {
        index += mPackageBooleanStartOffset;
        return mFlagValueList.getBoolean(index);
    }

    private int getPackageBooleanStartOffset(String packageName) {
        PackageTable.Node pNode = mPackageTable.get(packageName);
        if (pNode == null) {
            PackageTable.Header header = mPackageTable.getHeader();
            throw new AconfigStorageException(
                    String.format(
                            "Fail to get package %s from container %s",
                            packageName, header.getContainer()));
        }
        return pNode.getBooleanStartIndex();
    }

    // Map a storage file given file path
    private static MappedByteBuffer mapStorageFile(String file) {
        FileChannel channel = null;
        try {
            channel = FileChannel.open(Paths.get(file), StandardOpenOption.READ);
            return channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
        } catch (Exception e) {
            throw new AconfigStorageException(
                    String.format("Fail to mmap storage file %s", file), e);
        } finally {
            quietlyDispose(channel);
        }
    }

    private static void quietlyDispose(Closeable closable) {
        try {
            if (closable != null) {
                closable.close();
            }
        } catch (Exception e) {
            // no need to care, at least as of now
        }
    }
}
+4 −6
Original line number Diff line number Diff line
@@ -84,15 +84,13 @@ public class PlatformAconfigPackageInternal {
        try {
            pNode = fileProvider.getPackageTable(container).get(packageName);
            vList = fileProvider.getFlagValueList(container);
        } catch (AconfigStorageException e) {
            throw new AconfigStorageReadException(e.getErrorCode(), e.toString());
        } finally {
            StrictMode.setThreadPolicy(oldPolicy);
        }

        if (pNode == null || vList == null) {
            throw new AconfigStorageReadException(
                    AconfigStorageReadException.ERROR_PACKAGE_NOT_FOUND,
            throw new AconfigStorageException(
                    AconfigStorageException.ERROR_PACKAGE_NOT_FOUND,
                    String.format(
                            "package "
                                    + packageName
@@ -102,8 +100,8 @@ public class PlatformAconfigPackageInternal {
        }

        if (pNode.hasPackageFingerprint() && packageFingerprint != pNode.getPackageFingerprint()) {
            throw new AconfigStorageReadException(
                    5, // AconfigStorageReadException.ERROR_FILE_FINGERPRINT_MISMATCH,
            throw new AconfigStorageException(
                    AconfigStorageException.ERROR_FILE_FINGERPRINT_MISMATCH,
                    String.format(
                            "package "
                                    + packageName
+1 −44
Original line number Diff line number Diff line
@@ -26,7 +26,6 @@ import android.aconfig.storage.FlagReadContext;
import android.aconfig.storage.FlagReadContext.StoredFlagType;
import android.aconfig.storage.PackageReadContext;
import android.aconfig.storage.SipHasher13;
import android.aconfig.storage.StorageInternalReader;

import org.junit.Test;
import org.junit.runner.RunWith;
@@ -225,46 +224,4 @@ public class AconfigStorageReadAPITest {
            assertEquals(rHash, jHash);
        }
    }

    @Test
    public void testRustJavaEqualFlag() throws IOException {
        List<parsed_flag> flags = DeviceProtos.loadAndParseFlagProtos();

        String mapPath = "/metadata/aconfig/maps/";
        String flagsPath = "/metadata/aconfig/boot/";

        for (parsed_flag flag : flags) {

            String container = flag.container;
            String packageName = flag.package_;
            String flagName = flag.name;
            String fullFlagName = packageName + "/" + flagName;

            MappedByteBuffer packageMap =
                    AconfigStorageReadAPI.mapStorageFile(mapPath + container + ".package.map");
            MappedByteBuffer flagMap =
                    AconfigStorageReadAPI.mapStorageFile(mapPath + container + ".flag.map");
            MappedByteBuffer flagValList =
                    AconfigStorageReadAPI.mapStorageFile(flagsPath + container + ".val");

            PackageReadContext packageContext =
                    AconfigStorageReadAPI.getPackageReadContext(packageMap, packageName);

            FlagReadContext flagContext =
                    AconfigStorageReadAPI.getFlagReadContext(
                            flagMap, packageContext.mPackageId, flagName);

            boolean rVal =
                    AconfigStorageReadAPI.getBooleanFlagValue(
                            flagValList,
                            packageContext.mBooleanStartIndex + flagContext.mFlagIndex);

            StorageInternalReader reader = new StorageInternalReader(container, packageName);
            boolean jVal = reader.getBooleanFlagValue(flagContext.mFlagIndex);

            long rHash = AconfigStorageReadAPI.hash(packageName);
            long jHash = SipHasher13.hash(packageName.getBytes());
            assertEquals(rVal, jVal);
        }
    }
}
+8 −10
Original line number Diff line number Diff line
@@ -26,7 +26,7 @@ import android.aconfig.storage.FlagTable;
import android.aconfig.storage.FlagValueList;
import android.aconfig.storage.PackageTable;
import android.aconfig.storage.StorageFileProvider;
import android.os.flagging.AconfigStorageReadException;
import android.internal.aconfig.storage.AconfigStorageException;
import android.os.flagging.PlatformAconfigPackageInternal;

import org.junit.Test;
@@ -84,20 +84,20 @@ public class PlatformAconfigPackageInternalTest {
    @Test
    public void testAconfigPackage_load_withError() throws IOException {
        // container not found fake_container
        AconfigStorageReadException e =
        AconfigStorageException e =
                assertThrows(
                        AconfigStorageReadException.class,
                        AconfigStorageException.class,
                        () ->
                                PlatformAconfigPackageInternal.load(
                                        "fake_container", "fake_package", 0));
        assertEquals(AconfigStorageReadException.ERROR_CANNOT_READ_STORAGE_FILE, e.getErrorCode());
        assertEquals(AconfigStorageException.ERROR_CANNOT_READ_STORAGE_FILE, e.getErrorCode());

        // package not found
        e =
                assertThrows(
                        AconfigStorageReadException.class,
                        AconfigStorageException.class,
                        () -> PlatformAconfigPackageInternal.load("system", "fake_container", 0));
        assertEquals(AconfigStorageReadException.ERROR_PACKAGE_NOT_FOUND, e.getErrorCode());
        assertEquals(AconfigStorageException.ERROR_PACKAGE_NOT_FOUND, e.getErrorCode());

        // fingerprint doesn't match
        List<parsed_flag> flags = DeviceProtos.loadAndParseFlagProtos();
@@ -116,13 +116,11 @@ public class PlatformAconfigPackageInternalTest {
            long fingerprint = pNode.getPackageFingerprint();
            e =
                    assertThrows(
                            AconfigStorageReadException.class,
                            AconfigStorageException.class,
                            () ->
                                    PlatformAconfigPackageInternal.load(
                                            container, packageName, fingerprint + 1));
            assertEquals(
                    // AconfigStorageException.ERROR_FILE_FINGERPRINT_MISMATCH,
                    5, e.getErrorCode());
            assertEquals(AconfigStorageException.ERROR_FILE_FINGERPRINT_MISMATCH, e.getErrorCode());
        }
    }
}
Loading