{"record":{"id":"23ec0fa1015011fe","repo":"eclipse-vertx/vert.x","slug":"failed-to-truncate-p","errorCode":null,"errorMessage":"Failed to truncate ${p}","messagePattern":"Failed to truncate (.+?)","errorType":"exception","errorClass":"FileSystemException","httpStatus":null,"severity":"error","filePath":"vertx-core/src/main/java/io/vertx/core/file/impl/FileSystemImpl.java","lineNumber":510,"sourceCode":"  }\n\n  private BlockingAction<Void> truncateInternal(String p, long len) {\n    Objects.requireNonNull(p);\n    return new BlockingAction<Void>() {\n      public Void perform() {\n        try {\n          String path = resolveFile(p).getAbsolutePath();\n          if (len < 0) {\n            throw new FileSystemException(\"Cannot truncate file to size < 0\");\n          }\n          if (!Files.exists(Paths.get(path))) {\n            throw new FileSystemException(\"Cannot truncate file \" + path + \". Does not exist\");\n          }\n          try (RandomAccessFile raf = new RandomAccessFile(path, \"rw\")) {\n            raf.setLength(len);\n          }\n        } catch (IOException e) {\n          throw new FileSystemException(getFileAccessErrorMessage(\"truncate\", p) ,e);\n        }\n        return null;\n      }\n    };\n  }\n\n  private BlockingAction<Void> chmodInternal(String path, String perms) {\n    return chmodInternal(path, perms, null);\n  }\n\n  protected BlockingAction<Void> chmodInternal(String path, String perms, String dirPerms) {\n    Objects.requireNonNull(path);\n    Set<PosixFilePermission> permissions = PosixFilePermissions.fromString(perms);\n    Set<PosixFilePermission> dirPermissions = dirPerms == null ? null : PosixFilePermissions.fromString(dirPerms);\n    return new BlockingAction<Void>() {\n      public Void perform() {\n        try {\n          Path target = resolveFile(path).toPath();","sourceCodeStart":492,"sourceCodeEnd":528,"githubUrl":"https://github.com/eclipse-vertx/vert.x/blob/fb308bd8c3f12c79f4ae89bef67fadf6c80d036e/vertx-core/src/main/java/io/vertx/core/file/impl/FileSystemImpl.java#L492-L528","documentation":"The truncate action wraps any IOException from RandomAccessFile.setLength (or opening the file 'rw') into FileSystemException 'Failed to truncate <path>'. Vert.x throws this when the file exists and the size is valid but the OS-level truncation fails. The cause IOException carries the OS reason (permissions, busy, disk issues).","triggerScenarios":"vertx.fileSystem().truncate(path, len) where opening RandomAccessFile(path, \"rw\") fails due to missing write permission, the file being locked by another process, or setLength failing on a full/odd filesystem.","commonSituations":"Truncating a file owned by another user; on Windows, a file held open by a reader or antivirus; truncating a file on a read-only remount; truncating special/immutable files; SELinux/AppArmor denials.","solutions":["Read the cause: AccessDeniedException -> chmod/chown the file or run the process with sufficient privileges.","On Windows, close all handles reading the file before truncating (including log tails and antivirus scans).","Verify the filesystem is writable (mount options, disk not full, immutable attribute chattr -i).","Fall back to rewrite: read the desired prefix and write it back via fs.write if setLength keeps failing."],"exampleFix":"// before\nvertx.fileSystem().truncateBlocking(\"data/store.db\", 1024); // AccessDeniedException\n// after: ensure writable, then retry\nFile f = new File(\"data/store.db\");\nf.setWritable(true);\nFiles.setPosixFilePermissions(f.toPath(), PosixFilePermissions.fromString(\"rw-rw-r--\"));\nvertx.fileSystem().truncateBlocking(\"data/store.db\", 1024);","handlingStrategy":"try-catch","validationCode":"Path p = Paths.get(path);\nif (!Files.exists(p)) throw new FileNotFoundException(path);\nif (!Files.isWritable(p)) throw new AccessDeniedException(path);\nif ((Files.getPosixFilePermissions(p).contains(PosixFilePermission.OWNER_WRITE)) == false)\n  throw new AccessDeniedException(path);","typeGuard":"static boolean truncatable(Path p) {\n  return Files.isRegularFile(p) && Files.isWritable(p);\n}","tryCatchPattern":"try {\n  fs.truncateBlocking(path, len);\n} catch (FileSystemException e) {\n  Throwable c = e.getCause();\n  if (c instanceof AccessDeniedException) {\n    throw new SecurityException(\"No write permission on \" + path, c);\n  } else if (c instanceof IOException) {\n    // rewrite fallback: read prefix, write back\n  } else throw e;\n}","preventionTips":["Verify write permission (and ownership) on files you intend to truncate","On Windows, release file handles and pause AV/indexer scanning for hot files","Monitor mount status: read-only remounts and full disks break setLength","Prefer rewrite-through-Vert.x writes as a portable fallback"],"tags":["file-system","truncate","io","permissions"],"backgroundTag":"file-write-failed","analyzedSha":"fb308bd8c3f12c79f4ae89bef67fadf6c80d036e","analyzedAt":"2026-09-06T11:37:12.241Z","contentChangedAt":"2026-09-06T11:37:12.241Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}