Store a copy of the encryption key

This commit is contained in:
Thomas Goyne 2015-09-04 11:51:19 -07:00 committed by Ari Lazier
parent 2e537e9c68
commit 5d876b7caa
2 changed files with 12 additions and 6 deletions

View File

@ -17,9 +17,11 @@
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
#include "shared_realm.hpp" #include "shared_realm.hpp"
#include <realm/commit_log.hpp>
#include <realm/group_shared.hpp> #include <realm/group_shared.hpp>
#include <realm/lang_bind_helper.hpp> #include <realm/lang_bind_helper.hpp>
#include <realm/commit_log.hpp>
#include <memory> #include <memory>
using namespace realm; using namespace realm;
@ -27,25 +29,29 @@ using namespace realm;
RealmCache Realm::s_global_cache; RealmCache Realm::s_global_cache;
std::mutex Realm::s_init_mutex; std::mutex Realm::s_init_mutex;
Realm::Config::Config(const Config& c) : path(c.path), read_only(c.read_only), in_memory(c.in_memory), schema_version(c.schema_version), encryption_key(c.encryption_key), migration_function(c.migration_function) Realm::Config::Config(const Config& c) : path(c.path), read_only(c.read_only), in_memory(c.in_memory), schema_version(c.schema_version), migration_function(c.migration_function)
{ {
if (c.schema) { if (c.schema) {
schema = std::make_unique<Schema>(*c.schema); schema = std::make_unique<Schema>(*c.schema);
} }
if (c.encryption_key) {
encryption_key = std::make_unique<char[]>(64);
memcpy(encryption_key.get(), c.encryption_key.get(), 64);
}
} }
Realm::Realm(Config &config) : m_config(config), m_thread_id(std::this_thread::get_id()), m_auto_refresh(true), m_in_transaction(false) Realm::Realm(Config &config) : m_config(config), m_thread_id(std::this_thread::get_id()), m_auto_refresh(true), m_in_transaction(false)
{ {
try { try {
if (config.read_only) { if (config.read_only) {
m_read_only_group = std::make_unique<Group>(config.path, config.encryption_key.data(), Group::mode_ReadOnly); m_read_only_group = std::make_unique<Group>(config.path, config.encryption_key.get(), Group::mode_ReadOnly);
m_group = m_read_only_group.get(); m_group = m_read_only_group.get();
} }
else { else {
m_history = realm::make_client_history(config.path, config.encryption_key.data()); m_history = realm::make_client_history(config.path, config.encryption_key.get());
SharedGroup::DurabilityLevel durability = config.in_memory ? SharedGroup::durability_MemOnly : SharedGroup::DurabilityLevel durability = config.in_memory ? SharedGroup::durability_MemOnly :
SharedGroup::durability_Full; SharedGroup::durability_Full;
m_shared_group = std::make_unique<SharedGroup>(*m_history, durability, config.encryption_key.data()); m_shared_group = std::make_unique<SharedGroup>(*m_history, durability, config.encryption_key.get());
m_group = nullptr; m_group = nullptr;
} }
} }

View File

@ -45,7 +45,7 @@ namespace realm {
std::string path; std::string path;
bool read_only; bool read_only;
bool in_memory; bool in_memory;
StringData encryption_key; std::unique_ptr<char[]> encryption_key;
std::unique_ptr<Schema> schema; std::unique_ptr<Schema> schema;
uint64_t schema_version; uint64_t schema_version;