Skip to content

bug: fail to read files with trailing whitespaces #6577

@christer-eriksson

Description

@christer-eriksson

Describe the bug

When using the read() method in opendal 0.54.0

opendal fail to read the content from the file

Steps to Reproduce

Create a file named "file with trailing white space " with some content in it

Use the read method to read the file content. It will fail with Error.

Expected Behavior

I expect to be able to read the content of a file even if the file name ends with a white space

Additional Context

The calls in this snippet shows that the normalize_path in deed trims the string from any leading or trailing white space.

pub async fn read(&self, path: &str) -> Result<Buffer> {
      self.read_options(path, options::ReadOptions::default())
          .await
  }

 pub async fn read_options(&self, path: &str, opts: options::ReadOptions) -> Result<Buffer> {
      let path = normalize_path(path);
      Self::read_inner(self.inner().clone(), path, opts).await
  }
pub fn normalize_path(path: &str) -> String {
  // - all whitespace has been trimmed.
  // - all leading `/` has been trimmed.
  let path = path.trim().trim_start_matches('/');

  // Fast line for empty path.
  if path.is_empty() {
      return "/".to_string();
  }

  let has_trailing = path.ends_with('/');

  let mut p = path
      .split('/')
      .filter(|v| !v.is_empty())
      .collect::<Vec<&str>>()
      .join("/");

  // Append trailing back if input path is endswith `/`.
  if has_trailing {
      p.push('/');
  }

  p
}

Are you willing to submit a PR to fix this bug?

  • Yes, I would like to submit a PR.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingreleases-note/fixThe PR fixes a bug or has a title that begins with "fix"services/fs

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions