openMSX
DirAsDSK.cc
Go to the documentation of this file.
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 <cassert>
19#include <cstring>
20#include <vector>
21
22using std::string;
23using std::vector;
24
25namespace openmsx {
26
27static constexpr unsigned SECTOR_SIZE = sizeof(SectorBuffer);
28static constexpr unsigned SECTORS_PER_DIR = 7;
29static constexpr unsigned NUM_FATS = 2;
30static constexpr unsigned NUM_TRACKS = 80;
31static constexpr unsigned SECTORS_PER_CLUSTER = 2;
32static constexpr unsigned SECTORS_PER_TRACK = 9;
33static constexpr unsigned FIRST_FAT_SECTOR = 1;
34static constexpr unsigned DIR_ENTRIES_PER_SECTOR =
35 SECTOR_SIZE / sizeof(MSXDirEntry);
36
37// First valid regular cluster number.
38static constexpr unsigned FIRST_CLUSTER = 2;
39
40static constexpr unsigned FREE_FAT = 0x000;
41static constexpr unsigned BAD_FAT = 0xFF7;
42static constexpr unsigned EOF_FAT = 0xFFF; // actually 0xFF8-0xFFF
43
44
45// Transform BAD_FAT (0xFF7) and EOF_FAT-range (0xFF8-0xFFF)
46// to a single value: EOF_FAT (0xFFF).
47[[nodiscard]] static constexpr unsigned normalizeFAT(unsigned cluster)
48{
49 return (cluster < BAD_FAT) ? cluster : EOF_FAT;
50}
51
52unsigned DirAsDSK::readFATHelper(std::span<const SectorBuffer> fatBuf, unsigned cluster) const
53{
54 assert(FIRST_CLUSTER <= cluster);
55 assert(cluster < maxCluster);
56 std::span buf{fatBuf[0].raw.data(), fatBuf.size() * SECTOR_SIZE};
57 auto p = subspan<2>(buf, (cluster * 3) / 2);
58 unsigned result = (cluster & 1)
59 ? (p[0] >> 4) + (p[1] << 4)
60 : p[0] + ((p[1] & 0x0F) << 8);
61 return normalizeFAT(result);
62}
63
64void DirAsDSK::writeFATHelper(std::span<SectorBuffer> fatBuf, unsigned cluster, unsigned val) const
65{
66 assert(FIRST_CLUSTER <= cluster);
67 assert(cluster < maxCluster);
68 std::span buf{fatBuf[0].raw.data(), fatBuf.size() * SECTOR_SIZE};
69 auto p = subspan<2>(buf, (cluster * 3) / 2);
70 if (cluster & 1) {
71 p[0] = narrow_cast<uint8_t>((p[0] & 0x0F) + (val << 4));
72 p[1] = narrow_cast<uint8_t>(val >> 4);
73 } else {
74 p[0] = narrow_cast<uint8_t>(val);
75 p[1] = narrow_cast<uint8_t>((p[1] & 0xF0) + ((val >> 8) & 0x0F));
76 }
77}
78
79std::span<SectorBuffer> DirAsDSK::fat()
80{
81 return {&sectors[FIRST_FAT_SECTOR], nofSectorsPerFat};
82}
83std::span<SectorBuffer> DirAsDSK::fat2()
84{
85 return {&sectors[firstSector2ndFAT], nofSectorsPerFat};
86}
87
88// Read entry from FAT.
89unsigned DirAsDSK::readFAT(unsigned cluster)
90{
91 return readFATHelper(fat(), cluster);
92}
93
94// Write an entry to both FAT1 and FAT2.
95void DirAsDSK::writeFAT12(unsigned cluster, unsigned val)
96{
97 writeFATHelper(fat (), cluster, val);
98 writeFATHelper(fat2(), cluster, val);
99 // An alternative is to copy FAT1 to FAT2 after changes have been made
100 // to FAT1. This is probably more like what the real disk rom does.
101}
102
103// Returns maxCluster in case of no more free clusters
104unsigned DirAsDSK::findNextFreeCluster(unsigned cluster)
105{
106 assert(cluster < maxCluster);
107 do {
108 ++cluster;
109 assert(cluster >= FIRST_CLUSTER);
110 } while ((cluster < maxCluster) && (readFAT(cluster) != FREE_FAT));
111 return cluster;
112}
113unsigned DirAsDSK::findFirstFreeCluster()
114{
115 return findNextFreeCluster(FIRST_CLUSTER - 1);
116}
117
118// Throws when there are no more free clusters.
119unsigned DirAsDSK::getFreeCluster()
120{
121 unsigned cluster = findFirstFreeCluster();
122 if (cluster == maxCluster) {
123 throw MSXException("disk full");
124 }
125 return cluster;
126}
127
128unsigned DirAsDSK::clusterToSector(unsigned cluster) const
129{
130 assert(cluster >= FIRST_CLUSTER);
131 assert(cluster < maxCluster);
132 return firstDataSector + SECTORS_PER_CLUSTER *
133 (cluster - FIRST_CLUSTER);
134}
135
136std::pair<unsigned, unsigned> DirAsDSK::sectorToClusterOffset(unsigned sector) const
137{
138 assert(sector >= firstDataSector);
139 assert(sector < nofSectors);
140 sector -= firstDataSector;
141 unsigned cluster = (sector / SECTORS_PER_CLUSTER) + FIRST_CLUSTER;
142 unsigned offset = (sector % SECTORS_PER_CLUSTER) * SECTOR_SIZE;
143 return {cluster, offset};
144}
145unsigned DirAsDSK::sectorToCluster(unsigned sector) const
146{
147 auto [cluster, offset] = sectorToClusterOffset(sector);
148 return cluster;
149}
150
151MSXDirEntry& DirAsDSK::msxDir(DirIndex dirIndex)
152{
153 assert(dirIndex.sector < nofSectors);
154 assert(dirIndex.idx < DIR_ENTRIES_PER_SECTOR);
155 return sectors[dirIndex.sector].dirEntry[dirIndex.idx];
156}
157
158// Returns -1 when there are no more sectors for this directory.
159unsigned DirAsDSK::nextMsxDirSector(unsigned sector)
160{
161 if (sector < firstDataSector) {
162 // Root directory.
163 assert(firstDirSector <= sector);
164 ++sector;
165 if (sector == firstDataSector) {
166 // Root directory has a fixed number of sectors.
167 return unsigned(-1);
168 }
169 return sector;
170 } else {
171 // Subdirectory.
172 auto [cluster, offset] = sectorToClusterOffset(sector);
173 if (offset < ((SECTORS_PER_CLUSTER - 1) * SECTOR_SIZE)) {
174 // Next sector still in same cluster.
175 return sector + 1;
176 }
177 unsigned nextCl = readFAT(cluster);
178 if ((nextCl < FIRST_CLUSTER) || (maxCluster <= nextCl)) {
179 // No next cluster, end of directory reached.
180 return unsigned(-1);
181 }
182 return clusterToSector(nextCl);
183 }
184}
185
186// Check if a msx filename is used in a specific msx (sub)directory.
187bool DirAsDSK::checkMSXFileExists(
188 std::span<const char, 11> msxFilename, unsigned msxDirSector)
189{
190 vector<bool> visited(nofSectors, false);
191 do {
192 if (visited[msxDirSector]) {
193 // cycle detected, invalid disk, but don't crash on it
194 return false;
195 }
196 visited[msxDirSector] = true;
197
198 for (auto idx : xrange(DIR_ENTRIES_PER_SECTOR)) {
199 DirIndex dirIndex(msxDirSector, idx);
200 if (ranges::equal(msxDir(dirIndex).filename, msxFilename)) {
201 return true;
202 }
203 }
204 msxDirSector = nextMsxDirSector(msxDirSector);
205 } while (msxDirSector != unsigned(-1));
206
207 // Searched through all sectors of this (sub)directory.
208 return false;
209}
210
211// Returns msx directory entry for the given host file. Or -1 if the host file
212// is not mapped in the virtual disk.
213DirAsDSK::DirIndex DirAsDSK::findHostFileInDSK(std::string_view hostName) const
214{
215 for (const auto& [dirIdx, mapDir] : mapDirs) {
216 if (mapDir.hostName == hostName) {
217 return dirIdx;
218 }
219 }
220 return {unsigned(-1), unsigned(-1)};
221}
222
223// Check if a host file is already mapped in the virtual disk.
224bool DirAsDSK::checkFileUsedInDSK(std::string_view hostName) const
225{
226 DirIndex dirIndex = findHostFileInDSK(hostName);
227 return dirIndex.sector != unsigned(-1);
228}
229
230static std::array<char, 11> hostToMsxName(string hostName)
231{
232 // Create an MSX filename 8.3 format. TODO use vfat-like abbreviation
233 transform_in_place(hostName, [](char a) {
234 return (a == ' ') ? '_' : ::toupper(a);
235 });
236 auto [file, ext] = StringOp::splitOnLast(hostName, '.');
237 if (file.empty()) std::swap(file, ext);
238
239 std::array<char, 8 + 3> result;
240 ranges::fill(result, ' ');
241 ranges::copy(subspan(file, 0, std::min<size_t>(8, file.size())), subspan<8>(result, 0));
242 ranges::copy(subspan(ext, 0, std::min<size_t>(3, ext .size())), subspan<3>(result, 8));
243 ranges::replace(result, '.', '_');
244 return result;
245}
246
247static string msxToHostName(std::span<const char, 11> msxName)
248{
249 string result;
250 for (unsigned i = 0; (i < 8) && (msxName[i] != ' '); ++i) {
251 result += char(tolower(msxName[i]));
252 }
253 if (msxName[8] != ' ') {
254 result += '.';
255 for (unsigned i = 8; (i < (8 + 3)) && (msxName[i] != ' '); ++i) {
256 result += char(tolower(msxName[i]));
257 }
258 }
259 return result;
260}
261
262
263DirAsDSK::DirAsDSK(DiskChanger& diskChanger_, CliComm& cliComm_,
264 const Filename& hostDir_, SyncMode syncMode_,
265 BootSectorType bootSectorType)
266 : SectorBasedDisk(DiskName(hostDir_))
267 , diskChanger(diskChanger_)
268 , cliComm(cliComm_)
269 , hostDir(FileOperations::expandTilde(hostDir_.getResolved() + '/'))
270 , syncMode(syncMode_)
271 , nofSectors((diskChanger_.isDoubleSidedDrive() ? 2 : 1) * SECTORS_PER_TRACK * NUM_TRACKS)
272 , nofSectorsPerFat(narrow<unsigned>((((3 * nofSectors) / (2 * SECTORS_PER_CLUSTER)) + SECTOR_SIZE - 1) / SECTOR_SIZE))
273 , firstSector2ndFAT(FIRST_FAT_SECTOR + nofSectorsPerFat)
274 , firstDirSector(FIRST_FAT_SECTOR + NUM_FATS * nofSectorsPerFat)
275 , firstDataSector(firstDirSector + SECTORS_PER_DIR)
276 , maxCluster((nofSectors - firstDataSector) / SECTORS_PER_CLUSTER + FIRST_CLUSTER)
277 , sectors(nofSectors)
278{
279 if (!FileOperations::isDirectory(hostDir)) {
280 throw MSXException("Not a directory");
281 }
282
283 // First create structure for the virtual disk.
284 uint8_t numSides = diskChanger_.isDoubleSidedDrive() ? 2 : 1;
285 setNbSectors(nofSectors);
286 setSectorsPerTrack(SECTORS_PER_TRACK);
287 setNbSides(numSides);
288
289 // Initially the whole disk is filled with 0xE5 (at least on Philips
290 // NMS8250).
291 ranges::fill(std::span{sectors[0].raw.data(), sizeof(SectorBuffer) * nofSectors}, 0xE5);
292
293 // Use selected boot sector, fill-in values.
294 uint8_t mediaDescriptor = (numSides == 2) ? 0xF9 : 0xF8;
295 const auto& protoBootSector = bootSectorType == BootSectorType::DOS1
298 sectors[0] = protoBootSector;
299 auto& bootSector = sectors[0].bootSector;
300 bootSector.bpSector = SECTOR_SIZE;
301 bootSector.spCluster = SECTORS_PER_CLUSTER;
302 bootSector.nrFats = NUM_FATS;
303 bootSector.dirEntries = SECTORS_PER_DIR * (SECTOR_SIZE / sizeof(MSXDirEntry));
304 bootSector.nrSectors = narrow_cast<uint16_t>(nofSectors);
305 bootSector.descriptor = mediaDescriptor;
306 bootSector.sectorsFat = narrow_cast<uint16_t>(nofSectorsPerFat);
307 bootSector.sectorsTrack = SECTORS_PER_TRACK;
308 bootSector.nrSides = numSides;
309
310 // Clear FAT1 + FAT2.
311 ranges::fill(std::span{fat()[0].raw.data(), SECTOR_SIZE * nofSectorsPerFat * NUM_FATS}, 0);
312 // First 3 bytes are initialized specially:
313 // 'cluster 0' contains the media descriptor
314 // 'cluster 1' is marked as EOF_FAT
315 // So cluster 2 is the first usable cluster number
316 auto init = [&](auto f) {
317 f[0].raw[0] = mediaDescriptor;
318 f[0].raw[1] = 0xFF;
319 f[0].raw[2] = 0xFF;
320 };
321 init(fat());
322 init(fat2());
323
324 // Assign empty directory entries.
325 ranges::fill(std::span{sectors[firstDirSector].raw.data(), SECTOR_SIZE * SECTORS_PER_DIR}, 0);
326
327 // No host files are mapped to this disk yet.
328 assert(mapDirs.empty());
329
330 // Import the host filesystem.
331 syncWithHost();
332}
333
335{
336 return syncMode == SyncMode::READONLY;
337}
338
340{
341 // For simplicity, for now, always report content has (possibly) changed
342 // (in the future we could try to detect that no host files have
343 // actually changed).
344 // The effect is that the MSX always see a 'disk-changed-signal'.
345 // This fixes: https://github.com/openMSX/openMSX/issues/1410
346 return true;
347}
348
350{
351 bool needSync = [&] {
352 if (const auto* scheduler = diskChanger.getScheduler()) {
353 auto now = scheduler->getCurrentTime();
354 auto delta = now - lastAccess;
355 return delta > EmuDuration::sec(1);
356 // Do not update lastAccess because we don't actually call
357 // syncWithHost().
358 } else {
359 // Happens when dirasdisk is used in virtual_drive.
360 return true;
361 }
362 }();
363 if (needSync) {
364 flushCaches();
365 }
366}
367
368void DirAsDSK::readSectorImpl(size_t sector, SectorBuffer& buf)
369{
370 assert(sector < nofSectors);
371
372 // 'Peek-mode' is used to periodically calculate a sha1sum for the
373 // whole disk (used by reverse). We don't want this calculation to
374 // interfere with the access time we use for normal read/writes. So in
375 // peek-mode we skip the whole sync-step.
376 if (!isPeekMode()) {
377 bool needSync = [&] {
378 if (const auto* scheduler = diskChanger.getScheduler()) {
379 auto now = scheduler->getCurrentTime();
380 auto delta = now - lastAccess;
381 lastAccess = now;
382 return delta > EmuDuration::sec(1);
383 } else {
384 // Happens when dirasdisk is used in virtual_drive.
385 return true;
386 }
387 }();
388 if (needSync) {
389 syncWithHost();
390 flushCaches(); // e.g. sha1sum
391 // Let the disk drive report the disk has been ejected.
392 // E.g. a turbor machine uses this to flush its
393 // internal disk caches.
394 diskChanger.forceDiskChange(); // maybe redundant now? (see hasChanged()).
395 }
396 }
397
398 // Simply return the sector from our virtual disk image.
399 buf = sectors[sector];
400}
401
402void DirAsDSK::syncWithHost()
403{
404 // Check for removed host files. This frees up space in the virtual
405 // disk. Do this first because otherwise later actions may fail (run
406 // out of virtual disk space) for no good reason.
407 checkDeletedHostFiles();
408
409 // Next update existing files. This may enlarge or shrink virtual
410 // files. In case not all host files fit on the virtual disk it's
411 // better to update the existing files than to (partly) add a too big
412 // new file and have no space left to enlarge the existing files.
413 checkModifiedHostFiles();
414
415 // Last add new host files (this can only consume virtual disk space).
416 addNewHostFiles({}, firstDirSector);
417}
418
419void DirAsDSK::checkDeletedHostFiles()
420{
421 // This handles both host files and directories.
422 auto copy = mapDirs;
423 for (const auto& [dirIdx, mapDir] : copy) {
424 if (!mapDirs.contains(dirIdx)) {
425 // While iterating over (the copy of) mapDirs we delete
426 // entries of mapDirs (when we delete files only the
427 // current entry is deleted, when we delete
428 // subdirectories possibly many entries are deleted).
429 // At this point in the code we've reached such an
430 // entry that's still in the original (copy of) mapDirs
431 // but has already been deleted from the current
432 // mapDirs. Ignore it.
433 continue;
434 }
435 auto fullHostName = tmpStrCat(hostDir, mapDir.hostName);
436 auto isMSXDirectory = bool(msxDir(dirIdx).attrib &
438 auto fst = FileOperations::getStat(fullHostName);
439 if (!fst || (FileOperations::isDirectory(*fst) != isMSXDirectory)) {
440 // TODO also check access permission
441 // Error stat-ing file, or directory/file type is not
442 // the same on the msx and host side (e.g. a host file
443 // has been removed and a host directory with the same
444 // name has been created). In both cases delete the msx
445 // entry (if needed it will be recreated soon).
446 deleteMSXFile(dirIdx);
447 }
448 }
449}
450
451void DirAsDSK::deleteMSXFile(DirIndex dirIndex)
452{
453 // Remove mapping between host and msx file (if any).
454 mapDirs.erase(dirIndex);
455
456 if (msxDir(dirIndex).filename[0] == one_of(0, char(0xE5))) {
457 // Directory entry not in use, don't need to do anything.
458 return;
459 }
460
461 if (msxDir(dirIndex).attrib & MSXDirEntry::Attrib::DIRECTORY) {
462 // If we're deleting a directory then also (recursively)
463 // delete the files/directories in this directory.
464 if (const auto& msxName = msxDir(dirIndex).filename;
465 ranges::equal(msxName, std::string_view(". ")) ||
466 ranges::equal(msxName, std::string_view(".. "))) {
467 // But skip the "." and ".." entries.
468 return;
469 }
470 // Sanity check on cluster range.
471 unsigned cluster = msxDir(dirIndex).startCluster;
472 if ((FIRST_CLUSTER <= cluster) && (cluster < maxCluster)) {
473 // Recursively delete all files in this subdir.
474 deleteMSXFilesInDir(clusterToSector(cluster));
475 }
476 }
477
478 // At this point we have a regular file or an empty subdirectory.
479 // Delete it by marking the first filename char as 0xE5.
480 msxDir(dirIndex).filename[0] = char(0xE5);
481
482 // Clear the FAT chain to free up space in the virtual disk.
483 freeFATChain(msxDir(dirIndex).startCluster);
484}
485
486void DirAsDSK::deleteMSXFilesInDir(unsigned msxDirSector)
487{
488 vector<bool> visited(nofSectors, false);
489 do {
490 if (visited[msxDirSector]) {
491 // cycle detected, invalid disk, but don't crash on it
492 return;
493 }
494 visited[msxDirSector] = true;
495
496 for (auto idx : xrange(DIR_ENTRIES_PER_SECTOR)) {
497 deleteMSXFile(DirIndex(msxDirSector, idx));
498 }
499 msxDirSector = nextMsxDirSector(msxDirSector);
500 } while (msxDirSector != unsigned(-1));
501}
502
503void DirAsDSK::freeFATChain(unsigned cluster)
504{
505 // Follow a FAT chain and mark all clusters on this chain as free.
506 while ((FIRST_CLUSTER <= cluster) && (cluster < maxCluster)) {
507 unsigned nextCl = readFAT(cluster);
508 writeFAT12(cluster, FREE_FAT);
509 cluster = nextCl;
510 }
511}
512
513void DirAsDSK::checkModifiedHostFiles()
514{
515 auto copy = mapDirs;
516 for (const auto& [dirIdx, mapDir] : copy) {
517 if (!mapDirs.contains(dirIdx)) {
518 // See comment in checkDeletedHostFiles().
519 continue;
520 }
521 auto fullHostName = tmpStrCat(hostDir, mapDir.hostName);
522 auto isMSXDirectory = bool(msxDir(dirIdx).attrib &
524 auto fst = FileOperations::getStat(fullHostName);
525 if (fst && (FileOperations::isDirectory(*fst) == isMSXDirectory)) {
526 // Detect changes in host file.
527 // Heuristic: we use filesize and modification time to detect
528 // changes in file content.
529 // TODO do we need both filesize and mtime or is mtime alone
530 // enough?
531 // We ignore time/size changes in directories,
532 // typically such a change indicates one of the files
533 // in that directory is changed/added/removed. But such
534 // changes are handled elsewhere.
535 if (!isMSXDirectory &&
536 ((mapDir.mtime != fst->st_mtime) ||
537 (mapDir.filesize != size_t(fst->st_size)))) {
538 importHostFile(dirIdx, *fst);
539 }
540 } else {
541 // Only very rarely happens (because checkDeletedHostFiles()
542 // checked this just recently).
543 deleteMSXFile(dirIdx);
544 }
545 }
546}
547
548void DirAsDSK::importHostFile(DirIndex dirIndex, const FileOperations::Stat& fst)
549{
550 assert(!(msxDir(dirIndex).attrib & MSXDirEntry::Attrib::DIRECTORY));
551 assert(mapDirs.contains(dirIndex));
552
553 // Set _msx_ modification time.
554 setMSXTimeStamp(dirIndex, fst);
555
556 // Set _host_ modification time (and filesize)
557 // Note: this is the _only_ place where we update the mapDir.mtime
558 // field. We do _not_ update it when the msx writes to the file. So in
559 // case the msx does write a data sector, it writes to the correct
560 // offset in the host file, but mtime is not updated. On the next
561 // host->emu sync we will resync the full file content and only then
562 // update mtime. This may seem inefficient, but it makes sure that we
563 // never miss any file changes performed by the host. E.g. the host
564 // changed part of the file short before the msx wrote to the same
565 // file. We can never guarantee that such race-scenarios work
566 // correctly, but at least with this mtime-update convention the file
567 // content will be the same on the host and the msx side after every
568 // sync.
569 size_t hostSize = fst.st_size;
570 auto& mapDir = mapDirs[dirIndex];
571 mapDir.filesize = hostSize;
572 mapDir.mtime = fst.st_mtime;
573
574 bool moreClustersInChain = true;
575 unsigned curCl = msxDir(dirIndex).startCluster;
576 // If there is no cluster assigned yet to this file (curCl == 0), then
577 // find a free cluster. Treat invalid cases in the same way (curCl == 1
578 // or curCl >= maxCluster).
579 if ((curCl < FIRST_CLUSTER) || (curCl >= maxCluster)) {
580 moreClustersInChain = false;
581 curCl = findFirstFreeCluster(); // maxCluster in case of disk-full
582 }
583
584 auto remainingSize = hostSize;
585 unsigned prevCl = 0;
586 try {
587 File file(hostDir + mapDir.hostName,
588 "rb"); // don't uncompress
589
590 while (remainingSize && (curCl < maxCluster)) {
591 unsigned logicalSector = clusterToSector(curCl);
592 for (auto i : xrange(SECTORS_PER_CLUSTER)) {
593 unsigned sector = logicalSector + i;
594 assert(sector < nofSectors);
595 auto* buf = &sectors[sector];
596 auto sz = std::min(remainingSize, SECTOR_SIZE);
597 file.read(subspan(buf->raw, 0, sz));
598 ranges::fill(subspan(buf->raw, sz), 0); // in case (end of) file only fills partial sector
599 remainingSize -= sz;
600 if (remainingSize == 0) {
601 // Don't fill next sectors in this cluster
602 // if there is no data left.
603 break;
604 }
605 }
606
607 if (prevCl) {
608 writeFAT12(prevCl, curCl);
609 } else {
610 msxDir(dirIndex).startCluster = narrow<uint16_t>(curCl);
611 }
612 prevCl = curCl;
613
614 // Check if we can follow the existing FAT chain or
615 // need to allocate a free cluster.
616 if (moreClustersInChain) {
617 curCl = readFAT(curCl);
618 if ((curCl == EOF_FAT) || // normal end
619 (curCl < FIRST_CLUSTER) || // invalid
620 (curCl >= maxCluster)) { // invalid
621 // Treat invalid FAT chain the same as
622 // a normal EOF_FAT.
623 moreClustersInChain = false;
624 curCl = findFirstFreeCluster();
625 }
626 } else {
627 curCl = findNextFreeCluster(curCl);
628 }
629 }
630 if (remainingSize != 0) {
631 cliComm.printWarning("Virtual disk image full: ",
632 mapDir.hostName, " truncated.");
633 }
634 } catch (FileException& e) {
635 // Error opening or reading host file.
636 cliComm.printWarning("Error reading host file: ",
637 mapDir.hostName, ": ", e.getMessage(),
638 " Truncated file on MSX disk.");
639 }
640
641 // In all cases (no error / image full / host read error) we need to
642 // properly terminate the FAT chain.
643 if (prevCl) {
644 writeFAT12(prevCl, EOF_FAT);
645 } else {
646 // Filesize zero: don't allocate any cluster, write zero
647 // cluster number (checked on a MSXTurboR, DOS2 mode).
648 msxDir(dirIndex).startCluster = FREE_FAT;
649 }
650
651 // Clear remains of FAT if needed.
652 if (moreClustersInChain) {
653 freeFATChain(curCl);
654 }
655
656 // Write (possibly truncated) file size.
657 msxDir(dirIndex).size = uint32_t(hostSize - remainingSize);
658
659 // TODO in case of an error (disk image full, or host file read error),
660 // wouldn't it be better to remove the (half imported) msx file again?
661 // Sometimes when I'm using DirAsDSK I have one file that is too big
662 // (e.g. a core file, or a vim .swp file) and that one prevents
663 // DirAsDSK from importing the other (small) files in my directory.
664}
665
666void DirAsDSK::setMSXTimeStamp(DirIndex dirIndex, const FileOperations::Stat& fst)
667{
668 // Use intermediate param to prevent compilation error for Android
669 time_t mtime = fst.st_mtime;
670 const auto* mtim = localtime(&mtime);
671 int t1 = mtim ? (mtim->tm_sec >> 1) + (mtim->tm_min << 5) +
672 (mtim->tm_hour << 11)
673 : 0;
674 msxDir(dirIndex).time = narrow<uint16_t>(t1);
675 int t2 = mtim ? mtim->tm_mday + ((mtim->tm_mon + 1) << 5) +
676 ((mtim->tm_year + 1900 - 1980) << 9)
677 : 0;
678 msxDir(dirIndex).date = narrow<uint16_t>(t2);
679}
680
681// Used to add 'regular' files before 'derived' files. E.g. when editing a file
682// in a host editor, you often get backup/swap files like this:
683// myfile.txt myfile.txt~ .myfile.txt.swp
684// Currently the 1st and 2nd are mapped to the same MSX filename. If more
685// host files map to the same MSX file then (currently) one of the two is
686// ignored. Which one is ignored depends on the order in which they are added
687// to the virtual disk. This routine/heuristic tries to add 'regular' files
688// before derived files.
689static size_t weight(const string& hostName)
690{
691 // TODO this weight function can most likely be improved
692 size_t result = 0;
693 auto [file, ext] = StringOp::splitOnLast(hostName, '.');
694 // too many '.' characters
695 result += ranges::count(file, '.') * 100;
696 // too long extension
697 result += ext.size() * 10;
698 // too long file
699 result += file.size();
700 return result;
701}
702
703void DirAsDSK::addNewHostFiles(const string& hostSubDir, unsigned msxDirSector)
704{
705 assert(!hostSubDir.starts_with('/'));
706 assert(hostSubDir.empty() || hostSubDir.ends_with('/'));
707
708 vector<string> hostNames;
709 {
710 ReadDir dir(tmpStrCat(hostDir, hostSubDir));
711 while (auto* d = dir.getEntry()) {
712 hostNames.emplace_back(d->d_name);
713 }
714 }
715 ranges::sort(hostNames, {}, [](const string& n) { return weight(n); });
716
717 for (auto& hostName : hostNames) {
718 try {
719 if (hostName.starts_with('.')) {
720 // skip '.' and '..'
721 // also skip hidden files on unix
722 continue;
723 }
724 auto fullHostName = tmpStrCat(hostDir, hostSubDir, hostName);
725 auto fst = FileOperations::getStat(fullHostName);
726 if (!fst) {
727 throw MSXException("Error accessing ", fullHostName);
728 }
729 if (FileOperations::isDirectory(*fst)) {
730 addNewDirectory(hostSubDir, hostName, msxDirSector, *fst);
731 } else if (FileOperations::isRegularFile(*fst)) {
732 addNewHostFile(hostSubDir, hostName, msxDirSector, *fst);
733 } else {
734 throw MSXException("Not a regular file: ", fullHostName);
735 }
736 } catch (MSXException& e) {
737 cliComm.printWarning(e.getMessage());
738 }
739 }
740}
741
742void DirAsDSK::addNewDirectory(const string& hostSubDir, const string& hostName,
743 unsigned msxDirSector, const FileOperations::Stat& fst)
744{
745 DirIndex dirIndex = findHostFileInDSK(tmpStrCat(hostSubDir, hostName));
746 unsigned newMsxDirSector;
747 if (dirIndex.sector == unsigned(-1)) {
748 // MSX directory doesn't exist yet, create it.
749 // Allocate a cluster to hold the subdirectory entries.
750 unsigned cluster = getFreeCluster();
751 writeFAT12(cluster, EOF_FAT);
752
753 // Allocate and fill in directory entry.
754 try {
755 dirIndex = fillMSXDirEntry(hostSubDir, hostName, msxDirSector);
756 } catch (...) {
757 // Rollback allocation of directory cluster.
758 writeFAT12(cluster, FREE_FAT);
759 throw;
760 }
761 setMSXTimeStamp(dirIndex, fst);
762 msxDir(dirIndex).attrib = MSXDirEntry::Attrib::DIRECTORY;
763 msxDir(dirIndex).startCluster = narrow<uint16_t>(cluster);
764
765 // Initialize the new directory.
766 newMsxDirSector = clusterToSector(cluster);
767 ranges::fill(std::span{sectors[newMsxDirSector].raw.data(),
768 SECTORS_PER_CLUSTER * SECTOR_SIZE},
769 0);
770 DirIndex idx0(newMsxDirSector, 0); // entry for "."
771 DirIndex idx1(newMsxDirSector, 1); // ".."
772 auto& e0 = msxDir(idx0);
773 auto& e1 = msxDir(idx1);
774 auto& f0 = e0.filename;
775 auto& f1 = e1.filename;
776 f0[0] = '.'; ranges::fill(subspan(f0, 1), ' ');
777 f1[0] = '.'; f1[1] = '.'; ranges::fill(subspan(f1, 2), ' ');
780 setMSXTimeStamp(idx0, fst);
781 setMSXTimeStamp(idx1, fst);
782 e0.startCluster = narrow<uint16_t>(cluster);
783 e1.startCluster = msxDirSector == firstDirSector
784 ? uint16_t(0)
785 : narrow<uint16_t>(sectorToCluster(msxDirSector));
786 } else {
787 if (!(msxDir(dirIndex).attrib & MSXDirEntry::Attrib::DIRECTORY)) {
788 // Should rarely happen because checkDeletedHostFiles()
789 // recently checked this. (It could happen when a host
790 // directory is *just*recently* created with the same
791 // name as an existing msx file). Ignore, it will be
792 // corrected in the next sync.
793 return;
794 }
795 unsigned cluster = msxDir(dirIndex).startCluster;
796 if ((cluster < FIRST_CLUSTER) || (cluster >= maxCluster)) {
797 // Sanity check on cluster range.
798 return;
799 }
800 newMsxDirSector = clusterToSector(cluster);
801 }
802
803 // Recursively process this directory.
804 addNewHostFiles(strCat(hostSubDir, hostName, '/'), newMsxDirSector);
805}
806
807void DirAsDSK::addNewHostFile(const string& hostSubDir, const string& hostName,
808 unsigned msxDirSector, const FileOperations::Stat& fst)
809{
810 if (checkFileUsedInDSK(tmpStrCat(hostSubDir, hostName))) {
811 // File is already present in the virtual disk, do nothing.
812 return;
813 }
814 // TODO check for available free space on disk instead of max free space
815 if (auto diskSpace = (nofSectors - firstDataSector) * SECTOR_SIZE;
816 narrow<size_t>(fst.st_size) > diskSpace) {
817 cliComm.printWarning("File too large: ",
818 hostDir, hostSubDir, hostName);
819 return;
820 }
821
822 DirIndex dirIndex = fillMSXDirEntry(hostSubDir, hostName, msxDirSector);
823 importHostFile(dirIndex, fst);
824}
825
826DirAsDSK::DirIndex DirAsDSK::fillMSXDirEntry(
827 const string& hostSubDir, const string& hostName, unsigned msxDirSector)
828{
829 string hostPath = hostSubDir + hostName;
830 try {
831 // Get empty dir entry (possibly extends subdirectory).
832 DirIndex dirIndex = getFreeDirEntry(msxDirSector);
833
834 // Create correct MSX filename.
835 auto msxFilename = hostToMsxName(hostName);
836 if (checkMSXFileExists(msxFilename, msxDirSector)) {
837 // TODO: actually should increase vfat abbreviation if possible!!
838 throw MSXException(
839 "MSX name ", msxToHostName(msxFilename),
840 " already exists");
841 }
842
843 // Fill in hostName / msx filename.
844 assert(!hostPath.ends_with('/'));
845 mapDirs[dirIndex].hostName = hostPath;
846 memset(&msxDir(dirIndex), 0, sizeof(MSXDirEntry)); // clear entry
847 ranges::copy(msxFilename, msxDir(dirIndex).filename);
848 return dirIndex;
849 } catch (MSXException& e) {
850 throw MSXException("Couldn't add ", hostPath, ": ",
851 e.getMessage());
852 }
853}
854
855DirAsDSK::DirIndex DirAsDSK::getFreeDirEntry(unsigned msxDirSector)
856{
857 vector<bool> visited(nofSectors, false);
858 while (true) {
859 if (visited[msxDirSector]) {
860 // cycle detected, invalid disk, but don't crash on it
861 throw MSXException("cycle in FAT");
862 }
863 visited[msxDirSector] = true;
864
865 for (auto idx : xrange(DIR_ENTRIES_PER_SECTOR)) {
866 DirIndex dirIndex(msxDirSector, idx);
867 const auto& msxName = msxDir(dirIndex).filename;
868 if (msxName[0] == one_of(char(0x00), char(0xE5))) {
869 // Found an unused msx entry. There shouldn't
870 // be any host file mapped to this entry.
871 assert(!mapDirs.contains(dirIndex));
872 return dirIndex;
873 }
874 }
875 unsigned sector = nextMsxDirSector(msxDirSector);
876 if (sector == unsigned(-1)) break;
877 msxDirSector = sector;
878 }
879
880 // No free space in existing directory.
881 if (msxDirSector == (firstDataSector - 1)) {
882 // Can't extend root directory.
883 throw MSXException("root directory full");
884 }
885
886 // Extend sub-directory: allocate and clear a new cluster, add this
887 // cluster in the existing FAT chain.
888 unsigned cluster = sectorToCluster(msxDirSector);
889 unsigned newCluster = getFreeCluster(); // throws if disk full
890 unsigned sector = clusterToSector(newCluster);
891 ranges::fill(std::span{sectors[sector].raw.data(), SECTORS_PER_CLUSTER * SECTOR_SIZE}, 0);
892 writeFAT12(cluster, newCluster);
893 writeFAT12(newCluster, EOF_FAT);
894
895 // First entry in this newly allocated cluster is free. Return it.
896 return {sector, 0};
897}
898
899void DirAsDSK::writeSectorImpl(size_t sector_, const SectorBuffer& buf)
900{
901 assert(sector_ < nofSectors);
902 assert(syncMode != SyncMode::READONLY);
903 auto sector = unsigned(sector_);
904
905 // Update last access time.
906 if (const auto* scheduler = diskChanger.getScheduler()) {
907 lastAccess = scheduler->getCurrentTime();
908 }
909
910 if (sector == 0) {
911 // Ignore. We don't allow writing to the boot sector. It would
912 // be very bad if the MSX tried to format this disk using other
913 // disk parameters than this code assumes. It's also not useful
914 // to write a different boot program to this disk because it
915 // will be lost when this virtual disk is ejected.
916 } else if (sector < firstSector2ndFAT) {
917 writeFATSector(sector, buf);
918 } else if (sector < firstDirSector) {
919 // Write to 2nd FAT, only buffer it. Don't interpret the data
920 // in FAT2 in any way (nor trigger any action on this write).
921 sectors[sector] = buf;
922 } else if (auto dirDirIndex = isDirSector(sector)) {
923 // Either root- or sub-directory.
924 writeDIRSector(sector, *dirDirIndex, buf);
925 } else {
926 writeDataSector(sector, buf);
927 }
928}
929
930void DirAsDSK::writeFATSector(unsigned sector, const SectorBuffer& buf)
931{
932 // Create copy of old FAT (to be able to detect changes).
933 auto oldFAT = to_vector(fat());
934
935 // Update current FAT with new data.
936 sectors[sector] = buf;
937
938 // Look for changes.
939 for (auto i : xrange(FIRST_CLUSTER, maxCluster)) {
940 if (readFAT(i) != readFATHelper(oldFAT, i)) {
941 exportFileFromFATChange(i, oldFAT);
942 }
943 }
944 // At this point there should be no more differences.
945 // Note: we can't use
946 // assert(ranges::equal(fat(), oldFAT));
947 // because exportFileFromFATChange() only updates the part of the FAT
948 // that actually contains FAT info. E.g. not the media ID at the
949 // beginning nor the unused part at the end. And for example the 'CALL
950 // FORMAT' routine also writes these parts of the FAT.
951 for (auto i : xrange(FIRST_CLUSTER, maxCluster)) {
952 assert(readFAT(i) == readFATHelper(oldFAT, i)); (void)i;
953 }
954}
955
956void DirAsDSK::exportFileFromFATChange(unsigned cluster, std::span<SectorBuffer> oldFAT)
957{
958 // Get first cluster in the FAT chain that contains 'cluster'.
959 auto [startCluster, chainLength] = getChainStart(cluster);
960
961 // Copy this whole chain from FAT1 to FAT2 (so that the loop in
962 // writeFATSector() sees this part is already handled).
963 vector<bool> visited(maxCluster, false);
964 unsigned tmp = startCluster;
965 while ((FIRST_CLUSTER <= tmp) && (tmp < maxCluster)) {
966 if (visited[tmp]) {
967 // detected cycle, don't export file
968 return;
969 }
970 visited[tmp] = true;
971
972 unsigned next = readFAT(tmp);
973 writeFATHelper(oldFAT, tmp, next);
974 tmp = next;
975 }
976
977 // Find the corresponding direntry and (if found) export file based on
978 // new cluster chain.
979 if (auto result = getDirEntryForCluster(startCluster)) {
980 exportToHost(result->dirIndex, result->dirDirIndex);
981 }
982}
983
984std::pair<unsigned, unsigned> DirAsDSK::getChainStart(unsigned cluster)
985{
986 // Search for the first cluster in the chain that contains 'cluster'
987 // Note: worst case (this implementation of) the search is O(N^2), but
988 // because usually FAT chains are allocated in ascending order, this
989 // search is fast O(N).
990 unsigned chainLength = 0;
991 for (unsigned i = FIRST_CLUSTER; i < maxCluster; ++i) { // note: i changed in loop!
992 if (readFAT(i) == cluster) {
993 // Found a predecessor.
994 cluster = i;
995 ++chainLength;
996 i = FIRST_CLUSTER - 1; // restart search
997 }
998 }
999 return {cluster, chainLength};
1000}
1001
1002// Generic helper function that walks over the whole MSX directory tree
1003// (possibly it stops early so it doesn't always walk over the whole tree).
1004// The action that is performed while walking depends on the functor parameter.
1005template<typename FUNC> bool DirAsDSK::scanMsxDirs(FUNC&& func, unsigned sector)
1006{
1007 size_t rdIdx = 0;
1008 vector<unsigned> dirs; // TODO make vector of struct instead of
1009 vector<DirIndex> dirs2; // 2 parallel vectors.
1010 while (true) {
1011 do {
1012 // About to process a new directory sector.
1013 if (func.onDirSector(sector)) return true;
1014
1015 for (auto idx : xrange(DIR_ENTRIES_PER_SECTOR)) {
1016 // About to process a new directory entry.
1017 DirIndex dirIndex(sector, idx);
1018 const MSXDirEntry& entry = msxDir(dirIndex);
1019 if (func.onDirEntry(dirIndex, entry)) return true;
1020
1021 if ((entry.filename[0] == one_of(char(0x00), char(0xE5))) ||
1022 !(entry.attrib & MSXDirEntry::Attrib::DIRECTORY)) {
1023 // Not a directory.
1024 continue;
1025 }
1026 unsigned cluster = msxDir(dirIndex).startCluster;
1027 if ((cluster < FIRST_CLUSTER) ||
1028 (cluster >= maxCluster)) {
1029 // Cluster=0 happens for ".." entries to
1030 // the root directory, also be robust for
1031 // bogus data.
1032 continue;
1033 }
1034 unsigned dir = clusterToSector(cluster);
1035 if (contains(dirs, dir)) {
1036 // Already found this sector. Except
1037 // for the special "." and ".."
1038 // entries, loops should not occur in
1039 // valid disk images, but don't crash
1040 // on (intentionally?) invalid images.
1041 continue;
1042 }
1043 // Found a new directory, insert in the set of
1044 // yet-to-be-processed directories.
1045 dirs.push_back(dir);
1046 dirs2.push_back(dirIndex);
1047 }
1048 sector = nextMsxDirSector(sector);
1049 } while (sector != unsigned(-1));
1050
1051 // Scan next subdirectory (if any).
1052 if (rdIdx == dirs.size()) {
1053 // Visited all directories.
1054 return false;
1055 }
1056 // About to process a new subdirectory.
1057 func.onVisitSubDir(dirs2[rdIdx]);
1058 sector = dirs[rdIdx++];
1059 }
1060}
1061
1062// Base class for functor objects to be used in scanMsxDirs().
1063// This implements all required methods with empty implementations.
1065 // Called right before we enter a new subdirectory.
1066 void onVisitSubDir(DirAsDSK::DirIndex /*subdir*/) const {}
1067
1068 // Called when a new sector of a (sub)directory is being scanned.
1069 [[nodiscard]] bool onDirSector(unsigned /*dirSector*/) const {
1070 return false;
1071 }
1072
1073 // Called for each directory entry (in a sector).
1074 [[nodiscard]] bool onDirEntry(DirAsDSK::DirIndex /*dirIndex*/,
1075 const MSXDirEntry& /*entry*/) const {
1076 return false;
1077 }
1078};
1079
1080// Base class for the IsDirSector and DirEntryForCluster scanner algorithms
1081// below. This class remembers the directory entry of the last visited subdir.
1083 // Called right before we enter a new subdirectory.
1084 void onVisitSubDir(DirAsDSK::DirIndex subdir) {
1085 dirDirIndex = subdir;
1086 }
1087
1088 DirAsDSK::DirIndex dirDirIndex{0, 0}; // entry for root dir
1089};
1090
1091// Figure out whether a given sector is part of the msx directory structure.
1093 explicit IsDirSector(unsigned sector_) : sector(sector_) {}
1094
1095 [[nodiscard]] bool onDirSector(unsigned dirSector) const {
1096 return sector == dirSector;
1097 }
1098
1099 unsigned sector;
1100};
1101std::optional<DirAsDSK::DirIndex> DirAsDSK::isDirSector(unsigned sector)
1102{
1103 if (IsDirSector scanner{sector};
1104 scanMsxDirs(scanner, firstDirSector)) {
1105 return scanner.dirDirIndex;
1106 }
1107 return std::nullopt;
1108}
1109
1110// Search for the directory entry that has the given startCluster.
1112 explicit DirEntryForCluster(unsigned cluster_) : cluster(cluster_) {}
1113
1114 bool onDirEntry(DirAsDSK::DirIndex dirIndex_, const MSXDirEntry& entry) {
1115 if (entry.startCluster == cluster) {
1116 dirIndex = dirIndex_;
1117 return true;
1118 }
1119 return false;
1120 }
1121
1122 unsigned cluster;
1123 DirAsDSK::DirIndex dirIndex{0, 0}; // dummy initial value, not used
1124};
1125std::optional<DirAsDSK::DirEntryForClusterResult> DirAsDSK::getDirEntryForCluster(unsigned cluster)
1126{
1127 if (DirEntryForCluster scanner{cluster};
1128 scanMsxDirs(scanner, firstDirSector)) {
1129 return DirEntryForClusterResult{scanner.dirIndex, scanner.dirDirIndex};
1130 }
1131 return std::nullopt;
1132}
1133
1134// Remove the mapping between the msx and host for all the files/dirs in the
1135// given msx directory (+ subdirectories).
1138 : mapDirs(mapDirs_) {}
1139 bool onDirEntry(DirAsDSK::DirIndex dirIndex,
1140 const MSXDirEntry& /*entry*/) {
1141 mapDirs.erase(dirIndex);
1142 return false;
1143 }
1145};
1146void DirAsDSK::unmapHostFiles(unsigned msxDirSector)
1147{
1148 scanMsxDirs(UnmapHostFiles(mapDirs), msxDirSector);
1149}
1150
1151void DirAsDSK::exportToHost(DirIndex dirIndex, DirIndex dirDirIndex)
1152{
1153 // Handle both files and subdirectories.
1154 if (msxDir(dirIndex).attrib & MSXDirEntry::Attrib::VOLUME) {
1155 // But ignore volume ID.
1156 return;
1157 }
1158 const auto& msxName = msxDir(dirIndex).filename;
1159 string hostName;
1160 if (const auto* v = lookup(mapDirs, dirIndex)) {
1161 // Hostname is already known.
1162 hostName = v->hostName;
1163 } else {
1164 // Host file/dir does not yet exist, create hostname from
1165 // msx name.
1166 if (msxName[0] == one_of(char(0x00), char(0xE5))) {
1167 // Invalid MSX name, don't do anything.
1168 return;
1169 }
1170 string hostSubDir;
1171 if (dirDirIndex.sector != 0) {
1172 // Not the msx root directory.
1173 const auto* v2 = lookup(mapDirs, dirDirIndex);
1174 assert(v2);
1175 hostSubDir = v2->hostName;
1176 assert(!hostSubDir.ends_with('/'));
1177 hostSubDir += '/';
1178 }
1179 hostName = hostSubDir + msxToHostName(msxName);
1180 mapDirs[dirIndex].hostName = hostName;
1181 }
1182 if (msxDir(dirIndex).attrib & MSXDirEntry::Attrib::DIRECTORY) {
1183 if (ranges::equal(msxName, std::string_view(". ")) ||
1184 ranges::equal(msxName, std::string_view(".. "))) {
1185 // Don't export "." or "..".
1186 return;
1187 }
1188 exportToHostDir(dirIndex, hostName);
1189 } else {
1190 exportToHostFile(dirIndex, hostName);
1191 }
1192}
1193
1194void DirAsDSK::exportToHostDir(DirIndex dirIndex, const string& hostName)
1195{
1196 try {
1197 unsigned cluster = msxDir(dirIndex).startCluster;
1198 if ((cluster < FIRST_CLUSTER) || (cluster >= maxCluster)) {
1199 // Sanity check on cluster range.
1200 return;
1201 }
1202 unsigned msxDirSector = clusterToSector(cluster);
1203
1204 // Create the host directory.
1205 FileOperations::mkdirp(hostDir + hostName);
1206
1207 // Export all the components in this directory.
1208 vector<bool> visited(nofSectors, false);
1209 do {
1210 if (visited[msxDirSector]) {
1211 // detected cycle, invalid disk, but don't crash on it
1212 return;
1213 }
1214 visited[msxDirSector] = true;
1215
1216 if (readFAT(sectorToCluster(msxDirSector)) == FREE_FAT) {
1217 // This happens e.g. on a TurboR when a directory
1218 // is removed: first the FAT is cleared before
1219 // the directory entry is updated.
1220 return;
1221 }
1222 for (auto idx : xrange(DIR_ENTRIES_PER_SECTOR)) {
1223 exportToHost(DirIndex(msxDirSector, idx), dirIndex);
1224 }
1225 msxDirSector = nextMsxDirSector(msxDirSector);
1226 } while (msxDirSector != unsigned(-1));
1227 } catch (FileException& e) {
1228 cliComm.printWarning("Error while syncing host directory: ",
1229 hostName, ": ", e.getMessage());
1230 }
1231}
1232
1233void DirAsDSK::exportToHostFile(DirIndex dirIndex, const string& hostName)
1234{
1235 // We write a host file with length that is the minimum of:
1236 // - Length indicated in msx directory entry.
1237 // - Length of FAT-chain * cluster-size, this chain can have length=0
1238 // if startCluster is not (yet) filled in.
1239 try {
1240 unsigned curCl = msxDir(dirIndex).startCluster;
1241 unsigned msxSize = msxDir(dirIndex).size;
1242
1243 File file(hostDir + hostName, File::OpenMode::TRUNCATE);
1244 unsigned offset = 0;
1245 vector<bool> visited(maxCluster, false);
1246
1247 while ((FIRST_CLUSTER <= curCl) && (curCl < maxCluster)) {
1248 if (visited[curCl]) {
1249 // detected cycle, invalid disk, but don't crash on it
1250 return;
1251 }
1252 visited[curCl] = true;
1253
1254 unsigned logicalSector = clusterToSector(curCl);
1255 for (auto i : xrange(SECTORS_PER_CLUSTER)) {
1256 if (offset >= msxSize) break;
1257 unsigned sector = logicalSector + i;
1258 assert(sector < nofSectors);
1259 auto writeSize = std::min<size_t>(msxSize - offset, SECTOR_SIZE);
1260 file.write(subspan(sectors[sector].raw, 0, writeSize));
1261 offset += SECTOR_SIZE;
1262 }
1263 if (offset >= msxSize) break;
1264 curCl = readFAT(curCl);
1265 }
1266 } catch (FileException& e) {
1267 cliComm.printWarning("Error while syncing host file: ",
1268 hostName, ": ", e.getMessage());
1269 }
1270}
1271
1272void DirAsDSK::writeDIRSector(unsigned sector, DirIndex dirDirIndex,
1273 const SectorBuffer& buf)
1274{
1275 // Look for changed directory entries.
1276 for (auto idx : xrange(DIR_ENTRIES_PER_SECTOR)) {
1277 const auto& newEntry = buf.dirEntry[idx];
1278 DirIndex dirIndex(sector, idx);
1279 if (msxDir(dirIndex) != newEntry) {
1280 writeDIREntry(dirIndex, dirDirIndex, newEntry);
1281 }
1282 }
1283 // At this point sector should be updated.
1284 assert(ranges::equal(sectors[sector].raw, buf.raw));
1285}
1286
1287void DirAsDSK::writeDIREntry(DirIndex dirIndex, DirIndex dirDirIndex,
1288 const MSXDirEntry& newEntry)
1289{
1290 if ((msxDir(dirIndex).filename != newEntry.filename) ||
1291 ((msxDir(dirIndex).attrib & MSXDirEntry::Attrib::DIRECTORY) !=
1292 ( newEntry.attrib & MSXDirEntry::Attrib::DIRECTORY))) {
1293 // Name or file-type in the direntry was changed.
1294 if (auto it = mapDirs.find(dirIndex); it != end(mapDirs)) {
1295 // If there is an associated host file, then delete it
1296 // (in case of a rename, the file will be recreated
1297 // below).
1298 auto fullHostName = tmpStrCat(hostDir, it->second.hostName);
1299 FileOperations::deleteRecursive(fullHostName); // ignore return value
1300 // Remove mapping between msx and host file/dir.
1301 mapDirs.erase(it);
1302 if (msxDir(dirIndex).attrib & MSXDirEntry::Attrib::DIRECTORY) {
1303 // In case of a directory also unmap all
1304 // sub-components.
1305 unsigned cluster = msxDir(dirIndex).startCluster;
1306 if ((FIRST_CLUSTER <= cluster) &&
1307 (cluster < maxCluster)) {
1308 unmapHostFiles(clusterToSector(cluster));
1309 }
1310 }
1311 }
1312 }
1313
1314 // Copy the new msx directory entry.
1315 msxDir(dirIndex) = newEntry;
1316
1317 // (Re-)export the full file/directory.
1318 exportToHost(dirIndex, dirDirIndex);
1319}
1320
1321void DirAsDSK::writeDataSector(unsigned sector, const SectorBuffer& buf)
1322{
1323 assert(sector >= firstDataSector);
1324 assert(sector < nofSectors);
1325
1326 // Buffer the write, whether the sector is mapped to a file or not.
1327 sectors[sector] = buf;
1328
1329 // Get first cluster in the FAT chain that contains this sector.
1330 auto [cluster, offset] = sectorToClusterOffset(sector);
1331 auto [startCluster, chainLength] = getChainStart(cluster);
1332 offset += narrow<unsigned>((sizeof(buf) * SECTORS_PER_CLUSTER) * chainLength);
1333
1334 // Get corresponding directory entry.
1335 auto entry = getDirEntryForCluster(startCluster);
1336 if (!entry) return;
1337 const auto* v = lookup(mapDirs, entry->dirIndex);
1338 if (!v) return; // sector was not mapped to a file, nothing more to do.
1339
1340 // Actually write data to host file.
1341 string fullHostName = hostDir + v->hostName;
1342 try {
1343 File file(fullHostName, "rb+"); // don't uncompress
1344 file.seek(offset);
1345 unsigned msxSize = msxDir(entry->dirIndex).size;
1346 if (msxSize > offset) {
1347 auto writeSize = std::min<size_t>(msxSize - offset, sizeof(buf));
1348 file.write(subspan(buf.raw, 0, writeSize));
1349 }
1350 } catch (FileException& e) {
1351 cliComm.printWarning("Couldn't write to file ", fullHostName,
1352 ": ", e.getMessage());
1353 }
1354}
1355
1356} // namespace openmsx
bool contains(const K &k) const
Definition hash_map.hh:110
bool empty() const
Definition hash_set.hh:521
iterator find(const K &key)
Definition hash_set.hh:547
bool erase(const K &key)
Definition hash_set.hh:483
static const SectorBuffer dos2BootBlock
Definition BootBlocks.hh:15
static const SectorBuffer dos1BootBlock
Definition BootBlocks.hh:12
void printWarning(std::string_view message)
Definition CliComm.cc:12
friend struct UnmapHostFiles
Definition DirAsDSK.hh:120
void writeSectorImpl(size_t sector, const SectorBuffer &buf) override
Definition DirAsDSK.cc:899
friend struct DirEntryForCluster
Definition DirAsDSK.hh:119
bool isWriteProtectedImpl() const override
Definition DirAsDSK.cc:334
DirAsDSK(DiskChanger &diskChanger, CliComm &cliComm, const Filename &hostDir, SyncMode syncMode, BootSectorType bootSectorType)
Definition DirAsDSK.cc:263
void checkCaches() override
Definition DirAsDSK.cc:349
void readSectorImpl(size_t sector, SectorBuffer &buf) override
Definition DirAsDSK.cc:368
bool hasChanged() const override
Has the content of this disk changed, by some other means than the MSX writing to the disk.
Definition DirAsDSK.cc:339
bool isDoubleSidedDrive() const
Scheduler * getScheduler() const
void setNbSides(unsigned num)
Definition Disk.hh:44
void setSectorsPerTrack(unsigned num)
Definition Disk.hh:42
static constexpr EmuDuration sec(unsigned x)
This class represents a filename.
Definition Filename.hh:20
Abstract class for disk images that only represent the logical sector information (so not the raw tra...
void setNbSectors(size_t num)
const Value * lookup(const hash_map< Key, Value, Hasher, Equal > &map, const Key2 &key)
Definition hash_map.hh:118
constexpr double e
Definition Math.hh:21
std::pair< string_view, string_view > splitOnLast(string_view str, string_view chars)
Definition StringOp.cc:112
bool isRegularFile(const Stat &st)
int deleteRecursive(const std::string &path)
bool isDirectory(const Stat &st)
std::optional< Stat > getStat(zstring_view filename)
Call stat() and return the stat structure.
void mkdirp(string path)
Acts like the unix command "mkdir -p".
This file implemented 3 utility functions:
Definition Autofire.cc:11
constexpr void fill(ForwardRange &&range, const T &value)
Definition ranges.hh:315
auto count(InputRange &&range, const T &value)
Definition ranges.hh:349
constexpr bool equal(InputRange1 &&range1, InputRange2 &&range2, Pred pred={}, Proj1 proj1={}, Proj2 proj2={})
Definition ranges.hh:378
constexpr auto copy(InputRange &&range, OutputIter out)
Definition ranges.hh:252
constexpr void replace(ForwardRange &&range, const T &old_value, const T &new_value)
Definition ranges.hh:303
constexpr void sort(RandomAccessRange &&range)
Definition ranges.hh:51
size_t size(std::string_view utf8)
uint32_t next(octet_iterator &it, octet_iterator end)
constexpr To narrow(From from) noexcept
Definition narrow.hh:37
constexpr auto subspan(Range &&range, size_t offset, size_t count=std::dynamic_extent)
Definition ranges.hh:481
auto to_vector(Range &&range) -> std::vector< detail::ToVectorType< T, decltype(std::begin(range))> >
Definition stl.hh:275
auto transform_in_place(ForwardRange &&range, UnaryOperation op)
Definition stl.hh:196
constexpr bool contains(ITER first, ITER last, const VAL &val)
Check if a range contains a given value, using linear search.
Definition stl.hh:32
std::string strCat()
Definition strCat.hh:703
TemporaryString tmpStrCat(Ts &&... ts)
Definition strCat.hh:742
DirAsDSK::DirIndex dirIndex
Definition DirAsDSK.cc:1123
bool onDirEntry(DirAsDSK::DirIndex dirIndex_, const MSXDirEntry &entry)
Definition DirAsDSK.cc:1114
DirEntryForCluster(unsigned cluster_)
Definition DirAsDSK.cc:1112
void onVisitSubDir(DirAsDSK::DirIndex subdir)
Definition DirAsDSK.cc:1084
DirAsDSK::DirIndex dirDirIndex
Definition DirAsDSK.cc:1088
IsDirSector(unsigned sector_)
Definition DirAsDSK.cc:1093
bool onDirSector(unsigned dirSector) const
Definition DirAsDSK.cc:1095
std::array< char, 8+3 > filename
bool onDirSector(unsigned) const
Definition DirAsDSK.cc:1069
void onVisitSubDir(DirAsDSK::DirIndex) const
Definition DirAsDSK.cc:1066
bool onDirEntry(DirAsDSK::DirIndex, const MSXDirEntry &) const
Definition DirAsDSK.cc:1074
bool onDirEntry(DirAsDSK::DirIndex dirIndex, const MSXDirEntry &)
Definition DirAsDSK.cc:1139
DirAsDSK::MapDirs & mapDirs
Definition DirAsDSK.cc:1144
UnmapHostFiles(DirAsDSK::MapDirs &mapDirs_)
Definition DirAsDSK.cc:1137
constexpr auto xrange(T e)
Definition xrange.hh:132
constexpr auto end(const zstring_view &x)