Search in sources :

Example 21 with PackageLite

use of android.content.pm.PackageParser.PackageLite in project android_frameworks_base by crdroidandroid.

the class PackageInstallerSession method validateInstallLocked.

/**
     * Validate install by confirming that all application packages are have
     * consistent package name, version code, and signing certificates.
     * <p>
     * Clears and populates {@link #mResolvedBaseFile},
     * {@link #mResolvedStagedFiles}, and {@link #mResolvedInheritedFiles}.
     * <p>
     * Renames package files in stage to match split names defined inside.
     * <p>
     * Note that upgrade compatibility is still performed by
     * {@link PackageManagerService}.
     */
private void validateInstallLocked(PackageInfo pkgInfo, ApplicationInfo appInfo) throws PackageManagerException {
    mPackageName = null;
    mVersionCode = -1;
    mSignatures = null;
    mResolvedBaseFile = null;
    mResolvedStagedFiles.clear();
    mResolvedInheritedFiles.clear();
    final File[] removedFiles = mResolvedStageDir.listFiles(sRemovedFilter);
    final List<String> removeSplitList = new ArrayList<>();
    if (!ArrayUtils.isEmpty(removedFiles)) {
        for (File removedFile : removedFiles) {
            final String fileName = removedFile.getName();
            final String splitName = fileName.substring(0, fileName.length() - REMOVE_SPLIT_MARKER_EXTENSION.length());
            removeSplitList.add(splitName);
        }
    }
    final File[] addedFiles = mResolvedStageDir.listFiles(sAddedFilter);
    if (ArrayUtils.isEmpty(addedFiles) && removeSplitList.size() == 0) {
        throw new PackageManagerException(INSTALL_FAILED_INVALID_APK, "No packages staged");
    }
    // Verify that all staged packages are internally consistent
    final ArraySet<String> stagedSplits = new ArraySet<>();
    for (File addedFile : addedFiles) {
        final ApkLite apk;
        try {
            apk = PackageParser.parseApkLite(addedFile, PackageParser.PARSE_COLLECT_CERTIFICATES);
        } catch (PackageParserException e) {
            throw PackageManagerException.from(e);
        }
        if (!stagedSplits.add(apk.splitName)) {
            throw new PackageManagerException(INSTALL_FAILED_INVALID_APK, "Split " + apk.splitName + " was defined multiple times");
        }
        // Use first package to define unknown values
        if (mPackageName == null) {
            mPackageName = apk.packageName;
            mVersionCode = apk.versionCode;
        }
        if (mSignatures == null) {
            mSignatures = apk.signatures;
            mCertificates = apk.certificates;
        }
        assertApkConsistent(String.valueOf(addedFile), apk);
        // Take this opportunity to enforce uniform naming
        final String targetName;
        if (apk.splitName == null) {
            targetName = "base.apk";
        } else {
            targetName = "split_" + apk.splitName + ".apk";
        }
        if (!FileUtils.isValidExtFilename(targetName)) {
            throw new PackageManagerException(INSTALL_FAILED_INVALID_APK, "Invalid filename: " + targetName);
        }
        final File targetFile = new File(mResolvedStageDir, targetName);
        if (!addedFile.equals(targetFile)) {
            addedFile.renameTo(targetFile);
        }
        // Base is coming from session
        if (apk.splitName == null) {
            mResolvedBaseFile = targetFile;
        }
        mResolvedStagedFiles.add(targetFile);
    }
    if (removeSplitList.size() > 0) {
        // validate split names marked for removal
        for (String splitName : removeSplitList) {
            if (!ArrayUtils.contains(pkgInfo.splitNames, splitName)) {
                throw new PackageManagerException(INSTALL_FAILED_INVALID_APK, "Split not found: " + splitName);
            }
        }
        // ensure we've got appropriate package name, version code and signatures
        if (mPackageName == null) {
            mPackageName = pkgInfo.packageName;
            mVersionCode = pkgInfo.versionCode;
        }
        if (mSignatures == null) {
            mSignatures = pkgInfo.signatures;
        }
    }
    if (params.mode == SessionParams.MODE_FULL_INSTALL) {
        // Full installs must include a base package
        if (!stagedSplits.contains(null)) {
            throw new PackageManagerException(INSTALL_FAILED_INVALID_APK, "Full install must include a base package");
        }
    } else {
        // Partial installs must be consistent with existing install
        if (appInfo == null) {
            throw new PackageManagerException(INSTALL_FAILED_INVALID_APK, "Missing existing base package for " + mPackageName);
        }
        final PackageLite existing;
        final ApkLite existingBase;
        try {
            existing = PackageParser.parsePackageLite(new File(appInfo.getCodePath()), 0);
            existingBase = PackageParser.parseApkLite(new File(appInfo.getBaseCodePath()), PackageParser.PARSE_COLLECT_CERTIFICATES);
        } catch (PackageParserException e) {
            throw PackageManagerException.from(e);
        }
        assertApkConsistent("Existing base", existingBase);
        // Inherit base if not overridden
        if (mResolvedBaseFile == null) {
            mResolvedBaseFile = new File(appInfo.getBaseCodePath());
            mResolvedInheritedFiles.add(mResolvedBaseFile);
        }
        // Inherit splits if not overridden
        if (!ArrayUtils.isEmpty(existing.splitNames)) {
            for (int i = 0; i < existing.splitNames.length; i++) {
                final String splitName = existing.splitNames[i];
                final File splitFile = new File(existing.splitCodePaths[i]);
                final boolean splitRemoved = removeSplitList.contains(splitName);
                if (!stagedSplits.contains(splitName) && !splitRemoved) {
                    mResolvedInheritedFiles.add(splitFile);
                }
            }
        }
        // Inherit compiled oat directory.
        final File packageInstallDir = (new File(appInfo.getBaseCodePath())).getParentFile();
        mInheritedFilesBase = packageInstallDir;
        final File oatDir = new File(packageInstallDir, "oat");
        if (oatDir.exists()) {
            final File[] archSubdirs = oatDir.listFiles();
            // instruction set hierarchy and link compiled output.
            if (archSubdirs != null && archSubdirs.length > 0) {
                final String[] instructionSets = InstructionSets.getAllDexCodeInstructionSets();
                for (File archSubDir : archSubdirs) {
                    // Skip any directory that isn't an ISA subdir.
                    if (!ArrayUtils.contains(instructionSets, archSubDir.getName())) {
                        continue;
                    }
                    mResolvedInstructionSets.add(archSubDir.getName());
                    List<File> oatFiles = Arrays.asList(archSubDir.listFiles());
                    if (!oatFiles.isEmpty()) {
                        mResolvedInheritedFiles.addAll(oatFiles);
                    }
                }
            }
        }
    }
}
Also used : ArraySet(android.util.ArraySet) PackageParserException(android.content.pm.PackageParser.PackageParserException) PackageLite(android.content.pm.PackageParser.PackageLite) ArrayList(java.util.ArrayList) ApkLite(android.content.pm.PackageParser.ApkLite) File(java.io.File)

Aggregations

PackageLite (android.content.pm.PackageParser.PackageLite)21 PackageParserException (android.content.pm.PackageParser.PackageParserException)21 File (java.io.File)21 ApkLite (android.content.pm.PackageParser.ApkLite)19 IOException (java.io.IOException)14 ArrayList (java.util.ArrayList)10 RemoteException (android.os.RemoteException)9 DexFile (dalvik.system.DexFile)6 ArraySet (android.util.ArraySet)5 PrintWriter (java.io.PrintWriter)4 URISyntaxException (java.net.URISyntaxException)4 PackageParser.isApkFile (android.content.pm.PackageParser.isApkFile)2 StrictJarFile (android.util.jar.StrictJarFile)2