Skip to content

Commit

Permalink
Re-calculate total blocks when sector size changes
Browse files Browse the repository at this point in the history
Signed-off-by: Klaus Kämpf <[email protected]>
  • Loading branch information
kkaempf committed Jan 6, 2024
1 parent ecb45d3 commit 7ec9398
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions cpp/devices/disk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -696,11 +696,17 @@ uint32_t Disk::GetSectorSizeInBytes() const
void Disk::SetSectorSizeInBytes(uint32_t size_in_bytes)
{
if (DeviceFactory device_factory; !device_factory.GetSectorSizes(GetType()).contains(size_in_bytes)) {
throw io_exception("Invalid sector size of " + to_string(size_in_bytes) + " byte(s)");
throw io_exception("Invalid sector size of " + to_string(size_in_bytes) + " byte(s)");
}
uint64_t current_blocks = GetBlockCount();
uint32_t current_size_shift_count = size_shift_count;
uint64_t current_size = current_blocks << current_size_shift_count;

size_shift_count = CalculateShiftCount(size_in_bytes);
assert(size_shift_count);
if ((current_blocks > 0) && (current_size_shift_count > 0)) {
SetBlockCount(current_size >> size_shift_count);
}
}

uint32_t Disk::GetConfiguredSectorSize() const
Expand All @@ -714,7 +720,7 @@ bool Disk::SetConfiguredSectorSize(const DeviceFactory& device_factory, uint32_t
return false;
}

configured_sector_size = configured_size;
configured_sector_size = configured_size;

return true;
}
Expand Down

0 comments on commit 7ec9398

Please sign in to comment.