{"record":{"id":"d86b7504d0d4cfd4","repo":"MuntashirAkon/AppManager","slug":"failed-to-create-directory-for-mnewname","errorCode":null,"errorMessage":"Failed to create directory for \" + mNewName","messagePattern":"Failed to create directory for \" \\+ mNewName","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"libcore/io/src/main/java/io/github/muntashirakon/io/AtomicExtendedFile.java","lineNumber":95,"sourceCode":"     * a write, this will simply replace whatever that thread is writing\n     * with the new file being written by this thread, and when the other\n     * thread finishes the write the new write operation will no longer be\n     * safe (or will be lost).  You must do your own threading protection for\n     * access to AtomicFile.\n     */\n    @WorkerThread\n    @NonNull\n    public FileOutputStream startWrite() throws IOException {\n        if (mLegacyBackupName.exists()) {\n            rename(mLegacyBackupName, mBaseName);\n        }\n\n        try {\n            return mNewName.newOutputStream();\n        } catch (FileNotFoundException e) {\n            File parent = mNewName.getParentFile();\n            if (!parent.mkdirs()) {\n                throw new IOException(\"Failed to create directory for \" + mNewName, e);\n            }\n            try {\n                return mNewName.newOutputStream();\n            } catch (FileNotFoundException e2) {\n                throw new IOException(\"Failed to create new file \" + mNewName, e2);\n            }\n        }\n    }\n\n    /**\n     * Call when you have successfully finished writing to the stream\n     * returned by {@link #startWrite()}.  This will close, sync, and\n     * commit the new data.  The next attempt to read the atomic file\n     * will return the new file stream.\n     */\n    public void finishWrite(@Nullable FileOutputStream str) {\n        if (str == null) {\n            return;","sourceCodeStart":77,"sourceCodeEnd":113,"githubUrl":"https://github.com/MuntashirAkon/AppManager/blob/0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5/libcore/io/src/main/java/io/github/muntashirakon/io/AtomicExtendedFile.java#L77-L113","documentation":"AtomicExtendedFile.startWrite opens a new output stream for the temp/new file; if the parent directory doesn't exist it attempts mkdirs(), and when directory creation fails it throws IOException \"Failed to create directory for <mNewName>\" (wrapping the original FileNotFoundException).","triggerScenarios":"Calling startWrite (e.g. via doWriteState) when the target file's parent directory does not exist and cannot be created — due to missing write permission on an ancestor, a non-directory occupying the path, or read-only/external storage.","commonSituations":"Writing to a path on read-only storage or SD card without permissions; parent path component is actually a file; missing MANAGE_EXTERNAL_STORAGE/legacy storage access on newer Android; path typo creating a deep nonexistent directory.","solutions":["Create/verify the parent directory beforehand and check storage permissions (request runtime storage access)","Ensure no regular file exists at a parent path segment (delete/rename it)","Check available storage and mount state (read-only volume)","Point the atomic write at a directory the app can write (getFilesDir/getCacheDir)"],"exampleFix":"// before\natomicFile.startWrite(); // throws if dir missing & uncreatable\n// after\nFile dir = target.getParentFile();\nif (dir != null && !dir.exists() && !dir.mkdirs()) {\n    throw new IOException(\"Cannot create dir \" + dir + \": check permissions/storage\");\n}\nOutputStream os = atomicFile.startWrite();","handlingStrategy":"try-catch","validationCode":"File dir = target.getParentFile();\nboolean writable = dir != null\n    && (dir.exists() || dir.mkdirs())\n    && dir.canWrite();","typeGuard":null,"tryCatchPattern":"try {\n    OutputStream os = atomicFile.startWrite();\n} catch (IOException e) {\n    Log.e(TAG, \"Cannot create target dir/file — check permissions and storage\", e);\n}","preventionTips":["Request storage permissions before writing (scoped storage on API 29+)","Pre-create parent directories during setup","Check storage mount state and free space","Use app-private directories (getFilesDir) for reliable writes"],"tags":["io","filesystem","mkdir","atomic-file"],"backgroundTag":"file-write-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"}