
在此示例中,在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