Function validate_path_length

Source
pub fn validate_path_length(path: &Path) -> Result<Cow<'_, Path>>
Expand description

Validate the path, returning the valid path, a modified-but-now-valid path, or bailing with an error.

The behaviour of the file system changes depending on the OS, and indeed sometimes the FS implementation of the OS itself.

  • On Windows the limit for normal file paths is 260 characters, a holdover from the DOS days, but Rust will opportunistically rewrite paths to ‘UNC’ paths for supported path operations which can be up to 32767 characters long.
  • On macOS, the limit is traditionally 255 characters for the file name and a second limit of 1024 for the entire path (verified by running getconf PATH_MAX /).
  • On Linux, the limit differs between kernel (and by extension, distro) and filesystem. On most common file systems (e.g. ext4, btrfs, and xfs), individual file names can be up to 255 bytes with no hard limit on total path length. Some legacy POSIX APIs are restricted to the PATH_MAX value of 4096 bytes in limits.h, but most applications support longer paths.

For more details, refer to https://en.wikipedia.org/wiki/Comparison_of_file_systems#Limits.

Realistically, the output path lengths will be the same across all platforms, so we need to set a conservative limit and be particular about when we decide to bump it. Here we have opted for 255 characters, because it is the shortest of the three options.