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

Commit 9caf94e5 authored by Todd Kennedy's avatar Todd Kennedy
Browse files

force appropriate size

during install, the user could specify an invalid size [ie. a negative
number].

also error out if we're given a path that's not a file.

Change-Id: I79e9ef82723495782146208eb5469722d1f8ed02
Test: manually ran 'adb install' with invalid arguments
parent 00a981e5
Loading
Loading
Loading
Loading
+13 −5
Original line number Original line Diff line number Diff line
@@ -84,6 +84,7 @@ import java.util.concurrent.TimeUnit;


public final class Pm {
public final class Pm {
    private static final String TAG = "Pm";
    private static final String TAG = "Pm";
    private static final String STDIN_PATH = "-";


    IPackageManager mPm;
    IPackageManager mPm;
    IPackageInstaller mInstaller;
    IPackageInstaller mInstaller;
@@ -403,7 +404,7 @@ public final class Pm {
    private int runInstall() throws RemoteException {
    private int runInstall() throws RemoteException {
        final InstallParams params = makeInstallParams();
        final InstallParams params = makeInstallParams();
        final String inPath = nextArg();
        final String inPath = nextArg();
        if (params.sessionParams.sizeBytes < 0 && inPath != null) {
        if (params.sessionParams.sizeBytes == -1 && !STDIN_PATH.equals(inPath)) {
            File file = new File(inPath);
            File file = new File(inPath);
            if (file.isFile()) {
            if (file.isFile()) {
                try {
                try {
@@ -416,6 +417,9 @@ public final class Pm {
                    System.err.println("Error: Failed to parse APK file: " + e);
                    System.err.println("Error: Failed to parse APK file: " + e);
                    return 1;
                    return 1;
                }
                }
            } else {
                System.err.println("Error: Can't open non-file: " + inPath);
                return 1;
            }
            }
        }
        }


@@ -423,7 +427,7 @@ public final class Pm {
                params.installerPackageName, params.userId);
                params.installerPackageName, params.userId);


        try {
        try {
            if (inPath == null && params.sessionParams.sizeBytes == 0) {
            if (inPath == null && params.sessionParams.sizeBytes == -1) {
                System.err.println("Error: must either specify a package size or an APK file");
                System.err.println("Error: must either specify a package size or an APK file");
                return 1;
                return 1;
            }
            }
@@ -540,7 +544,11 @@ public final class Pm {
                    }
                    }
                    break;
                    break;
                case "-S":
                case "-S":
                    sessionParams.setSize(Long.parseLong(nextOptionData()));
                    final long sizeBytes = Long.parseLong(nextOptionData());
                    if (sizeBytes <= 0) {
                        throw new IllegalArgumentException("Size must be positive");
                    }
                    sessionParams.setSize(sizeBytes);
                    break;
                    break;
                case "--abi":
                case "--abi":
                    sessionParams.abiOverride = checkAbiArgument(nextOptionData());
                    sessionParams.abiOverride = checkAbiArgument(nextOptionData());
@@ -585,7 +593,7 @@ public final class Pm {


    private int doWriteSession(int sessionId, String inPath, long sizeBytes, String splitName,
    private int doWriteSession(int sessionId, String inPath, long sizeBytes, String splitName,
            boolean logSuccess) throws RemoteException {
            boolean logSuccess) throws RemoteException {
        if ("-".equals(inPath)) {
        if (STDIN_PATH.equals(inPath)) {
            inPath = null;
            inPath = null;
        } else if (inPath != null) {
        } else if (inPath != null) {
            final File file = new File(inPath);
            final File file = new File(inPath);
+46 −15
Original line number Original line Diff line number Diff line
@@ -75,6 +75,11 @@ import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeUnit;


class PackageManagerShellCommand extends ShellCommand {
class PackageManagerShellCommand extends ShellCommand {
    /** Path for streaming APK content */
    private static final String STDIN_PATH = "-";
    /** Whether or not APK content must be streamed from stdin */
    private static final boolean FORCE_STREAM_INSTALL = true;

    final IPackageManager mInterface;
    final IPackageManager mInterface;
    final private WeakHashMap<String, Resources> mResourceCache =
    final private WeakHashMap<String, Resources> mResourceCache =
            new WeakHashMap<String, Resources>();
            new WeakHashMap<String, Resources>();
@@ -139,30 +144,45 @@ class PackageManagerShellCommand extends ShellCommand {
        return -1;
        return -1;
    }
    }


    private int runInstall() throws RemoteException {
    private void setParamsSize(InstallParams params, String inPath) {
        // If we're forced to stream the package, the params size
        // must be set via command-line argument. There's nothing
        // to do here.
        if (FORCE_STREAM_INSTALL) {
            return;
        }
        final PrintWriter pw = getOutPrintWriter();
        final PrintWriter pw = getOutPrintWriter();
        final InstallParams params = makeInstallParams();
        if (params.sessionParams.sizeBytes == -1 && !STDIN_PATH.equals(inPath)) {
        final String inPath = getNextArg();
        if (params.sessionParams.sizeBytes < 0 && inPath != null) {
            File file = new File(inPath);
            File file = new File(inPath);
            if (file.isFile()) {
            if (file.isFile()) {
                try {
                try {
                    ApkLite baseApk = PackageParser.parseApkLite(file, 0);
                    ApkLite baseApk = PackageParser.parseApkLite(file, 0);
                    PackageLite pkgLite = new PackageLite(null, baseApk, null, null, null);
                    PackageLite pkgLite = new PackageLite(null, baseApk, null, null, null);
                    params.sessionParams.setSize(
                    params.sessionParams.setSize(PackageHelper.calculateInstalledSize(
                            PackageHelper.calculateInstalledSize(pkgLite,false, params.sessionParams.abiOverride));
                            pkgLite, false, params.sessionParams.abiOverride));
                } catch (PackageParserException | IOException e) {
                } catch (PackageParserException | IOException e) {
                    pw.println("Error: Failed to parse APK file : " + e);
                    pw.println("Error: Failed to parse APK file: " + file);
                    return 1;
                    throw new IllegalArgumentException(
                            "Error: Failed to parse APK file: " + file, e);
                }
            } else {
                pw.println("Error: Can't open non-file: " + inPath);
                throw new IllegalArgumentException("Error: Can't open non-file: " + inPath);
            }
            }
        }
        }
    }
    }


    private int runInstall() throws RemoteException {
        final PrintWriter pw = getOutPrintWriter();
        final InstallParams params = makeInstallParams();
        final String inPath = getNextArg();

        setParamsSize(params, inPath);
        final int sessionId = doCreateSession(params.sessionParams,
        final int sessionId = doCreateSession(params.sessionParams,
                params.installerPackageName, params.userId);
                params.installerPackageName, params.userId);
        boolean abandonSession = true;
        boolean abandonSession = true;
        try {
        try {
            if (inPath == null && params.sessionParams.sizeBytes == 0) {
            if (inPath == null && params.sessionParams.sizeBytes == -1) {
                pw.println("Error: must either specify a package size or an APK file");
                pw.println("Error: must either specify a package size or an APK file");
                return 1;
                return 1;
            }
            }
@@ -1075,7 +1095,11 @@ class PackageManagerShellCommand extends ShellCommand {
                    }
                    }
                    break;
                    break;
                case "-S":
                case "-S":
                    sessionParams.setSize(Long.parseLong(getNextArg()));
                    final long sizeBytes = Long.parseLong(getNextArg());
                    if (sizeBytes <= 0) {
                        throw new IllegalArgumentException("Size must be positive");
                    }
                    sessionParams.setSize(sizeBytes);
                    break;
                    break;
                case "--abi":
                case "--abi":
                    sessionParams.abiOverride = checkAbiArgument(getNextArg());
                    sessionParams.abiOverride = checkAbiArgument(getNextArg());
@@ -1180,15 +1204,22 @@ class PackageManagerShellCommand extends ShellCommand {
    private int doWriteSplit(int sessionId, String inPath, long sizeBytes, String splitName,
    private int doWriteSplit(int sessionId, String inPath, long sizeBytes, String splitName,
            boolean logSuccess) throws RemoteException {
            boolean logSuccess) throws RemoteException {
        final PrintWriter pw = getOutPrintWriter();
        final PrintWriter pw = getOutPrintWriter();
        if (sizeBytes <= 0) {
        if (FORCE_STREAM_INSTALL && inPath != null && !STDIN_PATH.equals(inPath)) {
            pw.println("Error: must specify a APK size");
            return 1;
        }
        if (inPath != null && !"-".equals(inPath)) {
            pw.println("Error: APK content must be streamed");
            pw.println("Error: APK content must be streamed");
            return 1;
            return 1;
        }
        }
        if (STDIN_PATH.equals(inPath)) {
            inPath = null;
            inPath = null;
        } else if (inPath != null) {
            final File file = new File(inPath);
            if (file.isFile()) {
                sizeBytes = file.length();
            }
        }
        if (sizeBytes <= 0) {
            pw.println("Error: must specify a APK size");
            return 1;
        }


        final SessionInfo info = mInterface.getPackageInstaller().getSessionInfo(sessionId);
        final SessionInfo info = mInterface.getPackageInstaller().getSessionInfo(sessionId);