Use pragma WAL in db connections

WAL mode allows db connections to open when the disk
is full. This is done in the current and legacy db and
tested manually by the commandline.

Testing: Filled a file with empty values until it took up all the space on the disk then accessed the database. This was not possible with this mode disabled but was once I enabled it on a new flash

Bug: 191777960
Test: atest keystore2_test and atest CtsKeystoreTestCases, filled real device to full and tested
Change-Id: Ic1a45fd635168061a6c5489a42a67cb59d3ddc6a
diff --git a/keystore2/src/database.rs b/keystore2/src/database.rs
index d6bfaf2..3a2f50f 100644
--- a/keystore2/src/database.rs
+++ b/keystore2/src/database.rs
@@ -55,11 +55,6 @@
     error::{Error as KsError, ErrorCode, ResponseCode},
     super_key::SuperKeyType,
 };
-use anyhow::{anyhow, Context, Result};
-use std::{convert::TryFrom, convert::TryInto, ops::Deref, time::SystemTimeError};
-use utils as db_utils;
-use utils::SqlField;
-
 use android_hardware_security_keymint::aidl::android::hardware::security::keymint::{
     HardwareAuthToken::HardwareAuthToken, HardwareAuthenticatorType::HardwareAuthenticatorType,
     SecurityLevel::SecurityLevel,
@@ -70,6 +65,11 @@
 use android_system_keystore2::aidl::android::system::keystore2::{
     Domain::Domain, KeyDescriptor::KeyDescriptor,
 };
+use anyhow::{anyhow, Context, Result};
+use keystore2_flags;
+use std::{convert::TryFrom, convert::TryInto, ops::Deref, time::SystemTimeError};
+use utils as db_utils;
+use utils::SqlField;
 
 use keystore2_crypto::ZVec;
 use lazy_static::lazy_static;
@@ -1036,6 +1036,11 @@
             break;
         }
 
+        if keystore2_flags::wal_db_journalmode() {
+            // Update journal mode to WAL
+            conn.pragma_update(None, "journal_mode", "WAL")
+                .context("Failed to connect in WAL mode for persistent db")?;
+        }
         // Drop the cache size from default (2M) to 0.5M
         conn.execute("PRAGMA persistent.cache_size = -500;", params![])
             .context("Failed to decrease cache size for persistent db")?;