
在此示例中,在root/config中创建了一个称为网络的文件。如果不存在配置文件,则盆景将创建它并将网络放置在其中。默认情况下,当Bonsai类实例化时,将创建根目录。
Bonsai bonsai;
bonsai.create_file( " root/config/networking " );文件old_data及其内容被删除。
Bonsai bonsai;
bonsai.delete_file( " root/config/old_data " );有关更多信息,请参见完整的API。
贡献是最欢迎的。
查看如何贡献。
一旦编写,文件就在闪存中的行边界处连续存在。文件可能跨越多行,只要它遵守其组成字段的大小限制,并且它适合盆景闪光灯分区。它的前四个字段的尺寸固定并构成文件标头。后续字段的大小 - 因此,该文件的整体是从标题中得出的。最后三个字段的大小各不相同,构成了文件结构的正文。值得注意的是,文件不会存储自己的地址,因为文件的地址是其第一个成员的地址。
| 手柄尺寸 | 数据大小 | 父地址 | 儿童地址# | 处理 | 数据 | 儿童地址 |
|---|---|---|---|---|---|---|
| 1字节 | 2个字节 | 4个字节 | 1字节 | 多变的 | 多变的 | 多变的 |
| 标题 | 身体 | |||||
因此,上表在固件中表示为file_t结构。
typedef struct {
uint8_t handle_size; // the size of handle in bytes
uint16_t data_size; // the size of data in bytes
uint32_t parent_addr; // the address of this file's parent file
uint8_t num_child_addrs; // the number of children immediately descendant from this file
uint8_t *handle; // the handle or name of this file
uint8_t *data; // the actual data this file contains
uint32_t *child_addrs; // children addresses
/* ... */
} file_t ;任何文件的大小都可以用file_t表示,因此:
handle_size + sizeof (handle_size) + // handle_size bytes plus one more for storing the size of handle_size
data_size + sizeof (data_size) + // same for data
sizeof (parent_addr) + // four bytes for the parent address
sizeof (num_child_addrs) + // one byte for storing the number of child addresses
(num_child_addrs * sizeof ( uint32_t )); // four bytes for every child address 初始化后,盆景将在其闪存分区开始时寻找系统文件。如果不存在,盆景将创建一个以及一个根文件。毫无疑问,根文件是所有用户创建文件的父母。它的句柄是root ,它是初始化的,没有任何数据。系统文件是一个特殊文件,该文件驻留在根文件地址之前。它既没有父母也没有孩子地址,也用于一般内存管理。至关重要的是,系统文件存储了可用空间地址(FSA)本身来确定要编写的下一个文件的位置(同样,系统文件将FSA存储为数据,而不是作为子女地址。FSA不是文件,并且像对待它一样将其视为没有意义)。该机制的起作用使得在书面文件结束后立即将FSA递增到行地址时,该机制的起作用是如此。相反,当文件被删除时,FSA将减少到删除文件所在的行地址。因此,内存片段的最大大小为ROW_SIZE - 1或255字节。
Bonsai类实现了一组基本文件读取和写作的方法。这些方法与文件路径遍历无关。相反,它们与地址直接互动,通常由顶级API调用。他们是:
void put ( file_t &file); // writes a file to flash memory
file_t get ( const uint32_t address); // retrieves a file from memory
void del ( const uint32_t address); // erases all rows containing a file
void mov ( const uint32_t dest, const uint32_t src); // moves a file from src to dest