BSB Developer Docs
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_ROOT_PREFIX "/"
16#define STORAGE_EXT_PATH_PREFIX "/ext"
17#define STORAGE_BACKUP_PATH_PREFIX "/bkp"
18#define STORAGE_APP_DATA_PATH_PREFIX "/data"
19#define STORAGE_APP_ASSETS_PATH_PREFIX "/assets"
20
21#define BACKUP_PATH(path) STORAGE_BACKUP_PATH_PREFIX "/" path
22#define EXT_PATH(path) STORAGE_EXT_PATH_PREFIX "/" path
23#define APP_DATA_PATH(path) STORAGE_APP_DATA_PATH_PREFIX "/" path
24#define APP_ASSETS_PATH(path) STORAGE_APP_ASSETS_PATH_PREFIX "/" path
25
26#define SHARED_ASSETS_PATH(path) EXT_PATH("apps_assets/shared/" path)
27#define SHARED_ANIM_PATH(path) SHARED_ASSETS_PATH("animations/" path)
28#define SHARED_IMG_PATH(path) SHARED_ASSETS_PATH("images/" path)
29#define SHARED_SOUND_PATH(path) SHARED_ASSETS_PATH("sounds/" path)
30
31#define RECORD_STORAGE "storage"
32
33typedef struct Storage Storage;
34
42
49void storage_file_free(File* file);
50
61
65typedef struct {
68
77FuriPubSub* storage_get_pubsub(Storage* storage);
78
79/******************* File Functions *******************/
80
93 File* file,
94 const char* path,
95 FS_AccessMode access_mode,
96 FS_OpenMode open_mode);
97
104bool storage_file_close(File* file);
105
112bool storage_file_is_open(File* file);
113
120bool storage_file_is_dir(File* file);
121
130size_t storage_file_read(File* file, void* buff, size_t bytes_to_read);
131
140size_t storage_file_write(File* file, const void* buff, size_t bytes_to_write);
141
150bool storage_file_seek(File* file, uint32_t offset, bool from_start);
151
158uint64_t storage_file_tell(File* file);
159
166bool storage_file_truncate(File* file);
167
174uint64_t storage_file_size(File* file);
175
182bool storage_file_sync(File* file);
183
190bool storage_file_eof(File* file);
191
199bool storage_file_exists(Storage* storage, const char* path);
200
214bool storage_file_copy_to_file(File* source, File* destination, size_t size);
215
216/******************* Directory Functions *******************/
217
229bool storage_dir_open(File* file, const char* path);
230
237bool storage_dir_close(File* file);
238
251bool storage_dir_read(File* file, FileInfo* fileinfo, char* name, uint16_t name_length);
252
259bool storage_dir_rewind(File* file);
260
268bool storage_dir_exists(Storage* storage, const char* path);
269
270/******************* Common Functions *******************/
271
280FS_Error storage_common_timestamp(Storage* storage, const char* path, uint32_t* timestamp);
281
290FS_Error storage_common_stat(Storage* storage, const char* path, FileInfo* fileinfo);
291
302FS_Error storage_common_remove(Storage* storage, const char* path);
303
318FS_Error storage_common_rename(Storage* storage, const char* old_path, const char* new_path);
319
330FS_Error storage_common_copy(Storage* storage, const char* old_path, const char* new_path);
331
340FS_Error storage_common_merge(Storage* storage, const char* old_path, const char* new_path);
341
349FS_Error storage_common_mkdir(Storage* storage, const char* path);
350
362 Storage* storage,
363 const char* fs_path,
364 uint64_t* total_space,
365 uint64_t* free_space,
366 bool* is_read_only);
367
377
388FS_Error storage_common_migrate(Storage* storage, const char* source, const char* dest);
389
397bool storage_common_exists(Storage* storage, const char* path);
398
415bool storage_common_equivalent_path(Storage* storage, const char* path1, const char* path2);
416
429bool storage_common_is_subdir(Storage* storage, const char* parent, const char* child);
430
438void storage_common_shutdown(Storage* storage);
439
447void storage_common_revive(Storage* storage);
448
449/******************* Error Functions *******************/
450
457const char* storage_error_get_desc(FS_Error error_id);
458
467FS_Error storage_file_get_error(File* file);
468
478
487const char* storage_file_get_error_desc(File* file);
488
489/******************* SD Card Functions *******************/
490
498FS_Error storage_sd_format(Storage* storage, const char* path);
499
510FS_Error storage_sd_make_filesystem(Storage* storage, const char* path);
511
523FS_Error storage_sd_unmount(Storage* storage, const char* path);
524
532FS_Error storage_sd_mount(Storage* storage, const char* path);
533
542FS_Error storage_sd_info(Storage* storage, const char* path, SDInfo* info);
543
551FS_Error storage_sd_status(Storage* storage, const char* path);
552
553/***************** Simplified Functions ******************/
554
566bool storage_simply_remove(Storage* storage, const char* path);
567
577bool storage_simply_remove_recursive(Storage* storage, const char* path);
578
586bool storage_simply_mkdir(Storage* storage, const char* path);
587
598bool storage_simply_mkpath(Storage* storage, const char* path);
599
610 Storage* storage,
611 const char* path,
612 void* buffer,
613 size_t buf_sz);
614
624 Storage* storage,
625 const char* path,
626 size_t size_limit);
627
638 Storage* storage,
639 const char* path,
640 const void* buffer,
641 size_t length);
642
652 Storage* storage,
653 const char* path,
654 FuriString* string);
655
691 Storage* storage,
692 const char* dirname,
693 const char* filename,
694 const char* fileextension,
695 FuriString* nextfilename,
696 uint8_t max_len);
697
698#ifdef __cplusplus
699}
700#endif
StorageEventType
Enumeration of events emitted by the storage through the PubSub system.
Definition storage.h:54
@ StorageEventTypeCardUnmount
SD card was unmounted.
Definition storage.h:56
@ StorageEventTypeDirClose
A directory was closed.
Definition storage.h:59
@ StorageEventTypeFileClose
A file was closed.
Definition storage.h:58
@ StorageEventTypeCardMountError
An error occurred during mounting of an SD card.
Definition storage.h:57
@ StorageEventTypeCardMount
SD card was mounted.
Definition storage.h:55
bool storage_simply_mkpath(Storage *storage, const char *path)
Create a directory and its full path.
Definition storage_external_api.c:1109
bool storage_simply_remove(Storage *storage, const char *path)
Remove a file or a directory.
Definition storage_external_api.c:1093
FuriString * storage_simply_read_entire_file_to_string(Storage *storage, const char *path, size_t size_limit)
Simply read the entire file into memory as a FuriString.
Definition storage_external_api.c:1171
void storage_common_shutdown(Storage *storage)
Sync all files with write access and put storage in shutdown mode.
Definition storage_external_api.c:1265
bool storage_file_eof(File *file)
Check whether the current access position is at the end of the file.
Definition storage_external_api.c:289
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:237
FS_Error storage_common_remove(Storage *storage, const char *path)
Remove a file or a directory.
Definition storage_external_api.c:476
FS_Error storage_sd_status(Storage *storage, const char *path)
Get SD card status.
Definition storage_external_api.c:973
FS_Error storage_sd_info(Storage *storage, const char *path, SDInfo *info)
Get SD card information.
Definition storage_external_api.c:958
bool storage_dir_rewind(File *file)
Change the access position to first item in the directory.
Definition storage_external_api.c:420
size_t storage_simply_read_entire_file(Storage *storage, const char *path, void *buffer, size_t buf_sz)
Simply read the entire file into memory.
Definition storage_external_api.c:1149
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:218
bool storage_simply_remove_recursive(Storage *storage, const char *path)
Recursively remove a file or a directory.
Definition storage_external_api.c:1029
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:875
FS_Error storage_sd_format(Storage *storage, const char *path)
Format the SD Card.
Definition storage_external_api.c:902
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:592
void storage_common_revive(Storage *storage)
Exit the shutdown mode (previously enabled by storage_common_shutdown) and continue normal operation.
Definition storage_external_api.c:1274
void storage_file_free(File *file)
Free the file instance.
Definition storage_external_api.c:1009
bool storage_file_close(File *file)
Close the file.
Definition storage_external_api.c:143
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:796
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:460
bool storage_simply_write_entire_file_from_string(Storage *storage, const char *path, FuriString *string)
Simply truncate and write the entire file from a string in memory.
Definition storage_external_api.c:1227
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:312
const char * storage_error_get_desc(FS_Error error_id)
Get the textual description of a numeric error identifier.
Definition storage_external_api.c:866
FS_Error storage_sd_unmount(Storage *storage, const char *path)
Unmount the SD card.
Definition storage_external_api.c:930
bool storage_dir_close(File *file)
Close the directory.
Definition storage_external_api.c:387
bool storage_dir_exists(Storage *storage, const char *path)
Check whether a directory exists.
Definition storage_external_api.c:429
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:95
bool storage_common_equivalent_path(Storage *storage, const char *path1, const char *path2)
Check whether two paths are equivalent.
Definition storage_external_api.c:857
FS_Error storage_file_get_error(File *file)
Get the numeric error identifier from a file instance.
Definition storage_external_api.c:870
bool storage_file_exists(Storage *storage, const char *path)
Check whether a file exists.
Definition storage_external_api.c:298
FS_Error storage_sd_make_filesystem(Storage *storage, const char *path)
Create a filesystem on the SD card.
Definition storage_external_api.c:916
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:403
FS_Error storage_sd_mount(Storage *storage, const char *path)
Mount the SD card.
Definition storage_external_api.c:944
bool storage_common_exists(Storage *storage, const char *path)
Check whether a file or a directory exists.
Definition storage_external_api.c:827
bool storage_file_truncate(File *file)
Truncate the file size to the current access position.
Definition storage_external_api.c:262
bool storage_simply_write_entire_file(Storage *storage, const char *path, const void *buffer, size_t length)
Simply truncate and write the entire file from memory.
Definition storage_external_api.c:1206
bool storage_file_sync(File *file)
Synchronise the file cache with the actual storage.
Definition storage_external_api.c:280
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:687
bool storage_simply_mkdir(Storage *storage, const char *path)
Create a directory.
Definition storage_external_api.c:1101
bool storage_dir_open(File *file, const char *path)
Open a directory.
Definition storage_external_api.c:355
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:491
FS_Error storage_common_fs_info(Storage *storage, const char *fs_path, uint64_t *total_space, uint64_t *free_space, bool *is_read_only)
Get the general information about the storage.
Definition storage_external_api.c:772
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:444
FS_Error storage_common_mkdir(Storage *storage, const char *path)
Create a directory.
Definition storage_external_api.c:757
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:880
File * storage_file_alloc(Storage *storage)
Allocate and initialize a file instance.
Definition storage_external_api.c:987
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:861
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:201
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:1238
uint64_t storage_file_size(File *file)
Get the file size.
Definition storage_external_api.c:271
bool storage_file_is_open(File *file)
Check whether the file is open.
Definition storage_external_api.c:999
FuriPubSub * storage_get_pubsub(Storage *storage)
Get the storage pubsub instance.
Definition storage_external_api.c:1024
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:811
uint64_t storage_file_tell(File *file)
Get the current access position.
Definition storage_external_api.c:253
bool storage_file_is_dir(File *file)
Check whether a file instance represents a directory.
Definition storage_external_api.c:1004
Structure that hold file index and returned api errors.
Definition filesystem_api_internal.h:18
Structure that hold file info.
Definition filesystem_api_defines.h:50
Definition storage_sd_api.h:19
Storage event (passed to the PubSub callback).
Definition storage.h:65
StorageEventType type
Type of the event.
Definition storage.h:66
Definition storage_i.h:20