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

@@ -25,18 +25,18 @@ struct TestDevice {
}
/** read a 512 byte block into dst */
bool readSingleBlock(LBA512 addr, uint8_t* dst) {
bool readBlock(LBA512 addr, uint8_t* dst) {
memcpy(dst, buf+addr*512, 512);
return true;
}
bool writeSingleBlock(LBA512 addr, const uint8_t* src) {
bool writeBlock(LBA512 addr, const uint8_t* src) {
memcpy(buf+addr*512, src, 512);
return true;
}
void reset(uint8_t val) {
for (int i = 0; i < sizeof(buf); ++i) {buf[i] = val;}
for (uint32_t i = 0; i < sizeof(buf); ++i) {buf[i] = val;}
}
};

View File

@@ -11,7 +11,7 @@
TEST(TestCreate, structure) {
TEST(TestCreat, structure) {
FAT32::FSHeader header;
@@ -26,25 +26,102 @@ TEST(TestCreate, structure) {
}
TEST (TestCreate, write) {
TEST (TestCreat, writeRead) {
using BlockDev = AccessHelper<TestDevice>;
using FS = FAT32::FS<BlockDev>;
size_t size = 32*1024*1024;
TestDevice dev(size);
BlockDev bDev(dev);
FAT32::FS<BlockDev> fs(bDev, 0);
FS fs(bDev, 0);
fs.setup(size);
const char* test = "This is a test";
FAT32::FS<BlockDev>::File2 f1 = fs.create2("test1.txt");
f1.write(strlen(test), (const uint8_t*)test, [](int percent) {});
for (int i = 0; i < 64; ++i) {
FAT32::FS<BlockDev>::File2 f2 = fs.create2("test2.txt");
f2.write(strlen(test), (const uint8_t*)test, [](int percent) {});
char name[64];
sprintf(name, "test%03d.txt", i);
const size_t size = 128 + i * 512;
const size_t sizeA = (size/4096+1) * 4096;
std::cout << name << " - " << size << std::endl;
FAT32::FS<BlockDev>::File2 f = fs.getOrCreateFile(name);
uint8_t* data = (uint8_t*)malloc(128 + i * 512);
for (uint32_t j = 0; j < size; ++j) {data[j] = j;}
uint32_t written = f.write(size, data);
free(data);
ASSERT_EQ(size, written);
ASSERT_EQ(size, f.getSize());
ASSERT_EQ(sizeA, f.getAllocatedSize());
}
dev.toFile("/tmp/ram/1.fat32");
// mount -t vfat 1.fat32 /mnt/fat/ && ls -l /mnt/fat/ && cat /mnt/fat/test1.txt && umount /mnt/fat
// mount -t vfat 1.fat32 /mnt/fat/ && ls -l /mnt/fat/ && cat /mnt/fat/test001.txt && umount /mnt/fat
// READ AGAIN
FS fs2(bDev, 0);
FS::DirIterator di = fs2.getRoot();
for (int i = 0; i < 64; ++i) {
char name[64];
sprintf(name, "test%03d.txt", i);
const size_t size = 128 + i * 512;
const size_t sizeA = (size/4096+1) * 4096;
FAT32::DirEntryAt dea = di.nextUsable();
FS::File2 f = fs.open2(dea);
ASSERT_EQ(name, dea.getName());
ASSERT_EQ(name, f.getName());
ASSERT_EQ(size, f.getSize());
ASSERT_EQ(sizeA, f.getAllocatedSize());
uint8_t* data = (uint8_t*)malloc(128 + i * 512);
uint32_t read = f.read(size, data);
ASSERT_EQ(size, read);
for (uint32_t j = 0; j < size; ++j) {
ASSERT_EQ((uint8_t)j, data[j]);
}
free(data);
}
}
TEST (TestCreate, getOrCreateFile) {
using BlockDev = AccessHelper<TestDevice>;
using FS = FAT32::FS<BlockDev>;
size_t size = 32*1024*1024;
TestDevice dev(size);
BlockDev bDev(dev);
FS fs(bDev, 0);
fs.setup(size);
FS::File2 f1 = fs.getOrCreateFile("test.txt");
ASSERT_EQ(0, f1.getSize());
ASSERT_EQ(4096, f1.getAllocatedSize());
ASSERT_EQ("test.txt", f1.getName());
uint8_t d1[128];
uint8_t d2[128];
for (int i = 0; i < 128; ++i) {d1[i] = 1;}
ASSERT_EQ(128, f1.write(128, d1));
FS::File2 f2 = fs.getOrCreateFile("test.txt");
ASSERT_EQ(128, f2.getSize());
ASSERT_EQ(128, f2.read(128, d2));
for (uint32_t i = 0; i < 128; ++i) {ASSERT_EQ(d1[i], d2[i]);}
}