Skip to content

Commit 34b4d63

Browse files
blockstoragedevice: clean up and add write support.
1 parent 9ca3e5e commit 34b4d63

2 files changed

Lines changed: 44 additions & 11 deletions

File tree

devices/storage/blockstoragedevice.cpp

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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-
3029
BlockStorageDevice::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
}

devices/storage/blockstoragedevice.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,19 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
3333
class BlockStorageDevice {
3434
public:
3535
BlockStorageDevice(const uint32_t cache_blocks, const uint32_t block_size=512,
36-
const uint64_t max_blocks=0xffffffffffffffff);
36+
const uint64_t max_blocks=UINT64_MAX);
3737
~BlockStorageDevice();
3838

3939
int set_host_file(std::string file_path);
4040
int set_block_size(const int blk_size);
4141

4242
int set_fpos(const uint64_t lba);
43-
int read_begin(int nblocks, uint32_t max_len = UINT32_MAX);
4443
int data_left() { return this->remain_size; }
44+
int read_begin(int nblocks, uint32_t max_len = UINT32_MAX);
4545
int read_more();
46-
int write_begin(char *buf, int nblocks);
46+
int write_begin(int nblocks, uint32_t max_len = UINT32_MAX);
47+
int write_more();
48+
void write_cache();
4749

4850
protected:
4951
void fill_cache(const int nblocks);
@@ -53,6 +55,7 @@ class BlockStorageDevice {
5355
uint64_t size_blocks = 0; // image file size in blocks
5456
uint64_t max_blocks = 0; // maximum number of blocks supported
5557
uint64_t cur_fpos = 0; // current image file pointer position
58+
uint64_t write_size = 0; // number of bytes to write next
5659
uint32_t block_size = 512; // physical block size
5760
uint32_t raw_blk_size = 512; // block size for raw images (CD-ROM)
5861
uint32_t data_offset = 0; // offset to block data for raw images

0 commit comments

Comments
 (0)