Skip to content

Commit

Permalink
vaidate_name: Clarify some error messages
Browse files Browse the repository at this point in the history
Return early if the name is just empty.

Simplify the messaging where the name can be interpreted as a directory
path.

Signed-off-by: mulhern <[email protected]>
  • Loading branch information
mulkieran committed Mar 19, 2024
1 parent feac03e commit 9ef2fb2
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/engine/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,12 @@ pub fn set_key_shared(key_fd: RawFd, memory: &mut [u8]) -> StratisResult<usize>

/// Validate a str for use as a Pool or Filesystem name.
pub fn validate_name(name: &str) -> StratisResult<()> {
if name.is_empty() {
return Err(StratisError::Msg(format!(
"Provided string is empty: {name}"
)));
}

if name.contains('\u{0}') {
return Err(StratisError::Msg(format!(
"Provided string contains NULL characters: {name}"
Expand Down Expand Up @@ -174,14 +180,9 @@ pub fn validate_name(name: &str) -> StratisResult<()> {
}

let name_path = Path::new(name);
if name_path.components().count() != 1 {
return Err(StratisError::Msg(format!(
"Provided string is a path with 0 or more than 1 components: {name}"
)));
}
if name_path.is_absolute() {
if name_path.components().count() > 1 || name_path.is_absolute() {
return Err(StratisError::Msg(format!(
"Provided string is an absolute path: {name}"
"Provided string is a directory path: {name}"
)));
}
Ok(())
Expand Down

0 comments on commit 9ef2fb2

Please sign in to comment.