@@ -22,11 +22,10 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
2222/* * @file Block storage device implementation. */
2323
2424#include < devices/storage/blockstoragedevice.h>
25+ #include < loguru.hpp>
2526
2627#include < cstring>
2728
28- using namespace std ;
29-
3029BlockStorageDevice::BlockStorageDevice (const uint32_t cache_blocks,
3130 const uint32_t block_size,
3231 const uint64_t max_blocks) {
@@ -49,7 +48,7 @@ int BlockStorageDevice::set_host_file(std::string file_path) {
4948 if (!this ->img_file .open (file_path))
5049 return -1 ;
5150
52- this ->size_bytes = this ->img_file .size ();
51+ this ->size_bytes = this ->img_file .size ();
5352
5453 this ->set_fpos (0 );
5554
@@ -105,9 +104,8 @@ int BlockStorageDevice::read_begin(int nblocks, uint32_t max_len) {
105104 if (read_size > xfer_len) {
106105 this ->remain_size = read_size - xfer_len;
107106 read_size = xfer_len;
108- } else {
107+ } else
109108 this ->remain_size = 0 ;
110- }
111109
112110 this ->fill_cache (read_size / this ->block_size );
113111
@@ -132,9 +130,41 @@ int BlockStorageDevice::read_more() {
132130 return read_size;
133131}
134132
135- int BlockStorageDevice::write_begin (char *buf, int nblocks ) {
133+ int BlockStorageDevice::write_begin (int nblocks, uint32_t max_len ) {
136134 if (!this ->is_writeable )
137- return - 1 ;
135+ ABORT_F ( " write attempt to read-only block storage device " ) ;
138136
139- return 0 ;
137+ uint64_t xfer_len = std::min (this ->cache_blocks * this ->block_size , max_len);
138+
139+ this ->write_size = nblocks * this ->block_size ;
140+ if (this ->write_size > xfer_len) {
141+ this ->remain_size = write_size - xfer_len;
142+ this ->write_size = xfer_len;
143+ } else
144+ this ->remain_size = 0 ;
145+
146+ return this ->write_size ;
147+ }
148+
149+ int BlockStorageDevice::write_more () {
150+ this ->write_cache ();
151+
152+ this ->write_size = this ->cache_blocks * this ->block_size ;
153+
154+ if (this ->remain_size > this ->write_size ) {
155+ this ->remain_size -= this ->write_size ;
156+ } else {
157+ this ->write_size = this ->remain_size ;
158+ this ->remain_size = 0 ;
159+ }
160+
161+ return this ->write_size ;
162+ }
163+
164+ void BlockStorageDevice::write_cache () {
165+ if (this ->write_size ) {
166+ this ->img_file .write (this ->data_cache .get (), this ->cur_fpos , this ->write_size );
167+ this ->cur_fpos += this ->write_size ;
168+ this ->write_size = 0 ;
169+ }
140170}
0 commit comments