Olli Vanhoja
/
mFS_sample
Examples for mFS.
main.cpp
- Committer:
- HBP
- Date:
- 2011-02-21
- Revision:
- 1:1d0e27aaee62
- Parent:
- 0:6b88a0d4bab8
- Child:
- 2:a608de004dcf
File content as of revision 1:1d0e27aaee62:
/* TODO: *Test forward function *Test multi block files (read/write & remove) */ #include "common.h" void ls(mfs *fs); void listReservedBlocks(mfs *fs); int main() { char data[1]; char buf[44]; unsigned int n; mfs fs(0xA0); pc.printf("\n\r\n\r[mFS] Formatting... %u bad block headers.\n\r", fs.mkfs(1)); // Quick format EEPROM // Mark block 2 as bad fs.write((char[]){'\x00','\x01','\x01'}, 2, 0, 3); pc.printf("Block 2 marked as a bad block.\n\r"); fs.read(data, 0, 0, 1); pc.printf("\n\rFirst byte: %X\n\r", data[0]); pc.printf("Create file testi.txt: %u\n\r", fs.createFile("testi.txt")); pc.printf("Create file koe.txt: %u\n\r", fs.createFile("koe.txt")); pc.printf("Rm file testi.txt: %u\n\r", fs.removeFile("testi.txt")); pc.printf("Create file new.txt: %u\n\r", fs.createFile("new.txt")); // Try to write multi block file new.txt file *fp3 = new file(&fs, "new.txt", 1); pc.printf("Trying to write multi block file new.txt...\n\r"); for (n=0; n < (BS-4)-20-1; n++) fp3->write("b", 1); pc.printf("Create file a: %u\n\r", fs.createFile("testi.txt")); file *fp = new file(&fs, "testi.txt", 1); pc.printf("File new.txt opened successfully\n\r"); fp->write("The quick brown fox jumps over the lazy dog", 44); pc.printf("File write OK\n\r"); fp->flush(); pc.printf("Continue to write koe.txt..."); for (n=0; n < 40; n++) fp3->write("u", 1); pc.printf(" End\n\r"); fp3->rewind(); fp3->read(buf, 1); pc.printf("Rewind test (koe.txt): %s\n\r", buf); fp3->forward(BS-20); fp3->read(buf, 1); pc.printf("Forward test (koe.txt): %s\n\r", buf); delete fp3; pc.printf("Create file d: %u\n\r", fs.createFile("yykaa.txt")); fp->rewind(); fp->read(buf, 44); pc.printf("\n\rRead file testi.txt: %s\n\r", buf); delete fp; // Open read-only file file *fp2 = new file(&fs, "new.txt", 0); /* Keep in mind that error will * * be thrown if RO file is * * opened in RW mode. So it's * * better to check flags before * * creating a handle. */ // This should fail pc.printf("Trying to write RO file new.txt: %X\n\r", fp2->write("aa", 3)); delete fp2; pc.printf("Set file flags 0x07 for file new.txt\n\r"); fs.setFileFlags((char[]){'\x07'}, "new.txt"); ls(&fs); listReservedBlocks(&fs); } /** Lists files stored in file system * * @param *fs Gets handle to file system in use. */ void ls(mfs *fs) { unsigned int n, fileCount=0; char name[20]; char flags[1]; pc.printf("\n\rls:\n\rName:\t\t\tBlock:\tFlags:\n\r"); n=0; while (1) { n = fs->findNextFile(n, name); if (n < 0xFFFF) { fs->getFileFlags(flags, name); if ((flags[0] & 0x2) != 0) goto next; // Skip hidden file pc.printf("%s", name); char len = strlen(name); for (char i=0; i < 7-ceil((float)len/2); i++) pc.printf("\t"); pc.printf("%X\t%X\n\r", n, flags[0]); } else break; // Reach end of fs next: n++; fileCount++; } // Get number of free blocks char iFreeBlocks = fs->free(); // Calculate amount of space used unsigned int iSpaceUsed = VOL_SIZE - (iFreeBlocks*BS); // Summary pc.printf("%u files total\n\r%u/%u kB space used, %u blocks free\n\r\n\r", fileCount, iSpaceUsed/1024, VOL_SIZE/1024, iFreeBlocks); } /** Lists blocks marked as used * * @param *fs Gets handle to file system in use. */ void listReservedBlocks(mfs *fs) { char data[3]; pc.printf("\n\rReserved blocks:\n\r"); for (unsigned int n=0; n < BC; n++) { fs->read(data, n, 0, 3); if (data[0] != 4) pc.printf("b%XB0: %X, %X, %X\n\r", n, data[0], data[1], data[2]); } }