[FL-3055] Getter for application data path (#2181)

* Threads: application id
* Unit tests: appsdata getter test
* Unit tests: moar test cases for appsdata getter
* Unit tests: remove folders after test
* Storage: dir_is_exist, migrate, + unit_tests
* Plugins: migration
* Storage: common_exists, moar unit_tests 4 "common_migrate", "common_migrate" and "common_merge" bugfixes
* Storage: use FuriString for path handling
* Storage API: send caller thread id with path
* Storage: remove StorageType field in storage file list
* Storage: simplify processing
* Storage API: send caller thread id with path everywhere
* Storage: /app alias, unit tests and path creation
* Storage, path helper: remove unused
* Examples: app data example
* App plugins: use new VFS path
* Storage: file_info_is_dir
* Services: handle alias if the service accepts a path.
* App plugins: fixes
* Make PVS happy
* Storage: fix storage_merge_recursive
* Storage: rename process_aliases to resolve_path. Rename APPS_DATA to APP_DATA.
* Apps: use predefined macro instead of raw paths. Example Apps Data: README fixes.
* Storage: rename storage_common_resolve_path to storage_common_resolve_path_and_ensure_app_directory
* Api: fix version
* Storage: rename alias message
* Storage: do not create app folders in path resolving process in certain cases.

---------

Co-authored-by: Astra <93453568+Astrrra@users.noreply.github.com>
Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
This commit is contained in:
Sergey Gavrilov
2023-03-01 20:57:27 +03:00
committed by GitHub
parent 9ae58f5462
commit 777a4d109d
52 changed files with 871 additions and 577 deletions

View File

@@ -4,17 +4,11 @@
#define FS_CALL(_storage, _fn) ret = _storage->fs_api->_fn;
static StorageData* storage_get_storage_by_type(Storage* app, StorageType type) {
furi_check(type == ST_EXT || type == ST_INT);
StorageData* storage = &app->storage[type];
return storage;
}
static bool storage_type_is_not_valid(StorageType type) {
static bool storage_type_is_valid(StorageType type) {
#ifdef FURI_RAM_EXEC
return type != ST_EXT;
return type == ST_EXT;
#else
return type >= ST_ERROR;
return type < ST_ERROR;
#endif
}
@@ -30,25 +24,21 @@ static StorageData* get_storage_by_file(File* file, StorageData* storages) {
return storage_data;
}
static const char* remove_vfs(const char* path) {
return path + MIN(4u, strlen(path));
static const char* cstr_path_without_vfs_prefix(FuriString* path) {
const char* path_cstr = furi_string_get_cstr(path);
return path_cstr + MIN(4u, strlen(path_cstr));
}
static StorageType storage_get_type_by_path(Storage* app, const char* path) {
static StorageType storage_get_type_by_path(FuriString* path) {
StorageType type = ST_ERROR;
if(memcmp(path, STORAGE_EXT_PATH_PREFIX, strlen(STORAGE_EXT_PATH_PREFIX)) == 0) {
type = ST_EXT;
} else if(memcmp(path, STORAGE_INT_PATH_PREFIX, strlen(STORAGE_INT_PATH_PREFIX)) == 0) {
type = ST_INT;
} else if(memcmp(path, STORAGE_ANY_PATH_PREFIX, strlen(STORAGE_ANY_PATH_PREFIX)) == 0) {
type = ST_ANY;
}
const char* path_cstr = furi_string_get_cstr(path);
if(type == ST_ANY) {
if(memcmp(path_cstr, STORAGE_EXT_PATH_PREFIX, strlen(STORAGE_EXT_PATH_PREFIX)) == 0) {
type = ST_EXT;
} else if(memcmp(path_cstr, STORAGE_INT_PATH_PREFIX, strlen(STORAGE_INT_PATH_PREFIX)) == 0) {
type = ST_INT;
if(storage_data_status(&app->storage[ST_EXT]) == StorageStatusOK) {
type = ST_EXT;
}
} else if(memcmp(path_cstr, STORAGE_ANY_PATH_PREFIX, strlen(STORAGE_ANY_PATH_PREFIX)) == 0) {
type = ST_ANY;
}
return type;
@@ -71,38 +61,51 @@ static void storage_path_change_to_real_storage(FuriString* path, StorageType re
}
}
FS_Error storage_get_data(Storage* app, FuriString* path, StorageData** storage) {
StorageType type = storage_get_type_by_path(path);
if(storage_type_is_valid(type)) {
if(type == ST_ANY) {
type = ST_INT;
if(storage_data_status(&app->storage[ST_EXT]) == StorageStatusOK) {
type = ST_EXT;
}
storage_path_change_to_real_storage(path, type);
}
furi_assert(type == ST_EXT || type == ST_INT);
*storage = &app->storage[type];
return FSE_OK;
} else {
return FSE_INVALID_NAME;
}
}
/******************* File Functions *******************/
bool storage_process_file_open(
Storage* app,
File* file,
const char* path,
FuriString* path,
FS_AccessMode access_mode,
FS_OpenMode open_mode) {
bool ret = false;
StorageType type = storage_get_type_by_path(app, path);
StorageData* storage;
file->error_id = FSE_OK;
file->error_id = storage_get_data(app, path, &storage);
if(storage_type_is_not_valid(type)) {
file->error_id = FSE_INVALID_NAME;
} else {
storage = storage_get_storage_by_type(app, type);
FuriString* real_path;
real_path = furi_string_alloc_set(path);
storage_path_change_to_real_storage(real_path, type);
if(storage_path_already_open(real_path, storage->files)) {
if(file->error_id == FSE_OK) {
if(storage_path_already_open(path, storage->files)) {
file->error_id = FSE_ALREADY_OPEN;
} else {
if(access_mode & FSAM_WRITE) {
storage_data_timestamp(storage);
}
storage_push_storage_file(file, real_path, type, storage);
FS_CALL(storage, file.open(storage, file, remove_vfs(path), access_mode, open_mode));
}
storage_push_storage_file(file, path, storage);
furi_string_free(real_path);
const char* path_cstr_no_vfs = cstr_path_without_vfs_prefix(path);
FS_CALL(storage, file.open(storage, file, path_cstr_no_vfs, access_mode, open_mode));
}
}
return ret;
@@ -243,27 +246,18 @@ static bool storage_process_file_eof(Storage* app, File* file) {
/******************* Dir Functions *******************/
bool storage_process_dir_open(Storage* app, File* file, const char* path) {
bool storage_process_dir_open(Storage* app, File* file, FuriString* path) {
bool ret = false;
StorageType type = storage_get_type_by_path(app, path);
StorageData* storage;
file->error_id = FSE_OK;
file->error_id = storage_get_data(app, path, &storage);
if(storage_type_is_not_valid(type)) {
file->error_id = FSE_INVALID_NAME;
} else {
storage = storage_get_storage_by_type(app, type);
FuriString* real_path;
real_path = furi_string_alloc_set(path);
storage_path_change_to_real_storage(real_path, type);
if(storage_path_already_open(real_path, storage->files)) {
if(file->error_id == FSE_OK) {
if(storage_path_already_open(path, storage->files)) {
file->error_id = FSE_ALREADY_OPEN;
} else {
storage_push_storage_file(file, real_path, type, storage);
FS_CALL(storage, dir.open(storage, file, remove_vfs(path)));
storage_push_storage_file(file, path, storage);
FS_CALL(storage, dir.open(storage, file, cstr_path_without_vfs_prefix(path)));
}
furi_string_free(real_path);
}
return ret;
@@ -320,73 +314,52 @@ bool storage_process_dir_rewind(Storage* app, File* file) {
/******************* Common FS Functions *******************/
static FS_Error
storage_process_common_timestamp(Storage* app, const char* path, uint32_t* timestamp) {
FS_Error ret = FSE_OK;
StorageType type = storage_get_type_by_path(app, path);
storage_process_common_timestamp(Storage* app, FuriString* path, uint32_t* timestamp) {
StorageData* storage;
FS_Error ret = storage_get_data(app, path, &storage);
if(storage_type_is_not_valid(type)) {
ret = FSE_INVALID_NAME;
} else {
StorageData* storage = storage_get_storage_by_type(app, type);
if(ret == FSE_OK) {
*timestamp = storage_data_get_timestamp(storage);
}
return ret;
}
static FS_Error storage_process_common_stat(Storage* app, const char* path, FileInfo* fileinfo) {
FS_Error ret = FSE_OK;
StorageType type = storage_get_type_by_path(app, path);
static FS_Error storage_process_common_stat(Storage* app, FuriString* path, FileInfo* fileinfo) {
StorageData* storage;
FS_Error ret = storage_get_data(app, path, &storage);
if(storage_type_is_not_valid(type)) {
ret = FSE_INVALID_NAME;
} else {
StorageData* storage = storage_get_storage_by_type(app, type);
FS_CALL(storage, common.stat(storage, remove_vfs(path), fileinfo));
if(ret == FSE_OK) {
FS_CALL(storage, common.stat(storage, cstr_path_without_vfs_prefix(path), fileinfo));
}
return ret;
}
static FS_Error storage_process_common_remove(Storage* app, const char* path) {
FS_Error ret = FSE_OK;
StorageType type = storage_get_type_by_path(app, path);
FuriString* real_path;
real_path = furi_string_alloc_set(path);
storage_path_change_to_real_storage(real_path, type);
static FS_Error storage_process_common_remove(Storage* app, FuriString* path) {
StorageData* storage;
FS_Error ret = storage_get_data(app, path, &storage);
do {
if(storage_type_is_not_valid(type)) {
ret = FSE_INVALID_NAME;
break;
}
StorageData* storage = storage_get_storage_by_type(app, type);
if(storage_path_already_open(real_path, storage->files)) {
if(storage_path_already_open(path, storage->files)) {
ret = FSE_ALREADY_OPEN;
break;
}
storage_data_timestamp(storage);
FS_CALL(storage, common.remove(storage, remove_vfs(path)));
FS_CALL(storage, common.remove(storage, cstr_path_without_vfs_prefix(path)));
} while(false);
furi_string_free(real_path);
return ret;
}
static FS_Error storage_process_common_mkdir(Storage* app, const char* path) {
FS_Error ret = FSE_OK;
StorageType type = storage_get_type_by_path(app, path);
static FS_Error storage_process_common_mkdir(Storage* app, FuriString* path) {
StorageData* storage;
FS_Error ret = storage_get_data(app, path, &storage);
if(storage_type_is_not_valid(type)) {
ret = FSE_INVALID_NAME;
} else {
StorageData* storage = storage_get_storage_by_type(app, type);
if(ret == FSE_OK) {
storage_data_timestamp(storage);
FS_CALL(storage, common.mkdir(storage, remove_vfs(path)));
FS_CALL(storage, common.mkdir(storage, cstr_path_without_vfs_prefix(path)));
}
return ret;
@@ -394,17 +367,16 @@ static FS_Error storage_process_common_mkdir(Storage* app, const char* path) {
static FS_Error storage_process_common_fs_info(
Storage* app,
const char* fs_path,
FuriString* path,
uint64_t* total_space,
uint64_t* free_space) {
FS_Error ret = FSE_OK;
StorageType type = storage_get_type_by_path(app, fs_path);
StorageData* storage;
FS_Error ret = storage_get_data(app, path, &storage);
if(storage_type_is_not_valid(type)) {
ret = FSE_INVALID_NAME;
} else {
StorageData* storage = storage_get_storage_by_type(app, type);
FS_CALL(storage, common.fs_info(storage, remove_vfs(fs_path), total_space, free_space));
if(ret == FSE_OK) {
FS_CALL(
storage,
common.fs_info(storage, cstr_path_without_vfs_prefix(path), total_space, free_space));
}
return ret;
@@ -471,14 +443,52 @@ static FS_Error storage_process_sd_status(Storage* app) {
return ret;
}
/******************** Aliases processing *******************/
void storage_process_alias(
Storage* app,
FuriString* path,
FuriThreadId thread_id,
bool create_folders) {
if(furi_string_start_with(path, STORAGE_APP_DATA_PATH_PREFIX)) {
FuriString* apps_data_path_with_appsid = furi_string_alloc_set(APPS_DATA_PATH "/");
furi_string_cat(apps_data_path_with_appsid, furi_thread_get_appid(thread_id));
// "/app" -> "/ext/apps_data/appsid"
furi_string_replace_at(
path,
0,
strlen(STORAGE_APP_DATA_PATH_PREFIX),
furi_string_get_cstr(apps_data_path_with_appsid));
// Create app data folder if not exists
if(create_folders &&
storage_process_common_stat(app, apps_data_path_with_appsid, NULL) != FSE_OK) {
furi_string_set(apps_data_path_with_appsid, APPS_DATA_PATH);
storage_process_common_mkdir(app, apps_data_path_with_appsid);
furi_string_cat(apps_data_path_with_appsid, "/");
furi_string_cat(apps_data_path_with_appsid, furi_thread_get_appid(thread_id));
storage_process_common_mkdir(app, apps_data_path_with_appsid);
}
furi_string_free(apps_data_path_with_appsid);
}
}
/****************** API calls processing ******************/
void storage_process_message_internal(Storage* app, StorageMessage* message) {
FuriString* path = NULL;
switch(message->command) {
// File operations
case StorageCommandFileOpen:
path = furi_string_alloc_set(message->data->fopen.path);
storage_process_alias(app, path, message->data->fopen.thread_id, true);
message->return_data->bool_value = storage_process_file_open(
app,
message->data->fopen.file,
message->data->fopen.path,
path,
message->data->fopen.access_mode,
message->data->fopen.open_mode);
break;
@@ -527,9 +537,12 @@ void storage_process_message_internal(Storage* app, StorageMessage* message) {
message->return_data->bool_value = storage_process_file_eof(app, message->data->file.file);
break;
// Dir operations
case StorageCommandDirOpen:
path = furi_string_alloc_set(message->data->dopen.path);
storage_process_alias(app, path, message->data->dopen.thread_id, true);
message->return_data->bool_value =
storage_process_dir_open(app, message->data->dopen.file, message->data->dopen.path);
storage_process_dir_open(app, message->data->dopen.file, path);
break;
case StorageCommandDirClose:
message->return_data->bool_value =
@@ -547,29 +560,42 @@ void storage_process_message_internal(Storage* app, StorageMessage* message) {
message->return_data->bool_value =
storage_process_dir_rewind(app, message->data->file.file);
break;
// Common operations
case StorageCommandCommonTimestamp:
message->return_data->error_value = storage_process_common_timestamp(
app, message->data->ctimestamp.path, message->data->ctimestamp.timestamp);
path = furi_string_alloc_set(message->data->ctimestamp.path);
storage_process_alias(app, path, message->data->ctimestamp.thread_id, false);
message->return_data->error_value =
storage_process_common_timestamp(app, path, message->data->ctimestamp.timestamp);
break;
case StorageCommandCommonStat:
message->return_data->error_value = storage_process_common_stat(
app, message->data->cstat.path, message->data->cstat.fileinfo);
path = furi_string_alloc_set(message->data->cstat.path);
storage_process_alias(app, path, message->data->cstat.thread_id, false);
message->return_data->error_value =
storage_process_common_stat(app, path, message->data->cstat.fileinfo);
break;
case StorageCommandCommonRemove:
message->return_data->error_value =
storage_process_common_remove(app, message->data->path.path);
path = furi_string_alloc_set(message->data->path.path);
storage_process_alias(app, path, message->data->path.thread_id, false);
message->return_data->error_value = storage_process_common_remove(app, path);
break;
case StorageCommandCommonMkDir:
message->return_data->error_value =
storage_process_common_mkdir(app, message->data->path.path);
path = furi_string_alloc_set(message->data->path.path);
storage_process_alias(app, path, message->data->path.thread_id, true);
message->return_data->error_value = storage_process_common_mkdir(app, path);
break;
case StorageCommandCommonFSInfo:
path = furi_string_alloc_set(message->data->cfsinfo.fs_path);
storage_process_alias(app, path, message->data->cfsinfo.thread_id, false);
message->return_data->error_value = storage_process_common_fs_info(
app,
message->data->cfsinfo.fs_path,
message->data->cfsinfo.total_space,
message->data->cfsinfo.free_space);
app, path, message->data->cfsinfo.total_space, message->data->cfsinfo.free_space);
break;
case StorageCommandCommonResolvePath:
storage_process_alias(
app, message->data->cresolvepath.path, message->data->cresolvepath.thread_id, true);
break;
// SD operations
case StorageCommandSDFormat:
message->return_data->error_value = storage_process_sd_format(app);
break;
@@ -585,6 +611,10 @@ void storage_process_message_internal(Storage* app, StorageMessage* message) {
break;
}
if(path != NULL) { //-V547
furi_string_free(path);
}
api_lock_unlock(message->lock);
}