{"record":{"id":"adabd85068b5cf27","repo":"apache/hadoop","slug":"cannot-truncate-to-a-larger-file-size-current-siz","errorCode":null,"errorMessage":"Cannot truncate to a larger file size. Current size: \" + oldLength + \", truncate size: \" + newLength + \".\"","messagePattern":"Cannot truncate to a larger file size\\. Current size: \" \\+ oldLength \\+ \", truncate size: \" \\+ newLength \\+ \"\\.\"","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/RawLocalFileSystem.java","lineNumber":768,"sourceCode":"        }\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  /**\n   * Delete the given path to a file or directory.\n   * @param p the path to delete\n   * @param recursive to delete sub-directories\n   * @return true if the file or directory and all its contents were deleted","sourceCodeStart":750,"sourceCodeEnd":786,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/RawLocalFileSystem.java#L750-L786","documentation":"RawLocalFileSystem.truncate(Path, long) throws IllegalArgumentException when newLength exceeds the file's current size: local filesystems cannot extend a file via truncate (unlike some object stores or HDFS-ordered semantics where newLength == len is the only allowed non-shrink). The message reports both current and requested sizes, which tells you exactly how far off the caller was.","triggerScenarios":"Calling fs.truncate(f, newLength) with newLength > f.length(): passing a desired total size instead of a truncation point, using a stale cached length (file shrank since stat), or computing newLength as oldLen + delta by mistake.","commonSituations":"Porting code that assumed truncate pads with zeros, rolling-log logic that mixes up 'target size' and 'bytes to keep', or files concurrently truncated by another process between the length check and the call.","solutions":["Clamp the request: long target = Math.min(newLength, fs.getFileStatus(f).getLen()); then truncate.","If extension is the real goal, open the file in append mode and pad: fs.append(f) then write zeros.","Re-stat the file immediately before truncating to avoid stale lengths.","Fix the arithmetic: to keep N bytes, pass N; to drop K bytes, pass len - K."],"exampleFix":"// before\nfs.truncate(logPath, 50_000_000); // file is only 12_000_000 bytes -> throws\n\n// after\nlong target = Math.min(50_000_000, fs.getFileStatus(logPath).getLen());\nfs.truncate(logPath, target);","handlingStrategy":"validation","validationCode":"long current = fs.getFileStatus(f).getLen();\nlong target = Math.min(newLength, current);\nif (target != newLength) {\n  LOG.warn(\"Truncate clamped from {} to {} (current size)\", newLength, target);\n}\nfs.truncate(f, target);","typeGuard":null,"tryCatchPattern":"try {\n  fs.truncate(f, newLength);\n} catch (IllegalArgumentException e) {\n  if (e.getMessage() != null && e.getMessage().contains(\"larger file size\")) {\n    fs.truncate(f, fs.getFileStatus(f).getLen()); // no-op semantics\n  } else {\n    throw e;\n  }\n}","preventionTips":["Clamp newLength to the freshly-statted file size before calling truncate.","Remember truncate only shrinks on local FS; use append to grow.","Re-stat immediately before truncating to avoid stale cached lengths."],"tags":["hadoop","local-filesystem","truncate","file-size","illegalargumentexception"],"backgroundTag":"truncate-invalid-size","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-23T01:17:44.959Z"}