Skip to content

Commit

Permalink
Try #257:
Browse files Browse the repository at this point in the history
  • Loading branch information
bors[bot] committed Apr 12, 2022
2 parents 492e0c8 + 3e16206 commit fc452c2
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGES.md
Expand Up @@ -15,6 +15,9 @@

- https://github.com/georust/gdal/pull/238

- 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.
58 changes: 57 additions & 1 deletion src/vsi.rs
Expand Up @@ -5,7 +5,29 @@ 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>> {
let path = _path_to_c_string(path.as_ref())?;
let data = unsafe {
if recursive {
let data = gdal_sys::VSIReadDirRecursive(path.as_ptr());
if data.is_null() {
return Err(_last_null_pointer_err("VSIReadDirRecursive"));
}
data
} else {
let data = 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 +299,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 fc452c2

Please sign in to comment.