{"record":{"id":"70baf5eb4eb80bf3","repo":"apache/hadoop","slug":"file-f-not-found","errorCode":null,"errorMessage":"File \" + f + \" not found","messagePattern":"File \" \\+ f \\+ \" not found","errorType":"exception","errorClass":"FileNotFoundException","httpStatus":null,"severity":"error","filePath":"hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/RawLocalFileSystem.java","lineNumber":761,"sourceCode":"          if (LOG.isDebugEnabled()) {\n            LOG.debug(\"Deleting empty destination and renaming \" + src +\n                \" to \" + dst);\n          }\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;","sourceCodeStart":743,"sourceCodeEnd":779,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/RawLocalFileSystem.java#L743-L779","documentation":"RawLocalFileSystem.truncate(Path, long) calls getFileStatus first; if the status lookup signals the file is absent it throws FileNotFoundException('File ... not found'). In practice getFileStatus itself throws FileNotFoundException for missing paths, so this branch guards the degenerate null case, but the user-visible meaning is the same: the local file to truncate does not exist.","triggerScenarios":"Calling fs.truncate(path, newLen) on a local path that does not exist: typo'd filename, output moved/cleaned before truncation, a race where another process deleted the file, or truncate issued after a failed create.","commonSituations":"Post-processing steps that shrink generated files that a cleanup job already removed, concurrent writers on shared scratch space deleting each other's files, or scripts referencing stale paths after a directory restructure.","solutions":["Verify the path: ls -l / the file on local disk; fix typos or stale references in configuration.","Check existence in code before truncating: if (!fs.exists(p)) recreate or skip.","For concurrent writers, move to unique per-writer files or coordinate deletion/truncation with a lock.","Treat FileNotFoundException from truncate as non-retryable unless a concurrent producer may recreate the file, in which case wait-and-recheck once."],"exampleFix":"// before\nfs.truncate(new Path(\"/data/scratch.log\"), 0); // file was deleted by cleanup\n\n// after\nPath p = new Path(\"/data/scratch.log\");\nif (fs.exists(p)) {\n  fs.truncate(p, 0);\n} else {\n  LOG.warn(\"{} missing; recreating\", p);\n  fs.create(p, true).close();\n}","handlingStrategy":"try-catch","validationCode":"if (!fs.exists(p) || !fs.getFileStatus(p).isFile()) {\n  throw new FileNotFoundException(p + \" missing; run the producer stage first\");\n}\nfs.truncate(p, newLen);","typeGuard":null,"tryCatchPattern":"try {\n  fs.truncate(p, newLen);\n} catch (FileNotFoundException e) {\n  LOG.warn(\"{} vanished before truncate; skipping\", p);\n}","preventionTips":["Check exists() and isFile() before truncate.","Sequence pipeline stages so files exist before post-processing.","Treat missing-file on truncate as skip-or-recreate, not retry-uniformly."],"tags":["hadoop","local-filesystem","truncate","file-not-found","filenotfoundexception"],"backgroundTag":"file-not-found","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-23T01:17:44.959Z"}