Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added error checking to compact() and support encryption on Linux #832

Merged
merged 8 commits into from
May 29, 2015
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,21 @@ each of our major platforms:
sudo apt-get install build-essential
sudo apt-get install python-cheetah
sudo apt-get install libproc-dev
sudo apt-get install libssl-dev

### Linux Mint 15, 16, Ubuntu 13.04, 13.10

sudo apt-get install build-essential
sudo apt-get install python-cheetah
sudo apt-get install libprocps0-dev
sudo apt-get install libssl-dev

### Linux Mint 17, 17.1, Ubuntu 14.04

sudo apt-get install build-essential
sudo apt-get install python-cheetah
sudo apt-get install libprocps3-dev
sudo apt-get install libssl-dev

### Fedora 17, 18, 19, 20, Amazon Linux 2012.09

Expand Down
3 changes: 2 additions & 1 deletion release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
### Enhancements:

* Changes the mmap doubling treshold on mobile devices from 128MB to 16MB.
* SharedGroup::compact() will now throw a runtime_error if called in detached state.

### Internals:

* Lorem ipsum.
* Can now be built with encryption enabled on Linux.

----------------------------------------------

Expand Down
3 changes: 3 additions & 0 deletions src/project.mk
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ ifneq ($(REALM_HAVE_CONFIG),)
endif
ifeq ($(ENABLE_ENCRYPTION),yes)
PROJECT_CFLAGS += -DREALM_ENABLE_ENCRYPTION
ifeq ($(OS),Linux)
PROJECT_LDFLAGS += -ldl
endif
endif
else
ifneq ($(REALM_ENABLE_REPLICATION),)
Expand Down
6 changes: 5 additions & 1 deletion src/realm/group_shared.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,10 @@ void SharedGroup::open(const std::string& path, bool no_create_file,

bool SharedGroup::compact()
{
// Verify that the database file is attached
if (is_attached() == false) {
throw std::runtime_error(m_db_path + ": compact must be done on an open/attached SharedGroup");
}
// Verify that preconditions for compacting is met:
if (m_transact_stage != transact_Ready) {
throw std::runtime_error(m_db_path + ": compact is not supported whithin a transaction");
Expand Down Expand Up @@ -887,7 +891,7 @@ bool SharedGroup::compact()
// update the versioning info to match
SharedInfo* r_info = m_reader_map.get_addr();
Ringbuffer::ReadCount& rc = const_cast<Ringbuffer::ReadCount&>(r_info->readers.get_last());
REALM_ASSERT(rc.version == info->latest_version_number);
REALM_ASSERT_3(rc.version, ==, info->latest_version_number);
rc.filesize = file_size;
rc.current_top = top_ref;
return true;
Expand Down
3 changes: 2 additions & 1 deletion src/realm/group_shared.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,9 @@ class SharedGroup {

/// Compact the database file.
/// - The method will throw if called inside a transaction.
/// - The method will throw if called in unattached state.
/// - The method will return false if other SharedGroups are accessing the database
/// in which case compaction is not done.
/// in which case compaction is not done. This is not necessarily an error.
/// It will return true following succesful compaction.
/// While compaction is in progress, attempts by other
/// threads or processes to open the database will wait.
Expand Down
10 changes: 8 additions & 2 deletions src/realm/util/encrypted_file_mapping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,17 @@ AESCryptor::AESCryptor(const uint8_t* key) {
CCCryptorCreate(kCCEncrypt, kCCAlgorithmAES, 0 /* options */, key, kCCKeySizeAES256, 0 /* IV */, &m_encr);
CCCryptorCreate(kCCDecrypt, kCCAlgorithmAES, 0 /* options */, key, kCCKeySizeAES256, 0 /* IV */, &m_decr);
#else
#ifdef REALM_ANDROID

#if defined(__linux__)
// libcrypto isn't exposed as part of the NDK, but it happens to be loaded
// into every process with every version of Android, so we can get to it
// with dlsym
// FIXME: Don't know where to write the following info:
// on linux, we add -ldl to the linking step to get dlsym to work,
// and set LD_PRELOAD to the location of libcrypto when running the executable.
// (on linux mint this is currently: "LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libcrypto.so")
// To be able to debug with gdb, use the command "handle SIGSEGV noprint"
// before running the program.
dlsym_cast(AES_set_encrypt_key, "AES_set_encrypt_key");
dlsym_cast(AES_set_decrypt_key, "AES_set_decrypt_key");
dlsym_cast(AES_cbc_encrypt, "AES_cbc_encrypt");
Expand All @@ -85,7 +92,6 @@ AESCryptor::AESCryptor(const uint8_t* key) {
dlsym_cast(SHA256_Update, "SHA256_Update");
dlsym_cast(SHA256_Final, "SHA256_Final");
#endif

AES_set_encrypt_key(key, 256 /* key size in bits */, &m_ectx);
AES_set_decrypt_key(key, 256 /* key size in bits */, &m_dctx);
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/realm/util/encrypted_file_mapping.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class AESCryptor {
AES_KEY m_dctx;
#endif

#ifdef REALM_ANDROID
#if defined(__linux__)
// Loaded at runtime with dysym
int (*AES_set_encrypt_key)(const unsigned char *, const int, AES_KEY *);
int (*AES_set_decrypt_key)(const unsigned char *, const int, AES_KEY *);
Expand Down
1 change: 1 addition & 0 deletions src/realm/util/file_mapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include <realm/util/shared_ptr.hpp>
#include <realm/util/terminate.hpp>
#include <realm/util/thread.hpp>
#include <string.h> // for memset

#ifdef __APPLE__
# include <mach/mach.h>
Expand Down
4 changes: 2 additions & 2 deletions test/test_shared.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ TEST(Shared_CompactingOnTheFly)
std::string tmp_path = std::string(path)+".tmp";
Thread writer_thread;
{
SharedGroup sg(path, false, SharedGroup::durability_Full);
SharedGroup sg(path, false, SharedGroup::durability_Full, crypt_key());
// Create table entries
{
WriteTransaction wt(sg);
Expand Down Expand Up @@ -258,7 +258,7 @@ TEST(Shared_CompactingOnTheFly)
}
writer_thread.join();
{
SharedGroup sg2(path, true, SharedGroup::durability_Full);
SharedGroup sg2(path, true, SharedGroup::durability_Full, crypt_key());
{
sg2.begin_write();
sg2.commit();
Expand Down