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 "DiskImageUtils.hh"
11#include "TclObject.hh"
12
13#include "MemBuffer.hh"
14#include "zstring_view.hh"
15
16#include <string_view>
17#include <variant>
18
19namespace openmsx {
20
21class SectorAccessibleDisk;
22class MsxChar2Unicode;
23
24namespace FAT {
25 struct Free {
26 [[nodiscard]] auto operator<=>(const Free&) const = default;
27 };
28
29 struct EndOfChain {
30 [[nodiscard]] auto operator<=>(const EndOfChain&) const = default;
31 };
32
33 struct Cluster {
34 unsigned index;
35 [[nodiscard]] auto operator<=>(const Cluster&) const = default;
36 };
37
38 using DirCluster = std::variant<Free, Cluster>;
39 using FatCluster = std::variant<Free, EndOfChain, Cluster>;
40
42}
43
44class MSXtar
45{
46public:
47 enum class Add {
50 };
51public:
52 explicit MSXtar(SectorAccessibleDisk& disk, const MsxChar2Unicode& msxChars_);
53 MSXtar(MSXtar&& other) noexcept;
54 ~MSXtar();
55
56 void chdir(std::string_view newRootDir);
57 void mkdir(std::string_view newRootDir);
58 std::string dir(); // formatted output
59 TclObject dirRaw(); // unformatted output
60 std::string addItem(const std::string& hostItemName, Add add); // add file or directory
61 std::string addFile(const std::string& filename, Add add);
62 std::string addDir(std::string_view rootDirName, Add add); // add files from host-dir, but does not create a top-level dir on the msx side
63 std::string getItemFromDir(std::string_view rootDirName, std::string_view itemName);
64 void getDir(std::string_view rootDirName);
65 std::string deleteItem(std::string_view itemName); // delete file or directory (recursive)
66 std::string renameItem(std::string_view currentName, std::string_view newName); // rename file or directory
67 std::string convertToMsxName(std::string_view name) const; // truncate to 8.3 filename
68
71 unsigned clusterSize; // in bytes
72 };
74
75private:
76 struct DirEntry {
77 unsigned sector;
78 unsigned index;
79 };
80
81 void writeLogicalSector(unsigned sector, const SectorBuffer& buf);
82 void readLogicalSector (unsigned sector, SectorBuffer& buf);
83
84 [[nodiscard]] unsigned clusterToSector(FAT::Cluster cluster) const;
85 [[nodiscard]] FAT::Cluster sectorToCluster(unsigned sector) const;
86 void parseBootSector(const MSXBootSector& boot);
87 [[nodiscard]] FAT::FatCluster readFAT(FAT::Cluster index) const;
88 void writeFAT(FAT::Cluster index, FAT::FatCluster value);
89 [[nodiscard]] FAT::Cluster findFirstFreeCluster();
90 [[nodiscard]] unsigned countFreeClusters() const;
91 [[nodiscard]] unsigned findUsableIndexInSector(unsigned sector);
92 [[nodiscard]] unsigned getNextSector(unsigned sector);
93 [[nodiscard]] unsigned appendClusterToSubdir(unsigned sector);
94 [[nodiscard]] DirEntry addEntryToDir(unsigned sector);
95 [[nodiscard]] unsigned addSubdir(const FAT::FileName& msxName,
96 uint16_t t, uint16_t d, unsigned sector);
97 void alterFileInDSK(MSXDirEntry& msxDirEntry, const std::string& hostName);
98 void deleteItem(std::string_view itemName, unsigned sector);
99 std::string deleteEntry(const FAT::FileName& msxName, unsigned rootSector);
100 void deleteEntry(MSXDirEntry& msxDirEntry);
101 void deleteDirectory(unsigned sector);
102 void freeFatChain(FAT::FatCluster cluster);
103 [[nodiscard]] unsigned addSubdirToDSK(zstring_view hostName,
104 const FAT::FileName& msxName, unsigned sector);
105 [[nodiscard]] DirEntry findEntryInDir(const FAT::FileName& msxName, unsigned sector,
106 SectorBuffer& sectorBuf);
107 std::string addFileToDSK(const std::string& fullHostName, unsigned sector, Add add);
108 std::string addOrCreateSubdir(zstring_view hostDirName, unsigned sector, Add add);
109 std::string recurseDirFill(std::string_view dirName, unsigned sector, Add add);
110 void fileExtract(const std::string& resultFile, const MSXDirEntry& dirEntry);
111 void recurseDirExtract(std::string_view dirName, unsigned sector);
112 std::string singleItemExtract(std::string_view dirName, std::string_view itemName,
113 unsigned sector);
114 void chroot(std::string_view newRootDir, bool createDir);
115
116private:
117 [[nodiscard]] FAT::DirCluster getStartCluster(const MSXDirEntry& entry) const;
118 void setStartCluster(MSXDirEntry& entry, FAT::DirCluster cluster) const;
119
120 [[nodiscard]] FAT::FileName hostToMSXFileName(std::string_view hostName) const;
121 [[nodiscard]] std::string msxToHostFileName(const FAT::FileName& msxName) const;
122
124 MemBuffer<SectorBuffer> fatBuffer;
125 const MsxChar2Unicode& msxChars;
126 FAT::Cluster findFirstFreeClusterStart; // all clusters before this one are in use
127
128 unsigned clusterCount;
129 unsigned fatCount;
130 unsigned sectorsPerCluster;
131 unsigned sectorsPerFat;
132 unsigned fatStart; // first sector of the first FAT
133 unsigned rootDirStart; // first sector of the root directory
134 unsigned dataStart; // first sector of the cluster data
135 unsigned chrootSector;
136 bool fat16;
137
138 bool fatCacheDirty;
139};
140
141} // namespace openmsx
142
143#endif
TclObject t
std::string getItemFromDir(std::string_view rootDirName, std::string_view itemName)
Definition MSXtar.cc:1135
void getDir(std::string_view rootDirName)
Definition MSXtar.cc:1140
std::string addDir(std::string_view rootDirName, Add add)
Definition MSXtar.cc:1125
void chdir(std::string_view newRootDir)
Definition MSXtar.cc:973
void mkdir(std::string_view newRootDir)
Definition MSXtar.cc:978
std::string convertToMsxName(std::string_view name) const
Definition MSXtar.cc:1151
std::string dir()
Definition MSXtar.cc:954
std::string addFile(const std::string &filename, Add add)
Definition MSXtar.cc:1130
std::string renameItem(std::string_view currentName, std::string_view newName)
Definition MSXtar.cc:755
std::string deleteItem(std::string_view itemName)
Definition MSXtar.cc:1145
FreeSpaceResult getFreeSpace() const
Definition MSXtar.cc:1157
std::string addItem(const std::string &hostItemName, Add add)
Definition MSXtar.cc:1113
TclObject dirRaw()
Definition MSXtar.cc:932
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.
std::variant< Free, Cluster > DirCluster
Definition MSXtar.hh:38
std::variant< Free, EndOfChain, Cluster > FatCluster
Definition MSXtar.hh:39
decltype(MSXDirEntry::filename) FileName
Definition MSXtar.hh:41
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