eclipse-vertx/vert.x · error · FileSystemException

Failed to truncate ${p}

Error message

Failed to truncate ${p}

What it means

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).

Source

Thrown at vertx-core/src/main/java/io/vertx/core/file/impl/FileSystemImpl.java:510

  }

  private BlockingAction<Void> truncateInternal(String p, long len) {
    Objects.requireNonNull(p);
    return new BlockingAction<Void>() {
      public Void perform() {
        try {
          String path = resolveFile(p).getAbsolutePath();
          if (len < 0) {
            throw new FileSystemException("Cannot truncate file to size < 0");
          }
          if (!Files.exists(Paths.get(path))) {
            throw new FileSystemException("Cannot truncate file " + path + ". Does not exist");
          }
          try (RandomAccessFile raf = new RandomAccessFile(path, "rw")) {
            raf.setLength(len);
          }
        } catch (IOException e) {
          throw new FileSystemException(getFileAccessErrorMessage("truncate", p) ,e);
        }
        return null;
      }
    };
  }

  private BlockingAction<Void> chmodInternal(String path, String perms) {
    return chmodInternal(path, perms, null);
  }

  protected BlockingAction<Void> chmodInternal(String path, String perms, String dirPerms) {
    Objects.requireNonNull(path);
    Set<PosixFilePermission> permissions = PosixFilePermissions.fromString(perms);
    Set<PosixFilePermission> dirPermissions = dirPerms == null ? null : PosixFilePermissions.fromString(dirPerms);
    return new BlockingAction<Void>() {
      public Void perform() {
        try {
          Path target = resolveFile(path).toPath();

View on GitHub (pinned to fb308bd8c3)

Solutions

  1. Read the cause: AccessDeniedException -> chmod/chown the file or run the process with sufficient privileges.
  2. On Windows, close all handles reading the file before truncating (including log tails and antivirus scans).
  3. Verify the filesystem is writable (mount options, disk not full, immutable attribute chattr -i).
  4. Fall back to rewrite: read the desired prefix and write it back via fs.write if setLength keeps failing.

Example fix

// before
vertx.fileSystem().truncateBlocking("data/store.db", 1024); // AccessDeniedException
// after: ensure writable, then retry
File f = new File("data/store.db");
f.setWritable(true);
Files.setPosixFilePermissions(f.toPath(), PosixFilePermissions.fromString("rw-rw-r--"));
vertx.fileSystem().truncateBlocking("data/store.db", 1024);
Defensive patterns

Strategy: try-catch

Validate before calling

Path p = Paths.get(path);
if (!Files.exists(p)) throw new FileNotFoundException(path);
if (!Files.isWritable(p)) throw new AccessDeniedException(path);
if ((Files.getPosixFilePermissions(p).contains(PosixFilePermission.OWNER_WRITE)) == false)
  throw new AccessDeniedException(path);

Type guard

static boolean truncatable(Path p) {
  return Files.isRegularFile(p) && Files.isWritable(p);
}

Try / catch

try {
  fs.truncateBlocking(path, len);
} catch (FileSystemException e) {
  Throwable c = e.getCause();
  if (c instanceof AccessDeniedException) {
    throw new SecurityException("No write permission on " + path, c);
  } else if (c instanceof IOException) {
    // rewrite fallback: read prefix, write back
  } else throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "failed to write file", "Could not save figure", "Error saving remote file" — file write failed: causes and fixes across languages and libraries — this error's family across 38 libraries.

Related errors


AI-assisted analysis of eclipse-vertx/vert.x@fb308bd8c3 (2026-09-06). Data as JSON: /api/errors/23ec0fa1015011fe. Report an issue: GitHub.