Skip to content

Commit

Permalink
mwalib now will detect and raise an error (MwalibError::Fits.CfitsioI…
Browse files Browse the repository at this point in the history
…sNotReentrant) if the CFITSIO mwalib is linked with has been built without the -D_REENTRANT directive. Fixes #82
  • Loading branch information
gsleap committed Nov 8, 2024
1 parent 2188509 commit c1dab5f
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/correlator_context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ impl CorrelatorContext {
metafits_filename: P,
gpubox_filenames: &[P2],
) -> Result<Self, MwalibError> {
// Check CFITSIO is reentrant before proceeding
if !fits_read::is_fitsio_reentrant() {
return Err(MwalibError::Fits(FitsError::CfitsioIsNotReentrant));
}

Self::new_inner(metafits_filename.as_ref(), gpubox_filenames)
}

Expand Down
4 changes: 4 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ use thiserror::Error;
/// MwalibError subtypes
#[derive(Error, Debug)]
pub enum MwalibError {
/// An error derived from `CfitsioError`.
//#[error("{0}")]
//Cfitsio(#[from] crate::fits_read::error::CfitsioError),

/// An error derived from `FitsError`.
#[error("{0}")]
Fits(#[from] crate::fits_read::error::FitsError),
Expand Down
4 changes: 4 additions & 0 deletions src/fits_read/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ use thiserror::Error;
/// FitsError subtypes - mainly used by CorrelatorContext
#[derive(Error, Debug)]
pub enum FitsError {
/// CFITSIO was not compiled with the REENTRANT directive
#[error("mwalib has been compiled with a CFITSIO library which was not built with the -DREENTRANT directive")]
CfitsioIsNotReentrant,

/// Error when opening a fits file.
#[error("{source_file}:{source_line}\nCouldn't open {fits_filename}: {fits_error}")]
Open {
Expand Down
10 changes: 10 additions & 0 deletions src/fits_read/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -975,3 +975,13 @@ pub fn read_cell_array_f64(
}
}
}

/// Returns True if CFITSIO was compiled with the REENTRANT directive
///
/// # Returns
///
/// * boolean value indicating if CFITSIO is compiled with the REENTRANT directive.
///
pub fn is_fitsio_reentrant() -> bool {
unsafe { fitsio_sys::fits_is_reentrant() == 1 }
}
5 changes: 5 additions & 0 deletions src/fits_read/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -993,3 +993,8 @@ fn test_read_cell_array_f64() {
let asdf = read_cell_array_f64(&mut fptr, &hdu, "NotReal", 0, 24);
assert!(asdf.is_err());
}

#[test]
fn test_is_fitsio_reentrant() {
assert!(is_fitsio_reentrant())
}
5 changes: 5 additions & 0 deletions src/metafits_context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,11 @@ impl MetafitsContext {
metafits: P,
mwa_version: Option<MWAVersion>,
) -> Result<Self, MwalibError> {
// Check CFITSIO is reentrant before proceeding
if !fits_read::is_fitsio_reentrant() {
return Err(MwalibError::Fits(FitsError::CfitsioIsNotReentrant));
}

Self::new_inner(metafits.as_ref(), mwa_version)
}

Expand Down
5 changes: 5 additions & 0 deletions src/voltage_context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,11 @@ impl VoltageContext {
metafits_filename: P,
voltage_filenames: &[P2],
) -> Result<Self, MwalibError> {
// Check CFITSIO is reentrant before proceeding
if !fits_read::is_fitsio_reentrant() {
return Err(MwalibError::Fits(FitsError::CfitsioIsNotReentrant));
}

Self::new_inner(metafits_filename.as_ref(), voltage_filenames)
}

Expand Down

0 comments on commit c1dab5f

Please sign in to comment.