vw_small

Hardened fork of Vaultwarden (https://github.com/dani-garcia/vaultwarden) with fewer features.
git clone https://git.philomathiclife.com/repos/vw_small
Log | Files | Refs | README

commit dc36f0cb6c47017e9e188ca55e42fdb75b7b87ce
parent 6c38026ef53535bbe88799dd5a85ecd02b95f541
Author: Nils Domrose <nils.domrose@inovex.de>
Date:   Mon, 27 May 2019 22:58:52 +0200

re-added sqlite check_db code, cleanup

Diffstat:
MCargo.toml | 4++--
Msrc/config.rs | 2--
Msrc/db/mod.rs | 10+++-------
Msrc/main.rs | 23+++++++++++++++++++++++
4 files changed, 28 insertions(+), 11 deletions(-)

diff --git a/Cargo.toml b/Cargo.toml @@ -13,8 +13,8 @@ build = "build.rs" [features] # Empty to keep compatibility, prefer to set USE_SYSLOG=true enable_syslog = [] -mysql = [] -sqlite = [] +mysql = ["diesel/mysql", "diesel_migrations/mysql"] +sqlite = ["diesel/sqlite", "diesel_migrations/sqlite"] [target."cfg(not(windows))".dependencies] syslog = "4.0.1" diff --git a/src/config.rs b/src/config.rs @@ -202,8 +202,6 @@ make_config! { folders { /// Data folder |> Main data folder data_folder: String, false, def, "data".to_string(); - - /// Database URL /// Database URL database_url: String, false, auto, |c| format!("{}/{}", c.data_folder, "db.sqlite3"); /// Icon chache folder diff --git a/src/db/mod.rs b/src/db/mod.rs @@ -2,10 +2,6 @@ use std::ops::Deref; use diesel::r2d2; use diesel::r2d2::ConnectionManager; -#[cfg(feature = "sqlite")] -use diesel::sqlite::SqliteConnection; -#[cfg(feature = "mysql")] -use diesel::mysql::MysqlConnection; use diesel::{Connection as DieselConnection, ConnectionError}; use rocket::http::Status; @@ -16,11 +12,11 @@ use crate::CONFIG; /// An alias to the database connection used #[cfg(feature = "sqlite")] -type Connection = SqliteConnection; +type Connection = diesel::sqlite::SqliteConnection; #[cfg(feature = "mysql")] -type Connection = MysqlConnection; +type Connection = diesel::mysql::MysqlConnection; -/// An alias to the type for a pool of Diesel MySQL connections. +/// An alias to the type for a pool of Diesel connections. type Pool = r2d2::Pool<ConnectionManager<Connection>>; /// Connection request guard type: a wrapper around an r2d2 pooled connection. diff --git a/src/main.rs b/src/main.rs @@ -45,6 +45,9 @@ fn main() { init_logging().ok(); } + #[cfg(all(feature = "sqlite", feature = "mysql"))] + compile_error!("Can't enable both backends"); + check_db(); check_rsa_keys(); check_web_vault(); @@ -123,6 +126,26 @@ fn chain_syslog(logger: fern::Dispatch) -> fern::Dispatch { fn check_db() { let url = CONFIG.database_url(); + if cfg!(feature = "sqlite") { + let path = Path::new(&url); + + if let Some(parent) = path.parent() { + use std::fs; + if fs::create_dir_all(parent).is_err() { + error!("Error creating database directory"); + exit(1); + } + } + + // Turn on WAL in SQLite + if CONFIG.enable_db_wal() { + use diesel::RunQueryDsl; + let connection = db::get_connection().expect("Can't conect to DB"); + diesel::sql_query("PRAGMA journal_mode=wal") + .execute(&connection) + .expect("Failed to turn on WAL"); + } + } println!("{}", url.to_string()); db::get_connection().expect("Can't conect to DB"); }