mFS file system library for EEPROM memory chips.

Committer:
HBP
Date:
Thu Feb 24 00:02:36 2011 +0000
Revision:
12:928346513c87
Parent:
11:6c4fcb9d6193
Child:
13:142b6be3e3c8
-16 bit block pointers
-Improved file writing capabilities
-Lot of bug fixes

See mfs.h for almost complete list of updates.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
HBP 5:a0fe74dce80d 1 /** @file mfs.h */
HBP 0:cbf45dde2b49 2 /*H****************************************************************************
HBP 7:5ac5121bb4e0 3 * FILENAME : mfs.h *
HBP 7:5ac5121bb4e0 4 * *
HBP 7:5ac5121bb4e0 5 * DESCRIPTION : *
HBP 7:5ac5121bb4e0 6 * mFS file system implementation for mBED with external I2C EEEPROM. *
HBP 8:e67733ad4427 7 * *
HBP 8:e67733ad4427 8 * ---------------------------------------------------------------------------*
HBP 8:e67733ad4427 9 * "THE BEER-WARE LICENSE" (Revision 42): *
HBP 8:e67733ad4427 10 * <olli.vanhoja@gmail.com> wrote this file. As long as you retain this notice*
HBP 8:e67733ad4427 11 * you can do whatever you want with this stuff. If we meet some day, and you *
HBP 8:e67733ad4427 12 * think this stuff is worth it, you can buy me a beer in return Olli Vanhoja *
HBP 8:e67733ad4427 13 * ---------------------------------------------------------------------------*
HBP 8:e67733ad4427 14 * *
HBP 8:e67733ad4427 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
HBP 8:e67733ad4427 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
HBP 8:e67733ad4427 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *
HBP 8:e67733ad4427 18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
HBP 8:e67733ad4427 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
HBP 8:e67733ad4427 20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
HBP 8:e67733ad4427 21 * DEALINGS IN THE SOFTWARE. *
HBP 8:e67733ad4427 22 * *
HBP 8:e67733ad4427 23 * *
HBP 8:e67733ad4427 24 * Block Flags: *
HBP 8:e67733ad4427 25 * 7:FBOF Begining of file *
HBP 8:e67733ad4427 26 * 6:LBOF Last block of file *
HBP 8:e67733ad4427 27 * 5:RO Read only file (Used only with FBOF) *
HBP 8:e67733ad4427 28 * 4:HIDDEN Hidden file (Used only with FBOF) *
HBP 8:e67733ad4427 29 * 3:INUSE Block in use *
HBP 8:e67733ad4427 30 * 2:NBAD Bad block (INV) *
HBP 8:e67733ad4427 31 * 1:VOL Volume label (Used only with FBOF) *
HBP 8:e67733ad4427 32 * 0:LOCK Locked file (Used only with FBOF) *
HBP 8:e67733ad4427 33 * *
HBP 8:e67733ad4427 34 * AUTHOR : Olli Vanhoja START DATE : 2011-02-18 *
HBP 8:e67733ad4427 35 ******************************************************************************
HBP 7:5ac5121bb4e0 36 *
HBP 7:5ac5121bb4e0 37 * CHANGES :
HBP 7:5ac5121bb4e0 38 *
HBP 7:5ac5121bb4e0 39 * VERSION DATE WHO DETAIL
HBP 7:5ac5121bb4e0 40 * 0.1 2011-02-21 Olli Vanhoja Initial release version
HBP 7:5ac5121bb4e0 41 * 0.2 2011-02-21 Olli Vanhoja Documentational comments added
HBP 10:211cb54339a0 42 * 0.3 2011-02-21 Olli Vanhoja * File::read issues fixed, rewind/forward
HBP 10:211cb54339a0 43 * functions improved
HBP 10:211cb54339a0 44 * * Added possibility change I2C speed
HBP 10:211cb54339a0 45 * * I2C autoreset on failure
HBP 10:211cb54339a0 46 * 0.4 2011-02-22 Olli Vanhoja * mfs::renameFile(char [20], char [20] function added
HBP 10:211cb54339a0 47 * * Incresed fault tolerance by first allocating new
HBP 10:211cb54339a0 48 * block and then linking to it from previous block
HBP 10:211cb54339a0 49 * * Reconstructed initialization and use of some variables
HBP 10:211cb54339a0 50 * 0.5 2011-02-22 Olli Vanhoja * Improved documentation
HBP 10:211cb54339a0 51 * * Block variables changed from char to uint32_t for
HBP 10:211cb54339a0 52 * code optimization (and for possible 16bit update which
HBP 11:6c4fcb9d6193 53 * would technically allow 256 TB volume sizes)
HBP 10:211cb54339a0 54 * * Optimized file searching algorithms
HBP 11:6c4fcb9d6193 55 * 0.6 2011-02-22 Olli Vanhoja * Fixed file remove issue
HBP 12:928346513c87 56 * * Fixed mkfs bug
HBP 12:928346513c87 57 * 0.7 2011-02-23 Olli Vanhoja * file::seek(uint32_t) added
HBP 12:928346513c87 58 * 0.7 2011-02-23 Olli Vanhoja * Fixed destroying prev link issue on flush
HBP 12:928346513c87 59 * * Fixed Forwarding issue which moved cursor at the begining
HBP 12:928346513c87 60 * of the filename block
HBP 12:928346513c87 61 * * Separated locating of next/prev links functionality
HBP 12:928346513c87 62 * into its own private function
HBP 12:928346513c87 63 * * 16 bit block number pointers 256 TB theoretical maximum
HBP 12:928346513c87 64 * volume size, WHOA \O/
HBP 12:928346513c87 65 * * Remove and rename respects RO bit
HBP 12:928346513c87 66 * * SetFileFlags fixed
HBP 12:928346513c87 67 * * New file write mode DWRITE
HBP 7:5ac5121bb4e0 68 *
HBP 7:5ac5121bb4e0 69 * TODO :
HBP 10:211cb54339a0 70 * * Directory support (VOL blocks?)
HBP 12:928346513c87 71 * * RAID 0 & 1
HBP 7:5ac5121bb4e0 72 *H*/
HBP 0:cbf45dde2b49 73
HBP 0:cbf45dde2b49 74 #ifndef MFS_H
HBP 0:cbf45dde2b49 75 #define MFS_H
HBP 0:cbf45dde2b49 76
HBP 0:cbf45dde2b49 77 #include "i2c_eeprom.h"
HBP 0:cbf45dde2b49 78
HBP 6:dd3346914d42 79 const unsigned int VOL_SIZE=65536; /**< EEPROM chip size in bytes */
HBP 6:dd3346914d42 80 const unsigned int BS=1024; /**< How many bytes per block (default: 4096 bytes) */
HBP 10:211cb54339a0 81 const unsigned int BC=VOL_SIZE / BS; // block count
HBP 0:cbf45dde2b49 82 const char mEOF='\x01'; // End Of File/Section marked
HBP 6:dd3346914d42 83 const unsigned int BUF=400; /**< File buffer length */
HBP 0:cbf45dde2b49 84
HBP 3:1cbc15648de1 85 /** mFS File System class
HBP 3:1cbc15648de1 86 *
HBP 3:1cbc15648de1 87 * This class is used as a handle for the fs in use.
HBP 3:1cbc15648de1 88 */
HBP 0:cbf45dde2b49 89 class mfs {
HBP 0:cbf45dde2b49 90 private:
HBP 0:cbf45dde2b49 91 i2c_eeprom *mem; // Only 512 kB I2C EEPROM is supported ATM
HBP 0:cbf45dde2b49 92 public:
HBP 3:1cbc15648de1 93 /** Create a new file system object
HBP 3:1cbc15648de1 94 *
HBP 3:1cbc15648de1 95 * @param xi2c_address a Physical I2C address of the EEPROM chip
HBP 3:1cbc15648de1 96 */
HBP 0:cbf45dde2b49 97 mfs(int i2c_address);
HBP 5:a0fe74dce80d 98
HBP 10:211cb54339a0 99 /** Read data from specified fs block
HBP 3:1cbc15648de1 100 *
HBP 3:1cbc15648de1 101 * @param *data Pointer for readed data
HBP 3:1cbc15648de1 102 * @param block Block number.
HBP 3:1cbc15648de1 103 * @param byte Selected byte.
HBP 3:1cbc15648de1 104 * @param n Bytes to be read.
HBP 10:211cb54339a0 105 * @returns Error code: 0 = OK, 1 = Incorrect input
HBP 3:1cbc15648de1 106 */
HBP 10:211cb54339a0 107 char read(char *data, uint32_t block, uint32_t byte, uint32_t n);
HBP 5:a0fe74dce80d 108
HBP 10:211cb54339a0 109 /** Write data to specified fs block
HBP 3:1cbc15648de1 110 *
HBP 3:1cbc15648de1 111 * @param *data Pointer for readed data
HBP 3:1cbc15648de1 112 * @param block Block number.
HBP 3:1cbc15648de1 113 * @param byte Selected byte.
HBP 3:1cbc15648de1 114 * @param n Bytes to be read.
HBP 10:211cb54339a0 115 * @returns Error code: 0 = OK, 1 = Incorrect input
HBP 3:1cbc15648de1 116 */
HBP 10:211cb54339a0 117 char write(char *data, uint32_t block, uint32_t byte, uint32_t n);
HBP 5:a0fe74dce80d 118
HBP 3:1cbc15648de1 119 /** Locate next free block
HBP 3:1cbc15648de1 120 *
HBP 3:1cbc15648de1 121 * @param *blockOut Returns next free block from begining of the fs.
HBP 10:211cb54339a0 122 * @returns Error code: 0 = OK, 1 = Out of space
HBP 3:1cbc15648de1 123 */
HBP 10:211cb54339a0 124 char getNextFreeBlock(uint32_t *blockOut);
HBP 5:a0fe74dce80d 125
HBP 3:1cbc15648de1 126 /** Locates next starting file from parameter block
HBP 3:1cbc15648de1 127 *
HBP 3:1cbc15648de1 128 * @param block Start scanning from this block.
HBP 3:1cbc15648de1 129 * @param *filenameOut Return name of the file found.
HBP 10:211cb54339a0 130 * @param Returns block number of the file found.
HBP 10:211cb54339a0 131 * @returns Error code: 0 = OK, 1 = Empty fs
HBP 3:1cbc15648de1 132 */
HBP 10:211cb54339a0 133 char findNextFile(uint32_t block, char *filenameOut, uint32_t *blockOut);
HBP 5:a0fe74dce80d 134
HBP 10:211cb54339a0 135 /** Get block number of the given file
HBP 10:211cb54339a0 136 *
HBP 10:211cb54339a0 137 * Returns block number of the block flaged with FBOF flag.
HBP 3:1cbc15648de1 138 *
HBP 3:1cbc15648de1 139 * @param filename[20] Filename input.
HBP 10:211cb54339a0 140 * @param Returns block number of the first block of the given file.
HBP 10:211cb54339a0 141 * @returns Error code: 0 = OK, 1 = File not found
HBP 3:1cbc15648de1 142 */
HBP 10:211cb54339a0 143 char getFirstBlockOfFile(char filename[20], uint32_t *blockOut);
HBP 5:a0fe74dce80d 144
HBP 4:c77812997c9c 145 /** Create a new empty file
HBP 3:1cbc15648de1 146 *
HBP 3:1cbc15648de1 147 * Reserves one block for the file created.
HBP 3:1cbc15648de1 148 *
HBP 3:1cbc15648de1 149 * @param filename[20] Filename input.
HBP 10:211cb54339a0 150 * @returns Error code: 0 = OK, 1 = File exists already, 2 = Out of space
HBP 3:1cbc15648de1 151 */
HBP 0:cbf45dde2b49 152 char createFile(char filename[20]);
HBP 5:a0fe74dce80d 153
HBP 10:211cb54339a0 154 /** Remove a file from the file system
HBP 3:1cbc15648de1 155 *
HBP 3:1cbc15648de1 156 * @param filename[20] Filename input.
HBP 12:928346513c87 157 * @returns Error code: 0 = OK, 1 = File doesn't exists, 2 = RO file
HBP 3:1cbc15648de1 158 */
HBP 0:cbf45dde2b49 159 char removeFile(char filename[20]);
HBP 5:a0fe74dce80d 160
HBP 10:211cb54339a0 161 /** Rename a file
HBP 7:5ac5121bb4e0 162 *
HBP 10:211cb54339a0 163 * @param oldFilename[20] Old filename.
HBP 10:211cb54339a0 164 * @param newFilename[20] New file name.
HBP 12:928346513c87 165 * @returns Error code: 0 = OK, 1 = File doesn't exists, 2 = RO file, 3 = fs is corrupted
HBP 7:5ac5121bb4e0 166 */
HBP 7:5ac5121bb4e0 167 char renameFile(char oldFilename[20], char newFilename[20]);
HBP 7:5ac5121bb4e0 168
HBP 3:1cbc15648de1 169 /** Set user modifiable flags.
HBP 3:1cbc15648de1 170 *
HBP 10:211cb54339a0 171 * \code
HBP 3:1cbc15648de1 172 * desc RO|HIDDEN|LOCK
HBP 3:1cbc15648de1 173 * bit 3 2 1
HBP 10:211cb54339a0 174 * \endcode
HBP 3:1cbc15648de1 175 *
HBP 3:1cbc15648de1 176 * @param *flags Flag input
HBP 3:1cbc15648de1 177 * @param filename[20] Filename input.
HBP 10:211cb54339a0 178 * @returns Error code: 0 = OK, 1 = File doesn't exists, 2 = File system is corrupted
HBP 3:1cbc15648de1 179 */
HBP 0:cbf45dde2b49 180 char setFileFlags(char *flags, char filename[20]);
HBP 5:a0fe74dce80d 181
HBP 3:1cbc15648de1 182 /** Read user modifiable flags.
HBP 3:1cbc15648de1 183 *
HBP 10:211cb54339a0 184 * \code
HBP 3:1cbc15648de1 185 * desc RO|HIDDEN|LOCK
HBP 3:1cbc15648de1 186 * bit 3 2 1
HBP 10:211cb54339a0 187 * \endcode
HBP 3:1cbc15648de1 188 *
HBP 3:1cbc15648de1 189 * @param *flags Flag output
HBP 3:1cbc15648de1 190 * @param filename[20] Filename input.
HBP 10:211cb54339a0 191 * @returns Error code: 0 = OK, 1 = File doesn't exists
HBP 3:1cbc15648de1 192 */
HBP 0:cbf45dde2b49 193 char getFileFlags(char *flags, char filename[20]);
HBP 5:a0fe74dce80d 194
HBP 3:1cbc15648de1 195 /** Get number of free blocks
HBP 3:1cbc15648de1 196 *
HBP 3:1cbc15648de1 197 * @returns Number of free blocks.
HBP 3:1cbc15648de1 198 */
HBP 10:211cb54339a0 199 uint32_t free();
HBP 5:a0fe74dce80d 200
HBP 3:1cbc15648de1 201 /** Format new file system
HBP 3:1cbc15648de1 202 *
HBP 10:211cb54339a0 203 * \note Keep in mind that only first byte is checked for functionality and
HBP 10:211cb54339a0 204 * if it's broken the who file system is useless.
HBP 3:1cbc15648de1 205 *
HBP 3:1cbc15648de1 206 * @param createLabel Create volume label at the begining of the file system. (there is no specified use for volume labels atm).
HBP 10:211cb54339a0 207 * @returns Number of bad block headers.
HBP 3:1cbc15648de1 208 */
HBP 10:211cb54339a0 209 uint32_t mkfs(bool createLabel);
HBP 0:cbf45dde2b49 210 };
HBP 0:cbf45dde2b49 211
HBP 12:928346513c87 212 enum BlockLinkType {NEXT, PREV};
HBP 12:928346513c87 213 enum FileOpenMode {RO, AWRITE, DWRITE};
HBP 12:928346513c87 214
HBP 4:c77812997c9c 215 /** mFS File handle class
HBP 4:c77812997c9c 216 *
HBP 10:211cb54339a0 217 * This class provides a file handle and data manipulation methods to be
HBP 10:211cb54339a0 218 * used for files stored in mFS files system.
HBP 4:c77812997c9c 219 */
HBP 0:cbf45dde2b49 220 class file {
HBP 0:cbf45dde2b49 221 private:
HBP 0:cbf45dde2b49 222 mfs *fs; // Reference to the file system in use
HBP 12:928346513c87 223 FileOpenMode fMode;
HBP 0:cbf45dde2b49 224 char buffer[BUF]; // Write buffer
HBP 9:52c01cb100ac 225 uint32_t bufPos; // "Cursor" position in buffer
HBP 12:928346513c87 226 uint32_t firstBlock; // First block of the file
HBP 12:928346513c87 227 uint32_t currBlock; // Current block in use
HBP 9:52c01cb100ac 228 uint32_t blockPos; // "head" position on the current block
HBP 12:928346513c87 229 uint32_t byteCount; // Stores current "cursor" position in file for seek
HBP 12:928346513c87 230 // Private functions
HBP 12:928346513c87 231 char getBlockLink(BlockLinkType linkSelection, uint32_t *blockOut);
HBP 12:928346513c87 232 char removeFollowingBlocks(uint32_t block); // Offers destructive write/very simple wear levelling
HBP 0:cbf45dde2b49 233 public:
HBP 10:211cb54339a0 234 /** Create file handle
HBP 3:1cbc15648de1 235 *
HBP 10:211cb54339a0 236 * \warning File must be created before it can be opened!
HBP 10:211cb54339a0 237 * Opening non-existing file will trip the system to error();
HBP 10:211cb54339a0 238 * If read only file is opened in rw mode system will trip to error().
HBP 3:1cbc15648de1 239 *
HBP 12:928346513c87 240 * \b AWRITE is a file access mode where cursor can be moved along the file
HBP 12:928346513c87 241 * and write can be started at any point. write() function will overwrite
HBP 12:928346513c87 242 * only as many bytes as you chosen to write.
HBP 12:928346513c87 243 *
HBP 12:928346513c87 244 * \b DWRITE is a file access mode similiar to AWRITE but when you start
HBP 12:928346513c87 245 * writing all the data after cursor will be removed permanently and flush()
HBP 12:928346513c87 246 * will set a new EOF marker.
HBP 12:928346513c87 247 *
HBP 3:1cbc15648de1 248 * @param filename[20] Filename input.
HBP 12:928346513c87 249 * @param operation RO = Read only, AWRITE = read and write, DWRITE = read + destructive write.
HBP 3:1cbc15648de1 250 */
HBP 12:928346513c87 251 file(mfs *fs_ref, char filename[20], FileOpenMode operation);
HBP 5:a0fe74dce80d 252
HBP 3:1cbc15648de1 253 /** Close file handle
HBP 3:1cbc15648de1 254 *
HBP 3:1cbc15648de1 255 * Flushes the file and closes the handle.
HBP 3:1cbc15648de1 256 */
HBP 0:cbf45dde2b49 257 ~file(); // Close file handle and flush
HBP 5:a0fe74dce80d 258
HBP 3:1cbc15648de1 259 /** Rewind to the start postion of the file
HBP 5:a0fe74dce80d 260 *
HBP 3:1cbc15648de1 261 */
HBP 3:1cbc15648de1 262 void rewind();
HBP 5:a0fe74dce80d 263
HBP 12:928346513c87 264 /** Reverse n bytes back
HBP 5:a0fe74dce80d 265 *
HBP 12:928346513c87 266 * @param n Number of bytes.
HBP 12:928346513c87 267 * @returns Error code: 0 = OK, 1 = First byte of file.
HBP 5:a0fe74dce80d 268 */
HBP 9:52c01cb100ac 269 char rewind(uint32_t n);
HBP 5:a0fe74dce80d 270
HBP 5:a0fe74dce80d 271 /** Forward one byte
HBP 5:a0fe74dce80d 272 *
HBP 10:211cb54339a0 273 * @returns Error code: 0 = OK, 1 = End of file.
HBP 3:1cbc15648de1 274 */
HBP 3:1cbc15648de1 275 char forward();
HBP 5:a0fe74dce80d 276
HBP 5:a0fe74dce80d 277 /** Forward n bytes
HBP 5:a0fe74dce80d 278 *
HBP 5:a0fe74dce80d 279 * @param n Number of blocks.
HBP 10:211cb54339a0 280 * @returns Error code: 0 = OK, 1 = End of file.
HBP 5:a0fe74dce80d 281 */
HBP 9:52c01cb100ac 282 char forward(uint32_t n);
HBP 5:a0fe74dce80d 283
HBP 12:928346513c87 284 /** Seek to byte given
HBP 12:928346513c87 285 *
HBP 12:928346513c87 286 * @param byte Byte number where to seek.
HBP 12:928346513c87 287 * @returns Error code: 0 = OK, 1 = End of file or already at first byte.
HBP 12:928346513c87 288 */
HBP 12:928346513c87 289 char seek(uint32_t byte);
HBP 12:928346513c87 290
HBP 3:1cbc15648de1 291 /** Reads a string of bytes
HBP 3:1cbc15648de1 292 *
HBP 3:1cbc15648de1 293 * Always places '\0' at the end of string.
HBP 3:1cbc15648de1 294 *
HBP 3:1cbc15648de1 295 * @param data Output buffer.
HBP 3:1cbc15648de1 296 * @param n Number of bytes to be read.
HBP 5:a0fe74dce80d 297 * @returns Error code. 0 = OK, 1 = Last block of the file
HBP 3:1cbc15648de1 298 */
HBP 9:52c01cb100ac 299 void read(char *data, uint32_t n);
HBP 12:928346513c87 300
HBP 3:1cbc15648de1 301 /** Reads a binary array of bytes
HBP 3:1cbc15648de1 302 *
HBP 3:1cbc15648de1 303 * Doesn't add '\0' at the end of data array and doesn't respect mEOF byte.
HBP 3:1cbc15648de1 304 *
HBP 3:1cbc15648de1 305 * @param data Output buffer.
HBP 3:1cbc15648de1 306 * @param n Number of bytes to be read.
HBP 12:928346513c87 307 */
HBP 12:928346513c87 308 void readBin(char *data, uint32_t n);
HBP 5:a0fe74dce80d 309
HBP 3:1cbc15648de1 310 /** Write byte array to a file (buffer)
HBP 3:1cbc15648de1 311 *
HBP 3:1cbc15648de1 312 * @param data Input data.
HBP 3:1cbc15648de1 313 * @param n Number of bytes to be read.
HBP 10:211cb54339a0 314 * @returns Error code: 0 = OK, 1 = Flush failed.
HBP 12:928346513c87 315 */
HBP 9:52c01cb100ac 316 char write(char *data, uint32_t n);
HBP 5:a0fe74dce80d 317
HBP 3:1cbc15648de1 318 /** Flush file buffer
HBP 3:1cbc15648de1 319 * Writes buffer to the EEPROM chip in use.
HBP 12:928346513c87 320 *
HBP 12:928346513c87 321 * @returns Error code: 0 = OK, 1 = Out of free space, 2 = Destructive operation failed (fs is corrupted).
HBP 3:1cbc15648de1 322 */
HBP 0:cbf45dde2b49 323 char flush();
HBP 0:cbf45dde2b49 324 };
HBP 0:cbf45dde2b49 325
HBP 0:cbf45dde2b49 326 #endif