@@ -30,7 +30,6 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
3030
3131#include < fstream>
3232#include < cstring>
33- #include < sys/stat.h>
3433
3534#define HDD_SECTOR_SIZE 512
3635
@@ -43,17 +42,12 @@ ScsiHardDisk::ScsiHardDisk(int my_id) : ScsiDevice(my_id) {
4342void ScsiHardDisk::insert_image (std::string filename) {
4443 // We don't want to store everything in memory, but
4544 // we want to keep the hard disk available.
46- this ->hdd_img .open (filename, ios::out | ios::in | ios::binary);
47-
48- struct stat stat_buf;
49- int rc = stat (filename.c_str (), &stat_buf);
50- if (!rc) {
51- this ->img_size = stat_buf.st_size ;
52- this ->total_blocks = (this ->img_size + HDD_SECTOR_SIZE - 1 ) / HDD_SECTOR_SIZE ;
53- } else {
54- ABORT_F (" ScsiHardDisk: could not determine file size using stat()" );
45+ if (!this ->hdd_img .open (filename)) {
46+ ABORT_F (" ScsiHardDisk: could not open image file" );
5547 }
56- this ->hdd_img .seekg (0 , std::ios_base::beg);
48+
49+ this ->img_size = this ->hdd_img .size ();
50+ this ->total_blocks = (this ->img_size + HDD_SECTOR_SIZE - 1 ) / HDD_SECTOR_SIZE ;
5751}
5852
5953void ScsiHardDisk::process_command () {
@@ -267,8 +261,7 @@ void ScsiHardDisk::read(uint32_t lba, uint16_t transfer_len, uint8_t cmd_len) {
267261 transfer_size *= HDD_SECTOR_SIZE ;
268262 uint64_t device_offset = lba * HDD_SECTOR_SIZE ;
269263
270- this ->hdd_img .seekg (device_offset, this ->hdd_img .beg );
271- this ->hdd_img .read (img_buffer, transfer_size);
264+ this ->hdd_img .read (img_buffer, device_offset, transfer_size);
272265
273266 this ->cur_buf_cnt = transfer_size;
274267 this ->msg_buf [0 ] = ScsiMessage::COMMAND_COMPLETE ;
@@ -288,20 +281,17 @@ void ScsiHardDisk::write(uint32_t lba, uint16_t transfer_len, uint8_t cmd_len) {
288281
289282 this ->incoming_size = transfer_size;
290283
291- this ->hdd_img .seekg (device_offset, this ->hdd_img .beg );
292-
293- this ->post_xfer_action = [this ]() {
294- this ->hdd_img .write (this ->img_buffer , this ->incoming_size );
284+ this ->post_xfer_action = [this , device_offset]() {
285+ this ->hdd_img .write (this ->img_buffer , device_offset, this ->incoming_size );
295286 };
296287}
297288
298289void ScsiHardDisk::seek (uint32_t lba) {
299- uint64_t device_offset = lba * HDD_SECTOR_SIZE ;
300- this ->hdd_img .seekg (device_offset, this ->hdd_img .beg );
290+ // No-op
301291}
302292
303293void ScsiHardDisk::rewind () {
304- this -> hdd_img . seekg ( 0 , this -> hdd_img . beg );
294+ // No-op
305295}
306296
307297static const PropMap SCSI_HD_Properties = {
0 commit comments