{"record":{"id":"faa59c9ad207295a","repo":"MuntashirAkon/AppManager","slug":"failed-to-create-new-file-mnewname","errorCode":null,"errorMessage":"Failed to create new file \" + mNewName","messagePattern":"Failed to create new file \" \\+ mNewName","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"libcore/io/src/main/java/io/github/muntashirakon/io/AtomicExtendedFile.java","lineNumber":100,"sourceCode":"     */\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;\n        }\n        if (!sync(str)) {\n            Log.e(TAG, \"Failed to sync file output stream\");\n        }\n        try {","sourceCodeStart":82,"sourceCodeEnd":118,"githubUrl":"https://github.com/MuntashirAkon/AppManager/blob/0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5/libcore/io/src/main/java/io/github/muntashirakon/io/AtomicExtendedFile.java#L82-L118","documentation":"AtomicExtendedFile.startWrite() wraps a FileNotFoundException thrown by newOutputStream() when the new file cannot be created/opened for writing. The library throws this IOException because the underlying filesystem refused to create the target file (parent directory exists and mkdirs was not needed, or creation failed after directory setup). It signals the atomic write could not even begin.","triggerScenarios":"Calling startWrite() (directly or via doWriteState) when mNewName.newOutputStream() throws FileNotFoundException: the target name is invalid, the parent directory is missing/renamed between check and open, or the process lacks write permission on the parent directory.","commonSituations":"Writing to a directory deleted concurrently by another process; path on storage that was unmounted; SELinux or filesystem permission denies creating files in the target directory; filename containing characters rejected by the filesystem.","solutions":["Verify the parent directory exists and is writable before calling startWrite(); recreate it if it was deleted.","Check SELinux contexts and Unix permissions (ls -ld) on the target directory; fix with appropriate permissions or use a writable location.","Confirm the target file name mNewName is a valid, non-empty path without illegal characters.","Catch the IOException and retry or fall back to a different storage location."],"exampleFix":"// before\nOutputStream os = atomicFile.startWrite();\n// after\nFile dir = atomicFile.getBaseFile().getParentFile();\nif (dir != null && !dir.exists() && !dir.mkdirs()) {\n    throw new IOException(\"Cannot create dir \" + dir);\n}\nOutputStream os = atomicFile.startWrite();","handlingStrategy":"try-catch","validationCode":"File dir = baseFile.getParentFile();\nif (dir == null || !(dir.isDirectory() && dir.canWrite())) throw new IOException(\"Parent dir not writable: \" + dir);","typeGuard":null,"tryCatchPattern":"try (OutputStream os = atomicFile.startWrite()) {\n    // write\n} catch (IOException e) {\n    if (e.getMessage() != null && e.getMessage().startsWith(\"Failed to create new file\")) {\n        // recreate parent dir or fall back to alternate location\n    }\n}","preventionTips":["Check directory existence/writability before starting the atomic write","Avoid target directories that other processes may delete","Prefer app-private or known-writable storage locations"],"tags":["io","file-write","android"],"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-14T11:17:12.474Z"}