MuntashirAkon/AppManager · error · BackupException

Could not create directory ${dataSourceFile}

Error message

Could not create directory ${dataSourceFile}

What it means

restoreDirectory calls File.mkdirs() on the target data directory when it doesn't exist; if mkdirs() returns false (directory could not be created), this BackupException is thrown. Typical causes are permission problems on the parent path or a leftover non-directory file occupying the path.

Source

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

                case BackupDataDirectoryInfo.TYPE_DEVICE_PROTECTED:
                    // NOP
                    break;
            }
        } else {
            // Skip if internal data restore is not requested.
            if (!mRequestedFlags.backupInternalData()) {
                return;
            }
        }
        // Create data folder if not exists
        if (!dataSourceFile.exists()) {
            if (dataDirectoryInfo.isExternal() && !dataDirectoryInfo.isMounted) {
                if (!Utils.isRoboUnitTest()) {
                    throw new BackupException("External directory containing " + dataSource + " is not mounted.");
                } // else Skip checking for mounted partition for robolectric tests
            }
            if (!dataSourceFile.mkdirs()) {
                throw new BackupException("Could not create directory " + dataSourceFile);
            }
            if (!dataDirectoryInfo.isExternal()) {
                // Restore UID, GID
                dataSourceFile.setUidGid(uidGidPair);
            }
        }
        // Decrypt data
        try {
            dataFiles = mBackupItem.decrypt(dataFiles);
        } catch (IOException e) {
            throw new BackupException("Failed to decrypt " + Arrays.toString(dataFiles), e);
        }
        // Extract data to the data directory
        try {
            String publicSourceDir = new File(Objects.requireNonNull(mPackageInfo.applicationInfo).publicSourceDir).getParent();
            TarUtils.extract(mBackupInfo.tarType, dataFiles, dataSourceFile, null, BackupUtils
                    .getExcludeDirs(!mRequestedFlags.backupCache(), null), publicSourceDir);
        } catch (Throwable th) {

View on GitHub (pinned to 0152f468fc)

Solutions

  1. Ensure the restore runs with root privileges so private app directories can be created.
  2. Delete any stale file or broken symlink at the target path, then retry.
  3. Free up storage space and confirm the target filesystem is mounted read-write.
  4. Restore ownership/permissions on the parent directory (chown to the app's uid, as the code does with uidGidPair).

Example fix

// before: mkdirs failing on stale path
// (inside restoreDirectory)
if (!dataSourceFile.mkdirs()) { throw new BackupException(...); }
// after: clean path first
if (dataSourceFile.exists() && !dataSourceFile.isDirectory()) {
    Paths.delete(dataSourceFile); // remove stale file/symlink
}
if (!dataSourceFile.mkdirs()) { throw new BackupException(...); }
Defensive patterns

Strategy: try-catch

Validate before calling

File target = new File(dataSource);
if (target.exists() && !target.isDirectory())
    throw new IllegalStateException("Stale file blocks directory creation: " + target);
if (target.getParentFile() != null && !target.getParentFile().canWrite())
    throw new IllegalStateException("Parent not writable (root needed?): " + target.getParent());

Type guard

boolean canCreateDir(File f) { return !f.exists() || f.isDirectory(); }

Try / catch

try { runRestore(); } catch (BackupException e) {
    if (e.getMessage().startsWith("Could not create directory")) {
        // parse path from message, clean stale file, ensure root, retry once
        cleanAndRetry(e.getMessage());
    } else throw e;
}

Prevention

When it happens

Trigger: mkdirs() fails because the parent directory is not writable (missing root on /data/data/<pkg>); a regular file already exists at the target path; SELinux denies creation; storage is full or read-only mount.

Common situations: Restoring without root to another app's private data dir; a stale file left by a previous failed restore; read-only filesystem after a crash; disk full on device.

Understand the failure class

Background: mkdir permission denied (EACCES): failed to create directory errors explained — this error's family across 32 libraries.

Related errors


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