{"record":{"id":"c8819aebe2353a3b","repo":"apache/hadoop","slug":"cannot-truncate-a-directory-f","errorCode":null,"errorMessage":"Cannot truncate a directory (=\" + f + \")","messagePattern":"Cannot truncate a directory \\(=\" \\+ f \\+ \"\\)","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/RawLocalFileSystem.java","lineNumber":764,"sourceCode":"          }\n          if (this.delete(dst, false) && srcFile.renameTo(dstFile)) {\n            return true;\n          }\n        }\n      }\n    } catch (FileNotFoundException ignored) {\n    }\n    return false;\n  }\n\n  @Override\n  public boolean truncate(Path f, final long newLength) throws IOException {\n    FileStatus status = getFileStatus(f);\n    if(status == null) {\n      throw new FileNotFoundException(\"File \" + f + \" not found\");\n    }\n    if(status.isDirectory()) {\n      throw new IOException(\"Cannot truncate a directory (=\" + f + \")\");\n    }\n    long oldLength = status.getLen();\n    if(newLength > oldLength) {\n      throw new IllegalArgumentException(\n          \"Cannot truncate to a larger file size. Current size: \" + oldLength +\n          \", truncate size: \" + newLength + \".\");\n    }\n    try (FileOutputStream out = new FileOutputStream(pathToFile(f), true)) {\n      try {\n        out.getChannel().truncate(newLength);\n      } catch(IOException e) {\n        throw new FSError(e);\n      }\n    }\n    return true;\n  }\n  \n  /**","sourceCodeStart":746,"sourceCodeEnd":782,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/RawLocalFileSystem.java#L746-L782","documentation":"RawLocalFileSystem.truncate(Path, long) throws IOException('Cannot truncate a directory') when getFileStatus reports the target is a directory. Truncation is defined only for regular files; a directory has no byte length to shrink. The path was created as a directory by an earlier mkdirs or output layout step.","triggerScenarios":"Calling fs.truncate(dirPath, newLen) where dirPath is a local directory, e.g. truncating a job output directory instead of a part-file, or a path that mkdirs(parent) created as the final component in a previous run.","commonSituations":"Confusing an output directory with the log/index file inside it, configuration pointing at a directory, or reusing a path whose type flipped from file to directory between runs.","solutions":["Target a concrete file inside the directory: truncate(new Path(dir, \"part-00000\"), newLen).","Check the type first: if (fs.getFileStatus(p).isDirectory()) throw/skip/iterate children.","If the directory is leftover garbage from a misconfigured run, delete it and recreate the path as a file.","For whole-directory truncation semantics, list children and truncate/delete each file instead."],"exampleFix":"// before\nfs.truncate(new Path(\"/out\"), 1024); // /out is a directory\n\n// after\nPath dir = new Path(\"/out\");\nif (fs.getFileStatus(dir).isDirectory()) {\n  for (FileStatus st : fs.listStatus(dir)) {\n    if (st.isFile()) fs.truncate(st.getPath(), 1024);\n  }\n} else {\n  fs.truncate(dir, 1024);\n}","handlingStrategy":"validation","validationCode":"FileStatus st = fs.getFileStatus(p);\nif (st.isDirectory()) {\n  throw new IllegalArgumentException(p + \" is a directory; truncate a file\");\n}\nfs.truncate(p, newLen);","typeGuard":null,"tryCatchPattern":"try {\n  fs.truncate(p, newLen);\n} catch (IOException e) {\n  if (fs.getFileStatus(p).isDirectory()) {\n    throw new IllegalArgumentException(p + \" is a directory\", e);\n  }\n  throw e;\n}","preventionTips":["Build truncation targets as explicit file paths (dir + filename).","Assert isFile() before truncate when paths come from configuration.","Clean up mis-created directories before switching a path to file semantics."],"tags":["hadoop","local-filesystem","truncate","directory","ioexception"],"backgroundTag":"truncate-invalid-target","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}