| 1 |
#include "DirAsDSK.hh" |
| 2 |
|
| 3 |
#include "BootBlocks.hh" |
| 4 |
#include "CliComm.hh" |
| 5 |
#include "DiskChanger.hh" |
| 6 |
#include "File.hh" |
| 7 |
#include "FileException.hh" |
| 8 |
#include "ReadDir.hh" |
| 9 |
#include "Scheduler.hh" |
| 10 |
|
| 11 |
#include "StringOp.hh" |
| 12 |
#include "narrow.hh" |
| 13 |
#include "one_of.hh" |
| 14 |
#include "ranges.hh" |
| 15 |
#include "stl.hh" |
| 16 |
#include "xrange.hh" |
| 17 |
|
| 18 |
#include <algorithm> |
| 19 |
#include <cassert> |
| 20 |
#include <cstring> |
| 21 |
#include <vector> |
| 22 |
|
| 23 |
namespace openmsx { |
| 24 |
|
| 25 |
static constexpr unsigned SECTOR_SIZE = sizeof(SectorBuffer); |
| 26 |
static constexpr unsigned NUM_FATS = 2; |
| 27 |
static constexpr unsigned SECTORS_PER_TRACK = 9; |
| 28 |
static constexpr unsigned FIRST_FAT_SECTOR = 1; |
| 29 |
static constexpr unsigned DIR_ENTRIES_PER_SECTOR = |
| 30 |
SECTOR_SIZE / sizeof(MSXDirEntry); |
| 31 |
|
| 32 |
|
| 33 |
static constexpr unsigned FIRST_CLUSTER = 2; |
| 34 |
|
| 35 |
static constexpr unsigned FREE_FAT = 0x000; |
| 36 |
static constexpr unsigned BAD_FAT = 0xFF7; |
| 37 |
static constexpr unsigned EOF_FAT = 0xFFF; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
[[nodiscard]] static constexpr unsigned normalizeFAT(unsigned cluster) |
| 43 |
{ |
| 44 |
return (cluster < BAD_FAT) ? cluster : EOF_FAT; |
| 45 |
} |
| 46 |
|
| 47 |
unsigned DirAsDSK::readFATHelper(std::span<const SectorBuffer> fatBuf, unsigned cluster) const |
| 48 |
{ |
| 49 |
assert(FIRST_CLUSTER <= cluster); |
| 50 |
assert(cluster < maxCluster); |
| 51 |
std::span buf{fatBuf[0].raw.data(), fatBuf.size() * SECTOR_SIZE}; |
| 52 |
auto p = subspan<2>(buf, (cluster * 3) / 2); |
| 53 |
unsigned result = (cluster & 1) |
| 54 |
? (p[0] >> 4) + (p[1] << 4) |
| 55 |
: p[0] + ((p[1] & 0x0F) << 8); |
| 56 |
return normalizeFAT(result); |
| 57 |
} |
| 58 |
|
| 59 |
void DirAsDSK::writeFATHelper(std::span<SectorBuffer> fatBuf, unsigned cluster, unsigned val) const |
| 60 |
{ |
| 61 |
assert(FIRST_CLUSTER <= cluster); |
| 62 |
assert(cluster < maxCluster); |
| 63 |
std::span buf{fatBuf[0].raw.data(), fatBuf.size() * SECTOR_SIZE}; |
| 64 |
auto p = subspan<2>(buf, (cluster * 3) / 2); |
| 65 |
if (cluster & 1) { |
| 66 |
p[0] = narrow_cast<uint8_t>((p[0] & 0x0F) + (val << 4)); |
| 67 |
p[1] = narrow_cast<uint8_t>(val >> 4); |
| 68 |
} else { |
| 69 |
p[0] = narrow_cast<uint8_t>(val); |
| 70 |
p[1] = narrow_cast<uint8_t>((p[1] & 0xF0) + ((val >> 8) & 0x0F)); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
std::span<SectorBuffer> DirAsDSK::fat() |
| 75 |
{ |
| 76 |
return {§ors[FIRST_FAT_SECTOR], nofSectorsPerFat}; |
| 77 |
} |
| 78 |
std::span<SectorBuffer> DirAsDSK::fat2() |
| 79 |
{ |
| 80 |
return {§ors[firstSector2ndFAT], nofSectorsPerFat}; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
unsigned DirAsDSK::readFAT(unsigned cluster) |
| 85 |
{ |
| 86 |
return readFATHelper(fat(), cluster); |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
void DirAsDSK::writeFAT12(unsigned cluster, unsigned val) |
| 91 |
{ |
| 92 |
writeFATHelper(fat (), cluster, val); |
| 93 |
writeFATHelper(fat2(), cluster, val); |
| 94 |
|
| 95 |
|
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
unsigned DirAsDSK::findNextFreeCluster(unsigned cluster) |
| 100 |
{ |
| 101 |
assert(cluster < maxCluster); |
| 102 |
do { |
| 103 |
++cluster; |
| 104 |
assert(cluster >= FIRST_CLUSTER); |
| 105 |
} while ((cluster < maxCluster) && (readFAT(cluster) != FREE_FAT)); |
| 106 |
return cluster; |
| 107 |
} |
| 108 |
unsigned DirAsDSK::findFirstFreeCluster() |
| 109 |
{ |
| 110 |
return findNextFreeCluster(FIRST_CLUSTER - 1); |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
unsigned DirAsDSK::getFreeCluster() |
| 115 |
{ |
| 116 |
unsigned cluster = findFirstFreeCluster(); |
| 117 |
if (cluster == maxCluster) { |
| 118 |
throw MSXException("disk full"); |
| 119 |
} |
| 120 |
return cluster; |
| 121 |
} |
| 122 |
|
| 123 |
unsigned DirAsDSK::clusterToSector(unsigned cluster) const |
| 124 |
{ |
| 125 |
assert(cluster >= FIRST_CLUSTER); |
| 126 |
assert(cluster < maxCluster); |
| 127 |
return firstDataSector + sectorsPerCluster * |
| 128 |
(cluster - FIRST_CLUSTER); |
| 129 |
} |
| 130 |
|
| 131 |
std::pair<unsigned, unsigned> DirAsDSK::sectorToClusterOffset(unsigned sector) const |
| 132 |
{ |
| 133 |
assert(sector >= firstDataSector); |
| 134 |
assert(sector < nofSectors); |
| 135 |
sector -= firstDataSector; |
| 136 |
unsigned cluster = (sector / sectorsPerCluster) + FIRST_CLUSTER; |
| 137 |
unsigned offset = (sector % sectorsPerCluster) * SECTOR_SIZE; |
| 138 |
return {cluster, offset}; |
| 139 |
} |
| 140 |
unsigned DirAsDSK::sectorToCluster(unsigned sector) const |
| 141 |
{ |
| 142 |
auto [cluster, offset] = sectorToClusterOffset(sector); |
| 143 |
return cluster; |
| 144 |
} |
| 145 |
|
| 146 |
MSXDirEntry& DirAsDSK::msxDir(DirIndex dirIndex) |
| 147 |
{ |
| 148 |
assert(dirIndex.sector < nofSectors); |
| 149 |
assert(dirIndex.idx < DIR_ENTRIES_PER_SECTOR); |
| 150 |
return sectors[dirIndex.sector].dirEntry[dirIndex.idx]; |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
unsigned DirAsDSK::nextMsxDirSector(unsigned sector) |
| 155 |
{ |
| 156 |
if (sector < firstDataSector) { |
| 157 |
|
| 158 |
assert(firstDirSector <= sector); |
| 159 |
++sector; |
| 160 |
if (sector == firstDataSector) { |
| 161 |
|
| 162 |
return unsigned(-1); |
| 163 |
} |
| 164 |
return sector; |
| 165 |
} else { |
| 166 |
|
| 167 |
auto [cluster, offset] = sectorToClusterOffset(sector); |
| 168 |
if (offset < ((sectorsPerCluster - 1) * SECTOR_SIZE)) { |
| 169 |
|
| 170 |
return sector + 1; |
| 171 |
} |
| 172 |
unsigned nextCl = readFAT(cluster); |
| 173 |
if ((nextCl < FIRST_CLUSTER) || (maxCluster <= nextCl)) { |
| 174 |
|
| 175 |
return unsigned(-1); |
| 176 |
} |
| 177 |
return clusterToSector(nextCl); |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
bool DirAsDSK::checkMSXFileExists( |
| 183 |
std::span<const char, 11> msxFilename, unsigned msxDirSector) |
| 184 |
{ |
| 185 |
std::vector<bool> visited(nofSectors, false); |
| 186 |
do { |
| 187 |
if (visited[msxDirSector]) { |
| 188 |
|
| 189 |
return false; |
| 190 |
} |
| 191 |
visited[msxDirSector] = true; |
| 192 |
|
| 193 |
for (auto idx : xrange(DIR_ENTRIES_PER_SECTOR)) { |
| 194 |
DirIndex dirIndex(msxDirSector, idx); |
| 195 |
if (std::ranges::equal(msxDir(dirIndex).filename, msxFilename)) { |
| 196 |
return true; |
| 197 |
} |
| 198 |
} |
| 199 |
msxDirSector = nextMsxDirSector(msxDirSector); |
| 200 |
} while (msxDirSector != unsigned(-1)); |
| 201 |
|
| 202 |
|
| 203 |
return false; |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
DirAsDSK::DirIndex DirAsDSK::findHostFileInDSK(std::string_view hostName) const |
| 209 |
{ |
| 210 |
for (const auto& [dirIdx, mapDir] : mapDirs) { |
| 211 |
if (mapDir.hostName == hostName) { |
| 212 |
return dirIdx; |
| 213 |
} |
| 214 |
} |
| 215 |
return {unsigned(-1), unsigned(-1)}; |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
bool DirAsDSK::checkFileUsedInDSK(std::string_view hostName) const |
| 220 |
{ |
| 221 |
DirIndex dirIndex = findHostFileInDSK(hostName); |
| 222 |
return dirIndex.sector != unsigned(-1); |
| 223 |
} |
| 224 |
|
| 225 |
static std::array<char, 11> hostToMsxName(std::string hostName) |
| 226 |
{ |
| 227 |
|
| 228 |
transform_in_place(hostName, [](char a) { |
| 229 |
return (a == ' ') ? '_' : ::toupper(a); |
| 230 |
}); |
| 231 |
auto [file, ext] = StringOp::splitOnLast(hostName, '.'); |
| 232 |
if (file.empty()) std::swap(file, ext); |
| 233 |
|
| 234 |
std::array<char, 8 + 3> result; |
| 235 |
std::ranges::fill(result, ' '); |
| 236 |
copy_to_range(subspan(file, 0, std::min<size_t>(8, file.size())), subspan<8>(result, 0)); |
| 237 |
#if defined(__GNUC__) && !defined(__clang__) |
| 238 |
#pragma GCC diagnostic push |
| 239 |
#pragma GCC diagnostic ignored "-Wstringop-overflow" |
| 240 |
#endif |
| 241 |
copy_to_range(subspan(ext, 0, std::min<size_t>(3, ext .size())), subspan<3>(result, 8)); |
| 242 |
#if defined(__GNUC__) && !defined(__clang__) |
| 243 |
#pragma GCC diagnostic pop |
| 244 |
#endif |
| 245 |
std::ranges::replace(result, '.', '_'); |
| 246 |
return result; |
| 247 |
} |
| 248 |
|
| 249 |
static std::string msxToHostName(std::span<const char, 11> msxName) |
| 250 |
{ |
| 251 |
std::string result; |
| 252 |
for (unsigned i = 0; (i < 8) && (msxName[i] != ' '); ++i) { |
| 253 |
result += char(tolower(msxName[i])); |
| 254 |
} |
| 255 |
if (msxName[8] != ' ') { |
| 256 |
result += '.'; |
| 257 |
for (unsigned i = 8; (i < (8 + 3)) && (msxName[i] != ' '); ++i) { |
| 258 |
result += char(tolower(msxName[i])); |
| 259 |
} |
| 260 |
} |
| 261 |
return result; |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
DirAsDSK::DiskFormat DirAsDSK::getDiskFormat(unsigned numTracks, bool doubleSided) |
| 275 |
{ |
| 276 |
if (numTracks == 40) { |
| 277 |
return doubleSided ? DiskFormat{0xFD, 2, 7} |
| 278 |
: DiskFormat{0xFC, 1, 4}; |
| 279 |
} |
| 280 |
return doubleSided ? DiskFormat{0xF9, 2, 7} |
| 281 |
: DiskFormat{0xF8, 2, 7}; |
| 282 |
} |
| 283 |
|
| 284 |
DirAsDSK::DirAsDSK(DiskChanger& diskChanger_, CliComm& cliComm_, |
| 285 |
const Filename& hostDir_, SyncMode syncMode_, |
| 286 |
BootSectorType bootSectorType) |
| 287 |
: SectorBasedDisk(DiskName(hostDir_)) |
| 288 |
, diskChanger(diskChanger_) |
| 289 |
, cliComm(cliComm_) |
| 290 |
, hostDir(FileOperations::expandTilde(hostDir_.getResolved() + '/')) |
| 291 |
, syncMode(syncMode_) |
| 292 |
, diskFormat(getDiskFormat(diskChanger_.getNominalTracks(), diskChanger_.isDoubleSidedDrive())) |
| 293 |
, sectorsPerCluster(diskFormat.sectorsPerCluster) |
| 294 |
, sectorsPerDir(diskFormat.sectorsPerDir) |
| 295 |
, nofSectors((diskChanger_.isDoubleSidedDrive() ? 2 : 1) * SECTORS_PER_TRACK * diskChanger_.getNominalTracks()) |
| 296 |
, nofSectorsPerFat(narrow<unsigned>((((3 * nofSectors) / (2 * sectorsPerCluster)) + SECTOR_SIZE - 1) / SECTOR_SIZE)) |
| 297 |
, firstSector2ndFAT(FIRST_FAT_SECTOR + nofSectorsPerFat) |
| 298 |
, firstDirSector(FIRST_FAT_SECTOR + NUM_FATS * nofSectorsPerFat) |
| 299 |
, firstDataSector(firstDirSector + sectorsPerDir) |
| 300 |
, maxCluster((nofSectors - firstDataSector) / sectorsPerCluster + FIRST_CLUSTER) |
| 301 |
, sectors(nofSectors) |
| 302 |
{ |
| 303 |
if (!FileOperations::isDirectory(hostDir)) { |
| 304 |
throw MSXException("Not a directory"); |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
uint8_t numSides = diskChanger_.isDoubleSidedDrive() ? 2 : 1; |
| 309 |
setNbSectors(nofSectors); |
| 310 |
setSectorsPerTrack(SECTORS_PER_TRACK); |
| 311 |
setNbSides(numSides); |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
std::ranges::fill(std::span{sectors[0].raw.data(), sizeof(SectorBuffer) * nofSectors}, 0xE5); |
| 316 |
|
| 317 |
|
| 318 |
uint8_t mediaDescriptor = diskFormat.mediaDescriptor; |
| 319 |
const auto& protoBootSector = bootSectorType == BootSectorType::DOS1 |
| 320 |
? BootBlocks::dos1BootBlock |
| 321 |
: BootBlocks::dos2BootBlock; |
| 322 |
sectors[0] = protoBootSector; |
| 323 |
auto& bootSector = sectors[0].bootSector; |
| 324 |
bootSector.bpSector = SECTOR_SIZE; |
| 325 |
bootSector.spCluster = sectorsPerCluster; |
| 326 |
bootSector.nrFats = NUM_FATS; |
| 327 |
bootSector.dirEntries = sectorsPerDir * (SECTOR_SIZE / sizeof(MSXDirEntry)); |
| 328 |
bootSector.nrSectors = narrow_cast<uint16_t>(nofSectors); |
| 329 |
bootSector.descriptor = mediaDescriptor; |
| 330 |
bootSector.sectorsFat = narrow_cast<uint16_t>(nofSectorsPerFat); |
| 331 |
bootSector.sectorsTrack = SECTORS_PER_TRACK; |
| 332 |
bootSector.nrSides = numSides; |
| 333 |
|
| 334 |
|
| 335 |
std::ranges::fill(std::span{fat()[0].raw.data(), SECTOR_SIZE * nofSectorsPerFat * NUM_FATS}, 0); |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
auto init = [&](auto f) { |
| 341 |
f[0].raw[0] = mediaDescriptor; |
| 342 |
f[0].raw[1] = 0xFF; |
| 343 |
f[0].raw[2] = 0xFF; |
| 344 |
}; |
| 345 |
init(fat()); |
| 346 |
init(fat2()); |
| 347 |
|
| 348 |
|
| 349 |
std::ranges::fill(std::span{sectors[firstDirSector].raw.data(), SECTOR_SIZE * sectorsPerDir}, 0); |
| 350 |
|
| 351 |
|
| 352 |
assert(mapDirs.empty()); |
| 353 |
|
| 354 |
|
| 355 |
syncWithHost(); |
| 356 |
} |
| 357 |
|
| 358 |
bool DirAsDSK::isWriteProtectedImpl() const |
| 359 |
{ |
| 360 |
return syncMode == SyncMode::READONLY; |
| 361 |
} |
| 362 |
|
| 363 |
bool DirAsDSK::hasChanged() const |
| 364 |
{ |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
return true; |
| 371 |
} |
| 372 |
|
| 373 |
void DirAsDSK::checkCaches() |
| 374 |
{ |
| 375 |
bool needSync = [&] { |
| 376 |
if (const auto* scheduler = diskChanger.getScheduler()) { |
| 377 |
auto now = scheduler->getCurrentTime(); |
| 378 |
auto delta = now - lastAccess; |
| 379 |
return delta > EmuDuration::sec(1); |
| 380 |
|
| 381 |
|
| 382 |
} else { |
| 383 |
|
| 384 |
return true; |
| 385 |
} |
| 386 |
}(); |
| 387 |
if (needSync) { |
| 388 |
flushCaches(); |
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
void DirAsDSK::readSectorImpl(size_t sector, SectorBuffer& buf) |
| 393 |
{ |
| 394 |
assert(sector < nofSectors); |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
if (!isPeekMode()) { |
| 401 |
bool needSync = [&] { |
| 402 |
if (const auto* scheduler = diskChanger.getScheduler()) { |
| 403 |
auto now = scheduler->getCurrentTime(); |
| 404 |
auto delta = now - lastAccess; |
| 405 |
lastAccess = now; |
| 406 |
return delta > EmuDuration::sec(1); |
| 407 |
} else { |
| 408 |
|
| 409 |
return true; |
| 410 |
} |
| 411 |
}(); |
| 412 |
if (needSync) { |
| 413 |
syncWithHost(); |
| 414 |
flushCaches(); |
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
diskChanger.forceDiskChange(); |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
|
| 423 |
buf = sectors[sector]; |
| 424 |
} |
| 425 |
|
| 426 |
void DirAsDSK::syncWithHost() |
| 427 |
{ |
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
checkDeletedHostFiles(); |
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
checkModifiedHostFiles(); |
| 438 |
|
| 439 |
|
| 440 |
addNewHostFiles({}, firstDirSector); |
| 441 |
} |
| 442 |
|
| 443 |
void DirAsDSK::checkDeletedHostFiles() |
| 444 |
{ |
| 445 |
|
| 446 |
auto copy = mapDirs; |
| 447 |
for (const auto& [dirIdx, mapDir] : copy) { |
| 448 |
if (!mapDirs.contains(dirIdx)) { |
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
continue; |
| 458 |
} |
| 459 |
auto fullHostName = tmpStrCat(hostDir, mapDir.hostName); |
| 460 |
auto isMSXDirectory = bool(msxDir(dirIdx).attrib & |
| 461 |
MSXDirEntry::Attrib::DIRECTORY); |
| 462 |
auto fst = FileOperations::getStat(fullHostName); |
| 463 |
if (!fst || (FileOperations::isDirectory(*fst) != isMSXDirectory)) { |
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
deleteMSXFile(dirIdx); |
| 471 |
} |
| 472 |
} |
| 473 |
} |
| 474 |
|
| 475 |
void DirAsDSK::deleteMSXFile(DirIndex dirIndex) |
| 476 |
{ |
| 477 |
|
| 478 |
mapDirs.erase(dirIndex); |
| 479 |
|
| 480 |
if (msxDir(dirIndex).filename[0] == one_of(0, char(0xE5))) { |
| 481 |
|
| 482 |
return; |
| 483 |
} |
| 484 |
|
| 485 |
if (msxDir(dirIndex).attrib & MSXDirEntry::Attrib::DIRECTORY) { |
| 486 |
|
| 487 |
|
| 488 |
if (const auto& msxName = msxDir(dirIndex).filename; |
| 489 |
std::ranges::equal(msxName, std::string_view(". ")) || |
| 490 |
std::ranges::equal(msxName, std::string_view(".. "))) { |
| 491 |
|
| 492 |
return; |
| 493 |
} |
| 494 |
|
| 495 |
unsigned cluster = msxDir(dirIndex).startCluster; |
| 496 |
if ((FIRST_CLUSTER <= cluster) && (cluster < maxCluster)) { |
| 497 |
|
| 498 |
deleteMSXFilesInDir(clusterToSector(cluster)); |
| 499 |
} |
| 500 |
} |
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
msxDir(dirIndex).filename[0] = char(0xE5); |
| 505 |
|
| 506 |
|
| 507 |
freeFATChain(msxDir(dirIndex).startCluster); |
| 508 |
} |
| 509 |
|
| 510 |
void DirAsDSK::deleteMSXFilesInDir(unsigned msxDirSector) |
| 511 |
{ |
| 512 |
std::vector<bool> visited(nofSectors, false); |
| 513 |
do { |
| 514 |
if (visited[msxDirSector]) { |
| 515 |
|
| 516 |
return; |
| 517 |
} |
| 518 |
visited[msxDirSector] = true; |
| 519 |
|
| 520 |
for (auto idx : xrange(DIR_ENTRIES_PER_SECTOR)) { |
| 521 |
deleteMSXFile(DirIndex(msxDirSector, idx)); |
| 522 |
} |
| 523 |
msxDirSector = nextMsxDirSector(msxDirSector); |
| 524 |
} while (msxDirSector != unsigned(-1)); |
| 525 |
} |
| 526 |
|
| 527 |
void DirAsDSK::freeFATChain(unsigned cluster) |
| 528 |
{ |
| 529 |
|
| 530 |
while ((FIRST_CLUSTER <= cluster) && (cluster < maxCluster)) { |
| 531 |
unsigned nextCl = readFAT(cluster); |
| 532 |
writeFAT12(cluster, FREE_FAT); |
| 533 |
cluster = nextCl; |
| 534 |
} |
| 535 |
} |
| 536 |
|
| 537 |
void DirAsDSK::checkModifiedHostFiles() |
| 538 |
{ |
| 539 |
auto copy = mapDirs; |
| 540 |
for (const auto& [dirIdx, mapDir] : copy) { |
| 541 |
if (!mapDirs.contains(dirIdx)) { |
| 542 |
|
| 543 |
continue; |
| 544 |
} |
| 545 |
auto fullHostName = tmpStrCat(hostDir, mapDir.hostName); |
| 546 |
auto isMSXDirectory = bool(msxDir(dirIdx).attrib & |
| 547 |
MSXDirEntry::Attrib::DIRECTORY); |
| 548 |
auto fst = FileOperations::getStat(fullHostName); |
| 549 |
if (fst && (FileOperations::isDirectory(*fst) == isMSXDirectory)) { |
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
if (!isMSXDirectory && |
| 560 |
((mapDir.mtime != fst->st_mtime) || |
| 561 |
(mapDir.filesize != size_t(fst->st_size)))) { |
| 562 |
importHostFile(dirIdx, *fst); |
| 563 |
} |
| 564 |
} else { |
| 565 |
|
| 566 |
|
| 567 |
deleteMSXFile(dirIdx); |
| 568 |
} |
| 569 |
} |
| 570 |
} |
| 571 |
|
| 572 |
void DirAsDSK::importHostFile(DirIndex dirIndex, const FileOperations::Stat& fst) |
| 573 |
{ |
| 574 |
assert(!(msxDir(dirIndex).attrib & MSXDirEntry::Attrib::DIRECTORY)); |
| 575 |
assert(mapDirs.contains(dirIndex)); |
| 576 |
|
| 577 |
|
| 578 |
setMSXTimeStamp(dirIndex, fst); |
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
size_t hostSize = fst.st_size; |
| 594 |
auto& mapDir = mapDirs[dirIndex]; |
| 595 |
mapDir.filesize = hostSize; |
| 596 |
mapDir.mtime = fst.st_mtime; |
| 597 |
|
| 598 |
bool moreClustersInChain = true; |
| 599 |
unsigned curCl = msxDir(dirIndex).startCluster; |
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
if ((curCl < FIRST_CLUSTER) || (curCl >= maxCluster)) { |
| 604 |
moreClustersInChain = false; |
| 605 |
curCl = findFirstFreeCluster(); |
| 606 |
} |
| 607 |
|
| 608 |
auto remainingSize = hostSize; |
| 609 |
unsigned prevCl = 0; |
| 610 |
try { |
| 611 |
File file(hostDir + mapDir.hostName, |
| 612 |
"rb"); |
| 613 |
|
| 614 |
while (remainingSize && (curCl < maxCluster)) { |
| 615 |
unsigned logicalSector = clusterToSector(curCl); |
| 616 |
for (auto i : xrange(sectorsPerCluster)) { |
| 617 |
unsigned sector = logicalSector + i; |
| 618 |
assert(sector < nofSectors); |
| 619 |
auto* buf = §ors[sector]; |
| 620 |
auto sz = std::min(remainingSize, SECTOR_SIZE); |
| 621 |
file.read(subspan(buf->raw, 0, sz)); |
| 622 |
std::ranges::fill(subspan(buf->raw, sz), 0); |
| 623 |
remainingSize -= sz; |
| 624 |
if (remainingSize == 0) { |
| 625 |
|
| 626 |
|
| 627 |
break; |
| 628 |
} |
| 629 |
} |
| 630 |
|
| 631 |
if (prevCl) { |
| 632 |
writeFAT12(prevCl, curCl); |
| 633 |
} else { |
| 634 |
msxDir(dirIndex).startCluster = narrow<uint16_t>(curCl); |
| 635 |
} |
| 636 |
prevCl = curCl; |
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
if (moreClustersInChain) { |
| 641 |
curCl = readFAT(curCl); |
| 642 |
if ((curCl == EOF_FAT) || |
| 643 |
(curCl < FIRST_CLUSTER) || |
| 644 |
(curCl >= maxCluster)) { |
| 645 |
|
| 646 |
|
| 647 |
moreClustersInChain = false; |
| 648 |
curCl = findFirstFreeCluster(); |
| 649 |
} |
| 650 |
} else { |
| 651 |
curCl = findNextFreeCluster(curCl); |
| 652 |
} |
| 653 |
} |
| 654 |
if (remainingSize != 0) { |
| 655 |
cliComm.printWarning("Virtual disk image full: ", |
| 656 |
mapDir.hostName, " truncated."); |
| 657 |
} |
| 658 |
} catch (FileException& e) { |
| 659 |
|
| 660 |
cliComm.printWarning("Error reading host file: ", |
| 661 |
mapDir.hostName, ": ", e.getMessage(), |
| 662 |
" Truncated file on MSX disk."); |
| 663 |
} |
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
if (prevCl) { |
| 668 |
writeFAT12(prevCl, EOF_FAT); |
| 669 |
} else { |
| 670 |
|
| 671 |
|
| 672 |
msxDir(dirIndex).startCluster = FREE_FAT; |
| 673 |
} |
| 674 |
|
| 675 |
|
| 676 |
if (moreClustersInChain) { |
| 677 |
freeFATChain(curCl); |
| 678 |
} |
| 679 |
|
| 680 |
|
| 681 |
msxDir(dirIndex).size = uint32_t(hostSize - remainingSize); |
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
} |
| 689 |
|
| 690 |
void DirAsDSK::setMSXTimeStamp(DirIndex dirIndex, const FileOperations::Stat& fst) |
| 691 |
{ |
| 692 |
|
| 693 |
time_t mtime = fst.st_mtime; |
| 694 |
const auto* mtim = localtime(&mtime); |
| 695 |
int t1 = mtim ? (mtim->tm_sec >> 1) + (mtim->tm_min << 5) + |
| 696 |
(mtim->tm_hour << 11) |
| 697 |
: 0; |
| 698 |
msxDir(dirIndex).time = narrow<uint16_t>(t1); |
| 699 |
int t2 = mtim ? mtim->tm_mday + ((mtim->tm_mon + 1) << 5) + |
| 700 |
((mtim->tm_year + 1900 - 1980) << 9) |
| 701 |
: 0; |
| 702 |
msxDir(dirIndex).date = narrow<uint16_t>(t2); |
| 703 |
} |
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
static size_t weight(const std::string& hostName) |
| 714 |
{ |
| 715 |
|
| 716 |
size_t result = 0; |
| 717 |
auto [file, ext] = StringOp::splitOnLast(hostName, '.'); |
| 718 |
|
| 719 |
result += std::ranges::count(file, '.') * 100; |
| 720 |
|
| 721 |
result += ext.size() * 10; |
| 722 |
|
| 723 |
result += file.size(); |
| 724 |
return result; |
| 725 |
} |
| 726 |
|
| 727 |
void DirAsDSK::addNewHostFiles(const std::string& hostSubDir, unsigned msxDirSector) |
| 728 |
{ |
| 729 |
assert(!hostSubDir.starts_with('/')); |
| 730 |
assert(hostSubDir.empty() || hostSubDir.ends_with('/')); |
| 731 |
|
| 732 |
std::vector<std::string> hostNames; |
| 733 |
{ |
| 734 |
ReadDir dir(tmpStrCat(hostDir, hostSubDir)); |
| 735 |
while (auto* d = dir.getEntry()) { |
| 736 |
hostNames.emplace_back(d->d_name); |
| 737 |
} |
| 738 |
} |
| 739 |
std::ranges::sort(hostNames, {}, [](const std::string& n) { return weight(n); }); |
| 740 |
|
| 741 |
for (auto& hostName : hostNames) { |
| 742 |
try { |
| 743 |
if (hostName.starts_with('.')) { |
| 744 |
|
| 745 |
|
| 746 |
continue; |
| 747 |
} |
| 748 |
auto fullHostName = tmpStrCat(hostDir, hostSubDir, hostName); |
| 749 |
auto fst = FileOperations::getStat(fullHostName); |
| 750 |
if (!fst) { |
| 751 |
throw MSXException("Error accessing ", fullHostName); |
| 752 |
} |
| 753 |
if (FileOperations::isDirectory(*fst)) { |
| 754 |
addNewDirectory(hostSubDir, hostName, msxDirSector, *fst); |
| 755 |
} else if (FileOperations::isRegularFile(*fst)) { |
| 756 |
addNewHostFile(hostSubDir, hostName, msxDirSector, *fst); |
| 757 |
} else { |
| 758 |
throw MSXException("Not a regular file: ", fullHostName); |
| 759 |
} |
| 760 |
} catch (MSXException& e) { |
| 761 |
cliComm.printWarning(e.getMessage()); |
| 762 |
} |
| 763 |
} |
| 764 |
} |
| 765 |
|
| 766 |
void DirAsDSK::addNewDirectory(const std::string& hostSubDir, const std::string& hostName, |
| 767 |
unsigned msxDirSector, const FileOperations::Stat& fst) |
| 768 |
{ |
| 769 |
DirIndex dirIndex = findHostFileInDSK(tmpStrCat(hostSubDir, hostName)); |
| 770 |
unsigned newMsxDirSector; |
| 771 |
if (dirIndex.sector == unsigned(-1)) { |
| 772 |
|
| 773 |
|
| 774 |
unsigned cluster = getFreeCluster(); |
| 775 |
writeFAT12(cluster, EOF_FAT); |
| 776 |
|
| 777 |
|
| 778 |
try { |
| 779 |
dirIndex = fillMSXDirEntry(hostSubDir, hostName, msxDirSector); |
| 780 |
} catch (...) { |
| 781 |
|
| 782 |
writeFAT12(cluster, FREE_FAT); |
| 783 |
throw; |
| 784 |
} |
| 785 |
setMSXTimeStamp(dirIndex, fst); |
| 786 |
msxDir(dirIndex).attrib = MSXDirEntry::Attrib::DIRECTORY; |
| 787 |
msxDir(dirIndex).startCluster = narrow<uint16_t>(cluster); |
| 788 |
|
| 789 |
|
| 790 |
newMsxDirSector = clusterToSector(cluster); |
| 791 |
std::ranges::fill(std::span{sectors[newMsxDirSector].raw.data(), |
| 792 |
sectorsPerCluster * SECTOR_SIZE}, |
| 793 |
0); |
| 794 |
DirIndex idx0(newMsxDirSector, 0); |
| 795 |
DirIndex idx1(newMsxDirSector, 1); |
| 796 |
auto& e0 = msxDir(idx0); |
| 797 |
auto& e1 = msxDir(idx1); |
| 798 |
auto& f0 = e0.filename; |
| 799 |
auto& f1 = e1.filename; |
| 800 |
f0[0] = '.'; std::ranges::fill(subspan(f0, 1), ' '); |
| 801 |
f1[0] = '.'; f1[1] = '.'; std::ranges::fill(subspan(f1, 2), ' '); |
| 802 |
e0.attrib = MSXDirEntry::Attrib::DIRECTORY; |
| 803 |
e1.attrib = MSXDirEntry::Attrib::DIRECTORY; |
| 804 |
setMSXTimeStamp(idx0, fst); |
| 805 |
setMSXTimeStamp(idx1, fst); |
| 806 |
e0.startCluster = narrow<uint16_t>(cluster); |
| 807 |
e1.startCluster = msxDirSector == firstDirSector |
| 808 |
? uint16_t(0) |
| 809 |
: narrow<uint16_t>(sectorToCluster(msxDirSector)); |
| 810 |
} else { |
| 811 |
if (!(msxDir(dirIndex).attrib & MSXDirEntry::Attrib::DIRECTORY)) { |
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
return; |
| 818 |
} |
| 819 |
unsigned cluster = msxDir(dirIndex).startCluster; |
| 820 |
if ((cluster < FIRST_CLUSTER) || (cluster >= maxCluster)) { |
| 821 |
|
| 822 |
return; |
| 823 |
} |
| 824 |
newMsxDirSector = clusterToSector(cluster); |
| 825 |
} |
| 826 |
|
| 827 |
|
| 828 |
addNewHostFiles(strCat(hostSubDir, hostName, '/'), newMsxDirSector); |
| 829 |
} |
| 830 |
|
| 831 |
void DirAsDSK::addNewHostFile(const std::string& hostSubDir, const std::string& hostName, |
| 832 |
unsigned msxDirSector, const FileOperations::Stat& fst) |
| 833 |
{ |
| 834 |
if (checkFileUsedInDSK(tmpStrCat(hostSubDir, hostName))) { |
| 835 |
|
| 836 |
return; |
| 837 |
} |
| 838 |
|
| 839 |
if (auto diskSpace = (nofSectors - firstDataSector) * SECTOR_SIZE; |
| 840 |
narrow<size_t>(fst.st_size) > diskSpace) { |
| 841 |
cliComm.printWarning("File too large: ", |
| 842 |
hostDir, hostSubDir, hostName); |
| 843 |
return; |
| 844 |
} |
| 845 |
|
| 846 |
DirIndex dirIndex = fillMSXDirEntry(hostSubDir, hostName, msxDirSector); |
| 847 |
importHostFile(dirIndex, fst); |
| 848 |
} |
| 849 |
|
| 850 |
DirAsDSK::DirIndex DirAsDSK::fillMSXDirEntry( |
| 851 |
const std::string& hostSubDir, const std::string& hostName, unsigned msxDirSector) |
| 852 |
{ |
| 853 |
std::string hostPath = hostSubDir + hostName; |
| 854 |
try { |
| 855 |
|
| 856 |
DirIndex dirIndex = getFreeDirEntry(msxDirSector); |
| 857 |
|
| 858 |
|
| 859 |
auto msxFilename = hostToMsxName(hostName); |
| 860 |
if (checkMSXFileExists(msxFilename, msxDirSector)) { |
| 861 |
|
| 862 |
throw MSXException( |
| 863 |
"MSX name ", msxToHostName(msxFilename), |
| 864 |
" already exists"); |
| 865 |
} |
| 866 |
|
| 867 |
|
| 868 |
assert(!hostPath.ends_with('/')); |
| 869 |
mapDirs[dirIndex].hostName = hostPath; |
| 870 |
memset(&msxDir(dirIndex), 0, sizeof(MSXDirEntry)); |
| 871 |
copy_to_range(msxFilename, msxDir(dirIndex).filename); |
| 872 |
return dirIndex; |
| 873 |
} catch (MSXException& e) { |
| 874 |
throw MSXException("Couldn't add ", hostPath, ": ", |
| 875 |
e.getMessage()); |
| 876 |
} |
| 877 |
} |
| 878 |
|
| 879 |
DirAsDSK::DirIndex DirAsDSK::getFreeDirEntry(unsigned msxDirSector) |
| 880 |
{ |
| 881 |
std::vector<bool> visited(nofSectors, false); |
| 882 |
while (true) { |
| 883 |
if (visited[msxDirSector]) { |
| 884 |
|
| 885 |
throw MSXException("cycle in FAT"); |
| 886 |
} |
| 887 |
visited[msxDirSector] = true; |
| 888 |
|
| 889 |
for (auto idx : xrange(DIR_ENTRIES_PER_SECTOR)) { |
| 890 |
DirIndex dirIndex(msxDirSector, idx); |
| 891 |
const auto& msxName = msxDir(dirIndex).filename; |
| 892 |
if (msxName[0] == one_of(char(0x00), char(0xE5))) { |
| 893 |
|
| 894 |
|
| 895 |
assert(!mapDirs.contains(dirIndex)); |
| 896 |
return dirIndex; |
| 897 |
} |
| 898 |
} |
| 899 |
unsigned sector = nextMsxDirSector(msxDirSector); |
| 900 |
if (sector == unsigned(-1)) break; |
| 901 |
msxDirSector = sector; |
| 902 |
} |
| 903 |
|
| 904 |
|
| 905 |
if (msxDirSector == (firstDataSector - 1)) { |
| 906 |
|
| 907 |
throw MSXException("root directory full"); |
| 908 |
} |
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
unsigned cluster = sectorToCluster(msxDirSector); |
| 913 |
unsigned newCluster = getFreeCluster(); |
| 914 |
unsigned sector = clusterToSector(newCluster); |
| 915 |
std::ranges::fill(std::span{sectors[sector].raw.data(), sectorsPerCluster * SECTOR_SIZE}, 0); |
| 916 |
writeFAT12(cluster, newCluster); |
| 917 |
writeFAT12(newCluster, EOF_FAT); |
| 918 |
|
| 919 |
|
| 920 |
return {sector, 0}; |
| 921 |
} |
| 922 |
|
| 923 |
void DirAsDSK::writeSectorImpl(size_t sector_, const SectorBuffer& buf) |
| 924 |
{ |
| 925 |
assert(sector_ < nofSectors); |
| 926 |
assert(syncMode != SyncMode::READONLY); |
| 927 |
auto sector = unsigned(sector_); |
| 928 |
|
| 929 |
|
| 930 |
if (const auto* scheduler = diskChanger.getScheduler()) { |
| 931 |
lastAccess = scheduler->getCurrentTime(); |
| 932 |
} |
| 933 |
|
| 934 |
if (sector == 0) { |
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
} else if (sector < firstSector2ndFAT) { |
| 941 |
writeFATSector(sector, buf); |
| 942 |
} else if (sector < firstDirSector) { |
| 943 |
|
| 944 |
|
| 945 |
sectors[sector] = buf; |
| 946 |
} else if (auto dirDirIndex = isDirSector(sector)) { |
| 947 |
|
| 948 |
writeDIRSector(sector, *dirDirIndex, buf); |
| 949 |
} else { |
| 950 |
writeDataSector(sector, buf); |
| 951 |
} |
| 952 |
} |
| 953 |
|
| 954 |
void DirAsDSK::writeFATSector(unsigned sector, const SectorBuffer& buf) |
| 955 |
{ |
| 956 |
|
| 957 |
auto oldFAT = to_vector(fat()); |
| 958 |
|
| 959 |
|
| 960 |
sectors[sector] = buf; |
| 961 |
|
| 962 |
|
| 963 |
for (auto i : xrange(FIRST_CLUSTER, maxCluster)) { |
| 964 |
if (readFAT(i) != readFATHelper(oldFAT, i)) { |
| 965 |
exportFileFromFATChange(i, oldFAT); |
| 966 |
} |
| 967 |
} |
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
for (auto i : xrange(FIRST_CLUSTER, maxCluster)) { |
| 976 |
assert(readFAT(i) == readFATHelper(oldFAT, i)); (void)i; |
| 977 |
} |
| 978 |
} |
| 979 |
|
| 980 |
void DirAsDSK::exportFileFromFATChange(unsigned cluster, std::span<SectorBuffer> oldFAT) |
| 981 |
{ |
| 982 |
|
| 983 |
auto [startCluster, chainLength] = getChainStart(cluster); |
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
std::vector<bool> visited(maxCluster, false); |
| 988 |
unsigned tmp = startCluster; |
| 989 |
while ((FIRST_CLUSTER <= tmp) && (tmp < maxCluster)) { |
| 990 |
if (visited[tmp]) { |
| 991 |
|
| 992 |
return; |
| 993 |
} |
| 994 |
visited[tmp] = true; |
| 995 |
|
| 996 |
unsigned next = readFAT(tmp); |
| 997 |
writeFATHelper(oldFAT, tmp, next); |
| 998 |
tmp = next; |
| 999 |
} |
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
if (auto result = getDirEntryForCluster(startCluster)) { |
| 1004 |
exportToHost(result->dirIndex, result->dirDirIndex); |
| 1005 |
} |
| 1006 |
} |
| 1007 |
|
| 1008 |
std::pair<unsigned, unsigned> DirAsDSK::getChainStart(unsigned cluster) |
| 1009 |
{ |
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
unsigned chainLength = 0; |
| 1015 |
for (unsigned i = FIRST_CLUSTER; i < maxCluster; ++i) { |
| 1016 |
if (readFAT(i) == cluster) { |
| 1017 |
|
| 1018 |
cluster = i; |
| 1019 |
++chainLength; |
| 1020 |
i = FIRST_CLUSTER - 1; |
| 1021 |
} |
| 1022 |
} |
| 1023 |
return {cluster, chainLength}; |
| 1024 |
} |
| 1025 |
|
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
template<typename FUNC> bool DirAsDSK::scanMsxDirs(FUNC&& func, unsigned sector) |
| 1030 |
{ |
| 1031 |
size_t rdIdx = 0; |
| 1032 |
std::vector<unsigned> dirs; |
| 1033 |
std::vector<DirIndex> dirs2; |
| 1034 |
while (true) { |
| 1035 |
do { |
| 1036 |
|
| 1037 |
if (func.onDirSector(sector)) return true; |
| 1038 |
|
| 1039 |
for (auto idx : xrange(DIR_ENTRIES_PER_SECTOR)) { |
| 1040 |
|
| 1041 |
DirIndex dirIndex(sector, idx); |
| 1042 |
const MSXDirEntry& entry = msxDir(dirIndex); |
| 1043 |
if (func.onDirEntry(dirIndex, entry)) return true; |
| 1044 |
|
| 1045 |
if ((entry.filename[0] == one_of(char(0x00), char(0xE5))) || |
| 1046 |
!(entry.attrib & MSXDirEntry::Attrib::DIRECTORY)) { |
| 1047 |
|
| 1048 |
continue; |
| 1049 |
} |
| 1050 |
unsigned cluster = msxDir(dirIndex).startCluster; |
| 1051 |
if ((cluster < FIRST_CLUSTER) || |
| 1052 |
(cluster >= maxCluster)) { |
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
continue; |
| 1057 |
} |
| 1058 |
unsigned dir = clusterToSector(cluster); |
| 1059 |
if (contains(dirs, dir)) { |
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
continue; |
| 1066 |
} |
| 1067 |
|
| 1068 |
|
| 1069 |
dirs.push_back(dir); |
| 1070 |
dirs2.push_back(dirIndex); |
| 1071 |
} |
| 1072 |
sector = nextMsxDirSector(sector); |
| 1073 |
} while (sector != unsigned(-1)); |
| 1074 |
|
| 1075 |
|
| 1076 |
if (rdIdx == dirs.size()) { |
| 1077 |
|
| 1078 |
return false; |
| 1079 |
} |
| 1080 |
|
| 1081 |
func.onVisitSubDir(dirs2[rdIdx]); |
| 1082 |
sector = dirs[rdIdx++]; |
| 1083 |
} |
| 1084 |
} |
| 1085 |
|
| 1086 |
|
| 1087 |
|
| 1088 |
struct NullScanner { |
| 1089 |
|
| 1090 |
void onVisitSubDir(DirAsDSK::DirIndex ) const {} |
| 1091 |
|
| 1092 |
|
| 1093 |
[[nodiscard]] bool onDirSector(unsigned ) const { |
| 1094 |
return false; |
| 1095 |
} |
| 1096 |
|
| 1097 |
|
| 1098 |
[[nodiscard]] bool onDirEntry(DirAsDSK::DirIndex , |
| 1099 |
const MSXDirEntry& ) const { |
| 1100 |
return false; |
| 1101 |
} |
| 1102 |
}; |
| 1103 |
|
| 1104 |
|
| 1105 |
|
| 1106 |
struct DirScanner : NullScanner { |
| 1107 |
|
| 1108 |
void onVisitSubDir(DirAsDSK::DirIndex subdir) { |
| 1109 |
dirDirIndex = subdir; |
| 1110 |
} |
| 1111 |
|
| 1112 |
DirAsDSK::DirIndex dirDirIndex{0, 0}; |
| 1113 |
}; |
| 1114 |
|
| 1115 |
|
| 1116 |
struct IsDirSector : DirScanner { |
| 1117 |
explicit IsDirSector(unsigned sector_) : sector(sector_) {} |
| 1118 |
|
| 1119 |
[[nodiscard]] bool onDirSector(unsigned dirSector) const { |
| 1120 |
return sector == dirSector; |
| 1121 |
} |
| 1122 |
|
| 1123 |
unsigned sector; |
| 1124 |
}; |
| 1125 |
std::optional<DirAsDSK::DirIndex> DirAsDSK::isDirSector(unsigned sector) |
| 1126 |
{ |
| 1127 |
if (IsDirSector scanner{sector}; |
| 1128 |
scanMsxDirs(scanner, firstDirSector)) { |
| 1129 |
return scanner.dirDirIndex; |
| 1130 |
} |
| 1131 |
return std::nullopt; |
| 1132 |
} |
| 1133 |
|
| 1134 |
|
| 1135 |
struct DirEntryForCluster : DirScanner { |
| 1136 |
explicit DirEntryForCluster(unsigned cluster_) : cluster(cluster_) {} |
| 1137 |
|
| 1138 |
bool onDirEntry(DirAsDSK::DirIndex dirIndex_, const MSXDirEntry& entry) { |
| 1139 |
if (entry.startCluster == cluster) { |
| 1140 |
dirIndex = dirIndex_; |
| 1141 |
return true; |
| 1142 |
} |
| 1143 |
return false; |
| 1144 |
} |
| 1145 |
|
| 1146 |
unsigned cluster; |
| 1147 |
DirAsDSK::DirIndex dirIndex{0, 0}; |
| 1148 |
}; |
| 1149 |
std::optional<DirAsDSK::DirEntryForClusterResult> DirAsDSK::getDirEntryForCluster(unsigned cluster) |
| 1150 |
{ |
| 1151 |
if (DirEntryForCluster scanner{cluster}; |
| 1152 |
scanMsxDirs(scanner, firstDirSector)) { |
| 1153 |
return DirEntryForClusterResult{.dirIndex = scanner.dirIndex, .dirDirIndex = scanner.dirDirIndex}; |
| 1154 |
} |
| 1155 |
return std::nullopt; |
| 1156 |
} |
| 1157 |
|
| 1158 |
|
| 1159 |
|
| 1160 |
struct UnmapHostFiles : NullScanner { |
| 1161 |
explicit UnmapHostFiles(DirAsDSK::MapDirs& mapDirs_) |
| 1162 |
: mapDirs(mapDirs_) {} |
| 1163 |
bool onDirEntry(DirAsDSK::DirIndex dirIndex, |
| 1164 |
const MSXDirEntry& ) { |
| 1165 |
mapDirs.erase(dirIndex); |
| 1166 |
return false; |
| 1167 |
} |
| 1168 |
DirAsDSK::MapDirs& mapDirs; |
| 1169 |
}; |
| 1170 |
void DirAsDSK::unmapHostFiles(unsigned msxDirSector) |
| 1171 |
{ |
| 1172 |
scanMsxDirs(UnmapHostFiles(mapDirs), msxDirSector); |
| 1173 |
} |
| 1174 |
|
| 1175 |
void DirAsDSK::exportToHost(DirIndex dirIndex, DirIndex dirDirIndex) |
| 1176 |
{ |
| 1177 |
|
| 1178 |
if (msxDir(dirIndex).attrib & MSXDirEntry::Attrib::VOLUME) { |
| 1179 |
|
| 1180 |
return; |
| 1181 |
} |
| 1182 |
const auto& msxName = msxDir(dirIndex).filename; |
| 1183 |
std::string hostName; |
| 1184 |
if (const auto* v = lookup(mapDirs, dirIndex)) { |
| 1185 |
|
| 1186 |
hostName = v->hostName; |
| 1187 |
} else { |
| 1188 |
|
| 1189 |
|
| 1190 |
if (msxName[0] == one_of(char(0x00), char(0xE5))) { |
| 1191 |
|
| 1192 |
return; |
| 1193 |
} |
| 1194 |
std::string hostSubDir; |
| 1195 |
if (dirDirIndex.sector != 0) { |
| 1196 |
|
| 1197 |
const auto* v2 = lookup(mapDirs, dirDirIndex); |
| 1198 |
assert(v2); |
| 1199 |
hostSubDir = v2->hostName; |
| 1200 |
assert(!hostSubDir.ends_with('/')); |
| 1201 |
hostSubDir += '/'; |
| 1202 |
} |
| 1203 |
hostName = hostSubDir + msxToHostName(msxName); |
| 1204 |
mapDirs[dirIndex].hostName = hostName; |
| 1205 |
} |
| 1206 |
if (msxDir(dirIndex).attrib & MSXDirEntry::Attrib::DIRECTORY) { |
| 1207 |
if (std::ranges::equal(msxName, std::string_view(". ")) || |
| 1208 |
std::ranges::equal(msxName, std::string_view(".. "))) { |
| 1209 |
|
| 1210 |
return; |
| 1211 |
} |
| 1212 |
exportToHostDir(dirIndex, hostName); |
| 1213 |
} else { |
| 1214 |
exportToHostFile(dirIndex, hostName); |
| 1215 |
} |
| 1216 |
} |
| 1217 |
|
| 1218 |
void DirAsDSK::exportToHostDir(DirIndex dirIndex, const std::string& hostName) |
| 1219 |
{ |
| 1220 |
try { |
| 1221 |
unsigned cluster = msxDir(dirIndex).startCluster; |
| 1222 |
if ((cluster < FIRST_CLUSTER) || (cluster >= maxCluster)) { |
| 1223 |
|
| 1224 |
return; |
| 1225 |
} |
| 1226 |
unsigned msxDirSector = clusterToSector(cluster); |
| 1227 |
|
| 1228 |
|
| 1229 |
FileOperations::mkdirp(hostDir + hostName); |
| 1230 |
|
| 1231 |
|
| 1232 |
std::vector<bool> visited(nofSectors, false); |
| 1233 |
do { |
| 1234 |
if (visited[msxDirSector]) { |
| 1235 |
|
| 1236 |
return; |
| 1237 |
} |
| 1238 |
visited[msxDirSector] = true; |
| 1239 |
|
| 1240 |
if (readFAT(sectorToCluster(msxDirSector)) == FREE_FAT) { |
| 1241 |
|
| 1242 |
|
| 1243 |
|
| 1244 |
return; |
| 1245 |
} |
| 1246 |
for (auto idx : xrange(DIR_ENTRIES_PER_SECTOR)) { |
| 1247 |
exportToHost(DirIndex(msxDirSector, idx), dirIndex); |
| 1248 |
} |
| 1249 |
msxDirSector = nextMsxDirSector(msxDirSector); |
| 1250 |
} while (msxDirSector != unsigned(-1)); |
| 1251 |
} catch (FileException& e) { |
| 1252 |
cliComm.printWarning("Error while syncing host directory: ", |
| 1253 |
hostName, ": ", e.getMessage()); |
| 1254 |
} |
| 1255 |
} |
| 1256 |
|
| 1257 |
void DirAsDSK::exportToHostFile(DirIndex dirIndex, const std::string& hostName) |
| 1258 |
{ |
| 1259 |
|
| 1260 |
|
| 1261 |
|
| 1262 |
|
| 1263 |
try { |
| 1264 |
unsigned curCl = msxDir(dirIndex).startCluster; |
| 1265 |
unsigned msxSize = msxDir(dirIndex).size; |
| 1266 |
|
| 1267 |
File file(hostDir + hostName, File::OpenMode::TRUNCATE); |
| 1268 |
unsigned offset = 0; |
| 1269 |
std::vector<bool> visited(maxCluster, false); |
| 1270 |
|
| 1271 |
while ((FIRST_CLUSTER <= curCl) && (curCl < maxCluster)) { |
| 1272 |
if (visited[curCl]) { |
| 1273 |
|
| 1274 |
return; |
| 1275 |
} |
| 1276 |
visited[curCl] = true; |
| 1277 |
|
| 1278 |
unsigned logicalSector = clusterToSector(curCl); |
| 1279 |
for (auto i : xrange(sectorsPerCluster)) { |
| 1280 |
if (offset >= msxSize) break; |
| 1281 |
unsigned sector = logicalSector + i; |
| 1282 |
assert(sector < nofSectors); |
| 1283 |
auto writeSize = std::min<size_t>(msxSize - offset, SECTOR_SIZE); |
| 1284 |
file.write(subspan(sectors[sector].raw, 0, writeSize)); |
| 1285 |
offset += SECTOR_SIZE; |
| 1286 |
} |
| 1287 |
if (offset >= msxSize) break; |
| 1288 |
curCl = readFAT(curCl); |
| 1289 |
} |
| 1290 |
} catch (FileException& e) { |
| 1291 |
cliComm.printWarning("Error while syncing host file: ", |
| 1292 |
hostName, ": ", e.getMessage()); |
| 1293 |
} |
| 1294 |
} |
| 1295 |
|
| 1296 |
void DirAsDSK::writeDIRSector(unsigned sector, DirIndex dirDirIndex, |
| 1297 |
const SectorBuffer& buf) |
| 1298 |
{ |
| 1299 |
|
| 1300 |
for (auto idx : xrange(DIR_ENTRIES_PER_SECTOR)) { |
| 1301 |
const auto& newEntry = buf.dirEntry[idx]; |
| 1302 |
DirIndex dirIndex(sector, idx); |
| 1303 |
if (msxDir(dirIndex) != newEntry) { |
| 1304 |
writeDIREntry(dirIndex, dirDirIndex, newEntry); |
| 1305 |
} |
| 1306 |
} |
| 1307 |
|
| 1308 |
assert(std::ranges::equal(sectors[sector].raw, buf.raw)); |
| 1309 |
} |
| 1310 |
|
| 1311 |
void DirAsDSK::writeDIREntry(DirIndex dirIndex, DirIndex dirDirIndex, |
| 1312 |
const MSXDirEntry& newEntry) |
| 1313 |
{ |
| 1314 |
if ((msxDir(dirIndex).filename != newEntry.filename) || |
| 1315 |
((msxDir(dirIndex).attrib & MSXDirEntry::Attrib::DIRECTORY) != |
| 1316 |
( newEntry.attrib & MSXDirEntry::Attrib::DIRECTORY))) { |
| 1317 |
|
| 1318 |
if (auto it = mapDirs.find(dirIndex); it != end(mapDirs)) { |
| 1319 |
|
| 1320 |
|
| 1321 |
|
| 1322 |
auto fullHostName = tmpStrCat(hostDir, it->second.hostName); |
| 1323 |
FileOperations::deleteRecursive(fullHostName); |
| 1324 |
|
| 1325 |
mapDirs.erase(it); |
| 1326 |
if (msxDir(dirIndex).attrib & MSXDirEntry::Attrib::DIRECTORY) { |
| 1327 |
|
| 1328 |
|
| 1329 |
unsigned cluster = msxDir(dirIndex).startCluster; |
| 1330 |
if ((FIRST_CLUSTER <= cluster) && |
| 1331 |
(cluster < maxCluster)) { |
| 1332 |
unmapHostFiles(clusterToSector(cluster)); |
| 1333 |
} |
| 1334 |
} |
| 1335 |
} |
| 1336 |
} |
| 1337 |
|
| 1338 |
|
| 1339 |
msxDir(dirIndex) = newEntry; |
| 1340 |
|
| 1341 |
|
| 1342 |
exportToHost(dirIndex, dirDirIndex); |
| 1343 |
} |
| 1344 |
|
| 1345 |
void DirAsDSK::writeDataSector(unsigned sector, const SectorBuffer& buf) |
| 1346 |
{ |
| 1347 |
assert(sector >= firstDataSector); |
| 1348 |
assert(sector < nofSectors); |
| 1349 |
|
| 1350 |
|
| 1351 |
|
| 1352 |
auto oldSector = sectors[sector]; |
| 1353 |
|
| 1354 |
|
| 1355 |
sectors[sector] = buf; |
| 1356 |
|
| 1357 |
|
| 1358 |
auto [cluster, offset] = sectorToClusterOffset(sector); |
| 1359 |
auto [startCluster, chainLength] = getChainStart(cluster); |
| 1360 |
offset += narrow<unsigned>((sizeof(buf) * sectorsPerCluster) * chainLength); |
| 1361 |
|
| 1362 |
|
| 1363 |
auto entry = getDirEntryForCluster(startCluster); |
| 1364 |
if (!entry) return; |
| 1365 |
const auto* v = lookup(mapDirs, entry->dirIndex); |
| 1366 |
if (!v) return; |
| 1367 |
|
| 1368 |
|
| 1369 |
unsigned msxSize = msxDir(entry->dirIndex).size; |
| 1370 |
if (msxSize <= offset) return; |
| 1371 |
auto writeSize = std::min<size_t>(msxSize - offset, sizeof(buf)); |
| 1372 |
|
| 1373 |
|
| 1374 |
|
| 1375 |
|
| 1376 |
|
| 1377 |
|
| 1378 |
|
| 1379 |
|
| 1380 |
|
| 1381 |
|
| 1382 |
|
| 1383 |
if (std::ranges::equal(subspan(buf.raw, 0, writeSize), |
| 1384 |
subspan(oldSector.raw, 0, writeSize))) { |
| 1385 |
return; |
| 1386 |
} |
| 1387 |
|
| 1388 |
|
| 1389 |
std::string fullHostName = hostDir + v->hostName; |
| 1390 |
try { |
| 1391 |
File file(fullHostName, "rb+"); |
| 1392 |
file.seek(offset); |
| 1393 |
file.write(subspan(buf.raw, 0, writeSize)); |
| 1394 |
} catch (FileException& e) { |
| 1395 |
cliComm.printWarning("Couldn't write to file ", fullHostName, |
| 1396 |
": ", e.getMessage()); |
| 1397 |
} |
| 1398 |
} |
| 1399 |
|
| 1400 |
} |