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

Commit f76e45bf authored by Yi-yo Chiang's avatar Yi-yo Chiang Committed by Gerrit Code Review
Browse files

Merge changes from topic "dynamic_system.data_transfer.shared_memory.size"

* changes:
  dynsystem.SparseInputStream: Implement read(buf, off, len)
  dynsystem: Enlarge default shared memory size and allow size override
parents 372b449b 9b4f3eaf
Loading
Loading
Loading
Loading
+18 −5
Original line number Diff line number Diff line
@@ -23,9 +23,11 @@ import android.os.AsyncTask;
import android.os.Build;
import android.os.MemoryFile;
import android.os.ParcelFileDescriptor;
import android.os.SystemProperties;
import android.os.image.DynamicSystemManager;
import android.service.persistentdata.PersistentDataBlockManager;
import android.util.Log;
import android.util.Range;
import android.webkit.URLUtil;

import org.json.JSONException;
@@ -48,7 +50,12 @@ class InstallationAsyncTask extends AsyncTask<String, Long, Throwable> {

    private static final String TAG = "InstallationAsyncTask";

    private static final int READ_BUFFER_SIZE = 1 << 13;
    private static final int MIN_SHARED_MEMORY_SIZE = 8 << 10; // 8KiB
    private static final int MAX_SHARED_MEMORY_SIZE = 1024 << 10; // 1MiB
    private static final int DEFAULT_SHARED_MEMORY_SIZE = 64 << 10; // 64KiB
    private static final String SHARED_MEMORY_SIZE_PROP =
            "dynamic_system.data_transfer.shared_memory.size";

    private static final long MIN_PROGRESS_TO_PUBLISH = 1 << 27;

    private static final List<String> UNSUPPORTED_PARTITIONS =
@@ -131,6 +138,7 @@ class InstallationAsyncTask extends AsyncTask<String, Long, Throwable> {
        void onResult(int resultCode, Throwable detail);
    }

    private final int mSharedMemorySize;
    private final String mUrl;
    private final String mDsuSlot;
    private final String mPublicKey;
@@ -164,6 +172,11 @@ class InstallationAsyncTask extends AsyncTask<String, Long, Throwable> {
            Context context,
            DynamicSystemManager dynSystem,
            ProgressListener listener) {
        mSharedMemorySize =
                Range.create(MIN_SHARED_MEMORY_SIZE, MAX_SHARED_MEMORY_SIZE)
                        .clamp(
                                SystemProperties.getInt(
                                        SHARED_MEMORY_SIZE_PROP, DEFAULT_SHARED_MEMORY_SIZE));
        mUrl = url;
        mDsuSlot = dsuSlot;
        mPublicKey = publicKey;
@@ -541,10 +554,10 @@ class InstallationAsyncTask extends AsyncTask<String, Long, Throwable> {

        Log.d(TAG, "Start installing: " + partitionName);

        MemoryFile memoryFile = new MemoryFile("dsu_" + partitionName, READ_BUFFER_SIZE);
        MemoryFile memoryFile = new MemoryFile("dsu_" + partitionName, mSharedMemorySize);
        ParcelFileDescriptor pfd = new ParcelFileDescriptor(memoryFile.getFileDescriptor());

        mInstallationSession.setAshmem(pfd, READ_BUFFER_SIZE);
        mInstallationSession.setAshmem(pfd, memoryFile.length());

        mPartitionName = partitionName;
        mPartitionSize = partitionSize;
@@ -553,10 +566,10 @@ class InstallationAsyncTask extends AsyncTask<String, Long, Throwable> {

        long prevInstalledSize = 0;
        long installedSize = 0;
        byte[] bytes = new byte[READ_BUFFER_SIZE];
        byte[] bytes = new byte[memoryFile.length()];
        int numBytesRead;

        while ((numBytesRead = sis.read(bytes, 0, READ_BUFFER_SIZE)) != -1) {
        while ((numBytesRead = sis.read(bytes, 0, bytes.length)) != -1) {
            if (isCancelled()) {
                return;
            }
+8 −12
Original line number Diff line number Diff line
@@ -133,36 +133,32 @@ public class SparseInputStream extends InputStream {
        return mLeft == 0;
    }

    /**
     * It overrides the InputStream.read(byte[] buf)
     */
    public int read(byte[] buf) throws IOException {
    @Override
    public int read(byte[] buf, int off, int len) throws IOException {
        if (!mIsSparse) {
            return mIn.read(buf);
            return mIn.read(buf, off, len);
        }
        if (prepareChunk()) return -1;
        int n = -1;
        switch (mCur.mChunkType) {
            case SparseChunk.RAW:
                n = mIn.read(buf, 0, (int) min(mLeft, buf.length));
                n = mIn.read(buf, off, (int) min(mLeft, len));
                mLeft -= n;
                return n;
            case SparseChunk.DONTCARE:
                n = (int) min(mLeft, buf.length);
                Arrays.fill(buf, 0, n - 1, (byte) 0);
                n = (int) min(mLeft, len);
                Arrays.fill(buf, off, off + n, (byte) 0);
                mLeft -= n;
                return n;
            case SparseChunk.FILL:
                // The FILL type is rarely used, so use a simple implmentation.
                return super.read(buf);
                return super.read(buf, off, len);
            default:
                throw new IOException("Unsupported Chunk:" + mCur.toString());
        }
    }

    /**
     * It overrides the InputStream.read()
     */
    @Override
    public int read() throws IOException {
        if (!mIsSparse) {
            return mIn.read();