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

Commit da18f1b6 authored by zhidou's avatar zhidou Committed by Zhi Dou
Browse files

remove StorageInternalReader

Test: presubmit
Change-Id: Ic938c7643129fd9756dad29cd8e3c2396c415d3f
parent c24fdb1f
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
        }
    }
}
+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);
        }
    }
}
+0 −45
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.test;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import android.aconfig.storage.StorageInternalReader;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class StorageInternalReaderTest {

    private String mStorageDir = "/data/local/tmp/aconfig_java_api_test";

    @Test
    public void testStorageInternalReader_getFlag() {

        String packageMapFile = mStorageDir + "/maps/mockup.package.map";
        String flagValueFile = mStorageDir + "/boot/mockup.val";

        StorageInternalReader reader =
                new StorageInternalReader(
                        "com.android.aconfig.storage.test_1", packageMapFile, flagValueFile);
        assertFalse(reader.getBooleanFlagValue(0));
        assertTrue(reader.getBooleanFlagValue(1));
    }
}
 No newline at end of file