{"record":{"id":"370c1d4d2b2aa15e","repo":"alibaba/arthas","slug":"directory-parent-could-not-be-created","errorCode":null,"errorMessage":"Directory '{parent}' could not be created","messagePattern":"Directory '(.+?)' could not be created","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"common/src/main/java/com/taobao/arthas/common/FileUtils.java","lineNumber":125,"sourceCode":"\t * @return a new {@link FileOutputStream} for the specified file\n\t * @throws IOException if the file object is a directory\n\t * @throws IOException if the file cannot be written to\n\t * @throws IOException if a parent directory needs creating but that fails\n\t * @since 2.1\n\t */\n\tpublic static FileOutputStream openOutputStream(final File file, final boolean append) throws IOException {\n\t\tif (file.exists()) {\n\t\t\tif (file.isDirectory()) {\n\t\t\t\tthrow new IOException(\"File '\" + file + \"' exists but is a directory\");\n\t\t\t}\n\t\t\tif (!file.canWrite()) {\n\t\t\t\tthrow new IOException(\"File '\" + file + \"' cannot be written to\");\n\t\t\t}\n\t\t} else {\n\t\t\tfinal File parent = file.getParentFile();\n\t\t\tif (parent != null) {\n\t\t\t\tif (!parent.mkdirs() && !parent.isDirectory()) {\n\t\t\t\t\tthrow new IOException(\"Directory '\" + parent + \"' could not be created\");\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn new FileOutputStream(file, append);\n\t}\n\t\n    /**\n     * Reads the contents of a file into a byte array.\n     * The file is always closed.\n     *\n     * @param file the file to read, must not be {@code null}\n     * @return the file contents, never {@code null}\n     * @throws IOException in case of an I/O error\n     * @since 1.1\n     */\n    public static byte[] readFileToByteArray(final File file) throws IOException {\n    \tInputStream in = null;\n    \ttry {","sourceCodeStart":107,"sourceCodeEnd":143,"githubUrl":"https://github.com/alibaba/arthas/blob/21cf2e9ba52b305290be7223b980ff504bb9cb5b/common/src/main/java/com/taobao/arthas/common/FileUtils.java#L107-L143","documentation":"FileUtils.openOutputStream(file, append): when the target file does NOT exist and has a parent directory, it calls parent.mkdirs(). If mkdirs() returns false AND parent.isDirectory() is also false (i.e. creation failed and it still isn't a directory), it throws IOException(\"Directory '<parent>' could not be created\").","triggerScenarios":"Writing to a file whose parent path does not exist and cannot be created — insufficient permission on the grandparent, a non-directory entry blocking the path, or a read-only filesystem.","commonSituations":"Output directory under /var/run or /opt owned by root while the process runs unprivileged; a file (not dir) already exists where a directory is expected along the parent chain; read-only/root filesystem in a constrained container; disk full or quota exceeded.","solutions":["Pre-create the parent directory with correct ownership before running.","Grant write permission on the ancestor directory so mkdirs can succeed.","Choose an output path under a writable location (e.g. /tmp, the user's home).","Remove any non-directory entry conflicting with the parent path."],"exampleFix":"# before\nFileUtils.openOutputStream(new File(\"/opt/arthas/out/trace.log\"), false);  // /opt/arthas/out absent, no perms\n\n# after\nmkdir -p /opt/arthas/out && chown app:app /opt/arthas/out\n# then the call succeeds","handlingStrategy":"validation","validationCode":"File parent = file.getParentFile();\nif (parent != null && !parent.exists() && !parent.mkdirs()) {\n    throw new IOException(\"cannot create parent dir: \" + parent);\n}","typeGuard":"static boolean parentReady(File f) {\n    File p = f == null ? null : f.getParentFile();\n    return p == null || p.isDirectory() || p.mkdirs();\n}","tryCatchPattern":"try {\n    return FileUtils.openOutputStream(file, append);\n} catch (IOException e) {\n    if (e.getMessage().contains(\"could not be created\")) {\n        // pre-create parent with correct perms, then retry\n    } else throw e;\n}","preventionTips":["Pre-create and chown output directories in provisioning.","Use writable locations (home, /tmp) for runtime outputs.","Remove conflicting non-directory entries along the parent chain."],"tags":["filesystem","io","permissions","common"],"backgroundTag":null,"analyzedSha":"21cf2e9ba52b305290be7223b980ff504bb9cb5b","analyzedAt":"2026-08-14T00:57:07.243Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}