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

@@ -20,11 +20,7 @@ public:
}
DirEntryAt cur() {
AbsPos pos = fs.clusterToAbsPos(curCluster) + (curSectorInCluster * fs.desc.bytesPerSector) + (curEntryInSector * sizeof(DirEntry));
DirEntry* dirEntry = reinterpret_cast<DirEntry*>(buf + (curEntryInSector * sizeof(DirEntry)));
return DirEntryAt(pos, dirEntry);
}
/** get the next usable entry within the current directory */
DirEntryAt nextUsable() {
@@ -86,6 +82,12 @@ public:
private:
DirEntryAt cur() {
AbsPos pos = fs.clusterToAbsPos(curCluster) + (curSectorInCluster * fs.desc.bytesPerSector) + (curEntryInSector * sizeof(DirEntry));
DirEntry* dirEntry = reinterpret_cast<DirEntry*>(buf + (curEntryInSector * sizeof(DirEntry)));
return DirEntryAt(pos, dirEntry);
}
DirEntryAt next(bool allocIfNeeded) {
while(true) {

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;
}
}

View File

@@ -29,11 +29,11 @@ public:
ClusterNr getFirstCluster() const {return dea.getFirstCluster();}
/** get the file's name */
const std::string& getName() const {return dea.getName();}
std::string getName() const {return dea.getName();}
/** read x bytes from the file */
uint32_t read(uint32_t size, uint8_t* dst, std::function<void(int)> callback) {
uint32_t read(uint32_t size, uint8_t* dst) {
Log::addInfo(NAME, "read %d bytes", size);
@@ -48,14 +48,14 @@ public:
remaining -= read;
dst += read;
totalRead += read;
callback(totalRead*100/size);
}
return totalRead;
}
uint32_t write(uint32_t size, const uint8_t* src, std::function<void(int)> callback) {
/* write the given data into the file */
uint32_t write(uint32_t size, const uint8_t* src) {
Log::addInfo(NAME, "write %d bytes", size);
@@ -67,10 +67,10 @@ public:
remaining -= written;
src += written;
totalWritten += written;
callback(totalWritten*100/size);
//callback(totalWritten*100/size);
}
// update the file header (size might have changed)
// update the file header (filesize might have changed)
fs.write(dea);
return totalWritten;
@@ -98,10 +98,10 @@ private:
}
// debug
char buf[1024];
char* dst = buf; dst += sprintf(dst, "clusters: ");
for (ClusterNr nr : clusters) {dst += sprintf(dst, "%d ", nr);}
Log::addInfo(NAME, buf);
//char buf[1024];
//char* dst = buf; dst += sprintf(dst, "clusters: ");
//for (ClusterNr nr : clusters) {dst += sprintf(dst, "%d ", nr);}
//Log::addInfo(NAME, buf);
}
@@ -136,6 +136,7 @@ private:
int32_t _write(uint32_t writeSize, const uint8_t* src) {
// do we need to append more free sectors to the end of the file?
if (curAbsPos >= getAllocatedSize()) {
if (clusters.empty()) {
const ClusterNr newCluster = fs.allocFreeCluster(0);
@@ -147,8 +148,6 @@ private:
clusters.push_back(newCluster);
Log::addInfo(NAME, "allocNextCluster(%d): %d", prevCluster, newCluster);
}
dea.setSize(dea.getSize() + fs.tmp.bytesPerCluster);
fs.write(dea);
}
// end of current cluster reached? -> switch to the next one

View File

@@ -2,7 +2,7 @@
#include <string.h>
#include <string>
#include "Types.h"
#include "../Types.h"
namespace FAT32 {