Kontinuation commented on code in PR #681: URL: https://github.com/apache/sedona-db/pull/681#discussion_r2881839837
########## c/sedona-gdal/src/gdal_api.rs: ########## @@ -0,0 +1,101 @@ +// 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. + +use std::ffi::CStr; +use std::path::PathBuf; + +use libloading::Library; + +use crate::dyn_load; +use crate::errors::{GdalError, GdalInitLibraryError}; +use crate::gdal_dyn_bindgen::SedonaGdalApi; + +/// Invoke a function pointer from the `SedonaGdalApi` struct. +/// +/// # Panics +/// +/// Panics if the function pointer is `None`. This is unreachable in correct usage +/// because all function pointers are guaranteed to be `Some` after successful +/// `sedona_gdal_dyn_api_init` (the C loader fails if any symbol is missing), +/// and you cannot obtain a `&GdalApi` without successful initialization +/// (it's behind `OnceLock`). +macro_rules! call_gdal_api { + ($api:expr, $func:ident $(, $arg:expr)*) => { + if let Some(func) = $api.inner.$func { + func($($arg),*) + } else { + panic!("{} function not available", stringify!($func)) + } + }; +} + +// Re-export for use in other modules in this crate +#[allow(unused_imports)] +pub(crate) use call_gdal_api; + +#[derive(Debug)] +pub struct GdalApi { + pub(crate) inner: SedonaGdalApi, + /// The dynamically loaded GDAL library. Kept alive for the lifetime of the + /// function pointers in `inner`. This is never dropped because the `GdalApi` + /// lives in a `static OnceLock` (see `register.rs`). + _lib: Library, + name: String, +} + +impl GdalApi { + pub fn try_from_shared_library(shared_library: PathBuf) -> Result<Self, GdalInitLibraryError> { + let (lib, inner) = dyn_load::load_gdal_from_path(&shared_library)?; + Ok(Self { + inner, + _lib: lib, + name: shared_library.to_string_lossy().into_owned(), + }) + } + + pub fn try_from_current_process() -> Result<Self, GdalInitLibraryError> { + let (lib, inner) = dyn_load::load_gdal_from_current_process()?; + Ok(Self { + inner, + _lib: lib, + name: "current_process".to_string(), + }) + } + + pub fn name(&self) -> &str { + &self.name + } + + /// Check the last CPL error and return a `GdalError` if there was one. + pub fn last_cpl_err(&self, default_err_class: u32) -> GdalError { + let err_no = unsafe { call_gdal_api!(self, CPLGetLastErrorNo) }; + let err_msg = unsafe { + let msg_ptr = call_gdal_api!(self, CPLGetLastErrorMsg); + if msg_ptr.is_null() { + String::new() + } else { + CStr::from_ptr(msg_ptr).to_string_lossy().into_owned() + } + }; + unsafe { call_gdal_api!(self, CPLErrorReset) }; + GdalError::CplError { + class: default_err_class, + number: err_no, + msg: err_msg, + } Review Comment: Clarified that it will always return an error, even if the error number is 0. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
