{"record":{"id":"3e9f4b753c80fb08","repo":"MuntashirAkon/AppManager","slug":"existing-file-is-not-a-directory","errorCode":null,"errorMessage":"Existing file is not a directory","messagePattern":"Existing file is not a directory","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"app/src/main/java/io/github/muntashirakon/io/PathImpl.java","lineNumber":528,"sourceCode":"    @NonNull\n    public Path findOrCreateDirectory(@NonNull String displayName) throws IOException {\n        displayName = Paths.sanitize(displayName, true);\n        if (displayName == null) {\n            throw new IOException(\"Empty display name.\");\n        }\n        if (displayName.indexOf(File.separatorChar) != -1) {\n            throw new IllegalArgumentException(\"Display name contains file separator.\");\n        }\n        DocumentFile documentFile = getRealDocumentFile(this.documentFile);\n        if (!documentFile.isDirectory()) {\n            throw new IOException(\"Current file is not a directory.\");\n        }\n        Path fsRoot = VirtualFileSystem.getFsRoot(Paths.appendPathSegment(documentFile.getUri(), displayName));\n        if (fsRoot != null) return fsRoot;\n        DocumentFile file = documentFile.findFile(displayName);\n        if (file != null) {\n            if (!file.isDirectory()) {\n                throw new IOException(\"Existing file is not a directory\");\n            }\n            return new PathImpl(context, file);\n        }\n        file = documentFile.createDirectory(displayName);\n        if (file == null) throw new IOException(\"Could not create directory named \" + displayName);\n        return new PathImpl(context, file);\n    }\n\n    @NonNull\n    public PathAttributes getAttributes() throws IOException {\n        if (documentFile instanceof ExtendedRawDocumentFile) {\n            return PathAttributesImpl.fromFile((ExtendedRawDocumentFile) documentFile);\n        }\n        if (documentFile instanceof VirtualDocumentFile) {\n            return PathAttributesImpl.fromVirtual((VirtualDocumentFile) documentFile);\n        }\n        return PathAttributesImpl.fromSaf(context, documentFile);\n    }","sourceCodeStart":510,"sourceCodeEnd":546,"githubUrl":"https://github.com/MuntashirAkon/AppManager/blob/0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5/app/src/main/java/io/github/muntashirakon/io/PathImpl.java#L510-L546","documentation":"When a child with the requested display name already exists but is a regular file, findOrCreateDirectory() throws IOException(\"Existing file is not a directory\") rather than overwriting or failing at create time. This protects existing file content from being clobbered by a directory-creation request.","triggerScenarios":"documentFile.findFile(displayName) returns an existing non-directory document; i.e. a file with the exact same name as the directory you want already exists in the parent.","commonSituations":"Name collisions after refactors (\"data\" used as both a file and a directory name), case-insensitive filesystems matching differently-cased names, or leftover files from a previous run.","solutions":["Check documentFile.findFile(name) first; if it exists and is a file, choose a different name or delete the file deliberately","Use Path.exists()/isDirectory() before attempting creation","Handle the IOException and fall back to an alternative directory name"],"exampleFix":"// before\npath.findOrCreateDirectory(\"data\"); // may collide with existing file\n// after\nPath existing = path.findFile(\"data\");\nif (existing != null && !existing.isDirectory()) {\n    existing.delete(); // deliberate decision\n}\npath.findOrCreateDirectory(\"data\");","handlingStrategy":"validation","validationCode":"Path existing = path.findFile(name);\nif (existing != null && !existing.isDirectory()) {\n    throw new IllegalStateException(\"'\" + name + \"' exists as a file\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    path.findOrCreateDirectory(name);\n} catch (IOException e) {\n    if (e.getMessage() != null && e.getMessage().startsWith(\"Existing file is not a directory\")) {\n        // choose another name or explicitly remove the file\n    } else throw e;\n}","preventionTips":["Check findFile()/exists() before creating directories","Use deterministic, collision-free directory names (e.g. include a suffix)","Never assume create-style APIs overwrite; this one preserves existing files"],"tags":["io","android","documentfile","name-collision"],"backgroundTag":"file-already-exists","analyzedSha":"0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5","analyzedAt":"2026-09-12T14:03:37.243Z","contentChangedAt":"2026-09-12T14:03:37.243Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}