apache/hadoop · error · FileAlreadyExistsException

Can't make directory for path '%s' since it is a file.

Error message

Can't make directory for path '%s' since it is a file.

What it means

The single-level mkdir(f) helper throws FileAlreadyExistsException when f already exists as a file. If f exists as a directory the call succeeds; if f is missing it writes a 0-byte 'directory marker' object (key suffixed with '/') into COS. This marker mechanism is exactly why a stray file at a directory key later blocks mkdirs as 'it is a file'.

Source

Thrown at hadoop-cloud-storage-project/hadoop-cos/src/main/java/org/apache/hadoop/fs/cosn/CosNFileSystem.java:569

        }
      } catch (FileNotFoundException e) {
        LOG.debug("Making dir: [{}] in COS", f);

        String folderPath = pathToKey(makeAbsolute(f));
        if (!folderPath.endsWith(PATH_DELIMITER)) {
          folderPath += PATH_DELIMITER;
        }
        store.storeEmptyFile(folderPath);
      }
    }
    return true;
  }

  private boolean mkdir(Path f) throws IOException {
    try {
      FileStatus fileStatus = getFileStatus(f);
      if (fileStatus.isFile()) {
        throw new FileAlreadyExistsException(
            String.format(
                "Can't make directory for path '%s' since it is a file.", f));
      }
    } catch (FileNotFoundException e) {
      if (LOG.isDebugEnabled()) {
        LOG.debug("Make directory: [{}] in COS.", f);
      }

      String folderPath = pathToKey(makeAbsolute(f));
      if (!folderPath.endsWith(PATH_DELIMITER)) {
        folderPath += PATH_DELIMITER;
      }
      store.storeEmptyFile(folderPath);
    }
    return true;
  }

  @Override

View on GitHub (pinned to 2add963021)

Solutions

  1. Delete or rename the file occupying the path, then retry mkdir.
  2. Pick a different directory name that cannot collide with existing object keys.
  3. Check getFileStatus(f).isFile() before calling mkdir in shared utilities.

Example fix

// before
if (!fs.exists(dirPath)) { fs.mkdirs(dirPath); } // still throws if dirPath is a FILE

// after
if (!fs.exists(dirPath)) {
  fs.mkdirs(dirPath);
} else if (fs.getFileStatus(dirPath).isFile()) {
  fs.delete(dirPath, false);
  fs.mkdirs(dirPath);
}
Defensive patterns

Strategy: validation

Validate before calling

if (fs.exists(f) && fs.getFileStatus(f).isFile()) {
  // mkdir would throw: delete, rename, or fail fast here
  throw new IllegalStateException('Path occupied by a file: ' + f);
}

Try / catch

try {
  fs.mkdirs(f);
} catch (FileAlreadyExistsException e) {
  if (fs.getFileStatus(f).isFile()) { fs.delete(f, false); fs.mkdirs(f); } else { throw e; }
}

Prevention

When it happens

Trigger: mkdir on a path occupied by an existing file object; creating a directory whose key equals an existing object key (no trailing slash).

Common situations: Layout migrations where a file key is promoted to a directory key; tools that assume mkdir on an existing key is always a no-op.

Related errors


AI-assisted analysis of apache/hadoop@2add963021 (2026-08-22). Data as JSON: /api/errors/c4cec18b977e0558. Report an issue: GitHub.