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
//! 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, ONCE_INIT};

#[cfg(target_os = "android")]
use log::Level;

#[cfg(target_os = "android")]
use android_logger::{self, Filter};

#[cfg(target_arch = "wasm32")]
use log::Level;

#[cfg(target_arch = "wasm32")]
use wasm_logger;

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

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

static INIT: Once = ONCE_INIT;

cfg_if! {
    if #[cfg(target_os = "android")] {
        pub fn init_env() {
            // only call the initialisation code once globally
            INIT.call_once(|| {
                android_logger::init_once(
                    Filter::default()
                        .with_min_level(Level::Trace)
                        .with_allowed_module_path("zbox::base")
                        .with_allowed_module_path("zbox::fs::fs")
                        .with_allowed_module_path("zbox::trans::txmgr"),
                    Some("zboxfs"),
                );
                crypto::Crypto::init().expect("Initialise crypto failed");
                info!(
                    "{} - Zero-details, privacy-focused in-app file system",
                    zbox_version()
                );
            });
        }
    } else if #[cfg(target_arch = "wasm32")] {
        pub fn init_env(level: Option<Level>) {
            INIT.call_once(|| {
                if let Some(lvl) = level {
                    wasm_logger::init(wasm_logger::Config::new(lvl));
                }
                crypto::Crypto::init().expect("Initialise crypto failed");
                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(|| {
                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))
    }
}