worked on FAT32 stuff

This commit is contained in:
2021-02-21 09:33:08 +01:00
parent da12992ae8
commit 4ac72c678f
13 changed files with 494 additions and 148 deletions

View File

@@ -2,6 +2,7 @@
#include <cstdint>
#include <functional>
#include <vector>
#include "Structs.h"
@@ -75,33 +76,24 @@ namespace FAT32 {
return File(*this, dea.getSize(), dea.getFirstCluster());
}
/** open the given file for reading*/
/** open the given file for reading */
File2 open2(const DirEntryAt& dea) {
return File2(*this, dea);
}
/** create a new file for writing, in the given directory */
File2 create2(const char* name) {
/** get or create a file with the given name */
File2 getOrCreateFile(const char* name) {
ClusterNr dirStartCluster = 2; // TODO
DirEntryAt dea = getDirEntry(name, true);
//if (!dea.isValid()) {Log::addError(NAME, "create2(): not a valid DirEntry");}
//if (!dea.isDirectory()) {Log::addError(NAME, "create2(): not a directory");}
DirIterator di(*this, dirStartCluster);
// new file -> allocate the first cluster
if (dea.getFirstCluster() == 0) {
ClusterNr firstCluster = allocFreeCluster(0);
dea.setFirstCluster(firstCluster);
write(dea);
}
// allocate a new directory entry for the file
DirEntryAt dea2 = di.nextFree();
dea2.setName(name);
dea2.setSize(0);
// allocate the file's first cluster
ClusterNr firstCluster = allocFreeCluster(0);
dea2.setFirstCluster(firstCluster);
// write the file description
write(dea2);
return File2(*this, dea2);
return File2(*this, dea);
}
@@ -132,18 +124,36 @@ namespace FAT32 {
Log::addInfo(NAME, "Bytes/Sector: %d, Sector/Cluster: %d, FATs: %d, RootDir: %d", desc.bytesPerSector, desc.sectorsPerCluster, desc.numberOfFATs, desc.rootDirFirstCluster);
/*
std::cout << (int)desc.bytesPerSector << std::endl;
std::cout << (int)desc.sectorsPerCluster << std::endl;
std::cout << (int)desc.numReservedSectors << std::endl;
std::cout << (int)desc.numberOfFATs << std::endl;
std::cout << (int)desc.sectorsPerFAT << std::endl;
std::cout << (int)desc.rootDirFirstCluster << std::endl;
}
std::cout << tmp.startOfFAT << std::endl;
std::cout << tmp.startOfFirstDataCluster << std::endl;
std::cout << tmp.startOfFirstRootDirCluster << std::endl;
*/
/** get (or create, if needed) a DirEntry with the given name */
DirEntryAt getDirEntry(const char* name, bool createIfNeeded) {
// TODO: support for sub folders)
// start at the root folder
ClusterNr dirStartCluster = 2;
{ // search for a matching existing entry
DirIterator di(*this, dirStartCluster);
while(true) {
DirEntryAt dea = di.nextUsable();
if (!dea.isValid()) {break;}
if (dea.getName() == name) {return dea;}
}
}
// no matching entry found
if (!createIfNeeded) {return DirEntryAt::invalid();}
{ // allocate a new DirEntry for the file
DirIterator di(*this, dirStartCluster);
DirEntryAt dea = di.nextFree();
dea.setName(name);
dea.setSize(0);
dea.setFirstCluster(0);
return dea;
}
}