From 49331635b2378600c1fc1354dbc4eb151cada89a Mon Sep 17 00:00:00 2001 From: Maxim Poliakovski Date: Mon, 7 Nov 2022 12:15:08 +0100 Subject: [PATCH] scsi_hd: determine image file size with stat(). --- devices/common/scsi/scsi_hd.cpp | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/devices/common/scsi/scsi_hd.cpp b/devices/common/scsi/scsi_hd.cpp index dbba5b3..2b9cf88 100644 --- a/devices/common/scsi/scsi_hd.cpp +++ b/devices/common/scsi/scsi_hd.cpp @@ -32,6 +32,7 @@ along with this program. If not, see . #include #include #include +#include #define sector_size 512 @@ -46,11 +47,13 @@ void ScsiHardDisk::insert_image(std::string filename) { //we want to keep the hard disk available. this->hdd_img.open(filename, ios::out | ios::in | ios::binary); - // Taken from: - // https://stackoverflow.com/questions/22984956/tellg-function-give-wrong-size-of-file/22986486 - this->hdd_img.ignore(std::numeric_limits::max()); - this->img_size = this->hdd_img.gcount(); - this->hdd_img.clear(); // Since ignore will have set eof. + struct stat stat_buf; + int rc = stat(filename.c_str(), &stat_buf); + if (!rc) { + this->img_size = stat_buf.st_size; + } else { + ABORT_F("ScsiHardDisk: could not determine file size using stat()"); + } this->hdd_img.seekg(0, std::ios_base::beg); }