2015-03-12 14:27:51 +01:00
# include "FakeBlock.h"
# include "FakeBlockStore.h"
2014-12-11 01:31:21 +01:00
# include <cstring>
2015-07-22 13:42:07 +02:00
# include <messmer/cpp-utils/assert/assert.h>
2014-12-11 01:31:21 +01:00
using std : : shared_ptr ;
using std : : istream ;
using std : : ostream ;
using std : : ifstream ;
using std : : ofstream ;
using std : : ios ;
using std : : string ;
2015-04-25 02:48:41 +02:00
using cpputils : : Data ;
2014-12-11 01:31:21 +01:00
namespace blockstore {
namespace testfake {
2015-04-15 14:51:41 +02:00
FakeBlock : : FakeBlock ( FakeBlockStore * store , const Key & key , shared_ptr < Data > data , bool dirty )
: Block ( key ) , _store ( store ) , _data ( data ) , _dataChanged ( dirty ) {
2014-12-11 01:31:21 +01:00
}
FakeBlock : : ~ FakeBlock ( ) {
flush ( ) ;
}
2015-03-04 20:47:02 +01:00
const void * FakeBlock : : data ( ) const {
2014-12-11 01:31:21 +01:00
return _data - > data ( ) ;
}
2015-03-04 20:47:02 +01:00
void FakeBlock : : write ( const void * source , uint64_t offset , uint64_t size ) {
2015-07-22 13:42:07 +02:00
ASSERT ( offset < = _data - > size ( ) & & offset + size < = _data - > size ( ) , " Write outside of valid area " ) ; //Also check offset < _data->size() because of possible overflow in the addition
2015-03-04 20:47:02 +01:00
std : : memcpy ( ( uint8_t * ) _data - > data ( ) + offset , source , size ) ;
2015-03-05 22:16:57 +01:00
_dataChanged = true ;
2014-12-11 01:31:21 +01:00
}
size_t FakeBlock : : size ( ) const {
return _data - > size ( ) ;
}
void FakeBlock : : flush ( ) {
2015-03-05 22:16:57 +01:00
if ( _dataChanged ) {
_store - > updateData ( key ( ) , * _data ) ;
_dataChanged = false ;
}
2014-12-11 01:31:21 +01:00
}
}
}