worked on FileSystem, started to migrate logging class

This commit is contained in:
2021-02-14 15:29:21 +01:00
parent faf6e55bc5
commit 6aa951190e
19 changed files with 612 additions and 241 deletions

View File

@@ -1,45 +1,79 @@
class DirIterator {
static constexpr const char* NAME = "FAT32_DirI";
FS& fs;
ClusterNr nextCluster;
int curEntryInCluster;
ClusterNr curCluster;
uint8_t curEntryInSector; // current DirEntry within the current sector
uint8_t curSectorInCluster; // current Sector within the current cluster
uint8_t buf[512];
public:
DirIterator(FS& fs, ClusterNr clusterNr) : fs(fs), nextCluster(clusterNr), curEntryInCluster(255) {
DirIterator(FS& fs, ClusterNr clusterNr) : fs(fs), curCluster(clusterNr), curEntryInSector(0), curSectorInCluster(0) {
Log::addInfo(NAME, "init @ Cluster %d", curCluster);
// read the first sector in the first cluster
read(curCluster, 0);
}
bool hasNext() {
DirEntry* next() {
while(true) {
++curEntryInCluster;
// end of sector reached?
if (curEntryInSector >= fs.tmp.dirEntriesPerSector) {
// next sector
++curSectorInCluster;
curEntryInSector = 0;
// end of cluster reached?
if (curSectorInCluster >= fs.desc.sectorsPerCluster) {
// find next cluster
curCluster = fs.getNextCluster(curCluster); // number of the next cluster (if any)
curSectorInCluster = 0;
}
// fetch from disk
read(curCluster, curSectorInCluster);
// reached end of cluster? load the next one
if (curEntryInCluster > fs.tmp.dirEntriesPerSector) {
fs.dev.read(fs.clusterToAbsPos(nextCluster), 512, buf);
nextCluster = fs.getNextCluster(nextCluster);
curEntryInCluster = 0;
}
DirEntry* desc = reinterpret_cast<DirEntry*>(buf + (sizeof(DirEntry) * curEntryInCluster));
// the current entry
DirEntry* dirEntry = reinterpret_cast<DirEntry*>(buf + (sizeof(DirEntry) * curEntryInSector));
++curEntryInSector;
if (desc->isLongFileName()) {continue;}
if (desc->isUnused()) {continue;}
if (desc->isEndOfDirectory()) {return false;}
// check it
if (dirEntry->isLongFileName()) {continue;}
if (dirEntry->isUnused()) {continue;}
if (dirEntry->isEndOfDirectory()) {
return nullptr;
}
return true;
// usable!
return dirEntry;
}
}
DirEntry next() {
DirEntry* de = reinterpret_cast<DirEntry*>(buf + (sizeof(DirEntry) * curEntryInCluster));
return *de;
private:
/** fetch one sector within a cluster */
void read(ClusterNr clusterNr, uint8_t sectorInCluster) {
Log::addInfo(NAME, "fetching sector %d in clusterNr %d", sectorInCluster, clusterNr) ;
const AbsPos pos = fs.clusterToAbsPos(clusterNr) + (curSectorInCluster * fs.desc.bytesPerSector);
fs.dev.read(pos, fs.desc.bytesPerSector, buf);
}
};