Loading...
Searching...
No Matches
storage.h
Go to the documentation of this file.
1
5#pragma once
6
7#include <stdint.h>
8#include "filesystem_api_defines.h"
9#include "storage_sd_api.h"
10
11#ifdef __cplusplus
12extern "C" {
13#endif
14
15#define STORAGE_INT_PATH_PREFIX "/int"
16#define STORAGE_EXT_PATH_PREFIX "/ext"
17#define STORAGE_ANY_PATH_PREFIX "/any"
18#define STORAGE_APP_DATA_PATH_PREFIX "/data"
19#define STORAGE_APP_ASSETS_PATH_PREFIX "/assets"
20
21#define INT_PATH(path) STORAGE_INT_PATH_PREFIX "/" path
22#define EXT_PATH(path) STORAGE_EXT_PATH_PREFIX "/" path
23#define ANY_PATH(path) STORAGE_ANY_PATH_PREFIX "/" path
24#define APP_DATA_PATH(path) STORAGE_APP_DATA_PATH_PREFIX "/" path
25#define APP_ASSETS_PATH(path) STORAGE_APP_ASSETS_PATH_PREFIX "/" path
26
27#define RECORD_STORAGE "storage"
28
29typedef struct Storage Storage;
30
38
45void storage_file_free(File* file);
46
57
61typedef struct {
64
74
75/******************* File Functions *******************/
76
89 File* file,
90 const char* path,
91 FS_AccessMode access_mode,
92 FS_OpenMode open_mode);
93
100bool storage_file_close(File* file);
101
108bool storage_file_is_open(File* file);
109
116bool storage_file_is_dir(File* file);
117
126size_t storage_file_read(File* file, void* buff, size_t bytes_to_read);
127
136size_t storage_file_write(File* file, const void* buff, size_t bytes_to_write);
137
146bool storage_file_seek(File* file, uint32_t offset, bool from_start);
147
154uint64_t storage_file_tell(File* file);
155
162bool storage_file_truncate(File* file);
163
170uint64_t storage_file_size(File* file);
171
178bool storage_file_sync(File* file);
179
186bool storage_file_eof(File* file);
187
195bool storage_file_exists(Storage* storage, const char* path);
196
210bool storage_file_copy_to_file(File* source, File* destination, size_t size);
211
212/******************* Directory Functions *******************/
213
225bool storage_dir_open(File* file, const char* path);
226
233bool storage_dir_close(File* file);
234
247bool storage_dir_read(File* file, FileInfo* fileinfo, char* name, uint16_t name_length);
248
255bool storage_dir_rewind(File* file);
256
264bool storage_dir_exists(Storage* storage, const char* path);
265
266/******************* Common Functions *******************/
267
276FS_Error storage_common_timestamp(Storage* storage, const char* path, uint32_t* timestamp);
277
286FS_Error storage_common_stat(Storage* storage, const char* path, FileInfo* fileinfo);
287
298FS_Error storage_common_remove(Storage* storage, const char* path);
299
314FS_Error storage_common_rename(Storage* storage, const char* old_path, const char* new_path);
315
326FS_Error storage_common_copy(Storage* storage, const char* old_path, const char* new_path);
327
336FS_Error storage_common_merge(Storage* storage, const char* old_path, const char* new_path);
337
345FS_Error storage_common_mkdir(Storage* storage, const char* path);
346
357 Storage* storage,
358 const char* fs_path,
359 uint64_t* total_space,
360 uint64_t* free_space);
361
371
382FS_Error storage_common_migrate(Storage* storage, const char* source, const char* dest);
383
391bool storage_common_exists(Storage* storage, const char* path);
392
409bool storage_common_equivalent_path(Storage* storage, const char* path1, const char* path2);
410
423bool storage_common_is_subdir(Storage* storage, const char* parent, const char* child);
424
425/******************* Error Functions *******************/
426
433const char* storage_error_get_desc(FS_Error error_id);
434
443FS_Error storage_file_get_error(File* file);
444
454
463const char* storage_file_get_error_desc(File* file);
464
465/******************* SD Card Functions *******************/
466
473FS_Error storage_sd_format(Storage* storage);
474
485FS_Error storage_sd_unmount(Storage* storage);
486
493FS_Error storage_sd_mount(Storage* storage);
494
502FS_Error storage_sd_info(Storage* storage, SDInfo* info);
503
510FS_Error storage_sd_status(Storage* storage);
511
512/************ Internal Storage Backup/Restore ************/
513
514typedef void (*StorageNameConverter)(FuriString*);
515
523FS_Error storage_int_backup(Storage* storage, const char* dstname);
524
533FS_Error
534 storage_int_restore(Storage* storage, const char* dstname, StorageNameConverter converter);
535
536/***************** Simplified Functions ******************/
537
549bool storage_simply_remove(Storage* storage, const char* path);
550
560bool storage_simply_remove_recursive(Storage* storage, const char* path);
561
569bool storage_simply_mkdir(Storage* storage, const char* path);
570
606 Storage* storage,
607 const char* dirname,
608 const char* filename,
609 const char* fileextension,
610 FuriString* nextfilename,
611 uint8_t max_len);
612
613#ifdef __cplusplus
614}
615#endif
StorageEventType
Enumeration of events emitted by the storage through the PubSub system.
Definition storage.h:50
@ StorageEventTypeCardUnmount
SD card was unmounted.
Definition storage.h:52
@ StorageEventTypeDirClose
A directory was closed.
Definition storage.h:55
@ StorageEventTypeFileClose
A file was closed.
Definition storage.h:54
@ StorageEventTypeCardMountError
An error occurred during mounting of an SD card.
Definition storage.h:53
@ StorageEventTypeCardMount
SD card was mounted.
Definition storage.h:51
FS_Error storage_int_backup(Storage *storage, const char *dstname)
Back up the internal storage contents to a *.tar archive.
Definition storage_internal_api.c:5
bool storage_simply_remove(Storage *storage, const char *path)
Remove a file or a directory.
Definition storage_external_api.c:1029
bool storage_file_eof(File *file)
Check whether the current access position is at the end of the file.
Definition storage_external_api.c:274
bool storage_file_seek(File *file, uint32_t offset, bool from_start)
Change the current access position in a file.
Definition storage_external_api.c:222
FS_Error storage_common_remove(Storage *storage, const char *path)
Remove a file or a directory.
Definition storage_external_api.c:463
FS_Error storage_sd_status(Storage *storage)
Get SD card status.
Definition storage_external_api.c:916
bool storage_dir_rewind(File *file)
Change the access position to first item in the directory.
Definition storage_external_api.c:407
size_t storage_file_write(File *file, const void *buff, size_t bytes_to_write)
Write bytes from a buffer to a file.
Definition storage_external_api.c:203
FS_Error storage_sd_mount(Storage *storage)
Mount the SD card.
Definition storage_external_api.c:893
bool storage_simply_remove_recursive(Storage *storage, const char *path)
Recursively remove a file or a directory.
Definition storage_external_api.c:968
int32_t storage_file_get_internal_error(File *file)
Get the internal (storage-specific) numeric error identifier from a file instance.
Definition storage_external_api.c:861
FS_Error storage_common_copy(Storage *storage, const char *old_path, const char *new_path)
Copy the file to a new location.
Definition storage_external_api.c:579
void storage_file_free(File *file)
Free the file instance.
Definition storage_external_api.c:948
bool storage_file_close(File *file)
Close the file.
Definition storage_external_api.c:127
void storage_common_resolve_path_and_ensure_app_directory(Storage *storage, FuriString *path)
Parse aliases in a path and replace them with the real path.
Definition storage_external_api.c:781
FS_Error storage_common_stat(Storage *storage, const char *path, FileInfo *fileinfo)
Get information about a file or a directory.
Definition storage_external_api.c:447
FS_Error storage_sd_format(Storage *storage)
Format the SD Card.
Definition storage_external_api.c:873
bool storage_file_copy_to_file(File *source, File *destination, size_t size)
Copy data from a source file to the destination file.
Definition storage_external_api.c:297
FS_Error storage_common_fs_info(Storage *storage, const char *fs_path, uint64_t *total_space, uint64_t *free_space)
Get the general information about the storage.
Definition storage_external_api.c:759
const char * storage_error_get_desc(FS_Error error_id)
Get the textual description of a numeric error identifer.
Definition storage_external_api.c:852
bool storage_dir_close(File *file)
Close the directory.
Definition storage_external_api.c:372
bool storage_dir_exists(Storage *storage, const char *path)
Check whether a directory exists.
Definition storage_external_api.c:416
bool storage_file_open(File *file, const char *path, FS_AccessMode access_mode, FS_OpenMode open_mode)
Open an existing file or create a new one.
Definition storage_external_api.c:91
FS_Error storage_sd_unmount(Storage *storage)
Unmount the SD card.
Definition storage_external_api.c:883
bool storage_common_equivalent_path(Storage *storage, const char *path1, const char *path2)
Check whether two paths are equivalent.
Definition storage_external_api.c:842
FS_Error storage_file_get_error(File *file)
Get the numeric error identifier from a file instance.
Definition storage_external_api.c:856
bool storage_file_exists(Storage *storage, const char *path)
Check whether a file exists.
Definition storage_external_api.c:283
bool storage_dir_read(File *file, FileInfo *fileinfo, char *name, uint16_t name_length)
Get the next item in the directory.
Definition storage_external_api.c:390
bool storage_common_exists(Storage *storage, const char *path)
Check whether a file or a directory exists.
Definition storage_external_api.c:812
bool storage_file_truncate(File *file)
Truncate the file size to the current access position.
Definition storage_external_api.c:247
FS_Error storage_sd_info(Storage *storage, SDInfo *info)
Get SD card information.
Definition storage_external_api.c:903
bool storage_file_sync(File *file)
Synchronise the file cache with the actual storage.
Definition storage_external_api.c:265
FS_Error storage_common_merge(Storage *storage, const char *old_path, const char *new_path)
Copy the contents of one directory into another and rename all conflicting files.
Definition storage_external_api.c:674
bool storage_simply_mkdir(Storage *storage, const char *path)
Create a directory.
Definition storage_external_api.c:1037
bool storage_dir_open(File *file, const char *path)
Open a directory.
Definition storage_external_api.c:340
FS_Error storage_common_rename(Storage *storage, const char *old_path, const char *new_path)
Rename a file or a directory.
Definition storage_external_api.c:478
FS_Error storage_int_restore(Storage *storage, const char *dstname, StorageNameConverter converter)
Restore the internal storage contents from a *.tar archive.
Definition storage_internal_api.c:17
FS_Error storage_common_timestamp(Storage *storage, const char *path, uint32_t *timestamp)
Get the last access time in UNIX format.
Definition storage_external_api.c:431
FS_Error storage_common_mkdir(Storage *storage, const char *path)
Create a directory.
Definition storage_external_api.c:744
const char * storage_file_get_error_desc(File *file)
Get the textual description of a the last error associated with a file instance.
Definition storage_external_api.c:866
File * storage_file_alloc(Storage *storage)
Allocate and initialize a file instance.
Definition storage_external_api.c:926
bool storage_common_is_subdir(Storage *storage, const char *parent, const char *child)
Check whether a path is a subpath of another path.
Definition storage_external_api.c:846
size_t storage_file_read(File *file, void *buff, size_t bytes_to_read)
Read bytes from a file into a buffer.
Definition storage_external_api.c:186
void storage_get_next_filename(Storage *storage, const char *dirname, const char *filename, const char *fileextension, FuriString *nextfilename, uint8_t max_len)
Get the next free filename in a directory.
Definition storage_external_api.c:1045
uint64_t storage_file_size(File *file)
Get the file size.
Definition storage_external_api.c:256
bool storage_file_is_open(File *file)
Check whether the file is open.
Definition storage_external_api.c:938
FuriPubSub * storage_get_pubsub(Storage *storage)
Get the storage pubsub instance.
Definition storage_external_api.c:963
FS_Error storage_common_migrate(Storage *storage, const char *source, const char *dest)
Move the contents of source folder to destination one and rename all conflicting files.
Definition storage_external_api.c:796
uint64_t storage_file_tell(File *file)
Get the current access position.
Definition storage_external_api.c:238
bool storage_file_is_dir(File *file)
Check whether a file instance represents a directory.
Definition storage_external_api.c:943
Structure that hold file index and returned api errors.
Definition filesystem_api_internal.h:17
Structure that hold file info.
Definition filesystem_api_defines.h:48
Definition pubsub.c:14
Definition string.c:4
Definition storage_sd_api.h:19
Storage event (passed to the PubSub callback).
Definition storage.h:61
StorageEventType type
Type of the event.
Definition storage.h:62
Definition storage_i.h:23