openMSX
MSXtar.cc
Go to the documentation of this file.
1 // Note: For Mac OS X 10.3 <ctime> must be included before <utime.h>.
2 #include <ctime>
3 #ifndef _MSC_VER
4 #include <utime.h>
5 #else
6 #include <sys/utime.h>
7 #endif
8 
9 #include "MSXtar.hh"
10 #include "SectorAccessibleDisk.hh"
11 #include "FileOperations.hh"
12 #include "foreach_file.hh"
13 #include "MSXException.hh"
14 #include "StringOp.hh"
15 #include "strCat.hh"
16 #include "File.hh"
17 #include "one_of.hh"
18 #include "stl.hh"
19 #include "xrange.hh"
20 #include <cstring>
21 #include <cassert>
22 #include <cctype>
23 #include <sys/stat.h>
24 
25 using std::string;
26 using std::string_view;
27 
28 namespace openmsx {
29 
30 constexpr unsigned BAD_FAT = 0xFF7;
31 constexpr unsigned EOF_FAT = 0xFFF; // actually 0xFF8-0xFFF, signals EOF in FAT12
33 
34 constexpr byte T_MSX_REG = 0x00; // Normal file
35 constexpr byte T_MSX_READ = 0x01; // Read-Only file
36 constexpr byte T_MSX_HID = 0x02; // Hidden file
37 constexpr byte T_MSX_SYS = 0x04; // System file
38 constexpr byte T_MSX_VOL = 0x08; // filename is Volume Label
39 constexpr byte T_MSX_DIR = 0x10; // entry is a subdir
40 constexpr byte T_MSX_ARC = 0x20; // Archive bit
41 // This particular combination of flags indicates that this dir entry is used
42 // to store a long Unicode file name.
43 // For details, read http://home.teleport.com/~brainy/lfn.htm
44 constexpr byte T_MSX_LFN = 0x0F; // LFN entry (long files names)
45 
49 unsigned MSXtar::clusterToSector(unsigned cluster) const
50 {
51  return 1 + rootDirLast + sectorsPerCluster * (cluster - 2);
52 }
53 
57 unsigned MSXtar::sectorToCluster(unsigned sector) const
58 {
59  return 2 + ((sector - (1 + rootDirLast)) / sectorsPerCluster);
60 }
61 
62 
65 void MSXtar::parseBootSector(const MSXBootSector& boot)
66 {
67  unsigned nbRootDirSectors = boot.dirEntries / 16;
68  sectorsPerFat = boot.sectorsFat;
69  sectorsPerCluster = boot.spCluster;
70 
71  if (boot.nrSectors == 0) { // TODO: check limits more accurately
72  throw MSXException(
73  "Illegal number of sectors: ", boot.nrSectors);
74  }
75  if (boot.nrSides == 0) { // TODO: check limits more accurately
76  throw MSXException(
77  "Illegal number of sides: ", boot.nrSides);
78  }
79  if (boot.nrFats == 0) { // TODO: check limits more accurately
80  throw MSXException(
81  "Illegal number of FATs: ", boot.nrFats);
82  }
83  if (sectorsPerFat == 0) { // TODO: check limits more accurately
84  throw MSXException(
85  "Illegal number sectors per FAT: ", sectorsPerFat);
86  }
87  if (nbRootDirSectors == 0) { // TODO: check limits more accurately
88  throw MSXException(
89  "Illegal number of root dir sectors: ", nbRootDirSectors);
90  }
91  if (sectorsPerCluster == 0) { // TODO: check limits more accurately
92  throw MSXException(
93  "Illegal number of sectors per cluster: ", sectorsPerCluster);
94  }
95 
96  rootDirStart = 1 + boot.nrFats * sectorsPerFat;
97  chrootSector = rootDirStart;
98  rootDirLast = rootDirStart + nbRootDirSectors - 1;
99  maxCluster = sectorToCluster(boot.nrSectors);
100 
101  // Some (invalid) diskimages have a too small FAT to be able to address
102  // all clusters of the image. OpenMSX SVN revisions pre-11326 even
103  // created such invalid images for some disk sizes!!
104  unsigned maxFatCluster = (2 * SECTOR_SIZE * sectorsPerFat) / 3;
105  maxCluster = std::min(maxCluster, maxFatCluster);
106 }
107 
108 void MSXtar::writeLogicalSector(unsigned sector, const SectorBuffer& buf)
109 {
110  assert(!fatBuffer.empty());
111  unsigned fatSector = sector - 1;
112  if (fatSector < sectorsPerFat) {
113  // we have a cache and this is a sector of the 1st FAT
114  // --> update cache
115  memcpy(&fatBuffer[fatSector], &buf, sizeof(buf));
116  fatCacheDirty = true;
117  } else {
118  disk.writeSector(sector, buf);
119  }
120 }
121 
122 void MSXtar::readLogicalSector(unsigned sector, SectorBuffer& buf)
123 {
124  assert(!fatBuffer.empty());
125  unsigned fatSector = sector - 1;
126  if (fatSector < sectorsPerFat) {
127  // we have a cache and this is a sector of the 1st FAT
128  // --> read from cache
129  memcpy(&buf, &fatBuffer[fatSector], sizeof(buf));
130  } else {
131  disk.readSector(sector, buf);
132  }
133 }
134 
136  : disk(sectordisk)
137 {
138  if (disk.getNbSectors() == 0) {
139  throw MSXException("No disk inserted.");
140  }
141  try {
142  SectorBuffer buf;
143  disk.readSector(0, buf);
144  parseBootSector(buf.bootSector);
145  } catch (MSXException& e) {
146  throw MSXException("Bad disk image: ", e.getMessage());
147  }
148 
149  // cache complete FAT
150  fatCacheDirty = false;
151  fatBuffer.resize(sectorsPerFat);
152  disk.readSectors(&fatBuffer[0], 1, sectorsPerFat);
153 }
154 
156 {
157  if (!fatCacheDirty) return;
158 
159  for (auto i : xrange(sectorsPerFat)) {
160  try {
161  disk.writeSector(i + 1, fatBuffer[i]);
162  } catch (MSXException&) {
163  // nothing
164  }
165  }
166 }
167 
168 // transform BAD_FAT (0xFF7) and EOF_FAT-range (0xFF8-0xFFF)
169 // to a single value: EOF_FAT (0xFFF)
170 static constexpr unsigned normalizeFAT(unsigned cluster)
171 {
172  return (cluster < BAD_FAT) ? cluster : EOF_FAT;
173 }
174 
175 // Get the next clusternumber from the FAT chain
176 unsigned MSXtar::readFAT(unsigned clNr) const
177 {
178  assert(!fatBuffer.empty()); // FAT must already be cached
179  const auto* data = fatBuffer[0].raw;
180  const auto* p = &data[(clNr * 3) / 2];
181  unsigned result = (clNr & 1)
182  ? (p[0] >> 4) + (p[1] << 4)
183  : p[0] + ((p[1] & 0x0F) << 8);
184  return normalizeFAT(result);
185 }
186 
187 // Write an entry to the FAT
188 void MSXtar::writeFAT(unsigned clNr, unsigned val)
189 {
190  assert(!fatBuffer.empty()); // FAT must already be cached
191  assert(val < 4096); // FAT12
192  auto* data = fatBuffer[0].raw;
193  auto* p = &data[(clNr * 3) / 2];
194  if (clNr & 1) {
195  p[0] = (p[0] & 0x0F) + (val << 4);
196  p[1] = val >> 4;
197  } else {
198  p[0] = val;
199  p[1] = (p[1] & 0xF0) + ((val >> 8) & 0x0F);
200  }
201  fatCacheDirty = true;
202 }
203 
204 // Find the next clusternumber marked as free in the FAT
205 // @throws When no more free clusters
206 unsigned MSXtar::findFirstFreeCluster()
207 {
208  for (auto cluster : xrange(2u, maxCluster)) {
209  if (readFAT(cluster) == 0) {
210  return cluster;
211  }
212  }
213  throw MSXException("Disk full.");
214 }
215 
216 // Get the next sector from a file or (root/sub)directory
217 // If no next sector then 0 is returned
218 unsigned MSXtar::getNextSector(unsigned sector)
219 {
220  if (sector <= rootDirLast) {
221  // sector is part of the root directory
222  return (sector == rootDirLast) ? 0 : sector + 1;
223  }
224  unsigned currCluster = sectorToCluster(sector);
225  if (currCluster == sectorToCluster(sector + 1)) {
226  // next sector of cluster
227  return sector + 1;
228  } else {
229  // first sector in next cluster
230  unsigned nextCl = readFAT(currCluster);
231  return (nextCl == EOF_FAT) ? 0 : clusterToSector(nextCl);
232  }
233 }
234 
235 // get start cluster from a directory entry,
236 // also takes care of BAD_FAT and EOF_FAT-range.
237 static unsigned getStartCluster(const MSXDirEntry& entry)
238 {
239  return normalizeFAT(entry.startCluster);
240 }
241 
242 // If there are no more free entries in a subdirectory, the subdir is
243 // expanded with an extra cluster. This function gets the free cluster,
244 // clears it and updates the fat for the subdir
245 // returns: the first sector in the newly appended cluster
246 // @throws When disk is full
247 unsigned MSXtar::appendClusterToSubdir(unsigned sector)
248 {
249  unsigned nextCl = findFirstFreeCluster();
250  unsigned nextSector = clusterToSector(nextCl);
251 
252  // clear this cluster
253  SectorBuffer buf;
254  memset(&buf, 0, sizeof(buf));
255  for (auto i : xrange(sectorsPerCluster)) {
256  writeLogicalSector(i + nextSector, buf);
257  }
258 
259  unsigned curCl = sectorToCluster(sector);
260  assert(readFAT(curCl) == EOF_FAT);
261  writeFAT(curCl, nextCl);
262  writeFAT(nextCl, EOF_FAT);
263  return nextSector;
264 }
265 
266 
267 // Returns the index of a free (or with deleted file) entry
268 // In: The current dir sector
269 // Out: index number, if no index is found then -1 is returned
270 unsigned MSXtar::findUsableIndexInSector(unsigned sector)
271 {
272  SectorBuffer buf;
273  readLogicalSector(sector, buf);
274 
275  // find a not used (0x00) or delete entry (0xE5)
276  for (auto i : xrange(16)) {
277  if (buf.dirEntry[i].filename[0] == one_of(0x00, char(0xE5))) {
278  return i;
279  }
280  }
281  return unsigned(-1);
282 }
283 
284 // This function returns the sector and dirindex for a new directory entry
285 // if needed the involved subdirectroy is expanded by an extra cluster
286 // returns: a DirEntry containing sector and index
287 // @throws When either root dir is full or disk is full
288 MSXtar::DirEntry MSXtar::addEntryToDir(unsigned sector)
289 {
290  // this routine adds the msx name to a directory sector, if needed (and
291  // possible) the directory is extened with an extra cluster
292  DirEntry result;
293  result.sector = sector;
294 
295  if (sector <= rootDirLast) {
296  // add to the root directory
297  for (/* */ ; result.sector <= rootDirLast; result.sector++) {
298  result.index = findUsableIndexInSector(result.sector);
299  if (result.index != unsigned(-1)) {
300  return result;
301  }
302  }
303  throw MSXException("Root directory full.");
304 
305  } else {
306  // add to a subdir
307  while (true) {
308  result.index = findUsableIndexInSector(result.sector);
309  if (result.index != unsigned(-1)) {
310  return result;
311  }
312  unsigned nextSector = getNextSector(result.sector);
313  if (nextSector == 0) {
314  nextSector = appendClusterToSubdir(result.sector);
315  }
316  result.sector = nextSector;
317  }
318  }
319 }
320 
321 // create an MSX filename 8.3 format, if needed in vfat like abbreviation
322 static char toMSXChr(char a)
323 {
324  a = toupper(a);
325  if (a == one_of(' ', '.')) {
326  a = '_';
327  }
328  return a;
329 }
330 
331 // Transform a long hostname in a 8.3 uppercase filename as used in the
332 // direntries on an MSX
333 static string makeSimpleMSXFileName(string_view fullFilename)
334 {
335  auto [dir, fullFile] = StringOp::splitOnLast(fullFilename, '/');
336 
337  // handle special case '.' and '..' first
338  string result(8 + 3, ' ');
339  if (fullFile == one_of(".", "..")) {
340  memcpy(result.data(), fullFile.data(), fullFile.size());
341  return result;
342  }
343 
344  auto [file, ext] = StringOp::splitOnLast(fullFile, '.');
345  if (file.empty()) std::swap(file, ext);
346 
347  StringOp::trimRight(file, ' ');
348  StringOp::trimRight(ext, ' ');
349 
350  // put in major case and create '_' if needed
351  string fileS(file.data(), std::min<size_t>(8, file.size()));
352  string extS (ext .data(), std::min<size_t>(3, ext .size()));
353  transform_in_place(fileS, toMSXChr);
354  transform_in_place(extS, toMSXChr);
355 
356  // add correct number of spaces
357  memcpy(result.data() + 0, fileS.data(), fileS.size());
358  memcpy(result.data() + 8, extS .data(), extS .size());
359  return result;
360 }
361 
362 // This function creates a new MSX subdir with given date 'd' and time 't'
363 // in the subdir pointed at by 'sector'. In the newly
364 // created subdir the entries for '.' and '..' are created
365 // returns: the first sector of the new subdir
366 // @throws in case no directory could be created
367 unsigned MSXtar::addSubdir(
368  std::string_view msxName, unsigned t, unsigned d, unsigned sector)
369 {
370  // returns the sector for the first cluster of this subdir
371  DirEntry result = addEntryToDir(sector);
372 
373  // load the sector
374  SectorBuffer buf;
375  readLogicalSector(result.sector, buf);
376 
377  auto& dirEntry = buf.dirEntry[result.index];
378  dirEntry.attrib = T_MSX_DIR;
379  dirEntry.time = t;
380  dirEntry.date = d;
381  memcpy(&dirEntry, makeSimpleMSXFileName(msxName).data(), 11);
382 
383  // dirEntry.filesize = fsize;
384  unsigned curCl = findFirstFreeCluster();
385  dirEntry.startCluster = curCl;
386  writeFAT(curCl, EOF_FAT);
387 
388  // save the sector again
389  writeLogicalSector(result.sector, buf);
390 
391  // clear this cluster
392  unsigned logicalSector = clusterToSector(curCl);
393  memset(&buf, 0, sizeof(buf));
394  for (auto i : xrange(sectorsPerCluster)) {
395  writeLogicalSector(i + logicalSector, buf);
396  }
397 
398  // now add the '.' and '..' entries!!
399  memset(&buf.dirEntry[0], 0, sizeof(MSXDirEntry));
400  memset(&buf.dirEntry[0], ' ', 11);
401  memset(&buf.dirEntry[0], '.', 1);
402  buf.dirEntry[0].attrib = T_MSX_DIR;
403  buf.dirEntry[0].time = t;
404  buf.dirEntry[0].date = d;
405  buf.dirEntry[0].startCluster = curCl;
406 
407  memset(&buf.dirEntry[1], 0, sizeof(MSXDirEntry));
408  memset(&buf.dirEntry[1], ' ', 11);
409  memset(&buf.dirEntry[1], '.', 2);
410  buf.dirEntry[1].attrib = T_MSX_DIR;
411  buf.dirEntry[1].time = t;
412  buf.dirEntry[1].date = d;
413  buf.dirEntry[1].startCluster = sectorToCluster(sector);
414 
415  // and save this in the first sector of the new subdir
416  writeLogicalSector(logicalSector, buf);
417 
418  return logicalSector;
419 }
420 
421 struct TimeDate {
422  unsigned time, date;
423 };
424 static TimeDate getTimeDate(time_t totalSeconds)
425 {
426  if (tm* mtim = localtime(&totalSeconds)) {
427  unsigned time = (mtim->tm_sec >> 1) + (mtim->tm_min << 5) +
428  (mtim->tm_hour << 11);
429  unsigned date = mtim->tm_mday + ((mtim->tm_mon + 1) << 5) +
430  ((mtim->tm_year + 1900 - 1980) << 9);
431  return {time, date};
432  }
433  return {0, 0};
434 }
435 
436 // Get the time/date from a host file in MSX format
437 static TimeDate getTimeDate(zstring_view filename)
438 {
439  struct stat st;
440  if (stat(filename.c_str(), &st)) {
441  // stat failed
442  return {0, 0};
443  } else {
444  // Some info indicates that st.st_mtime could be useless on win32 with vfat.
445  // On Android 'st_mtime' is 'unsigned long' instead of 'time_t'
446  // (like on linux), so we require a reinterpret_cast. That cast
447  // is fine (but redundant) on linux.
448  return getTimeDate(reinterpret_cast<time_t&>(st.st_mtime));
449  }
450 }
451 
452 // Add an MSXsubdir with the time properties from the HOST-OS subdir
453 // @throws when subdir could not be created
454 unsigned MSXtar::addSubdirToDSK(zstring_view hostName, std::string_view msxName,
455  unsigned sector)
456 {
457  auto [time, date] = getTimeDate(hostName);
458  return addSubdir(msxName, time, date, sector);
459 }
460 
461 // This file alters the filecontent of a given file
462 // It only changes the file content (and the filesize in the msxDirEntry)
463 // It doesn't changes timestamps nor filename, filetype etc.
464 // @throws when something goes wrong
465 void MSXtar::alterFileInDSK(MSXDirEntry& msxDirEntry, const string& hostName)
466 {
467  // get host file size
468  struct stat st;
469  if (stat(hostName.c_str(), &st)) {
470  throw MSXException("Error reading host file: ", hostName);
471  }
472  unsigned hostSize = st.st_size;
473  unsigned remaining = hostSize;
474 
475  // open host file for reading
476  File file(hostName, "rb");
477 
478  // copy host file to image
479  unsigned prevCl = 0;
480  unsigned curCl = getStartCluster(msxDirEntry);
481  while (remaining) {
482  // allocate new cluster if needed
483  try {
484  if (curCl == one_of(0u, EOF_FAT)) {
485  unsigned newCl = findFirstFreeCluster();
486  if (prevCl == 0) {
487  msxDirEntry.startCluster = newCl;
488  } else {
489  writeFAT(prevCl, newCl);
490  }
491  writeFAT(newCl, EOF_FAT);
492  curCl = newCl;
493  }
494  } catch (MSXException&) {
495  // no more free clusters
496  break;
497  }
498 
499  // fill cluster
500  unsigned logicalSector = clusterToSector(curCl);
501  for (unsigned j = 0; (j < sectorsPerCluster) && remaining; ++j) {
502  SectorBuffer buf;
503  memset(&buf, 0, sizeof(buf));
504  unsigned chunkSize = std::min(SECTOR_SIZE, remaining);
505  file.read(&buf, chunkSize);
506  writeLogicalSector(logicalSector + j, buf);
507  remaining -= chunkSize;
508  }
509 
510  // advance to next cluster
511  prevCl = curCl;
512  curCl = readFAT(curCl);
513  }
514 
515  // terminate FAT chain
516  if (prevCl == 0) {
517  msxDirEntry.startCluster = 0;
518  } else {
519  writeFAT(prevCl, EOF_FAT);
520  }
521 
522  // free rest of FAT chain
523  while (curCl != one_of(EOF_FAT, 0u)) {
524  unsigned nextCl = readFAT(curCl);
525  writeFAT(curCl, 0);
526  curCl = nextCl;
527  }
528 
529  // write (possibly truncated) file size
530  msxDirEntry.size = hostSize - remaining;
531 
532  if (remaining) {
533  throw MSXException("Disk full, ", hostName, " truncated.");
534  }
535 }
536 
537 // Find the dir entry for 'name' in subdir starting at the given 'sector'
538 // with given 'index'
539 // returns: a DirEntry with sector and index filled in
540 // sector is 0 if no match was found
541 MSXtar::DirEntry MSXtar::findEntryInDir(
542  const string& name, unsigned sector, SectorBuffer& buf)
543 {
544  DirEntry result;
545  result.sector = sector;
546  result.index = 0; // avoid warning (only some gcc versions complain)
547  while (result.sector) {
548  // read sector and scan 16 entries
549  readLogicalSector(result.sector, buf);
550  for (result.index = 0; result.index < 16; ++result.index) {
551  if (string_view(buf.dirEntry[result.index].filename, 11) == name) {
552  return result;
553  }
554  }
555  // try next sector
556  result.sector = getNextSector(result.sector);
557  }
558  return result;
559 }
560 
561 // Add file to the MSX disk in the subdir pointed to by 'sector'
562 // @throws when file could not be added
563 string MSXtar::addFileToDSK(const string& fullHostName, unsigned rootSector)
564 {
565  auto [directory, hostName] = StringOp::splitOnLast(fullHostName, "/\\");
566  string msxName = makeSimpleMSXFileName(hostName);
567 
568  // first find out if the filename already exists in current dir
569  SectorBuffer dummy;
570  DirEntry fullMsxDirEntry = findEntryInDir(msxName, rootSector, dummy);
571  if (fullMsxDirEntry.sector != 0) {
572  // TODO implement overwrite option
573  return strCat("Warning: preserving entry ", hostName, '\n');
574  }
575 
576  SectorBuffer buf;
577  DirEntry entry = addEntryToDir(rootSector);
578  readLogicalSector(entry.sector, buf);
579  auto& dirEntry = buf.dirEntry[entry.index];
580  memset(&dirEntry, 0, sizeof(dirEntry));
581  memcpy(&dirEntry, msxName.data(), 11);
582  dirEntry.attrib = T_MSX_REG;
583 
584  // compute time/date stamps
585  auto [time, date] = getTimeDate(fullHostName);
586  dirEntry.time = time;
587  dirEntry.date = date;
588 
589  try {
590  alterFileInDSK(dirEntry, fullHostName);
591  } catch (MSXException&) {
592  // still write directory entry
593  writeLogicalSector(entry.sector, buf);
594  throw;
595  }
596  writeLogicalSector(entry.sector, buf);
597  return {};
598 }
599 
600 // Transfer directory and all its subdirectories to the MSX diskimage
601 // @throws when an error occurs
602 string MSXtar::recurseDirFill(string_view dirName, unsigned sector)
603 {
604  string messages;
605 
606  auto fileAction = [&](const string& path) {
607  // add new file
608  messages += addFileToDSK(path, sector);
609  };
610  auto dirAction = [&](const string& path, std::string_view name) {
611  string msxFileName = makeSimpleMSXFileName(name);
612  SectorBuffer buf;
613  DirEntry entry = findEntryInDir(msxFileName, sector, buf);
614  if (entry.sector != 0) {
615  // entry already exists ..
616  auto& msxDirEntry = buf.dirEntry[entry.index];
617  if (msxDirEntry.attrib & T_MSX_DIR) {
618  // .. and is a directory
619  unsigned nextSector = clusterToSector(
620  getStartCluster(msxDirEntry));
621  messages += recurseDirFill(path, nextSector);
622  } else {
623  // .. but is NOT a directory
624  strAppend(messages,
625  "MSX file ", msxFileName,
626  " is not a directory.\n");
627  }
628  } else {
629  // add new directory
630  unsigned nextSector = addSubdirToDSK(path, name, sector);
631  messages += recurseDirFill(path, nextSector);
632  }
633  };
634  foreach_file_and_directory(std::string(dirName), fileAction, dirAction);
635 
636  return messages;
637 }
638 
639 
640 static string condensName(const MSXDirEntry& dirEntry)
641 {
642  string result;
643  for (unsigned i = 0; (i < 8) && (dirEntry.name.base[i] != ' '); ++i) {
644  result += char(tolower(dirEntry.name.base[i]));
645  }
646  if (dirEntry.name.ext[0] != ' ') {
647  result += '.';
648  for (unsigned i = 0; (i < 3) && (dirEntry.name.ext[i] != ' '); ++i) {
649  result += char(tolower(dirEntry.name.ext[i]));
650  }
651  }
652  return result;
653 }
654 
655 
656 // Set the entries from dirEntry to the timestamp of resultFile
657 static void changeTime(zstring_view resultFile, const MSXDirEntry& dirEntry)
658 {
659  unsigned t = dirEntry.time;
660  unsigned d = dirEntry.date;
661  struct tm mTim;
662  struct utimbuf uTim;
663  mTim.tm_sec = (t & 0x001f) << 1;
664  mTim.tm_min = (t & 0x07e0) >> 5;
665  mTim.tm_hour = (t & 0xf800) >> 11;
666  mTim.tm_mday = (d & 0x001f);
667  mTim.tm_mon = ((d & 0x01e0) >> 5) - 1;
668  mTim.tm_year = ((d & 0xfe00) >> 9) + 80;
669  mTim.tm_isdst = -1;
670  uTim.actime = mktime(&mTim);
671  uTim.modtime = mktime(&mTim);
672  utime(resultFile.c_str(), &uTim);
673 }
674 
675 string MSXtar::dir()
676 {
677  string result;
678  for (unsigned sector = chrootSector; sector != 0; sector = getNextSector(sector)) {
679  SectorBuffer buf;
680  readLogicalSector(sector, buf);
681  for (auto& dirEntry : buf.dirEntry) {
682  if ((dirEntry.filename[0] == one_of(char(0xe5), char(0x00))) ||
683  (dirEntry.attrib == T_MSX_LFN)) continue;
684 
685  // filename first (in condensed form for human readability)
686  string tmp = condensName(dirEntry);
687  tmp.resize(13, ' ');
688  strAppend(result, tmp,
689  // attributes
690  (dirEntry.attrib & T_MSX_DIR ? 'd' : '-'),
691  (dirEntry.attrib & T_MSX_READ ? 'r' : '-'),
692  (dirEntry.attrib & T_MSX_HID ? 'h' : '-'),
693  (dirEntry.attrib & T_MSX_VOL ? 'v' : '-'), // TODO check if this is the output of files,l
694  (dirEntry.attrib & T_MSX_ARC ? 'a' : '-'), // TODO check if this is the output of files,l
695  " ",
696  // filesize
697  dirEntry.size << '\n');
698  }
699  }
700  return result;
701 }
702 
703 // routines to update the global vars: chrootSector
704 void MSXtar::chdir(string_view newRootDir)
705 {
706  chroot(newRootDir, false);
707 }
708 
709 void MSXtar::mkdir(string_view newRootDir)
710 {
711  unsigned tmpMSXchrootSector = chrootSector;
712  chroot(newRootDir, true);
713  chrootSector = tmpMSXchrootSector;
714 }
715 
716 void MSXtar::chroot(string_view newRootDir, bool createDir)
717 {
718  if (StringOp::startsWith(newRootDir, '/') || StringOp::startsWith(newRootDir, '\\')) {
719  // absolute path, reset chrootSector
720  chrootSector = rootDirStart;
721  StringOp::trimLeft(newRootDir, "/\\");
722  }
723 
724  while (!newRootDir.empty()) {
725  auto [firstPart, lastPart] = StringOp::splitOnFirst(newRootDir, "/\\");
726  newRootDir = lastPart;
727  StringOp::trimLeft(newRootDir, "/\\");
728 
729  // find 'firstPart' directory or create it if requested
730  SectorBuffer buf;
731  string simple = makeSimpleMSXFileName(firstPart);
732  DirEntry entry = findEntryInDir(simple, chrootSector, buf);
733  if (entry.sector == 0) {
734  if (!createDir) {
735  throw MSXException("Subdirectory ", firstPart,
736  " not found.");
737  }
738  // creat new subdir
739  time_t now;
740  time(&now);
741  auto [t, d] = getTimeDate(now);
742  chrootSector = addSubdir(simple, t, d, chrootSector);
743  } else {
744  auto& dirEntry = buf.dirEntry[entry.index];
745  if (!(dirEntry.attrib & T_MSX_DIR)) {
746  throw MSXException(firstPart, " is not a directory.");
747  }
748  chrootSector = clusterToSector(getStartCluster(dirEntry));
749  }
750  }
751 }
752 
753 void MSXtar::fileExtract(const string& resultFile, const MSXDirEntry& dirEntry)
754 {
755  unsigned size = dirEntry.size;
756  unsigned sector = clusterToSector(getStartCluster(dirEntry));
757 
758  File file(resultFile, "wb");
759  while (size && sector) {
760  SectorBuffer buf;
761  readLogicalSector(sector, buf);
762  unsigned savesize = std::min(size, SECTOR_SIZE);
763  file.write(&buf, savesize);
764  size -= savesize;
765  sector = getNextSector(sector);
766  }
767  // now change the access time
768  changeTime(resultFile, dirEntry);
769 }
770 
771 // extracts a single item (file or directory) from the msx image to the host OS
772 string MSXtar::singleItemExtract(string_view dirName, string_view itemName,
773  unsigned sector)
774 {
775  // first find out if the filename exists in current dir
776  SectorBuffer buf;
777  string msxName = makeSimpleMSXFileName(itemName);
778  DirEntry entry = findEntryInDir(msxName, sector, buf);
779  if (entry.sector == 0) {
780  return strCat(itemName, " not found!\n");
781  }
782 
783  auto& msxDirEntry = buf.dirEntry[entry.index];
784  // create full name for local filesystem
785  string fullName = strCat(dirName, '/', condensName(msxDirEntry));
786 
787  // ...and extract
788  if (msxDirEntry.attrib & T_MSX_DIR) {
789  // recursive extract this subdir
790  FileOperations::mkdirp(fullName);
791  recurseDirExtract(
792  fullName,
793  clusterToSector(getStartCluster(msxDirEntry)));
794  } else {
795  // it is a file
796  fileExtract(fullName, msxDirEntry);
797  }
798  return {};
799 }
800 
801 
802 // extracts the contents of the directory (at sector) and all its subdirs to the host OS
803 void MSXtar::recurseDirExtract(string_view dirName, unsigned sector)
804 {
805  for (/* */ ; sector != 0; sector = getNextSector(sector)) {
806  SectorBuffer buf;
807  readLogicalSector(sector, buf);
808  for (auto& dirEntry : buf.dirEntry) {
809  if (dirEntry.filename[0] == one_of(char(0xe5), char(0x00), '.')) {
810  continue;
811  }
812  string filename = condensName(dirEntry);
813  string fullName = filename;
814  if (!dirName.empty()) {
815  fullName = strCat(dirName, '/', filename);
816  }
817  if (dirEntry.attrib != T_MSX_DIR) { // TODO
818  fileExtract(fullName, dirEntry);
819  }
820  if (dirEntry.attrib == T_MSX_DIR) {
821  FileOperations::mkdirp(fullName);
822  // now change the access time
823  changeTime(fullName, dirEntry);
824  recurseDirExtract(
825  fullName,
826  clusterToSector(getStartCluster(dirEntry)));
827  }
828  }
829  }
830 }
831 
832 string MSXtar::addDir(string_view rootDirName)
833 {
834  return recurseDirFill(rootDirName, chrootSector);
835 }
836 
837 string MSXtar::addFile(const string& filename)
838 {
839  return addFileToDSK(filename, chrootSector);
840 }
841 
842 string MSXtar::getItemFromDir(string_view rootDirName, string_view itemName)
843 {
844  return singleItemExtract(rootDirName, itemName, chrootSector);
845 }
846 
847 void MSXtar::getDir(string_view rootDirName)
848 {
849  recurseDirExtract(rootDirName, chrootSector);
850 }
851 
852 } // namespace openmsx
void swap(openmsx::MemBuffer< T > &l, openmsx::MemBuffer< T > &r) noexcept
Definition: MemBuffer.hh:202
TclObject t
Definition: one_of.hh:7
const std::string & getMessage() const &
Definition: MSXException.hh:23
std::string getItemFromDir(std::string_view rootDirName, std::string_view itemName)
Definition: MSXtar.cc:842
MSXtar(SectorAccessibleDisk &disk)
Definition: MSXtar.cc:135
void getDir(std::string_view rootDirName)
Definition: MSXtar.cc:847
std::string addFile(const std::string &filename)
Definition: MSXtar.cc:837
void chdir(std::string_view newRootDir)
Definition: MSXtar.cc:704
void mkdir(std::string_view newRootDir)
Definition: MSXtar.cc:709
std::string addDir(std::string_view rootDirName)
Definition: MSXtar.cc:832
std::string dir()
Definition: MSXtar.cc:675
void readSector(size_t sector, SectorBuffer &buf)
void readSectors(SectorBuffer *buffers, size_t startSector, size_t nbSectors)
static constexpr size_t SECTOR_SIZE
void writeSector(size_t sector, const SectorBuffer &buf)
Like std::string_view, but with the extra guarantee that it refers to a zero-terminated string.
Definition: zstring_view.hh:21
constexpr const char * c_str() const
Definition: zstring_view.hh:48
std::pair< string_view, string_view > splitOnLast(string_view str, string_view chars)
Definition: StringOp.cc:132
void trimRight(string &str, const char *chars)
Definition: StringOp.cc:53
std::pair< string_view, string_view > splitOnFirst(string_view str, string_view chars)
Definition: StringOp.cc:115
void trimLeft(string &str, const char *chars)
Definition: StringOp.cc:82
bool startsWith(string_view total, string_view part)
Definition: StringOp.cc:33
constexpr vecN< N, T > min(const vecN< N, T > &x, const vecN< N, T > &y)
Definition: gl_vec.hh:269
void mkdirp(string path)
Acts like the unix command "mkdir -p".
This file implemented 3 utility functions:
Definition: Autofire.cc:5
constexpr unsigned SECTOR_SIZE
Definition: DirAsDSK.cc:23
constexpr unsigned EOF_FAT
Definition: DirAsDSK.cc:38
constexpr byte T_MSX_READ
Definition: MSXtar.cc:35
constexpr byte T_MSX_SYS
Definition: MSXtar.cc:37
constexpr const char *const filename
constexpr byte T_MSX_ARC
Definition: MSXtar.cc:40
constexpr unsigned BAD_FAT
Definition: DirAsDSK.cc:37
constexpr byte T_MSX_REG
Definition: MSXtar.cc:34
constexpr byte T_MSX_VOL
Definition: MSXtar.cc:38
constexpr byte T_MSX_HID
Definition: MSXtar.cc:36
constexpr byte T_MSX_DIR
Definition: MSXtar.cc:39
constexpr byte T_MSX_LFN
Definition: MSXtar.cc:44
bool foreach_file_and_directory(std::string path, FileAction fileAction, DirAction dirAction)
size_t size(std::string_view utf8)
auto transform_in_place(ForwardRange &&range, UnaryOperation op)
Definition: stl.hh:244
std::string strCat(Ts &&...ts)
Definition: strCat.hh:591
void strAppend(std::string &result, Ts &&...ts)
Definition: strCat.hh:669
unsigned date
Definition: MSXtar.cc:422
unsigned time
Definition: MSXtar.cc:422
MSXBootSector bootSector
MSXDirEntry dirEntry[16]
constexpr auto xrange(T e)
Definition: xrange.hh:155