openMSX
SectorAccessibleDisk.cc
Go to the documentation of this file.
2 #include "EmptyDiskPatch.hh"
3 #include "IPSPatch.hh"
4 #include "DiskExceptions.hh"
5 #include "sha1.hh"
6 #include "xrange.hh"
7 #include <memory>
8 
9 namespace openmsx {
10 
12  : patch(std::make_unique<EmptyDiskPatch>(*this))
13 {
14 }
15 
17 
19 {
20  if (!isDummyDisk() && // in that case we want DriveEmptyException
21  (sector > 1) && // allow reading sector 0 and 1 without calling
22  // getNbSectors() because this potentially calls
23  // detectGeometry() and that would cause an
24  // infinite loop
25  (getNbSectors() <= sector)) {
26  throw NoSuchSectorException("No such sector");
27  }
28  try {
29  // in the end this calls readSectorImpl()
30  patch->copyBlock(sector * sizeof(buf), buf.raw, sizeof(buf));
31  } catch (MSXException& e) {
32  throw DiskIOErrorException("Disk I/O error: ", e.getMessage());
33  }
34 }
35 
36 void SectorAccessibleDisk::writeSector(size_t sector, const SectorBuffer& buf)
37 {
38  if (isWriteProtected()) {
40  }
41  if (!isDummyDisk() && (getNbSectors() <= sector)) {
42  throw NoSuchSectorException("No such sector");
43  }
44  try {
45  writeSectorImpl(sector, buf);
46  } catch (MSXException& e) {
47  throw DiskIOErrorException("Disk I/O error: ", e.getMessage());
48  }
49  flushCaches();
50 }
51 
53 {
54  return getNbSectorsImpl();
55 }
56 
58 {
59  patch = std::make_unique<IPSPatch>(std::move(patchFile), std::move(patch));
60 }
61 
62 std::vector<Filename> SectorAccessibleDisk::getPatches() const
63 {
64  return patch->getFilenames();
65 }
66 
68 {
69  return !patch->isEmptyPatch();
70 }
71 
73 {
74  checkCaches();
75  if (sha1cache.empty()) {
76  sha1cache = getSha1SumImpl(filePool);
77  }
78  return sha1cache;
79 }
80 
82 {
83  try {
84  setPeekMode(true);
85  SHA1 sha1;
86  for (auto i : xrange(getNbSectors())) {
87  SectorBuffer buf;
88  readSector(i, buf);
89  sha1.update(buf.raw, sizeof(buf));
90  }
91  setPeekMode(false);
92  return sha1.digest();
93  } catch (MSXException&) {
94  setPeekMode(false);
95  throw;
96  }
97 }
98 
100  SectorBuffer* buffers, size_t startSector, size_t nbSectors)
101 {
102  try {
103  for (auto i : xrange(nbSectors)) {
104  readSector(startSector + i, buffers[i]);
105  }
106  return 0;
107  } catch (MSXException&) {
108  return -1;
109  }
110 }
111 
113  const SectorBuffer* buffers, size_t startSector, size_t nbSectors)
114 {
115  try {
116  for (auto i : xrange(nbSectors)) {
117  writeSector(startSector + i, buffers[i]);
118  }
119  return 0;
120  } catch (MSXException&) {
121  return -1;
122  }
123 }
124 
126 {
127  return forcedWriteProtect || isWriteProtectedImpl();
128 }
129 
131 {
132  // can't be undone
133  forcedWriteProtect = true;
134 }
135 
137 {
138  return false;
139 }
140 
142 {
143  // nothing
144 }
145 
147 {
148  sha1cache.clear();
149 }
150 
151 } // namespace openmsx
openmsx::SectorAccessibleDisk::getSha1SumImpl
virtual Sha1Sum getSha1SumImpl(FilePool &filepool)
Definition: SectorAccessibleDisk.cc:81
openmsx::SHA1
Helper class to perform a sha1 calculation.
Definition: sha1.hh:79
xrange
auto xrange(T e)
Definition: xrange.hh:170
openmsx::SectorBuffer
Definition: DiskImageUtils.hh:90
openmsx::SectorAccessibleDisk::forceWriteProtect
void forceWriteProtect()
Definition: SectorAccessibleDisk.cc:130
DiskExceptions.hh
openmsx::Sha1Sum::clear
void clear()
Definition: utils/sha1.cc:248
sha1.hh
openmsx::SectorAccessibleDisk::writeSector
void writeSector(size_t sector, const SectorBuffer &buf)
Definition: SectorAccessibleDisk.cc:36
openmsx::WriteProtectedException
Definition: DiskExceptions.hh:23
SectorAccessibleDisk.hh
openmsx::SectorAccessibleDisk::isWriteProtected
bool isWriteProtected() const
Definition: SectorAccessibleDisk.cc:125
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::MSXException
Definition: MSXException.hh:10
openmsx::SHA1::digest
Sha1Sum digest()
Get the final hash.
Definition: utils/sha1.cc:355
openmsx::SectorBuffer::raw
byte raw[512]
Definition: DiskImageUtils.hh:91
openmsx::DiskIOErrorException
Definition: DiskExceptions.hh:13
openmsx::SectorAccessibleDisk::hasPatches
bool hasPatches() const
Definition: SectorAccessibleDisk.cc:67
openmsx::SectorAccessibleDisk::isDummyDisk
virtual bool isDummyDisk() const
Definition: SectorAccessibleDisk.cc:136
openmsx::SectorAccessibleDisk::flushCaches
virtual void flushCaches()
Definition: SectorAccessibleDisk.cc:146
openmsx::Sha1Sum
This class represents the result of a sha1 calculation (a 160-bit value).
Definition: sha1.hh:20
openmsx::SectorAccessibleDisk::setPeekMode
void setPeekMode(bool peek)
Definition: SectorAccessibleDisk.hh:60
openmsx::SectorAccessibleDisk::checkCaches
virtual void checkCaches()
Definition: SectorAccessibleDisk.cc:141
openmsx::SectorAccessibleDisk::SectorAccessibleDisk
SectorAccessibleDisk()
Definition: SectorAccessibleDisk.cc:11
EmptyDiskPatch.hh
openmsx::FilePool
Definition: FilePool.hh:16
openmsx::Sha1Sum::empty
bool empty() const
Definition: utils/sha1.cc:244
openmsx::SectorAccessibleDisk::applyPatch
void applyPatch(Filename patchFile)
Definition: SectorAccessibleDisk.cc:57
openmsx::SectorAccessibleDisk::~SectorAccessibleDisk
~SectorAccessibleDisk()
openmsx::SectorAccessibleDisk::getSha1Sum
Sha1Sum getSha1Sum(FilePool &filepool)
Calculate SHA1 of the content of this disk.
Definition: SectorAccessibleDisk.cc:72
openmsx::SectorAccessibleDisk::readSectors
int readSectors(SectorBuffer *buffers, size_t startSector, size_t nbSectors)
Definition: SectorAccessibleDisk.cc:99
openmsx::Filename
This class represents a filename.
Definition: Filename.hh:18
openmsx::MSXException::getMessage
const std::string & getMessage() const &
Definition: MSXException.hh:23
openmsx::SectorAccessibleDisk::writeSectors
int writeSectors(const SectorBuffer *buffers, size_t startSector, size_t nbSectors)
Definition: SectorAccessibleDisk.cc:112
openmsx::NoSuchSectorException
Definition: DiskExceptions.hh:8
openmsx
This file implemented 3 utility functions:
Definition: Autofire.cc:5
openmsx::SectorAccessibleDisk::getPatches
std::vector< Filename > getPatches() const
Definition: SectorAccessibleDisk.cc:62
xrange.hh
IPSPatch.hh
openmsx::SHA1::update
void update(const uint8_t *data, size_t len)
Incrementally calculate the hash value.
Definition: utils/sha1.cc:318
openmsx::EmptyDiskPatch
Definition: EmptyDiskPatch.hh:11