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