{"record":{"id":"3bd4d6175c05af75","repo":"MuntashirAkon/AppManager","slug":"failed-to-restore-rules-file","errorCode":null,"errorMessage":"Failed to restore rules file.","messagePattern":"Failed to restore rules file\\.","errorType":"exception","errorClass":"BackupException","httpStatus":null,"severity":"error","filePath":"app/src/main/java/io/github/muntashirakon/AppManager/backup/RestoreOp.java","lineNumber":788,"sourceCode":"            if (!checksum.equals(mChecksum.get(rulesFile.getName()))) {\n                throw new BackupException(\"Couldn't verify permission file.\" +\n                        \"\\nFile: \" + rulesFile +\n                        \"\\nFound: \" + checksum +\n                        \"\\nRequired: \" + mChecksum.get(rulesFile.getName()));\n            }\n        }\n        // Decrypt rules file\n        try {\n            rulesFile = mBackupItem.decrypt(new Path[]{rulesFile})[0];\n        } catch (IOException | IndexOutOfBoundsException e) {\n            throw new BackupException(\"Failed to decrypt \" + rulesFile.getName(), e);\n        }\n        try (RulesImporter importer = new RulesImporter(Arrays.asList(RuleType.values()), new int[]{mUserId})) {\n            importer.addRulesFromPath(rulesFile);\n            importer.setPackagesToImport(Collections.singletonList(mPackageName));\n            importer.applyRules(true);\n        } catch (IOException e) {\n            throw new BackupException(\"Failed to restore rules file.\", e);\n        }\n    }\n\n    private void deleteFiles(@NonNull Path[] files) {\n        for (Path file : files) {\n            file.delete();\n        }\n    }\n}\n","sourceCodeStart":770,"sourceCodeEnd":798,"githubUrl":"https://github.com/MuntashirAkon/AppManager/blob/0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5/app/src/main/java/io/github/muntashirakon/AppManager/backup/RestoreOp.java#L770-L798","documentation":"restoreRules throws this when RulesImporter.applyRules() raises an IOException after the rules file was successfully decrypted. The file contents are readable/valid enough to decrypt, but parsing or applying the rules to the package database failed at the I/O level.","triggerScenarios":"runRestore on a backup whose decrypted rules file cannot be parsed into paths (addRulesFromPath) or applied (applyRules(true)) due to unreadable app-private storage, unsupported rules content for the current RuleType set, or an I/O failure while writing imported rules.","commonSituations":"Restoring an old backup whose rules format is no longer understood, insufficient storage or SELinux denials preventing writes to the rules directory, or importing rules for a package/user (mUserId) that no longer exists.","solutions":["Check device storage and that the AppManager data/rules directory is writable.","Update AppManager so the current RuleType set understands the backup's rules format.","Recreate the backup from the source device with the current app version, then restore again.","Verify the target package and user ID (mUserId) exist on this device before restoring."],"exampleFix":"// before: old backup format fails silently into IOException\nimporter.applyRules(true);\n// after: regenerate the backup with the installed version, or upgrade AppManager first\n// adb shell pm list packages --user <mUserId>  # confirm user/package exists, then retry restore","handlingStrategy":"try-catch","validationCode":"// Pre-check: package and user exist, rules dir writable\nboolean userExists = userManager.getUserIds().length > mUserId;\nboolean writable = new File(rulesDir).canWrite();","typeGuard":null,"tryCatchPattern":"try {\n    restoreOp.runRestore();\n} catch (BackupException e) {\n    if (\"Failed to restore rules file.\".equals(e.getMessage())) {\n        // check storage space, AppManager version, and target user\n    }\n}","preventionTips":["Back up and restore with the same (or newer) AppManager version on both ends.","Ensure sufficient free storage before running restores.","Confirm the target user ID (work profile etc.) exists on the destination device."],"tags":["rules-import","io","backup-restore","format-compatibility"],"backgroundTag":"file-read-failed","analyzedSha":"0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5","analyzedAt":"2026-09-12T14:03:37.243Z","contentChangedAt":"2026-09-12T14:03:37.243Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}