hdd_image: Add framework for reporting I/O errors, and stop removing drives with locked images

This commit is contained in:
RichardG867
2024-10-29 11:41:59 -03:00
parent e1b116a299
commit 416125390b
2 changed files with 44 additions and 23 deletions

View File

@@ -184,6 +184,9 @@ hdd_image_calc_chs(uint32_t *c, uint32_t *h, uint32_t *s, uint32_t size)
static int
prepare_new_hard_disk(uint8_t id, uint64_t full_size)
{
if (!hdd_images[id].file)
return -1;
uint64_t target_size = (full_size + hdd_images[id].base) - ftello64(hdd_images[id].file);
#ifndef __unix__
@@ -390,7 +393,8 @@ retry_vhd:
} else {
/* Failed for another reason */
hdd_image_log("Failed for another reason\n");
return 0;
hdd_images[id].type = HDD_IMAGE_RAW;
return 1;
}
} else {
if (image_is_hdi(fn)) {
@@ -500,7 +504,7 @@ retry_vhd:
return ret;
}
void
int
hdd_image_seek(uint8_t id, uint32_t sector)
{
off64_t addr = sector;
@@ -508,12 +512,16 @@ hdd_image_seek(uint8_t id, uint32_t sector)
hdd_images[id].pos = sector;
if (hdd_images[id].type != HDD_IMAGE_VHD) {
if (fseeko64(hdd_images[id].file, addr + hdd_images[id].base, SEEK_SET) == -1)
fatal("hdd_image_seek(): Error seeking\n");
if (!hdd_images[id].file || (fseeko64(hdd_images[id].file, addr + hdd_images[id].base, SEEK_SET) == -1)) {
hdd_image_log("hdd_image_seek(): Error seeking\n");
return -1;
}
}
return 0;
}
void
int
hdd_image_read(uint8_t id, uint32_t sector, uint32_t count, uint8_t *buffer)
{
int non_transferred_sectors;
@@ -523,14 +531,18 @@ hdd_image_read(uint8_t id, uint32_t sector, uint32_t count, uint8_t *buffer)
non_transferred_sectors = mvhd_read_sectors(hdd_images[id].vhd, sector, count, buffer);
hdd_images[id].pos = sector + count - non_transferred_sectors - 1;
} else {
if (fseeko64(hdd_images[id].file, ((uint64_t) (sector) << 9LL) + hdd_images[id].base, SEEK_SET) == -1) {
fatal("Hard disk image %i: Read error during seek\n", id);
return;
if (!hdd_images[id].file || (fseeko64(hdd_images[id].file, ((uint64_t) (sector) << 9LL) + hdd_images[id].base, SEEK_SET) == -1)) {
hdd_image_log("Hard disk image %i: Read error during seek\n", id);
return -1;
}
num_read = fread(buffer, 512, count, hdd_images[id].file);
if (count && !num_read)
return -1;
hdd_images[id].pos = sector + num_read;
}
return 0;
}
uint32_t
@@ -554,14 +566,15 @@ hdd_image_read_ex(uint8_t id, uint32_t sector, uint32_t count, uint8_t *buffer)
if ((sectors - sector) < transfer_sectors)
transfer_sectors = sectors - sector;
hdd_image_read(id, sector, transfer_sectors, buffer);
if (hdd_image_read(id, sector, transfer_sectors, buffer) < 0)
return -1;
if (count != transfer_sectors)
return 1;
return 0;
}
void
int
hdd_image_write(uint8_t id, uint32_t sector, uint32_t count, uint8_t *buffer)
{
int non_transferred_sectors;
@@ -571,15 +584,19 @@ hdd_image_write(uint8_t id, uint32_t sector, uint32_t count, uint8_t *buffer)
non_transferred_sectors = mvhd_write_sectors(hdd_images[id].vhd, sector, count, buffer);
hdd_images[id].pos = sector + count - non_transferred_sectors - 1;
} else {
if (fseeko64(hdd_images[id].file, ((uint64_t) (sector) << 9LL) + hdd_images[id].base, SEEK_SET) == -1) {
fatal("Hard disk image %i: Write error during seek\n", id);
return;
if (!hdd_images[id].file || (fseeko64(hdd_images[id].file, ((uint64_t) (sector) << 9LL) + hdd_images[id].base, SEEK_SET) == -1)) {
hdd_image_log("Hard disk image %i: Write error during seek\n", id);
return -1;
}
num_write = fwrite(buffer, 512, count, hdd_images[id].file);
if (count && !num_write)
return -1;
fflush(hdd_images[id].file);
hdd_images[id].pos = sector + num_write;
}
return 0;
}
int
@@ -591,14 +608,15 @@ hdd_image_write_ex(uint8_t id, uint32_t sector, uint32_t count, uint8_t *buffer)
if ((sectors - sector) < transfer_sectors)
transfer_sectors = sectors - sector;
hdd_image_write(id, sector, transfer_sectors, buffer);
if (hdd_image_write(id, sector, transfer_sectors, buffer) < 0)
return -1;
if (count != transfer_sectors)
return 1;
return 0;
}
void
int
hdd_image_zero(uint8_t id, uint32_t sector, uint32_t count)
{
if (hdd_images[id].type == HDD_IMAGE_VHD) {
@@ -607,9 +625,9 @@ hdd_image_zero(uint8_t id, uint32_t sector, uint32_t count)
} else {
memset(empty_sector, 0, 512);
if (fseeko64(hdd_images[id].file, ((uint64_t) (sector) << 9LL) + hdd_images[id].base, SEEK_SET) == -1) {
fatal("Hard disk image %i: Zero error during seek\n", id);
return;
if (!hdd_images[id].file || (fseeko64(hdd_images[id].file, ((uint64_t) (sector) << 9LL) + hdd_images[id].base, SEEK_SET) == -1)) {
hdd_image_log("Hard disk image %i: Zero error during seek\n", id);
return -1;
}
for (uint32_t i = 0; i < count; i++) {
@@ -617,11 +635,14 @@ hdd_image_zero(uint8_t id, uint32_t sector, uint32_t count)
break;
hdd_images[id].pos = sector + i;
fwrite(empty_sector, 512, 1, hdd_images[id].file);
if (!fwrite(empty_sector, 512, 1, hdd_images[id].file))
return -1;
}
fflush(hdd_images[id].file);
}
return 0;
}
int

View File

@@ -197,12 +197,12 @@ extern int hdd_is_valid(int c);
extern void hdd_image_init(void);
extern int hdd_image_load(int id);
extern void hdd_image_seek(uint8_t id, uint32_t sector);
extern void hdd_image_read(uint8_t id, uint32_t sector, uint32_t count, uint8_t *buffer);
extern int hdd_image_seek(uint8_t id, uint32_t sector);
extern int hdd_image_read(uint8_t id, uint32_t sector, uint32_t count, uint8_t *buffer);
extern int hdd_image_read_ex(uint8_t id, uint32_t sector, uint32_t count, uint8_t *buffer);
extern void hdd_image_write(uint8_t id, uint32_t sector, uint32_t count, uint8_t *buffer);
extern int hdd_image_write(uint8_t id, uint32_t sector, uint32_t count, uint8_t *buffer);
extern int hdd_image_write_ex(uint8_t id, uint32_t sector, uint32_t count, uint8_t *buffer);
extern void hdd_image_zero(uint8_t id, uint32_t sector, uint32_t count);
extern int hdd_image_zero(uint8_t id, uint32_t sector, uint32_t count);
extern int hdd_image_zero_ex(uint8_t id, uint32_t sector, uint32_t count);
extern uint32_t hdd_image_get_last_sector(uint8_t id);
extern uint32_t hdd_image_get_pos(uint8_t id);