{"record":{"id":"c837158feaea35cd","repo":"shwenzhang/AndResGuard","slug":"pathalreadyexists","errorCode":null,"errorMessage":"PathAlreadyExists","messagePattern":"PathAlreadyExists","errorType":"exception","errorClass":"PathAlreadyExists","httpStatus":null,"severity":"error","filePath":"AndResGuard-core/src/main/java/com/tencent/mm/directory/AbstractDirectory.java","lineNumber":139,"sourceCode":"  @Override\n  public Directory getDir(String path) throws PathNotExist {\n    SubPath subpath = getSubPath(path);\n    if (subpath.dir != null) {\n      return subpath.dir.getDir(subpath.path);\n    }\n    if (!getAbstractDirs().containsKey(subpath.path)) {\n      throw new PathNotExist(path);\n    }\n    return getAbstractDirs().get(subpath.path);\n  }\n\n  @Override\n  public Directory createDir(String path) throws DirectoryException {\n    ParsedPath parsed = parsePath(path);\n    AbstractDirectory dir;\n    if (parsed.dir == null) {\n      if (getAbstractDirs().containsKey(parsed.subpath)) {\n        throw new PathAlreadyExists(path);\n      }\n      dir = createDirLocal(parsed.subpath);\n      getAbstractDirs().put(parsed.subpath, dir);\n      return dir;\n    }\n\n    if (getAbstractDirs().containsKey(parsed.dir)) {\n      dir = getAbstractDirs().get(parsed.dir);\n    } else {\n      dir = createDirLocal(parsed.dir);\n      getAbstractDirs().put(parsed.dir, dir);\n    }\n    return dir.createDir(parsed.subpath);\n  }\n\n  @Override\n  public boolean removeFile(String path) {\n    SubPath subpath;","sourceCodeStart":121,"sourceCodeEnd":157,"githubUrl":"https://github.com/shwenzhang/AndResGuard/blob/e4df245d82f27d9a2d0dd108260a3510cbaba849/AndResGuard-core/src/main/java/com/tencent/mm/directory/AbstractDirectory.java#L121-L157","documentation":"PathAlreadyExists is thrown by AbstractDirectory.createDir when the directory being created already exists in the abstract dirs map. The library treats duplicate creation as an error rather than returning the existing directory.","triggerScenarios":"Calling createDir(path) twice on the same AbstractDirectory for the same path, or calling createDir on a path that already exists in the loaded tree (e.g. res/values already loaded from the APK/zip).","commonSituations":"Re-running a resource-obfuscation pipeline on a directory object that already loaded all entries; looping over resource names where the same parent dir appears multiple times.","solutions":["Check containsDir(path) before calling createDir and reuse the existing Directory via getDir.","Wrap createDir in try-catch for PathAlreadyExists and fetch the existing dir with getDir(path) in the catch block.","Restructure loops so each unique path is created only once (dedupe with a Set)."],"exampleFix":"// before\nDirectory dir = root.createDir(\"res/values\");\n// after\nDirectory dir = root.containsDir(\"res/values\")\n    ? root.getDir(\"res/values\")\n    : root.createDir(\"res/values\");","handlingStrategy":"try-catch","validationCode":"boolean exists = dir.containsDir(path);","typeGuard":null,"tryCatchPattern":"try { return dir.createDir(path); } catch (PathAlreadyExists e) { return dir.getDir(path); }","preventionTips":["Dedupe paths with a Set before createDir loops","Use a get-or-create helper around the containsDir/createDir pair","Never call createDir blindly inside per-file loops"],"tags":["directory","duplicate","idempotency"],"backgroundTag":"file-already-exists","analyzedSha":"e4df245d82f27d9a2d0dd108260a3510cbaba849","analyzedAt":"2026-09-12T17:49:07.798Z","contentChangedAt":"2026-09-12T17:49:07.798Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}