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

Commit 3602f76d authored by Suchi Amalapurapu's avatar Suchi Amalapurapu
Browse files

Null check for scheme.

If scheme is unspecified let package parse continue. We will return
errors if parsing fails anyway.
If scheme is not null, restrict it to being a file uri.
parent 01f4385d
Loading
Loading
Loading
Loading
+60 −33
Original line number Original line Diff line number Diff line
@@ -93,11 +93,16 @@ public class DefaultContainerService extends IntentService {
         *  PackageHelper.RECOMMEND_FAILED_INVALID_APK for parse errors.
         *  PackageHelper.RECOMMEND_FAILED_INVALID_APK for parse errors.
         */
         */
        public int getRecommendedInstallLocation(final Uri fileUri) {
        public int getRecommendedInstallLocation(final Uri fileUri) {
            if (!fileUri.getScheme().equals("file")) {
            if (fileUri == null) {
                Log.i(TAG, "Invalid package uri " + fileUri);
                return PackageHelper.RECOMMEND_FAILED_INVALID_APK;
            }
            String scheme = fileUri.getScheme();
            if (scheme != null && !scheme.equals("file")) {
                Log.w(TAG, "Falling back to installing on internal storage only");
                Log.w(TAG, "Falling back to installing on internal storage only");
                return PackageHelper.RECOMMEND_INSTALL_INTERNAL;
                return PackageHelper.RECOMMEND_INSTALL_INTERNAL;
            }
            }
            final String archiveFilePath = fileUri.getPath();
            String archiveFilePath = fileUri.getPath();
            PackageParser packageParser = new PackageParser(archiveFilePath);
            PackageParser packageParser = new PackageParser(archiveFilePath);
            File sourceFile = new File(archiveFilePath);
            File sourceFile = new File(archiveFilePath);
            DisplayMetrics metrics = new DisplayMetrics();
            DisplayMetrics metrics = new DisplayMetrics();
@@ -166,41 +171,62 @@ public class DefaultContainerService extends IntentService {
        String codePath = packageURI.getPath();
        String codePath = packageURI.getPath();
        File codeFile = new File(codePath);
        File codeFile = new File(codePath);
        String newCachePath = null;
        String newCachePath = null;
        final int CREATE_FAILED = 1;
        final int COPY_FAILED = 2;
        final int FINALIZE_FAILED = 3;
        final int PASS = 4;
        int errCode = CREATE_FAILED;
        // Create new container
        // Create new container
        if ((newCachePath = PackageHelper.createSdDir(codeFile,
        if ((newCachePath = PackageHelper.createSdDir(codeFile,
                newCid, key, Process.myUid())) == null) {
                newCid, key, Process.myUid())) != null) {
            Log.e(TAG, "Failed creating container " + newCid);
            return null;
        }
            if (localLOGV) Log.i(TAG, "Created container for " + newCid
            if (localLOGV) Log.i(TAG, "Created container for " + newCid
                    + " at path : " + newCachePath);
                    + " at path : " + newCachePath);
            File resFile = new File(newCachePath, resFileName);
            File resFile = new File(newCachePath, resFileName);
            errCode = COPY_FAILED;
            // Copy file from codePath
            // Copy file from codePath
        if (!FileUtils.copyFile(new File(codePath), resFile)) {
            if (FileUtils.copyFile(new File(codePath), resFile)) {
            Log.e(TAG, "Failed to copy " + codePath + " to " + resFile);
            // Clean up created container
            PackageHelper.destroySdDir(newCid);
            return null;
        }
                if (localLOGV) Log.i(TAG, "Copied " + codePath + " to " + resFile);
                if (localLOGV) Log.i(TAG, "Copied " + codePath + " to " + resFile);
        // Finalize container now
                errCode = FINALIZE_FAILED;
        if (!PackageHelper.finalizeSdDir(newCid)) {
                if (PackageHelper.finalizeSdDir(newCid)) {
            Log.e(TAG, "Failed to finalize " + newCid + " at cache path " + newCachePath);
            // Clean up created container
            PackageHelper.destroySdDir(newCid);
            return null;
        }
                    if (localLOGV) Log.i(TAG, "Finalized container " + newCid);
                    if (localLOGV) Log.i(TAG, "Finalized container " + newCid);
        // Force a gc to avoid being killed.
                    errCode = PASS;
        Runtime.getRuntime().gc();
                }
        // Unmount container
            }
        }
        // Print error based on errCode
        String errMsg = "";
        switch (errCode) {
            case CREATE_FAILED:
                errMsg = "CREATE_FAILED";
                break;
            case COPY_FAILED:
                errMsg = "COPY_FAILED";
                if (localLOGV) Log.i(TAG, "Destroying " + newCid +
                        " at path " + newCachePath + " after " + errMsg);
                PackageHelper.destroySdDir(newCid);
                break;
            case FINALIZE_FAILED:
                errMsg = "FINALIZE_FAILED";
                if (localLOGV) Log.i(TAG, "Destroying " + newCid +
                        " at path " + newCachePath + " after " + errMsg);
                PackageHelper.destroySdDir(newCid);
                break;
            default:
                errMsg = "PASS";
                if (PackageHelper.isContainerMounted(newCid)) {
                if (PackageHelper.isContainerMounted(newCid)) {
                    if (localLOGV) Log.i(TAG, "Unmounting " + newCid +
                    if (localLOGV) Log.i(TAG, "Unmounting " + newCid +
                    " at path " + newCachePath);
                            " at path " + newCachePath + " after " + errMsg);
                    // Force a gc to avoid being killed.
                    Runtime.getRuntime().gc();
                    PackageHelper.unMountSdDir(newCid);
                    PackageHelper.unMountSdDir(newCid);
                } else {
                } else {
                    if (localLOGV) Log.i(TAG, "Container " + newCid + " not mounted");
                    if (localLOGV) Log.i(TAG, "Container " + newCid + " not mounted");
                }
                }
                break;
        }
        if (errCode != PASS) {
            return null;
        }
        return newCachePath;
        return newCachePath;
    }
    }


@@ -231,7 +257,8 @@ public class DefaultContainerService extends IntentService {
    }
    }


    private  boolean copyFile(Uri pPackageURI, FileOutputStream outStream) {
    private  boolean copyFile(Uri pPackageURI, FileOutputStream outStream) {
        if (pPackageURI.getScheme().equals("file")) {
        String scheme = pPackageURI.getScheme();
        if (scheme == null || scheme.equals("file")) {
            final File srcPackageFile = new File(pPackageURI.getPath());
            final File srcPackageFile = new File(pPackageURI.getPath());
            // We copy the source package file to a temp file and then rename it to the
            // We copy the source package file to a temp file and then rename it to the
            // destination file in order to eliminate a window where the package directory
            // destination file in order to eliminate a window where the package directory
@@ -240,7 +267,7 @@ public class DefaultContainerService extends IntentService {
                Log.e(TAG, "Couldn't copy file: " + srcPackageFile);
                Log.e(TAG, "Couldn't copy file: " + srcPackageFile);
                return false;
                return false;
            }
            }
        } else if (pPackageURI.getScheme().equals("content")) {
        } else if (scheme.equals("content")) {
            ParcelFileDescriptor fd = null;
            ParcelFileDescriptor fd = null;
            try {
            try {
                fd = getContentResolver().openFileDescriptor(pPackageURI, "r");
                fd = getContentResolver().openFileDescriptor(pPackageURI, "r");