Mbed OS Reference
Loading...
Searching...
No Matches
FileSystemStore.h
1/* mbed Microcontroller Library
2 * Copyright (c) 2018 ARM Limited
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18#ifndef MBED_FILE_SYSTEM_STORE_H
19#define MBED_FILE_SYSTEM_STORE_H
20
21#include "kvstore/KVStore.h"
22#include "filesystem/FileSystem.h"
23
24namespace mbed {
25
26/**
27 * \addtogroup kvstore
28 * @{
29 */
30
31/** FileSystemStore is a lightweight implementation of the KVStore interface over file systems.
32 *
33 * FileSystemStore implements the get/set interface using files, where a single file represents each key.
34 * A key is represented by the file name, and its value is stored as file data. Therefore, FileSystemStore
35 * imitates the get/set actions using simple file operations. Set is achieved using open-write-close, get
36 * using open-read-close and so on.
37 *
38 * All files are concentrated under a single directory, whose name is hard coded. So actions such as "reset"
39 * are mapped to the deletion of all files under this directory, and iteration actions use file system APIs to
40 * traverse the directory.
41 */
42class FileSystemStore : public KVStore {
43
44public:
45 /** Create FileSystemStore - A Key Value API on top of FS
46 *
47 * @param fs File system (FAT/LITTLE) on top of which FileSystemStore is adding KV API
48 */
50
51 /** Destroy FileSystemStore instance
52 *
53 */
54 virtual ~FileSystemStore() {}
55
56 /**
57 * @brief Initialize FileSystemStore, checking validity of
58 * KVStore writing folder and if it doesn't exist, creating it.
59 *
60 * @returns MBED_SUCCESS Success.
61 * MBED_ERROR_FAILED_OPERATION Underlying file system failed operation.
62 */
63 virtual int init();
64
65 /**
66 * @brief Deinitialize FileSystemStore, release and free resources.
67 *
68 * @returns MBED_SUCCESS Success.
69 */
70 virtual int deinit();
71
72 /**
73 * @brief Reset FileSystemStore contents (clear all keys)
74 *
75 * @returns MBED_SUCCESS Success.
76 * MBED_ERROR_NOT_READY Not initialized.
77 * MBED_ERROR_FAILED_OPERATION Underlying file system failed operation.
78 */
79 virtual int reset();
80
81 /**
82 * @brief Set one FileSystemStore item, given key and value.
83 *
84 * @param[in] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
85 * @param[in] buffer Value data buffer.
86 * @param[in] size Value data size.
87 * @param[in] create_flags Flag mask.
88 *
89 * @returns MBED_SUCCESS Success.
90 * MBED_ERROR_NOT_READY Not initialized.
91 * MBED_ERROR_FAILED_OPERATION Underlying file system failed operation.
92 * MBED_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
93 * MBED_ERROR_INVALID_SIZE Invalid size given in function arguments.
94 * MBED_ERROR_WRITE_PROTECTED Already stored with "write once" flag.
95 */
96 virtual int set(const char *key, const void *buffer, size_t size, uint32_t create_flags);
97
98 /**
99 * @brief Get one FileSystemStore item by given key.
100 *
101 * @param[in] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
102 * @param[in] buffer Value data buffer.
103 * @param[in] buffer_size Value data buffer size.
104 * @param[out] actual_size Actual read size.
105 * @param[in] offset Offset to read from in data.
106 *
107 * @returns MBED_SUCCESS Success.
108 * MBED_ERROR_NOT_READY Not initialized.
109 * MBED_ERROR_FAILED_OPERATION Underlying file system failed operation.
110 * MBED_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
111 * MBED_ERROR_INVALID_SIZE Invalid size given in function arguments.
112 * MBED_ERROR_INVALID_DATA_DETECTED Data is corrupted.
113 * MBED_ERROR_ITEM_NOT_FOUND No such key.
114 */
115 virtual int get(const char *key, void *buffer, size_t buffer_size, size_t *actual_size = NULL, size_t offset = 0);
116
117 /**
118 * @brief Get information of a given key. The returned info contains size and flags
119 *
120 * @param[in] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
121 * @param[out] info Returned information structure.
122 *
123 * @returns MBED_SUCCESS Success.
124 * MBED_ERROR_NOT_READY Not initialized.
125 * MBED_ERROR_FAILED_OPERATION Underlying file system failed operation.
126 * MBED_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
127 * MBED_ERROR_INVALID_SIZE Invalid size given in function arguments.
128 * MBED_ERROR_INVALID_DATA_DETECTED Data is corrupted.
129 * MBED_ERROR_ITEM_NOT_FOUND No such key.
130 */
131 virtual int get_info(const char *key, info_t *info);
132
133 /**
134 * @brief Remove a FileSystemStore item by given key.
135 *
136 * @param[in] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
137 *
138 * @returns MBED_SUCCESS Success.
139 * MBED_ERROR_NOT_READY Not initialized.
140 * MBED_ERROR_FAILED_OPERATION Underlying file system failed operation.
141 * MBED_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
142 * MBED_ERROR_ITEM_NOT_FOUND No such key.
143 * MBED_ERROR_WRITE_PROTECTED Already stored with "write once" flag.
144 */
145 virtual int remove(const char *key);
146
147 /**
148 * @brief Start an incremental FileSystemStore set sequence. This operation is blocking other operations.
149 * Any get/set/remove/iterator operation will be blocked until set_finalize is called.
150 *
151 * @param[out] handle Returned incremental set handle.
152 * @param[in] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
153 * @param[in] final_data_size Final value data size.
154 * @param[in] create_flags Flag mask.
155 *
156 * @returns MBED_SUCCESS Success.
157 * MBED_ERROR_NOT_READY Not initialized.
158 * MBED_ERROR_FAILED_OPERATION Underlying file system failed operation.
159 * MBED_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
160 * MBED_ERROR_INVALID_SIZE Invalid size given in function arguments.
161 * MBED_ERROR_WRITE_PROTECTED Already stored with "write once" flag.
162 */
163 virtual int set_start(set_handle_t *handle, const char *key, size_t final_data_size, uint32_t create_flags);
164
165 /**
166 * @brief Add data to incremental FileSystemStore set sequence. This operation is blocking other operations.
167 * Any get/set/remove operation will be blocked until set_finalize is called.
168 *
169 * @param[in] handle Incremental set handle.
170 * @param[in] value_data Value data to add.
171 * @param[in] data_size Value data size.
172 *
173 * @returns MBED_SUCCESS Success.
174 * MBED_ERROR_NOT_READY Not initialized.
175 * MBED_ERROR_FAILED_OPERATION Underlying file system failed operation.
176 * MBED_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
177 * MBED_ERROR_INVALID_SIZE Invalid size given in function arguments.
178 * MBED_ERROR_WRITE_PROTECTED Already stored with "write once" flag.
179 */
180 virtual int set_add_data(set_handle_t handle, const void *value_data, size_t data_size);
181
182 /**
183 * @brief Finalize an incremental FileSystemStore set sequence.
184 *
185 * @param[in] handle Incremental set handle.
186 *
187 * @returns MBED_SUCCESS Success.
188 * MBED_ERROR_NOT_READY Not initialized.
189 * MBED_ERROR_FAILED_OPERATION Underlying file system failed operation.
190 * MBED_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
191 */
192 virtual int set_finalize(set_handle_t handle);
193
194 /**
195 * @brief Start an iteration over FileSystemStore keys.
196 * There are no issues with any other operations while iterator is open.
197 *
198 * @param[out] it Returned iterator handle.
199 * @param[in] prefix Key prefix (null for all keys).
200 *
201 * @returns MBED_SUCCESS Success.
202 * MBED_ERROR_NOT_READY Not initialized.
203 * MBED_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
204 */
205 virtual int iterator_open(iterator_t *it, const char *prefix = NULL);
206
207 /**
208 * @brief Get next key in iteration.
209 * There are no issues with any other operations while iterator is open.
210 *
211 * @param[in] it Iterator handle.
212 * @param[in] key Buffer for returned key.
213 * @param[in] key_size Key buffer size.
214 *
215 * @returns MBED_SUCCESS Success.
216 * MBED_ERROR_NOT_READY Not initialized.
217 * MBED_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
218 * MBED_ERROR_ITEM_NOT_FOUND No more keys found.
219 */
220 virtual int iterator_next(iterator_t it, char *key, size_t key_size);
221
222 /**
223 * @brief Close iteration.
224 *
225 * @param[in] it Iterator handle.
226 *
227 * @returns MBED_SUCCESS Success.
228 * MBED_ERROR_NOT_READY Not initialized.
229 * MBED_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
230 */
231 virtual int iterator_close(iterator_t it);
232
233#if !defined(DOXYGEN_ONLY)
234private:
235
236 // Key metadata
237 typedef struct {
238 uint32_t magic;
239 uint16_t metadata_size;
240 uint16_t revision;
241 uint32_t user_flags;
242 } key_metadata_t;
243
244 /**
245 * @brief Build Full name class member from Key, as a combination of FSST folder and key name
246 *
247 * @param[in] key_src key file name
248 *
249 * @returns 0 on success or a negative error code on failure
250 */
251 int _build_full_path_key(const char *key_src);
252
253 /**
254 * @brief Verify Key file metadata validity and open it if valid
255 *
256 * @param[in] key In validated key file name.
257 * @param[in] key_metadata Returned key file metadata.
258 * @param[in] kv_file Opened KV file handle (unless file doesn't exist)
259 *
260 * @returns 0 on success or a negative error code on failure
261 */
262 int _verify_key_file(const char *key, key_metadata_t *key_metadata, File *kv_file);
263
264 FileSystem *_fs;
265 rtos::Mutex _mutex;
266 rtos::Mutex _inc_data_add_mutex;
267
268 bool _is_initialized;
269 char *_cfg_fs_path; /* FileSystemStore path name on FileSystem */
270 size_t _cfg_fs_path_size; /* Size of configured FileSystemStore path name on FileSystem */
271 char *_full_path_key; /* Full name of Key file currently working on */
272 size_t _cur_inc_data_size; /* Amount of data added to Key file so far, during incremental add data */
273 set_handle_t _cur_inc_set_handle; /* handle of currently key file under incremental set process */
274#endif
275};
276
277/// @}
278
279} //namespace mbed
280#endif //MBED_FILE_SYSTEM_STORE_H
File class.
Definition: File.h:31
A file system object.
Definition: FileSystem.h:50
FileSystemStore is a lightweight implementation of the KVStore interface over file systems.
virtual int get_info(const char *key, info_t *info)
Get information of a given key.
virtual int iterator_open(iterator_t *it, const char *prefix=NULL)
Start an iteration over FileSystemStore keys.
virtual int remove(const char *key)
Remove a FileSystemStore item by given key.
virtual int iterator_next(iterator_t it, char *key, size_t key_size)
Get next key in iteration.
virtual int set_finalize(set_handle_t handle)
Finalize an incremental FileSystemStore set sequence.
virtual int set(const char *key, const void *buffer, size_t size, uint32_t create_flags)
Set one FileSystemStore item, given key and value.
virtual ~FileSystemStore()
Destroy FileSystemStore instance.
virtual int iterator_close(iterator_t it)
Close iteration.
FileSystemStore(FileSystem *fs)
Create FileSystemStore - A Key Value API on top of FS.
virtual int set_start(set_handle_t *handle, const char *key, size_t final_data_size, uint32_t create_flags)
Start an incremental FileSystemStore set sequence.
virtual int deinit()
Deinitialize FileSystemStore, release and free resources.
virtual int reset()
Reset FileSystemStore contents (clear all keys)
virtual int get(const char *key, void *buffer, size_t buffer_size, size_t *actual_size=NULL, size_t offset=0)
Get one FileSystemStore item by given key.
virtual int set_add_data(set_handle_t handle, const void *value_data, size_t data_size)
Add data to incremental FileSystemStore set sequence.
virtual int init()
Initialize FileSystemStore, checking validity of KVStore writing folder and if it doesn't exist,...
KVStore class.
Definition: KVStore.h:38
The Mutex class is used to synchronize the execution of threads.
Definition: Mutex.h:70
Holds key information.
Definition: KVStore.h:56