scsi_hd: determine image file size with stat().

This commit is contained in:
Maxim Poliakovski 2022-11-07 12:15:08 +01:00
parent 0c4da80c93
commit 49331635b2

View File

@ -32,6 +32,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <limits> #include <limits>
#include <cstring> #include <cstring>
#include <stdio.h> #include <stdio.h>
#include <sys/stat.h>
#define sector_size 512 #define sector_size 512
@ -46,11 +47,13 @@ void ScsiHardDisk::insert_image(std::string filename) {
//we want to keep the hard disk available. //we want to keep the hard disk available.
this->hdd_img.open(filename, ios::out | ios::in | ios::binary); this->hdd_img.open(filename, ios::out | ios::in | ios::binary);
// Taken from: struct stat stat_buf;
// https://stackoverflow.com/questions/22984956/tellg-function-give-wrong-size-of-file/22986486 int rc = stat(filename.c_str(), &stat_buf);
this->hdd_img.ignore(std::numeric_limits<std::streamsize>::max()); if (!rc) {
this->img_size = this->hdd_img.gcount(); this->img_size = stat_buf.st_size;
this->hdd_img.clear(); // Since ignore will have set eof. } else {
ABORT_F("ScsiHardDisk: could not determine file size using stat()");
}
this->hdd_img.seekg(0, std::ios_base::beg); this->hdd_img.seekg(0, std::ios_base::beg);
} }