Skip to main content

validate_component

Function validate_component 

Source
pub fn validate_component(name: &str) -> Result<(), PathSafetyError>
Expand description

Validate that name is a safe, portable single path component: a non-empty plain file or directory name that every supported platform accepts and that cannot leave its directory.

Rejected, in order of checking:

  • the empty string
  • separators / and \
  • . and ..
  • characters Windows disallows in names (: ? * " > < |) and control characters (C0, C1, DEL — these also enable terminal-escape spoofing when a path is echoed)
  • Windows reserved device names (CON, NUL, COM1, …), with or without an extension
  • trailing . or space (Windows strips them, silently aliasing names)
  • names longer than 255 bytes (NAME_MAX)
  • anything Path::components does not parse as exactly one normal component on the host platform (e.g. a bare drive prefix)

§Errors

Returns a PathSafetyError describing the first rule the name violates.