bijou/
lib.rs

1// Copyright 2023 Mivik
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15
16pub mod algo;
17mod bijou;
18mod cache;
19mod crypto;
20mod db;
21mod error;
22mod fs;
23mod id_lock;
24mod secret;
25mod serde_ext;
26mod sodium;
27
28pub(crate) use error::{anyhow, bail, Context};
29
30pub use bijou::{Bijou, BijouFs, DirIterator, File};
31pub use error::{Error, ErrorKind, Result};
32pub use fs::{
33    config::{self, Config},
34    path, raw as raw_fs, FileId, FileKind, FileMeta, LowLevelFile, OpenOptions,
35};
36pub use secret::SecretBytes;
37pub use sodium::pwhash::Limit;
38
39#[cfg(feature = "fuse")]
40pub use bijou::BijouFuse;
41#[cfg(feature = "fuse")]
42pub use fuser::MountOption;
43
44/// Initialize Bijou.
45///
46/// Should be called before any use of this library.
47pub fn init() -> Result<()> {
48    unsafe {
49        if libsodium_sys::sodium_init() != 0 {
50            bail!(@CryptoError "failed to initialize libsodium");
51        }
52    }
53    Ok(())
54}
55
56#[cfg(debug_assertions)]
57struct TimeSpan(String, std::time::Instant);
58#[cfg(debug_assertions)]
59fn begin_span(name: impl Into<String>) -> TimeSpan {
60    TimeSpan(name.into(), std::time::Instant::now())
61}
62#[cfg(debug_assertions)]
63impl Drop for TimeSpan {
64    fn drop(&mut self) {
65        let elapsed = self.1.elapsed();
66        tracing::debug!(name = self.0, elapsed = elapsed.as_nanos(), "time span");
67    }
68}
69
70#[cfg(not(debug_assertions))]
71fn begin_span(_name: impl Into<String>) {}