Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement read_dir function and test #257

Merged
merged 5 commits into from May 13, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 21 additions & 9 deletions src/vsi.rs
Expand Up @@ -8,11 +8,11 @@ use crate::errors::{GdalError, Result};
use crate::utils::{_last_null_pointer_err, _path_to_c_string, _string_array};

/// Read the file names from a virtual file system with optional recursion.
pub fn read_dir<P: AsRef<Path>>(path: P, recursive: bool) -> Result<Vec<String>> {
pub fn read_dir<P: AsRef<Path>>(path: P, recursive: bool) -> Result<Vec<PathBuf>> {
_read_dir(path.as_ref(), recursive)
}

fn _read_dir(path: &Path, recursive: bool) -> Result<Vec<String>> {
fn _read_dir(path: &Path, recursive: bool) -> Result<Vec<PathBuf>> {
let path = _path_to_c_string(path)?;
let data = if recursive {
let data = unsafe { gdal_sys::VSIReadDirRecursive(path.as_ptr()) };
Expand All @@ -28,7 +28,14 @@ fn _read_dir(path: &Path, recursive: bool) -> Result<Vec<String>> {
data
};

Ok(_string_array(data))
let strings = _string_array(data);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can do it in a later PR, but you could add _pathbuf and _pathbuf_array functions to avoid the reallocation here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's getting late here. Let me do that tomorrow.

let mut paths = Vec::new();
paths.reserve(strings.len());
for string in strings {
paths.push(string.into());
}

Ok(paths)
}

/// Creates a new VSIMemFile from a given buffer.
Expand Down Expand Up @@ -317,17 +324,22 @@ mod tests {
let path = ["/vsizip/", zip_path.to_str().unwrap()].concat();

// Read without recursion.
let expected = ["folder", "File 1.txt", "File 2.txt", "File 3.txt"];
let expected = [
Path::new("folder"),
Path::new("File 1.txt"),
Path::new("File 2.txt"),
Path::new("File 3.txt"),
];
let files = read_dir(path.as_str(), false).unwrap();
assert_eq!(files, expected);

// Read with recursion.
let expected = [
"folder/",
"folder/File 4.txt",
"File 1.txt",
"File 2.txt",
"File 3.txt",
Path::new("folder/"),
Path::new("folder/File 4.txt"),
Path::new("File 1.txt"),
Path::new("File 2.txt"),
Path::new("File 3.txt"),
];
let files = read_dir(path.as_str(), true).unwrap();
assert_eq!(files, expected);
Expand Down