Repository: rust
Issue: [discussion] `ErrorKind::InvalidFilename` from `io_error_more`
Labels: T-libs-api,proposed-final-comment-period,disposition-merge,C-discussion

@rustbot label C-discussion

Main tracking issue: #86442

## Background

The [`io_error_more` feature](https://github.com/rust-lang/rust/issues/86442) introduced 21 new variants into [`ErrorKind`](https://doc.rust-lang.org/nightly/std/io/enum.ErrorKind.html). They were [FCP'd back in December 2022](https://github.com/rust-lang/rust/issues/86442#issuecomment-1368082102), but there appeared to be quite a lot of disagreement about 4 of the added variants, so the stabilization (#106375) got stalled for over twenty months. Thankfully, the 17 uncontroversial variants got stabilized in #128316, so now we just need to iron out a satisfactory design for the remaining 4 variants, and then they can be stabilized too.

In order to not block any of the remaining variants on each other and to not intertwine the discussions, I've created 4 separate issues, which summarize the concerns & suggestions voiced up until this point and can serve as a place for further discussion.

-  `FilesystemLoop`:  #130188
-  `FilesystemQuotaExceeded`: #130190
-  `CrossesDevices`: #130191
-  `InvalidFilename`: you are here

# `InvalidFilename`

Currently corresponds to `ENAMETOOLONG` on Unix, `ERROR_FILENAME_EXCED_RANGE` & `ERROR_INVALID_NAME` on Windows. (https://github.com/rust-lang/rust/issues/86442#issuecomment-1235763183)

Current docs description:

> A filename was invalid.
> 
> This error can also cause if it exceeded the filename length limit.

## Docs

I'm not sure "_This error can also cause if it exceeded the filename length limit._" is well-formed coherent English, but I'm not a native speaker.

## Name capitalization

> std uses `file_name` in functions, rather than `filename`. It'd be odd to break correspondence between snake_case and CamelCase, so I think this should be `FileNameTooLong` to match. Same goes for `InvalidFileName`.

_Originally posted by Kornel in https://github.com/rust-lang/rust/pull/79965#discussion_r1059215891_

## Separate "the name is too long" case

> I might be a little too late, but I'm not sure combining `ERROR_FILENAME_EXCED_RANGE` and `ERROR_INVALID_NAME` into `InvalidFilename` is a good idea. What was the reason behind this decision?
> 
> For example if one is writing an unarchiver to extract files, if a filename is too long one could simply truncate the filename and tell the user this. But if the filename is invalid/reserved (eg. COM) then the name would have to be completely different. 
> 
> An app could guess which of the two it is, but it would be more straightforward if the two are distinguished from the start.

_Originally posted by cktkw in https://github.com/rust-lang/rust/issues/86442#issuecomment-1441480697_

There is a long reply from Aritz Beobide-Cardinal in https://github.com/rust-lang/rust/issues/106375#issuecomment-1959633299 arguing against the separation.
            
## Map more raw os errors to this `ErrorKind`

> I was speculating there might be an OS that returns "Invalid Filename" when the filename is too long.
In which case, this decision will make sense.
But otherwise, "Filename Too Long" is one of the few errors that seem to be consistent among platforms.
> 
> With current PR, Unix doesn't have `InvalidFilename` mapped to anything else (at least from my quick skim through `sys/unix/mod.rs`).
> However, UNIX-like OS fails with varying error when invalid UTF-8 filename is used to `open()` in C.
> Mac APFS fails with `EILSEQ` (Illegal byte sequence).
> Linux EXT4 (formatted with strict case-insensitive option `-O casefold -E encoding_flags=strict`, because otherwise any byte sequence is allowed) fails with `EINVAL`(invalid argument).
> 
> In theory, I think these should be mapped to `InvalidFilename` in the context of `File::open()`, `File::create()`, etc. But I understand that that would need major rewrite.
I don't know enough to say what would be the best way, but current use of `InvalidFilename` doesn't feel optimal.

_Originally posted by cktkw in https://github.com/rust-lang/rust/issues/86442#issuecomment-1441709738_