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
//! base module document
//!

pub(crate) mod crypto;
pub(crate) mod lru;
pub(crate) mod lz4;
mod refcnt;
mod time;
pub(crate) mod utils;
pub(crate) mod version;
pub(crate) mod vio;

pub use self::refcnt::RefCnt;
pub use self::time::Time;
pub use self::version::Version;

use std::sync::{Arc, Once, RwLock};

#[cfg(target_os = "android")]
use std::ptr::NonNull;

#[cfg(target_os = "android")]
use std::sync::Mutex;

#[cfg(target_os = "android")]
use jni::{JNIEnv, JavaVM};

#[cfg(all(not(target_os = "android"), not(target_arch = "wasm32")))]
use env_logger;

/// Get ZboxFS library version string.
///
/// This method return ZboxFS library version as a string, e.g. "ZboxFS v0.9.1".
#[inline]
pub fn zbox_version() -> String {
    format!("ZboxFS v{}", Version::lib_version())
}

static INIT: Once = Once::new();

#[cfg(target_os = "android")]
lazy_static! {
    // global JVM pointer
    pub static ref JVM: Mutex<JavaVM> = unsafe {
        let p = NonNull::dangling();
        Mutex::new(JavaVM::from_raw(p.as_ptr()).unwrap())
    };
}

cfg_if! {
    if #[cfg(target_os = "android")] {
        pub fn init_env(env: JNIEnv) {
            // only call the initialisation code once globally
            INIT.call_once(|| {
                crypto::Crypto::init().expect("Initialise crypto failed");

                // save global JVM pointer
                let jvm = env.get_java_vm().unwrap();
                let mut jvm_ptr = JVM.lock().unwrap();
                *jvm_ptr = jvm;

                info!(
                    "{} - Zero-details, privacy-focused in-app file system",
                    zbox_version()
                );
            });
        }
    } else {
        /// Initialise ZboxFS environment.
        ///
        /// This method should be called before any other methods provided
        /// by ZboxFS.
        /// This method can be called more than one time.
        pub fn init_env() {
            INIT.call_once(|| {
                #[cfg(not(target_arch = "wasm32"))]
                {
                    env_logger::try_init().ok();
                }
                crypto::Crypto::init().expect("Initialise crypto failed");
                info!(
                    "{} - Zero-details, privacy-focused in-app file system",
                    zbox_version()
                );
            });
        }
    }
}

/// Wrap type into reference type Arc<RwLock<T>>
pub trait IntoRef: Sized {
    fn into_ref(self) -> Arc<RwLock<Self>> {
        Arc::new(RwLock::new(self))
    }
}