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
extern crate libc;

use std::sync::{Once, ONCE_INIT};

pub mod db;
pub mod engine;
mod error;
mod ffi;
pub mod scan_settings;
pub mod version;

pub use error::ClamError;

/// Initializes clamav
///
/// This must be called once per process. This is safe to call multiple times.
pub fn initialize() -> Result<(), ClamError> {
    // the cl_init implementation isn't thread-safe, which is painful for tests
    static ONCE: Once = ONCE_INIT;
    static mut RESULT: ffi::cl_error = ffi::cl_error::CL_SUCCESS;
    unsafe {
        ONCE.call_once(|| {
            RESULT = ffi::cl_init(ffi::CL_INIT_DEFAULT);
            // this function always returns OK
            if RESULT == ffi::cl_error::CL_SUCCESS {
                ffi::cl_initialize_crypto();
                libc::atexit(cleanup);
            }
        });

        extern "C" fn cleanup() {
            unsafe {
                ffi::cl_cleanup_crypto();
            }
        }

        match RESULT {
            ffi::cl_error::CL_SUCCESS => Ok(()),
            _ => Err(ClamError::new(RESULT)),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn initialize_success() {
        assert!(initialize().is_ok(), "initialize should succeed");
    }
}