Guest User

FlatFS

a guest
Jan 25th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.02 KB | None | 0 0
  1. #ifndef _FLATFS_H_
  2. #define _FLATFS_H_
  3.  
  4. #include <stdint.h>
  5. #include <stdbool.h>
  6.  
  7.  
  8. /**
  9.  * A file entry in the flat file system.
  10.  */
  11. struct ffsfile
  12. {
  13.     /**
  14.      * File size in blocks
  15.      */
  16.     uint32_t size;
  17.    
  18.     /**
  19.      * The name of the file.
  20.      */
  21.     int8_t name[8];
  22.    
  23.     /**
  24.      * The file extension
  25.      */
  26.     int8_t extension[2];
  27.    
  28.     /**
  29.      * A set of generic flags that can be used for this file.
  30.      */
  31.     uint16_t flags;
  32. };
  33.  
  34. /**
  35.  * The definition of a flat file system. Contains all access methods
  36.  * and meta-information to access files.
  37.  */
  38. struct ffs
  39. {
  40.     /**
  41.      * The block size of this file system.
  42.      */
  43.     uint32_t blockSize;
  44.    
  45.     /**
  46.      * A function that allows the ffs driver to read a block of memory.
  47.      * @param ffs      The file system structure that wants to access the read operation.
  48.      * @param blockNum The block ffs wants to read.
  49.      * @param buffer   A pointer to target memory where the block should be stored. Must be at least @ref blockSize large.
  50.      * @return         true on success, otherwise false.
  51.      */
  52.     bool (*readBlock)(struct ffs * ffs, uint32_t blockNum, void * buffer);
  53.    
  54.     /**
  55.      * A function that allows the ffs driver to write a block of memory.
  56.      * @param ffs      The file system structure that wants to access the writes operation.
  57.      * @param blockNum The block ffs wants to write.
  58.      * @param buffer   A pointer to target memory where the block should read from. Must be at least @ref blockSize large.
  59.      * @return         true on success, otherwise false.
  60.      */
  61.     bool (*writeBlock)(struct ffs * ffs, uint32_t blockNum, void const * buffer);
  62. };
  63.  
  64. /**
  65.  * This function will return the n-th file in the file system, including empty
  66.  * files.
  67.  * @param ffs    The file system that should be enumerated.
  68.  * @param index  The zero-based index of the file that should be enumerated.
  69.  * @param result If the file was found, this pointer will received the file information.
  70.  * @returns      true on success, otherwise false.
  71.  */
  72. bool ffs_enumerate(struct ffs * ffs, uint32_t index, struct ffsfile * result);
  73.  
  74. /**
  75.  * This function will search for a file with the given name and extension.
  76.  * @param ffs  The file system that should be searched.
  77.  * @param name An either zero-terminated or at least 8 byte long string that will contain the file name.
  78.  * @param ext  An either zero-terminated or at least 2 byte long string that will contain the file extension.
  79.  * @returns    Zero-based index of the file or -1 of the file was not found.
  80.  */
  81. int ffs_find(struct ffs * ffs, char const * name, char const * ext);
  82.  
  83. /**
  84.  * This function will store a file on the given storage.
  85.  * @param ffs    The file system that should store the file.
  86.  * @param name   An either zero-terminated or at least 8 byte long string that will contain the file name.
  87.  * @param ext    An either zero-terminated or at least 2 byte long string that will contain the file extension.
  88.  * @param data   A pointer to the file data that should be stored.
  89.  * @param length The length of the file in bytes.
  90.  * @returns      true on success, otherwise false.
  91.  */
  92. bool ffs_store(
  93.     struct ffs * ffs,
  94.     char const * name,
  95.     char const * ext,
  96.     void const * data,
  97.     uint32_t length);
  98.  
  99. /**
  100.  * This function will load a file from the given storage.
  101.  * @param ffs  The file system that should load the file.
  102.  * @param name An either zero-terminated or at least 8 byte long string that will contain the file name.
  103.  * @param ext  An either zero-terminated or at least 2 byte long string that will contain the file extension.
  104.  * @param data A pointer to a buffer that will receive the loaded data. Should provide at least the file size of bytes.
  105.  * @returns true on success, otherwise false.
  106.  */
  107. bool ffs_load(
  108.     struct ffs * ffs,
  109.     char const * name,
  110.     char const * ext,
  111.           void * data);
  112.  
  113. /**
  114.  * This function will compact the file system and squish out all empty blocks at the begin of the file system.
  115.  * @param ffs The file system that should be squished
  116.  * @returns   true on success, otherwise false.
  117.  */
  118. bool ffs_compact(struct ffs * ffs);
  119.                
  120. #endif
Advertisement
Add Comment
Please, Sign In to add comment