mFS file system library for EEPROM memory chips.

Revision:
12:928346513c87
Parent:
11:6c4fcb9d6193
Child:
13:142b6be3e3c8
--- a/mfs.h	Tue Feb 22 21:39:41 2011 +0000
+++ b/mfs.h	Thu Feb 24 00:02:36 2011 +0000
@@ -53,11 +53,22 @@
  *                                     would technically allow 256 TB volume sizes)
  *                                   * Optimized file searching algorithms
  * 0.6     2011-02-22 Olli Vanhoja   * Fixed file remove issue
- *                                   * Fixed mkfs bug 
+ *                                   * Fixed mkfs bug
+ * 0.7     2011-02-23 Olli Vanhoja   * file::seek(uint32_t) added
+ * 0.7     2011-02-23 Olli Vanhoja   * Fixed destroying prev link issue on flush
+ *                                   * Fixed Forwarding issue which moved cursor at the begining
+ *                                     of the filename block
+ *                                   * Separated locating of next/prev links functionality
+ *                                     into its own private function
+ *                                   * 16 bit block number pointers 256 TB theoretical maximum
+ *                                     volume size, WHOA \O/
+ *                                   * Remove and rename respects RO bit
+ *                                   * SetFileFlags fixed
+ *                                   * New file write mode DWRITE
  *
  * TODO :
  *          * Directory support (VOL blocks?)
- *          * Support for >256 blocks
+ *          * RAID 0 & 1
  *H*/
 
 #ifndef MFS_H
@@ -143,7 +154,7 @@
     /** Remove a file from the file system
     *
     * @param filename[20] Filename input.
-    * @returns Error code: 0 = OK, 1 = File doesn't exists
+    * @returns Error code: 0 = OK, 1 = File doesn't exists, 2 = RO file
     */
     char removeFile(char filename[20]);
     
@@ -151,7 +162,7 @@
     *
     * @param oldFilename[20] Old filename.
     * @param newFilename[20] New file name.
-    * @returns Error code: 0 = OK, 1 = File doesn't exists
+    * @returns Error code: 0 = OK, 1 = File doesn't exists, 2 = RO file, 3 = fs is corrupted
     */
     char renameFile(char oldFilename[20], char newFilename[20]);
     
@@ -198,6 +209,9 @@
     uint32_t mkfs(bool createLabel);
 };
 
+enum BlockLinkType {NEXT, PREV};
+enum FileOpenMode {RO, AWRITE, DWRITE};
+
 /** mFS File handle class
  * 
  * This class provides a file handle and data manipulation methods to be
@@ -206,12 +220,16 @@
 class file {
 private:
     mfs *fs;   // Reference to the file system in use
-    char attr; // RW = 1; RO = 0
+    FileOpenMode fMode;
     char buffer[BUF]; // Write buffer
     uint32_t bufPos; // "Cursor" position in buffer
-    char firstBlock; // First block of the file
-    char currBlock; // Current block in use
+    uint32_t firstBlock; // First block of the file
+    uint32_t currBlock; // Current block in use
     uint32_t blockPos; // "head" position on the current block
+    uint32_t byteCount; // Stores current "cursor" position in file for seek
+    // Private functions
+    char getBlockLink(BlockLinkType linkSelection, uint32_t *blockOut);
+    char removeFollowingBlocks(uint32_t block); // Offers destructive write/very simple wear levelling
 public:
     /** Create file handle
     *
@@ -219,10 +237,18 @@
     * Opening non-existing file will trip the system to error();
     * If read only file is opened in rw mode system will trip to error().
     *
+    * \b AWRITE is a file access mode where cursor can be moved along the file
+    * and write can be started at any point. write() function will overwrite
+    * only as many bytes as you chosen to write.
+    *
+    * \b DWRITE is a file access mode similiar to AWRITE but when you start
+    * writing all the data after cursor will be removed permanently and flush()
+    * will set a new EOF marker.
+    *
     * @param filename[20] Filename input.
-    * @param operation 0 = read only, 1 = read and write.
+    * @param operation RO = Read only, AWRITE = read and write, DWRITE = read + destructive write.
     */
-    file(mfs *fs_ref, char filename[20], char operation);
+    file(mfs *fs_ref, char filename[20], FileOpenMode operation);
     
     /** Close file handle
     *
@@ -235,10 +261,10 @@
     */
     void rewind();
     
-    /** Rewind n bytes back
+    /** Reverse n bytes back
     *
-    * @param n Number of blocks to be rewinded.
-    * @returns Error code: 0 = OK, 1 = First block of file.
+    * @param n Number of bytes.
+    * @returns Error code: 0 = OK, 1 = First byte of file.
     */
     char rewind(uint32_t n);
     
@@ -255,6 +281,13 @@
     */
     char forward(uint32_t n);
     
+    /** Seek to byte given
+    *
+    * @param byte Byte number where to seek.
+    * @returns Error code: 0 = OK, 1 = End of file or already at first byte.
+    */
+    char seek(uint32_t byte);
+    
     /** Reads a string of bytes
     *
     * Always places '\0' at the end of string.
@@ -264,26 +297,28 @@
     * @returns Error code. 0 = OK, 1 = Last block of the file
     */
     void read(char *data, uint32_t n);
+    
     /** Reads a binary array of bytes
     *
     * Doesn't add '\0' at the end of data array and doesn't respect mEOF byte.
     *
     * @param data Output buffer.
     * @param n Number of bytes to be read.
-    */
+    */   
+    void readBin(char *data, uint32_t n);
     
-    void readBin(char *data, uint32_t n);
     /** Write byte array to a file (buffer)
     *
     * @param data Input data.
     * @param n Number of bytes to be read.
     * @returns Error code: 0 = OK, 1 = Flush failed.
-    */
-    
+    */    
     char write(char *data, uint32_t n);
     
     /** Flush file buffer
     * Writes buffer to the EEPROM chip in use.
+    *
+    * @returns Error code: 0 = OK, 1 = Out of free space, 2 = Destructive operation failed (fs is corrupted).
     */
     char flush();
 };