-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathvsi.rs
More file actions
379 lines (319 loc) · 11 KB
/
Copy pathvsi.rs
File metadata and controls
379 lines (319 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//! Ported (and contains copied code) from georust/gdal:
//! <https://github.com/georust/gdal/blob/v0.19.0/src/vsi.rs>.
//! Original code is licensed under MIT.
//!
//! GDAL Virtual File System (VSI) wrappers.
use std::ffi::CString;
use std::ops::Deref;
use crate::errors::{GdalError, Result};
use crate::gdal_api::{call_gdal_api, GdalApi};
/// An owned GDAL-allocated VSI memory buffer.
pub struct VSIBuffer {
api: &'static GdalApi,
ptr: *mut u8,
len: usize,
}
// SAFETY: `VsiBuffer` uniquely owns the GDAL-allocated buffer it wraps. Ownership may
// move across threads, and the buffer is released exactly once on drop using GDAL's
// allocator.
unsafe impl Send for VSIBuffer {}
// SAFETY: `VsiBuffer` exposes only shared read-only slice access to an immutable
// GDAL-owned byte buffer. Concurrent reads are therefore safe, and the buffer is
// still released exactly once on drop using GDAL's allocator.
unsafe impl Sync for VSIBuffer {}
impl VSIBuffer {
pub fn len(&self) -> usize {
self.len
}
pub fn is_empty(&self) -> bool {
self.len == 0
}
}
impl AsRef<[u8]> for VSIBuffer {
fn as_ref(&self) -> &[u8] {
if self.len == 0 {
&[]
} else {
unsafe { std::slice::from_raw_parts(self.ptr, self.len) }
}
}
}
impl Deref for VSIBuffer {
type Target = [u8];
fn deref(&self) -> &Self::Target {
self.as_ref()
}
}
impl Drop for VSIBuffer {
fn drop(&mut self) {
if !self.ptr.is_null() {
unsafe { call_gdal_api!(self.api, VSIFree, self.ptr.cast::<std::ffi::c_void>()) };
}
}
}
/// Creates a new VSI in-memory file from a given buffer.
///
/// The data is copied into GDAL-allocated memory (via `VSIMalloc`) so that
/// GDAL can safely free it with `VSIFree` when ownership is taken, without
/// crossing allocator boundaries back into Rust.
pub fn create_mem_file(api: &'static GdalApi, file_name: &str, data: &[u8]) -> Result<()> {
let c_file_name = CString::new(file_name)?;
let len = data.len();
let len_i64 = i64::try_from(len)?;
let gdal_buf = if len == 0 {
std::ptr::null_mut()
} else {
// Allocate via GDAL's allocator so GDAL can safely free it.
let gdal_buf = unsafe { call_gdal_api!(api, VSIMalloc, len) } as *mut u8;
if gdal_buf.is_null() {
return Err(api.last_null_pointer_err("VSIMalloc"));
}
// Copy data into GDAL-allocated buffer.
unsafe {
std::ptr::copy_nonoverlapping(data.as_ptr(), gdal_buf, len);
}
gdal_buf
};
let handle = unsafe {
call_gdal_api!(
api,
VSIFileFromMemBuffer,
c_file_name.as_ptr(),
gdal_buf,
len_i64,
1 // bTakeOwnership = true — GDAL will VSIFree gdal_buf
)
};
if handle.is_null() {
// GDAL did not take ownership, so we must free.
if !gdal_buf.is_null() {
unsafe { call_gdal_api!(api, VSIFree, gdal_buf as *mut std::ffi::c_void) };
}
return Err(api.last_null_pointer_err("VSIFileFromMemBuffer"));
}
unsafe {
call_gdal_api!(api, VSIFCloseL, handle);
}
Ok(())
}
/// Unlink (delete) a VSI in-memory file.
pub fn unlink_mem_file(api: &'static GdalApi, file_name: &str) -> Result<()> {
let c_file_name = CString::new(file_name)?;
let rv = unsafe { call_gdal_api!(api, VSIUnlink, c_file_name.as_ptr()) };
if rv != 0 {
return Err(GdalError::UnlinkMemFile {
file_name: file_name.to_string(),
});
}
Ok(())
}
/// Returns an owned GDAL-allocated buffer containing the bytes of the VSI in-memory
/// file, taking ownership and freeing the GDAL memory on drop.
pub fn get_vsi_mem_file_buffer_owned(api: &'static GdalApi, file_name: &str) -> Result<VSIBuffer> {
let c_file_name = CString::new(file_name)?;
let mut length: i64 = 0;
let bytes = unsafe {
call_gdal_api!(
api,
VSIGetMemFileBuffer,
c_file_name.as_ptr(),
&mut length,
1 // bUnlinkAndSeize = true
)
};
if length < 0 {
if !bytes.is_null() {
unsafe { call_gdal_api!(api, VSIFree, bytes.cast::<std::ffi::c_void>()) };
}
return Err(GdalError::BadArgument(format!(
"VSIGetMemFileBuffer returned negative length: {length}"
)));
}
if bytes.is_null() {
if length == 0 {
return Ok(VSIBuffer {
api,
ptr: std::ptr::null_mut(),
len: 0,
});
}
return Err(api.last_null_pointer_err("VSIGetMemFileBuffer"));
}
let len = usize::try_from(length)?;
Ok(VSIBuffer {
api,
ptr: bytes.cast::<u8>(),
len,
})
}
/// Copies the bytes of the VSI in-memory file, taking ownership and freeing the GDAL memory.
pub fn get_vsi_mem_file_bytes_owned(api: &'static GdalApi, file_name: &str) -> Result<Vec<u8>> {
let buffer = get_vsi_mem_file_buffer_owned(api, file_name)?;
Ok(buffer.as_ref().to_vec())
}
#[cfg(all(test, feature = "gdal-sys"))]
mod tests {
use super::*;
use crate::global::with_global_gdal_api;
#[test]
fn create_and_retrieve_mem_file() {
let file_name = "/vsimem/525ebf24-a030-4677-bb4e-a921741cabe0";
with_global_gdal_api(|api| {
create_mem_file(api, file_name, &[1_u8, 2, 3, 4]).unwrap();
let bytes = get_vsi_mem_file_bytes_owned(api, file_name).unwrap();
assert_eq!(bytes, vec![1_u8, 2, 3, 4]);
// mem file must not be there anymore
assert!(matches!(
unlink_mem_file(api, file_name).unwrap_err(),
GdalError::UnlinkMemFile {
file_name: err_file_name
}
if err_file_name == file_name
));
})
.unwrap();
}
#[test]
fn create_and_retrieve_mem_file_buffer() {
let file_name = "/vsimem/2e3c48a5-d2ef-4f5c-896d-5467cdca9406";
with_global_gdal_api(|api| {
create_mem_file(api, file_name, &[1_u8, 2, 3, 4]).unwrap();
let buffer = get_vsi_mem_file_buffer_owned(api, file_name).unwrap();
assert_eq!(buffer.len(), 4);
assert_eq!(buffer.as_ref(), &[1_u8, 2, 3, 4]);
})
.unwrap();
}
#[test]
fn create_and_unlink_mem_file() {
let file_name = "/vsimem/bbf5f1d6-c1e9-4469-a33b-02cd9173132d";
with_global_gdal_api(|api| {
create_mem_file(api, file_name, &[1_u8, 2, 3, 4]).unwrap();
unlink_mem_file(api, file_name).unwrap();
})
.unwrap();
}
#[test]
fn create_and_retrieve_empty_mem_file() {
let file_name = "/vsimem/3f9e6282-313d-4c51-81ab-f020ff2134d8";
with_global_gdal_api(|api| {
create_mem_file(api, file_name, &[]).unwrap();
let bytes = get_vsi_mem_file_bytes_owned(api, file_name).unwrap();
assert!(bytes.is_empty());
})
.unwrap();
}
#[test]
fn create_and_retrieve_empty_mem_file_buffer() {
let file_name = "/vsimem/17319db4-775b-4380-a9af-802c160dcb24";
with_global_gdal_api(|api| {
create_mem_file(api, file_name, &[]).unwrap();
let buffer = get_vsi_mem_file_buffer_owned(api, file_name).unwrap();
assert!(buffer.is_empty());
assert_eq!(buffer.as_ref(), b"");
})
.unwrap();
}
#[test]
fn no_mem_file() {
with_global_gdal_api(|api| {
let bytes = get_vsi_mem_file_bytes_owned(api, "foobar").unwrap();
assert!(bytes.is_empty());
})
.unwrap();
}
}
pub struct VsiDirEntry {
pub name: String,
pub mode: Option<i32>,
pub size: Option<crate::gdal_dyn_bindgen::vsi_l_offset>,
pub mtime: Option<crate::gdal_dyn_bindgen::GIntBig>,
}
pub struct VsiDir {
api: &'static crate::gdal_api::GdalApi,
handle: *mut crate::gdal_dyn_bindgen::VSIDIR,
}
impl VsiDir {
pub fn next_entry(&mut self) -> Option<VsiDirEntry> {
let entry = unsafe { (self.api.inner.VSIGetNextDirEntry?)(self.handle) };
if entry.is_null() {
return None;
}
let entry = unsafe { &*entry };
let name = if entry.pszName.is_null() {
String::new()
} else {
unsafe { std::ffi::CStr::from_ptr(entry.pszName) }
.to_string_lossy()
.into_owned()
};
Some(VsiDirEntry {
name,
mode: (entry.bModeKnown != 0).then_some(entry.nMode),
size: (entry.bSizeKnown != 0).then_some(entry.nSize),
mtime: (entry.bMTimeKnown != 0).then_some(entry.nMTime),
})
}
}
impl Iterator for VsiDir {
type Item = VsiDirEntry;
fn next(&mut self) -> Option<Self::Item> {
self.next_entry()
}
}
impl Drop for VsiDir {
fn drop(&mut self) {
if !self.handle.is_null() {
if let Some(close) = self.api.inner.VSICloseDir {
unsafe { close(self.handle) };
}
self.handle = std::ptr::null_mut();
}
}
}
pub fn open_dir(
api: &'static crate::gdal_api::GdalApi,
path: &str,
recurse_depth: i32,
options: Option<&crate::cpl::CslStringList>,
) -> crate::errors::Result<VsiDir> {
let c_path = std::ffi::CString::new(path)?;
let options_ptr: *const *const std::os::raw::c_char = options
.map(|opts| opts.as_ptr() as *const *const std::os::raw::c_char)
.unwrap_or(std::ptr::null());
let handle =
unsafe { (api.inner.VSIOpenDir.unwrap())(c_path.as_ptr(), recurse_depth, options_ptr) };
if handle.is_null() {
return Err(api.last_null_pointer_err("VSIOpenDir"));
}
Ok(VsiDir { api, handle })
}
pub fn get_directory_separator(
api: &'static crate::gdal_api::GdalApi,
path: &str,
) -> crate::errors::Result<String> {
let c_path = std::ffi::CString::new(path)?;
let separator_ptr = unsafe { (api.inner.VSIGetDirectorySeparator.unwrap())(c_path.as_ptr()) };
if separator_ptr.is_null() {
return Err(api.last_null_pointer_err("VSIGetDirectorySeparator"));
}
Ok(unsafe { std::ffi::CStr::from_ptr(separator_ptr) }
.to_string_lossy()
.into_owned())
}