{"record":{"id":"d267c648d44e29bb","repo":"Tencent/matrix","slug":"path-absolutepath-is-pointed-to-an-existing-eleme","errorCode":null,"errorMessage":"Path $absolutePath is pointed to an existing element but it is not a directory.","messagePattern":"Path \\$absolutePath is pointed to an existing element but it is not a directory\\.","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"matrix/matrix-android/matrix-resource-canary/matrix-resource-canary-android/src/main/java/com/tencent/matrix/resource/MemoryUtil.kt","lineNumber":37,"sourceCode":"\nprivate fun info(message: String) {\n    MatrixLog.i(TAG, message)\n}\n\nprivate fun error(message: String, throwable: Throwable? = null) {\n    if (throwable != null)\n        MatrixLog.printErrStackTrace(TAG, throwable, message)\n    else\n        MatrixLog.e(TAG, message)\n}\n\nprivate val currentTime: Long\n    get() = System.currentTimeMillis()\n\nprivate fun File.assureIsDirectory() {\n    if (!isDirectory) {\n        if (exists())\n            throw IllegalStateException(\"Path $absolutePath is pointed to an existing element but it is not a directory.\")\n        mkdirs()\n    }\n}\n\nprivate class OrderedStreamWrapper(\n    private val order: ByteOrder,\n    private val stream: InputStream\n) {\n\n    fun readOrderedInt(): Int {\n        val buffer = ByteBuffer.allocate(4)\n            .apply {\n                order(order)\n            }\n        stream.read(buffer.array(), 0, 4)\n        return buffer.getInt(0)\n    }\n","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/Tencent/matrix/blob/3b8293bd65d47eeea7caf1f32a3a5d4d5eab60e7/matrix/matrix-android/matrix-resource-canary/matrix-resource-canary-android/src/main/java/com/tencent/matrix/resource/MemoryUtil.kt#L19-L55","documentation":"The File.assureIsDirectory extension in MemoryUtil throws IllegalStateException when the path exists but is a regular file (or other non-directory element) instead of creating the directory. It is used to guarantee the hprof/leak output directory is valid before writing.","triggerScenarios":"Calling MemoryUtil APIs (e.g. parsing a leak chain or dumping hprof metadata) with an output directory path that currently points to an existing file.","commonSituations":"A stale file occupies the intended directory path (previous crash wrote a file there); user configured a cache dir path that collides with a file; sync tools replaced the directory with a file.","solutions":["Delete the conflicting file at that path so the directory can be created: File(path).delete() before invoking the API.","Choose a different, dedicated directory path for Matrix resource-canary output.","On startup, ensure the path is a directory (mkdirs) before enabling leak dumping."],"exampleFix":"// before\nval out = File(context.filesDir, \"hprof\") // some prior run created a FILE named 'hprof'\nMemoryUtil.parse(out, ...)\n// after\nval out = File(context.filesDir, \"hprof\")\nif (out.exists() && !out.isDirectory) out.delete()\nout.mkdirs()\nMemoryUtil.parse(out, ...)","handlingStrategy":"validation","validationCode":"fun assureOutputDir(path: File): File {\n  if (path.exists() && !path.isDirectory) path.delete()\n  if (!path.isDirectory) path.mkdirs()\n  return path\n}","typeGuard":"fun File.isUsableDir(): Boolean = isDirectory || (!exists() && parentFile?.canWrite() == true)","tryCatchPattern":"try {\n  MemoryUtil.parse(dir, ...)\n} catch (e: IllegalStateException) {\n  MatrixLog.e(TAG, \"output path invalid: ${e.message}\")\n  dir.delete(); dir.mkdirs()\n}","preventionTips":["Reserve a dedicated, app-private directory for Matrix output at app startup.","Never write arbitrary files into configured cache/dump directories.","Sanitize the output path (delete conflicting files) before enabling leak analysis."],"tags":["android","filesystem","path-is-not-a-directory","memory-leak"],"backgroundTag":"path-is-not-a-directory","analyzedSha":"3b8293bd65d47eeea7caf1f32a3a5d4d5eab60e7","analyzedAt":"2026-09-08T08:01:39.722Z","contentChangedAt":"2026-09-08T08:01:39.722Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}