From d8443a79d7c7bda64283da0337a04d8aed634dcf Mon Sep 17 00:00:00 2001 From: George Hazan Date: Sun, 15 Apr 2018 13:34:49 +0300 Subject: fixes #1230 (Database encryption mode) --- plugins/Dbx_mdbx/src/dbcrypt.cpp | 88 +++++++++++++++++++++------------------- 1 file changed, 47 insertions(+), 41 deletions(-) (limited to 'plugins/Dbx_mdbx/src/dbcrypt.cpp') diff --git a/plugins/Dbx_mdbx/src/dbcrypt.cpp b/plugins/Dbx_mdbx/src/dbcrypt.cpp index e22b3a293d..36fb76a978 100644 --- a/plugins/Dbx_mdbx/src/dbcrypt.cpp +++ b/plugins/Dbx_mdbx/src/dbcrypt.cpp @@ -169,7 +169,6 @@ int CDbxMDBX::EnableEncryption(bool bEncrypted) std::vector lstEvents; lstEvents.reserve(st.ms_entries); - { cursor_ptr_ro cursor(m_curEvents); MDBX_val key, data; @@ -178,49 +177,56 @@ int CDbxMDBX::EnableEncryption(bool bEncrypted) lstEvents.push_back(hDbEvent); } } - for (auto it = lstEvents.begin(); it != lstEvents.end(); ++it) { - MEVENT &hDbEvent = *it; - MDBX_val key = { &hDbEvent, sizeof(MEVENT) }, data; - int rc = mdbx_get(txnro, m_dbEvents, &key, &data); - /* FIXME: throw an exception */ - assert(rc == MDBX_SUCCESS); - (void)rc; - - const DBEvent *dbEvent = (const DBEvent*)data.iov_base; - const BYTE *pBlob = (BYTE*)(dbEvent + 1); - - if (((dbEvent->flags & DBEF_ENCRYPTED) != 0) != bEncrypted) { - mir_ptr pNewBlob; - size_t nNewBlob; - uint32_t dwNewFlags; - - if (dbEvent->flags & DBEF_ENCRYPTED) { - pNewBlob = (BYTE*)m_crypto->decodeBuffer(pBlob, dbEvent->cbBlob, &nNewBlob); - dwNewFlags = dbEvent->flags & (~DBEF_ENCRYPTED); - } - else { - pNewBlob = m_crypto->encodeBuffer(pBlob, dbEvent->cbBlob, &nNewBlob); - dwNewFlags = dbEvent->flags | DBEF_ENCRYPTED; - } - - data.iov_len = sizeof(DBEvent) + nNewBlob; - mir_ptr pData((BYTE*)mir_alloc(data.iov_len)); - data.iov_base = pData.get(); - DBEvent *pNewDBEvent = (DBEvent *)data.iov_base; - *pNewDBEvent = *dbEvent; - pNewDBEvent->cbBlob = (uint16_t)nNewBlob; - pNewDBEvent->flags = dwNewFlags; - memcpy(pNewDBEvent + 1, pNewBlob, nNewBlob); - - txn_ptr trnlck(StartTran()); - if (mdbx_put(trnlck, m_dbEvents, &key, &data, 0) != MDBX_SUCCESS) - return 1; - - if (trnlck.commit() != MDBX_SUCCESS) - return 1; + do { + size_t portion = min(lstEvents.size(), 1000); + + txn_ptr trnlck(StartTran()); + for (size_t i = 0; i < portion; i++) { + MEVENT &hDbEvent = lstEvents[i]; + MDBX_val key = { &hDbEvent, sizeof(MEVENT) }, data; + int rc = mdbx_get(txnro, m_dbEvents, &key, &data); + /* FIXME: throw an exception */ + assert(rc == MDBX_SUCCESS); + (void)rc; + + const DBEvent *dbEvent = (const DBEvent*)data.iov_base; + const BYTE *pBlob = (BYTE*)(dbEvent + 1); + + if (((dbEvent->flags & DBEF_ENCRYPTED) != 0) != bEncrypted) { + mir_ptr pNewBlob; + size_t nNewBlob; + uint32_t dwNewFlags; + + if (dbEvent->flags & DBEF_ENCRYPTED) { + pNewBlob = (BYTE*)m_crypto->decodeBuffer(pBlob, dbEvent->cbBlob, &nNewBlob); + dwNewFlags = dbEvent->flags & (~DBEF_ENCRYPTED); + } + else { + pNewBlob = m_crypto->encodeBuffer(pBlob, dbEvent->cbBlob, &nNewBlob); + dwNewFlags = dbEvent->flags | DBEF_ENCRYPTED; + } + + data.iov_len = sizeof(DBEvent) + nNewBlob; + mir_ptr pData((BYTE*)mir_alloc(data.iov_len)); + data.iov_base = pData.get(); + + DBEvent *pNewDBEvent = (DBEvent *)data.iov_base; + *pNewDBEvent = *dbEvent; + pNewDBEvent->cbBlob = (uint16_t)nNewBlob; + pNewDBEvent->flags = dwNewFlags; + memcpy(pNewDBEvent + 1, pNewBlob, nNewBlob); + + if (mdbx_put(trnlck, m_dbEvents, &key, &data, 0) != MDBX_SUCCESS) + return 1; + } } + + lstEvents.erase(lstEvents.begin(), lstEvents.begin()+portion); + if (trnlck.commit() != MDBX_SUCCESS) + return 1; } + while (lstEvents.size() > 0); } txn_ptr trnlck(StartTran()); -- cgit v1.2.3