{"record":{"id":"47a985682059ab9f","repo":"stanfordnlp/CoreNLP","slug":"cp-could-not-create-directory-truetarget","errorCode":null,"errorMessage":"cp: could not create directory: <trueTarget>","messagePattern":"cp: could not create directory: <trueTarget>","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/io/IOUtils.java","lineNumber":1914,"sourceCode":"      copyFile(source, trueTarget);\n    } else if (source.isDirectory()) {\n      // Case: copying a directory\n      File[] children = source.listFiles();\n      if (children == null) { throw new IOException(\"cp: could not list files in source: \" + source); }\n\n      if (target.exists()) {\n        // Case: cp -r a b -- b exists\n        if (!target.isDirectory()) {\n          // cp -r a b -- b is a regular file\n          throw new IOException(\"cp: cannot copy directory into regular file: \" + target);\n        }\n        if (trueTarget.exists() && !trueTarget.isDirectory()) {\n          // cp -r a b -- b/a is not a directory\n          throw new IOException(\"cp: overwriting a file with a directory: \" + trueTarget);\n        }\n        if (!trueTarget.exists() && !trueTarget.mkdir()) {\n          // cp -r a b -- b/a cannot be created\n          throw new IOException(\"cp: could not create directory: \" + trueTarget);\n        }\n      } else {\n        // Case: cp -r a b -- b does not exist\n        assert trueTarget == target;\n        if (!trueTarget.mkdir()) {\n          // cp -r a b -- cannot create b as a directory\n          throw new IOException(\"cp: could not create target directory: \" + trueTarget);\n        }\n      }\n      // Actually do the copy\n      for (File child : children) {\n        File childTarget = new File(trueTarget.getPath() + File.separator + child.getName());\n        cp(child, childTarget, recursive);\n      }\n    } else {\n      throw new IOException(\"cp: unknown file type: \" + source);\n    }\n  }","sourceCodeStart":1896,"sourceCodeEnd":1932,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/io/IOUtils.java#L1896-L1932","documentation":"During a recursive cp onto an existing directory, trueTarget (target/sourceName) does not exist and File.mkdir() fails, so the method throws this IOException. mkdir() fails only when the directory cannot be created (permissions, or a same-named file appearing concurrently).","triggerScenarios":"IOUtils.cp(dirA, existingDirB, true) where existingDirB exists but existingDirB/dirA.getName() cannot be created via mkdir() (no write permission on B, or a file with that name created between the exists() check and mkdir()).","commonSituations":"Read-only or root-owned output directories; running under a restricted service account; race with another process writing into the target.","solutions":["Check write permission on the existing target directory and fix it (chmod/chown) or run as an account with write access","Ensure no concurrent process creates a file with the source's name inside the target","Wrap the copy in try-catch for IOException and surface the path so the user can fix permissions"],"exampleFix":"// before\nIOUtils.cp(srcDir, new File(\"/var/log/app\"), true); // read-only dir\n// after\nFile dest = new File(\"/var/log/app\");\nif (!dest.canWrite()) { throw new IllegalStateException(\"No write permission on \" + dest); }\nIOUtils.cp(srcDir, dest, true);","handlingStrategy":"validation","validationCode":"File tt = new File(target, source.getName()); if (!tt.exists() && !target.canWrite()) { throw new IllegalStateException(\"Cannot write into \" + target); }","typeGuard":null,"tryCatchPattern":"try { IOUtils.cp(src, target, true); } catch (IOException e) { if (e.getMessage().startsWith(\"cp: could not create directory\")) { /* fix permissions / stop concurrent writer */ } else throw e; }","preventionTips":["Verify canWrite() on target directories","Avoid concurrent writers into the same target dir","Run under an account with rights on the output tree"],"tags":["io","filesystem","mkdir","permissions"],"backgroundTag":"mkdir-permission-denied","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}