openMSX
MSXtar.hh
Go to the documentation of this file.
1// This code implements the functionality of my older msxtar program
2// that could manipulate files and directories on dsk and ide-hd images
3// Integrating it is seen as temporary bypassing of the need for a
4// DirAsDisk2 that supports subdirs, partitions etc. since this class will
5// of those functionalities although not on a dynamic base
6
7#ifndef MSXTAR_HH
8#define MSXTAR_HH
9
10#include "MemBuffer.hh"
11#include "DiskImageUtils.hh"
12#include "zstring_view.hh"
13#include <string_view>
14#include <variant>
15
16namespace openmsx {
17
18class SectorAccessibleDisk;
19class MsxChar2Unicode;
20
21namespace FAT {
22 struct Free {
23 [[nodiscard]] auto operator<=>(const Free&) const = default;
24 };
25
26 struct EndOfChain {
27 [[nodiscard]] auto operator<=>(const EndOfChain&) const = default;
28 };
29
30 struct Cluster {
31 unsigned index;
32 [[nodiscard]] auto operator<=>(const Cluster&) const = default;
33 };
34
35 using DirCluster = std::variant<Free, Cluster>;
36 using FatCluster = std::variant<Free, EndOfChain, Cluster>;
37
39}
40
41class MSXtar
42{
43public:
44 explicit MSXtar(SectorAccessibleDisk& disk, const MsxChar2Unicode& msxChars_);
45 MSXtar(MSXtar&& other) noexcept;
46 ~MSXtar();
47
48 void chdir(std::string_view newRootDir);
49 void mkdir(std::string_view newRootDir);
50 std::string dir();
51 std::string addFile(const std::string& filename);
52 std::string addDir(std::string_view rootDirName);
53 std::string getItemFromDir(std::string_view rootDirName, std::string_view itemName);
54 void getDir(std::string_view rootDirName);
55
56private:
57 struct DirEntry {
58 unsigned sector;
59 unsigned index;
60 };
61
62 void writeLogicalSector(unsigned sector, const SectorBuffer& buf);
63 void readLogicalSector (unsigned sector, SectorBuffer& buf);
64
65 [[nodiscard]] unsigned clusterToSector(FAT::Cluster cluster) const;
66 [[nodiscard]] FAT::Cluster sectorToCluster(unsigned sector) const;
67 void parseBootSector(const MSXBootSector& boot);
68 [[nodiscard]] FAT::FatCluster readFAT(FAT::Cluster index) const;
69 void writeFAT(FAT::Cluster index, FAT::FatCluster value);
70 FAT::Cluster findFirstFreeCluster();
71 unsigned findUsableIndexInSector(unsigned sector);
72 unsigned getNextSector(unsigned sector);
73 unsigned appendClusterToSubdir(unsigned sector);
74 DirEntry addEntryToDir(unsigned sector);
75 unsigned addSubdir(const FAT::FileName& msxName,
76 uint16_t t, uint16_t d, unsigned sector);
77 void alterFileInDSK(MSXDirEntry& msxDirEntry, const std::string& hostName);
78 unsigned addSubdirToDSK(zstring_view hostName,
79 const FAT::FileName& msxName, unsigned sector);
80 DirEntry findEntryInDir(const FAT::FileName& msxName, unsigned sector,
81 SectorBuffer& sectorBuf);
82 std::string addFileToDSK(const std::string& fullHostName, unsigned sector);
83 std::string recurseDirFill(std::string_view dirName, unsigned sector);
84 void fileExtract(const std::string& resultFile, const MSXDirEntry& dirEntry);
85 void recurseDirExtract(std::string_view dirName, unsigned sector);
86 std::string singleItemExtract(std::string_view dirName, std::string_view itemName,
87 unsigned sector);
88 void chroot(std::string_view newRootDir, bool createDir);
89
90private:
91 [[nodiscard]] FAT::DirCluster getStartCluster(const MSXDirEntry& entry) const;
92 void setStartCluster(MSXDirEntry& entry, FAT::DirCluster cluster) const;
93
94 [[nodiscard]] FAT::FileName hostToMSXFileName(std::string_view hostName) const;
95 [[nodiscard]] std::string msxToHostFileName(const FAT::FileName& msxName) const;
96
99 const MsxChar2Unicode& msxChars;
100 FAT::Cluster findFirstFreeClusterStart; // all clusters before this one are in use
101
102 unsigned clusterCount;
103 unsigned fatCount;
104 unsigned sectorsPerCluster;
105 unsigned sectorsPerFat;
106 unsigned fatStart; // first sector of the first FAT
107 unsigned rootDirStart; // first sector of the root directory
108 unsigned dataStart; // first sector of the cluster data
109 unsigned chrootSector;
110 bool fat16;
111
112 bool fatCacheDirty;
113};
114
115} // namespace openmsx
116
117#endif
TclObject t
std::string getItemFromDir(std::string_view rootDirName, std::string_view itemName)
Definition: MSXtar.cc:1022
void getDir(std::string_view rootDirName)
Definition: MSXtar.cc:1027
std::string addFile(const std::string &filename)
Definition: MSXtar.cc:1017
void chdir(std::string_view newRootDir)
Definition: MSXtar.cc:872
void mkdir(std::string_view newRootDir)
Definition: MSXtar.cc:877
MSXtar(SectorAccessibleDisk &disk, const MsxChar2Unicode &msxChars_)
Definition: MSXtar.cc:197
std::string addDir(std::string_view rootDirName)
Definition: MSXtar.cc:1012
std::string dir()
Definition: MSXtar.cc:839
This class manages the lifetime of a block of memory.
Definition: MemBuffer.hh:29
Like std::string_view, but with the extra guarantee that it refers to a zero-terminated string.
Definition: zstring_view.hh:22
std::variant< Free, Cluster > DirCluster
Definition: MSXtar.hh:35
std::variant< Free, EndOfChain, Cluster > FatCluster
Definition: MSXtar.hh:36
decltype(MSXDirEntry::filename) FileName
Definition: MSXtar.hh:38
This file implemented 3 utility functions:
Definition: Autofire.cc:9
auto operator<=>(const Cluster &) const =default
auto operator<=>(const EndOfChain &) const =default
auto operator<=>(const Free &) const =default
std::array< char, 8+3 > filename