Skip to content

Commit

Permalink
Merge #257
Browse files Browse the repository at this point in the history
257: Implement read_dir function and test r=lnicola a=Barugon

Implement a `gdal::vsi::read_dir` function and test.

- [x] I agree to follow the project's [code of conduct](https://github.com/georust/gdal/blob/master/CODE_OF_CONDUCT.md).
- [x] I added an entry to `CHANGES.md` if knowledge of this change could be valuable to users.
---



Co-authored-by: Barugon <barugon@dungeonbox.net>
  • Loading branch information
bors[bot] and Barugon committed May 13, 2022
2 parents d8aa19a + 359dc10 commit e482405
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGES.md
Expand Up @@ -23,6 +23,10 @@

- <https://github.com/georust/gdal/pull/271>

- Add `gdal::vsi::read_dir` function.

- <https://github.com/georust/gdal/pull/257>

## 0.12

- Bump Rust edition to 2021
Expand Down
Binary file added fixtures/test_vsi_read_dir.zip
Binary file not shown.
60 changes: 59 additions & 1 deletion src/vsi.rs
Expand Up @@ -5,7 +5,31 @@ use std::path::{Path, PathBuf};
use gdal_sys::{VSIFCloseL, VSIFileFromMemBuffer, VSIFree, VSIGetMemFileBuffer, VSIUnlink};

use crate::errors::{GdalError, Result};
use crate::utils::{_last_null_pointer_err, _path_to_c_string};
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>> {
_read_dir(path.as_ref(), recursive)
}

fn _read_dir(path: &Path, recursive: bool) -> Result<Vec<String>> {
let path = _path_to_c_string(path)?;
let data = if recursive {
let data = unsafe { gdal_sys::VSIReadDirRecursive(path.as_ptr()) };
if data.is_null() {
return Err(_last_null_pointer_err("VSIReadDirRecursive"));
}
data
} else {
let data = unsafe { gdal_sys::VSIReadDir(path.as_ptr()) };
if data.is_null() {
return Err(_last_null_pointer_err("VSIReadDir"));
}
data
};

Ok(_string_array(data))
}

/// Creates a new VSIMemFile from a given buffer.
pub fn create_mem_file<P: AsRef<Path>>(file_name: P, data: Vec<u8>) -> Result<()> {
Expand Down Expand Up @@ -277,4 +301,38 @@ mod tests {
})
);
}

#[test]
fn test_vsi_read_dir() {
use std::path::Path;
let zip_path = Path::new(file!())
.parent()
.unwrap()
.parent()
.unwrap()
.join("fixtures")
.join("test_vsi_read_dir.zip");

// Concatenate "/vsizip/" prefix.
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 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",
];
let files = read_dir(path.as_str(), true).unwrap();
assert_eq!(files, expected);

// Attempting to read without VSI prefix returns error.
assert!(read_dir(zip_path, false).is_err());
}
}

0 comments on commit e482405

Please sign in to comment.