MuntashirAkon/AppManager · error · BackupException

Failed to restore the KeyStore files.

Error message

Failed to restore the KeyStore files.

What it means

RestoreOp.restoreKeyStore throws this BackupException when TarUtils.extract fails to unpack the backed-up KeyStore tar entries into the KeyStore directory, or when the subsequent chown/chmod on the KeyStore folder fails. It wraps the original Throwable as the cause. It signals the KeyStore portion of an app backup could not be laid down on disk.

Source

Thrown at app/src/main/java/io/github/muntashirakon/AppManager/backup/RestoreOp.java:460

        // Restore KeyStore files to the /data/misc/keystore folder
        Path keyStorePath = KeyStoreUtils.getKeyStorePath(mUserId);
        // Note down UID/GID
        UidGidPair uidGidPair;
        int mode;
        try {
            uidGidPair = Objects.requireNonNull(keyStorePath.getFile()).getUidGid();
            mode = keyStorePath.getFile().getMode();
        } catch (ErrnoException e) {
            throw new BackupException("Failed to access properties of the KeyStore folder.", e);
        }
        try {
            TarUtils.extract(mBackupInfo.tarType, keyStoreFiles, keyStorePath, null, null, null);
            // Restore folder permission
            Paths.chown(keyStorePath, uidGidPair.uid, uidGidPair.gid);
            //noinspection OctalInteger
            Paths.chmod(keyStorePath, mode & 0777);
        } catch (Throwable th) {
            throw new BackupException("Failed to restore the KeyStore files.", th);
        }
        // Rename files
        List<String> keyStoreFileNames = KeyStoreUtils.getKeyStoreFiles(KEYSTORE_PLACEHOLDER, mUserId);
        for (String keyStoreFileName : keyStoreFileNames) {
            try {
                String newFilename = Utils.replaceOnce(keyStoreFileName, String.valueOf(KEYSTORE_PLACEHOLDER), String.valueOf(mUid));
                keyStorePath.findFile(keyStoreFileName).renameTo(newFilename);
                Path targetFile = keyStorePath.findFile(newFilename);
                // Restore file permission
                Paths.chown(targetFile, uidGidPair.uid, uidGidPair.gid);
                //noinspection OctalInteger
                Paths.chmod(targetFile, 0600);
            } catch (IOException | ErrnoException e) {
                throw new BackupException("Failed to rename KeyStore files", e);
            }
        }
        Runner.runCommand(new String[]{"restorecon", "-R", keyStorePath.getFilePath()});
    }

View on GitHub (pinned to 0152f468fc)

Solutions

  1. Verify the device is rooted and App Manager has root/su access, since chown/chmod on the KeyStore path require it.
  2. Check the integrity of the backup: confirm the KeyStore backup files exist and the tar is not truncated; re-create the backup if corrupted.
  3. Inspect the wrapped cause (getCause()) for the underlying exception from TarUtils.extract or Paths.chown/chmod.
  4. Confirm the backup was made on a compatible Android version; KeyStore paths/format differ across releases.
  5. Retry the restore after rebooting (transient mount/ownership issues on /data).

Example fix

// before: blind restore that fails on non-root
catch (Throwable th) { throw new BackupException("Failed to restore the KeyStore files.", th); }
// after: check root before attempting
if (!RootUtils.isRooted()) {
    throw new BackupException("KeyStore restore requires root access.");
}
// then proceed with TarUtils.extract(...) inside try/catch
Defensive patterns

Strategy: try-catch

Validate before calling

// Before restore: confirm root and backup presence
if (!Shell.getRootAccess()) throw new IllegalStateException("Root required for KeyStore restore");
if (keyStoreBackupFiles.length == 0) throw new IllegalStateException("No KeyStore files in backup");

Type guard

boolean canRestoreKeyStore(BackupOp op) { return op != null && op.hasKeyStoreFiles() && Shell.getRootAccess(); }

Try / catch

try { runRestore(); } catch (BackupException e) {
    if (e.getMessage().contains("KeyStore files")) {
        Log.e(TAG, "KeyStore restore failed", e.getCause());
        // surface root/integrity guidance to the user
    } else throw e;
}

Prevention

When it happens

Trigger: TarUtils.extract() throws (corrupt/encrypted KeyStore tar, missing backup files, wrong tarType); Paths.chown() fails because the target uid:gid pair is invalid or the caller lacks root; Paths.chmod() fails on the keyStorePath.

Common situations: Restoring a backup on a device without root so permission changes fail; a truncated or tampered keystore.tar in the backup directory; restoring a backup made on another Android version whose /data/keystore layout changed; checksum-passing files that were later modified.

Understand the failure class

Background: "failed to write file", "Could not save figure", "Error saving remote file" — file write failed: causes and fixes across languages and libraries — this error's family across 38 libraries.

Related errors


AI-assisted analysis of MuntashirAkon/AppManager@0152f468fc (2026-09-12). Data as JSON: /api/errors/87a5e8c375cbca96. Report an issue: GitHub.