{"record":{"id":"e42c37928fc63ce4","repo":"apache/hadoop","slug":"getclass-getsimplename-doesn-t-support-trunc","errorCode":null,"errorMessage":"{getClass().getSimpleName()} doesn't support truncate","messagePattern":"(.+?) doesn't support truncate","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/AbstractFileSystem.java","lineNumber":759,"sourceCode":"      UnresolvedLinkException, IOException;\n\n  /**\n   * The specification of this method matches that of\n   * {@link FileContext#truncate(Path, long)} except that Path f must be for\n   * this file system.\n   *\n   * @param f the path.\n   * @param newLength new length.\n   * @throws AccessControlException access control exception.\n   * @throws FileNotFoundException file not found exception.\n   * @throws UnresolvedLinkException unresolved link exception.\n   * @throws IOException raised on errors performing I/O.\n   * @return if successfully truncate success true, not false.\n   */\n  public boolean truncate(Path f, long newLength)\n      throws AccessControlException, FileNotFoundException,\n      UnresolvedLinkException, IOException {\n    throw new UnsupportedOperationException(getClass().getSimpleName()\n        + \" doesn't support truncate\");\n  }\n\n  /**\n   * The specification of this method matches that of\n   * {@link FileContext#setReplication(Path, short)} except that Path f must be\n   * for this file system.\n   *\n   * @param f the path.\n   * @param replication replication.\n   * @return if successfully set replication success true, not false.\n   * @throws AccessControlException access control exception.\n   * @throws FileNotFoundException file not found exception.\n   * @throws UnresolvedLinkException unresolved link exception.\n   * @throws IOException raised on errors performing I/O.\n   */\n  public abstract boolean setReplication(final Path f,\n      final short replication) throws AccessControlException,","sourceCodeStart":741,"sourceCodeEnd":777,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/AbstractFileSystem.java#L741-L777","documentation":"truncate() is an optional AbstractFileSystem operation; the base implementation at AbstractFileSystem.java:756 throws UnsupportedOperationException naming the concrete class. Only file systems that override it support truncation — HDFS does (HDFS-3107), and local does in this version via RawLocalFileSystem.truncate reached through DelegateToFileSystem — but many third-party or custom AbstractFileSystem implementations do not.","triggerScenarios":"FileContext.truncate(path, newLength) or direct afs.truncate(...) on a file system whose AbstractFileSystem does not override truncate: custom in-house filesystems, ftp/sftp-backed DelegateToFileSystem whose underlying FileSystem throws, some object-store adapters.","commonSituations":"Portable code assuming truncate works everywhere because it works on HDFS; the same job running against test/local/HDFS mounts with different capability sets; downstream libraries (compaction, format writers) that truncate tail data as an optimization.","solutions":["Guard with the capability probe: fs.hasPathCapability(path, CommonPathCapabilities.FS_CAPABILITY_PATH_TRUNCATE) before calling truncate","Fallback rewrite: copy the wanted prefix to a temp file, then delete + rename over the original","For your own AbstractFileSystem, implement truncate() (and expose the capability) instead of relying on the base throw"],"exampleFix":"// before\nfs.truncate(p, newLen); // RawCustomFs doesn't support truncate\n\n// after\nif (fs.hasPathCapability(p, CommonPathCapabilities.FS_CAPABILITY_PATH_TRUNCATE)) {\n  fs.truncate(p, newLen);\n} else {\n  rewritePrefix(fs, p, newLen); // copy prefix -> temp, delete, rename\n}","handlingStrategy":"try-catch","validationCode":"FileSystem fs = ...;\nif (fs.hasPathCapability(p, CommonPathCapabilities.FS_CAPABILITY_PATH_TRUNCATE)) {\n  fs.truncate(p, newLen);\n} else {\n  rewritePrefix(fs, p, newLen); // copy prefix to temp, delete, rename\n}","typeGuard":"static boolean supportsTruncate(FileSystem fs, Path p) {\n  return fs.hasPathCapability(p, CommonPathCapabilities.FS_CAPABILITY_PATH_TRUNCATE);\n}","tryCatchPattern":"try { fc.truncate(p, newLen); } catch (UnsupportedOperationException e) { rewritePrefix(fc, p, newLen); }","preventionTips":["Probe hasPathCapability with fs.capability.paths.truncate before truncating","Keep a delete+rename (rewrite) fallback for filesystems without truncate","Implement truncate in custom AbstractFileSystem subclasses or expose the capability accurately"],"tags":["hadoop","truncate","unsupported-operation","filesystem","capability"],"backgroundTag":"unsupported-filesystem-operation","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}