Files
fluxengine/lib/data/image.cc
2025-10-06 22:58:24 +02:00

109 lines
3.3 KiB
C++

#include "lib/core/globals.h"
#include "lib/data/sector.h"
#include "lib/data/image.h"
#include "lib/data/layout.h"
#include "lib/config/config.h"
Image::Image() {}
Image::Image(std::vector<std::shared_ptr<const Sector>>& sectors):
_filesystemOrder(Layout::computeFilesystemLogicalOrdering())
{
for (auto& sector : sectors)
_sectors[{sector->logicalCylinder,
sector->logicalHead,
sector->logicalSector}] = sector;
for (auto& location : _filesystemOrder)
if (!_sectors.contains(location))
{
auto trackInfo = Layout::getLayoutOfTrack(
location.logicalCylinder, location.logicalHead);
auto sector = std::make_shared<Sector>(trackInfo, location);
sector->status = Sector::MISSING;
_sectors[location] = sector;
}
calculateSize();
}
void Image::clear()
{
_sectors.clear();
_geometry = {0, 0, 0};
}
void Image::createBlankImage()
{
clear();
for (const auto& trackAndHead : Layout::getTrackOrdering(
globalConfig()->layout().filesystem_track_order()))
{
unsigned track = trackAndHead.first;
unsigned side = trackAndHead.second;
auto trackLayout = Layout::getLayoutOfTrack(track, side);
Bytes blank(trackLayout->sectorSize);
for (unsigned sectorId : trackLayout->naturalSectorOrder)
put(track, side, sectorId)->data = blank;
}
}
bool Image::empty() const
{
return _sectors.empty();
}
bool Image::contains(unsigned track, unsigned side, unsigned sectorid) const
{
return _sectors.find({track, side, sectorid}) != _sectors.end();
}
std::shared_ptr<const Sector> Image::get(
unsigned track, unsigned side, unsigned sectorid) const
{
auto i = _sectors.find({track, side, sectorid});
if (i == _sectors.end())
return nullptr;
return i->second;
}
std::shared_ptr<Sector> Image::put(
unsigned track, unsigned side, unsigned sectorid)
{
auto trackLayout = Layout::getLayoutOfTrack(track, side);
std::shared_ptr<Sector> sector = std::make_shared<Sector>(
trackLayout, LogicalLocation{track, side, sectorid});
sector->physicalCylinder = Layout::remapCylinderLogicalToPhysical(track);
sector->physicalHead = side;
_sectors[{track, side, sectorid}] = sector;
return sector;
}
void Image::erase(unsigned track, unsigned side, unsigned sectorid)
{
_sectors.erase({track, side, sectorid});
}
void Image::calculateSize()
{
_geometry = {};
unsigned maxSector = 0;
for (const auto& i : _sectors)
{
const auto& sector = i.second;
if (sector)
{
_geometry.numCylinders = std::max(
_geometry.numCylinders, (unsigned)sector->logicalCylinder + 1);
_geometry.numHeads =
std::max(_geometry.numHeads, (unsigned)sector->logicalHead + 1);
_geometry.firstSector = std::min(
_geometry.firstSector, (unsigned)sector->logicalSector);
maxSector = std::max(maxSector, (unsigned)sector->logicalSector);
_geometry.sectorSize =
std::max(_geometry.sectorSize, sector->trackLayout->sectorSize);
_geometry.totalBytes += sector->trackLayout->sectorSize;
}
}
_geometry.numSectors = maxSector - _geometry.firstSector + 1;
}