{"record":{"id":"841e7e90364f7b95","repo":"stanfordnlp/CoreNLP","slug":"cp-could-not-list-files-in-source-source","errorCode":null,"errorMessage":"cp: could not list files in source: <source>","messagePattern":"cp: could not list files in source: <source>","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/io/IOUtils.java","lineNumber":1900,"sourceCode":"    if (!target.getParentFile().isDirectory()) {\n      // cp a b/c/d/e -- b/c/d is a regular file\n      throw new IOException(\"cp: cannot copy to directory: \" + recursive + \" (parent isn't a directory)\");\n    }\n    // Get true target\n    File trueTarget;\n    if (target.exists() && target.isDirectory()) {\n      trueTarget = new File(target.getPath() + File.separator + source.getName());\n    } else {\n      trueTarget = target;\n    }\n    // Copy\n    if (source.isFile()) {\n      // Case: copying a file\n      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;","sourceCodeStart":1882,"sourceCodeEnd":1918,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/io/IOUtils.java#L1882-L1918","documentation":"During recursive directory copy, IOUtils.cp calls source.listFiles(); when that returns null — which File.listFiles() does if an I/O error occurs or the caller lacks read permission on the directory — cp throws this IOException instead of proceeding with a null array.","triggerScenarios":"Calling IOUtils.cp(dirSource, target, true) where dirSource is a directory but listFiles() returns null: no read/execute permission on the source directory, the directory was removed concurrently, or a filesystem-level I/O error.","commonSituations":"Copying directory trees owned by another user (missing +x/+r bits); NFS/network mounts failing mid-walk; a directory deleted by a concurrent process (e.g. tmp cleaner) during the copy; sandboxed environments denying directory read access.","solutions":["Check and fix read/execute permissions on the source directory for the running user (ls -ld source; chmod/chown).","Verify the directory still exists immediately before copying (it may have been deleted concurrently).","Confirm the directory is on an accessible (not failed/stale) filesystem, especially for network mounts.","As an alternative, enumerate the tree yourself with Files.walk and copy per-file so you control error reporting."],"exampleFix":"// before\nIOUtils.cp(new File(\"/var/data/corpus\"), new File(\"/backup/corpus\"), true);\n// after\nFile src = new File(\"/var/data/corpus\");\nif (!src.canRead()) {\n  throw new IllegalStateException(\"Cannot read source directory: \" + src.getAbsolutePath());\n}\nIOUtils.cp(src, new File(\"/backup/corpus\"), true);","handlingStrategy":"validation","validationCode":"File src = new File(sourceDir);\nif (!src.isDirectory()) throw new IllegalArgumentException(\"Not a directory: \" + src);\nif (!src.canRead() || src.listFiles() == null)\n  throw new IllegalStateException(\"Cannot list source directory: \" + src.getAbsolutePath());","typeGuard":null,"tryCatchPattern":"try {\n  IOUtils.cp(src, target, true);\n} catch (IOException e) {\n  if (e.getMessage().startsWith(\"cp: could not list files\")) {\n    throw new IllegalStateException(\"Lost read access to \" + src.getAbsolutePath() + \" (perms? deleted? mount stale?)\", e);\n  }\n  throw e;\n}","preventionTips":["Check canRead() (r + x bits) on directories before recursive copies.","Avoid copying from locations cleaned concurrently (e.g. /tmp scrubbers).","Prefer NIO Files.walk-based copying when you need per-entry error control."],"tags":["io","filesystem","copy","permissions","java"],"backgroundTag":"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"}