[FL-1191][FL-1524] Filesystem rework (#568)
* FS-Api: removed datetime manipulation functions and most of the file flags * Filesystem: common proxy api * Filesystem: renamed to Storage. Work has begun on a glue layer. Added functions for reentrance. * Storage: sd mount and sd file open * Storage: sd file close * Storage: temporary test app * Storage: free filedata on close * Storage: sd file read and write * Storage: added internal storage (LittleFS) * Storage: renamed internal commands * Storage: seek, tell, truncate, size, sync, eof * Storage: error descriptions * Storage: directory management api (open, close, read, rewind) * Storage: common management api (stat, fs_stat, remove, rename, mkdir) * Dolphin app and Notifications app now use raw storage. * Storage: storage statuses renamed. Implemented sd card icon. * Storage: added raw sd-card api. * Storage settings: work started * Assets: use new icons approach * Storage settings: working storage settings * Storage: completely redesigned api, no longer sticking out FS_Api * Storage: more simplified api, getting error_id from file is hidden from user, pointer to api is hidden inside file * Storage: cli info and format commands * Storage-cli: file list * Storage: a simpler and more reliable api * FatFS: slightly lighter and faster config. Also disabled reentrancy and file locking functions. They moved to a storage service. * Storage-cli: accommodate to the new cli api. * Storage: filesystem api is separated into internal and common api. * Cli: added the ability to print the list of free heap blocks * Storage: uses a list instead of an array to store the StorageFile. Rewrote api calls to use semaphores instead of thread flags. * Storage settings: added the ability to benchmark the SD card. * Gui module file select: uses new storage api * Apps: removed deprecated sd_card_test application * Args lib: support for enquoted arguments * Dialogs: a new gui app for simple non-asynchronous apps * Dialogs: view holder for easy single view work * File worker: use new storage api * IButton and lfrrfid apps: save keys to any storage * Apps: fix ibutton and lfrfid stack, remove sd_card_test. * SD filesystem: app removed * File worker: fixed api pointer type * Subghz: loading assets using the new storage api * NFC: use the new storage api * Dialogs: the better api for the message element * Archive: use new storage api * Irda: changed assest path, changed app path * FileWorker: removed unused file_buf_cnt * Storage: copying and renaming files now works between storages * Storage cli: read, copy, remove, rename commands * Archive: removed commented code * Storage cli: write command * Applications: add SRV_STORAGE and SRV_DIALOGS * Internal-storage: removed * Storage: improved api * Storage app: changed api pointer from StorageApp to Storage * Storage: better file_id handling * Storage: more consistent errors * Loader: support for NULL icons * Storage: do nothing with the lfs file or directory if it is not open * Storage: fix typo * Storage: minor float usage cleanup, rename some symbols. * Storage: compact doxygen comments. Co-authored-by: あく <alleteam@gmail.com>
This commit is contained in:
@@ -1,16 +1,13 @@
|
||||
#include "file-worker.h"
|
||||
#include "m-string.h"
|
||||
#include <hex.h>
|
||||
#include <sd-card-api.h>
|
||||
#include <dialogs/dialogs.h>
|
||||
#include <furi.h>
|
||||
|
||||
struct FileWorker {
|
||||
FS_Api* fs_api;
|
||||
SdCard_Api* sd_ex_api;
|
||||
Storage* api;
|
||||
bool silent;
|
||||
File file;
|
||||
char file_buf[48];
|
||||
size_t file_buf_cnt;
|
||||
File* file;
|
||||
};
|
||||
|
||||
bool file_worker_check_common_errors(FileWorker* file_worker);
|
||||
@@ -26,16 +23,15 @@ bool file_worker_seek_internal(FileWorker* file_worker, uint64_t position, bool
|
||||
FileWorker* file_worker_alloc(bool _silent) {
|
||||
FileWorker* file_worker = malloc(sizeof(FileWorker));
|
||||
file_worker->silent = _silent;
|
||||
file_worker->fs_api = furi_record_open("sdcard");
|
||||
file_worker->sd_ex_api = furi_record_open("sdcard-ex");
|
||||
file_worker->file_buf_cnt = 0;
|
||||
file_worker->api = furi_record_open("storage");
|
||||
file_worker->file = storage_file_alloc(file_worker->api);
|
||||
|
||||
return file_worker;
|
||||
}
|
||||
|
||||
void file_worker_free(FileWorker* file_worker) {
|
||||
furi_record_close("sdcard");
|
||||
furi_record_close("sdcard-ex");
|
||||
storage_file_free(file_worker->file);
|
||||
furi_record_close("storage");
|
||||
free(file_worker);
|
||||
}
|
||||
|
||||
@@ -44,8 +40,7 @@ bool file_worker_open(
|
||||
const char* filename,
|
||||
FS_AccessMode access_mode,
|
||||
FS_OpenMode open_mode) {
|
||||
bool result =
|
||||
file_worker->fs_api->file.open(&file_worker->file, filename, access_mode, open_mode);
|
||||
bool result = storage_file_open(file_worker->file, filename, access_mode, open_mode);
|
||||
|
||||
if(!result) {
|
||||
file_worker_show_error_internal(file_worker, "Cannot open\nfile");
|
||||
@@ -56,13 +51,15 @@ bool file_worker_open(
|
||||
}
|
||||
|
||||
bool file_worker_close(FileWorker* file_worker) {
|
||||
file_worker->fs_api->file.close(&file_worker->file);
|
||||
if(storage_file_is_open(file_worker->file)) {
|
||||
storage_file_close(file_worker->file);
|
||||
}
|
||||
|
||||
return file_worker_check_common_errors(file_worker);
|
||||
}
|
||||
|
||||
bool file_worker_mkdir(FileWorker* file_worker, const char* dirname) {
|
||||
FS_Error fs_result = file_worker->fs_api->common.mkdir(dirname);
|
||||
FS_Error fs_result = storage_common_mkdir(file_worker->api, dirname);
|
||||
|
||||
if(fs_result != FSE_OK && fs_result != FSE_EXIST) {
|
||||
file_worker_show_error_internal(file_worker, "Cannot create\nfolder");
|
||||
@@ -73,7 +70,7 @@ bool file_worker_mkdir(FileWorker* file_worker, const char* dirname) {
|
||||
}
|
||||
|
||||
bool file_worker_remove(FileWorker* file_worker, const char* filename) {
|
||||
FS_Error fs_result = file_worker->fs_api->common.remove(filename);
|
||||
FS_Error fs_result = storage_common_remove(file_worker->api, filename);
|
||||
if(fs_result != FSE_OK && fs_result != FSE_NOT_EXIST) {
|
||||
file_worker_show_error_internal(file_worker, "Cannot remove\nold file");
|
||||
return false;
|
||||
@@ -96,9 +93,8 @@ bool file_worker_read_until(FileWorker* file_worker, string_t str_result, char s
|
||||
uint8_t buffer[buffer_size];
|
||||
|
||||
do {
|
||||
uint16_t read_count =
|
||||
file_worker->fs_api->file.read(&file_worker->file, buffer, buffer_size);
|
||||
if(file_worker->file.error_id != FSE_OK) {
|
||||
uint16_t read_count = storage_file_read(file_worker->file, buffer, buffer_size);
|
||||
if(storage_file_get_error(file_worker->file) != FSE_OK) {
|
||||
file_worker_show_error_internal(file_worker, "Cannot read\nfile");
|
||||
return false;
|
||||
}
|
||||
@@ -210,7 +206,16 @@ bool file_worker_write_hex(FileWorker* file_worker, const uint8_t* buffer, uint1
|
||||
}
|
||||
|
||||
void file_worker_show_error(FileWorker* file_worker, const char* error_text) {
|
||||
file_worker->sd_ex_api->show_error(file_worker->sd_ex_api->context, error_text);
|
||||
DialogsApp* dialogs = furi_record_open("dialogs");
|
||||
|
||||
DialogMessage* message = dialog_message_alloc();
|
||||
dialog_message_set_text(message, error_text, 88, 32, AlignCenter, AlignCenter);
|
||||
dialog_message_set_icon(message, &I_SDQuestion_35x43, 5, 6);
|
||||
dialog_message_set_buttons(message, "Back", NULL, NULL);
|
||||
dialog_message_show(dialogs, message);
|
||||
dialog_message_free(message);
|
||||
|
||||
furi_record_close("dialogs");
|
||||
}
|
||||
|
||||
bool file_worker_file_select(
|
||||
@@ -220,16 +225,17 @@ bool file_worker_file_select(
|
||||
char* result,
|
||||
uint8_t result_size,
|
||||
const char* selected_filename) {
|
||||
return file_worker->sd_ex_api->file_select(
|
||||
file_worker->sd_ex_api->context, path, extension, result, result_size, selected_filename);
|
||||
DialogsApp* dialogs = furi_record_open("dialogs");
|
||||
bool ret =
|
||||
dialog_file_select_show(dialogs, path, extension, result, result_size, selected_filename);
|
||||
furi_record_close("dialogs");
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool file_worker_check_common_errors(FileWorker* file_worker) {
|
||||
//TODO remove
|
||||
/* TODO: [FL-1431] Add return value to file_parser.get_sd_api().check_error() and replace get_fs_info(). */
|
||||
FS_Error fs_err = file_worker->fs_api->common.get_fs_info(NULL, NULL);
|
||||
if(fs_err != FSE_OK)
|
||||
file_worker->sd_ex_api->show_error(file_worker->sd_ex_api->context, "SD card not found");
|
||||
return fs_err == FSE_OK;
|
||||
return true;
|
||||
}
|
||||
|
||||
void file_worker_show_error_internal(FileWorker* file_worker, const char* error_text) {
|
||||
@@ -239,10 +245,9 @@ void file_worker_show_error_internal(FileWorker* file_worker, const char* error_
|
||||
}
|
||||
|
||||
bool file_worker_read_internal(FileWorker* file_worker, void* buffer, uint16_t bytes_to_read) {
|
||||
uint16_t read_count =
|
||||
file_worker->fs_api->file.read(&file_worker->file, buffer, bytes_to_read);
|
||||
uint16_t read_count = storage_file_read(file_worker->file, buffer, bytes_to_read);
|
||||
|
||||
if(file_worker->file.error_id != FSE_OK || read_count != bytes_to_read) {
|
||||
if(storage_file_get_error(file_worker->file) != FSE_OK || read_count != bytes_to_read) {
|
||||
file_worker_show_error_internal(file_worker, "Cannot read\nfile");
|
||||
return false;
|
||||
}
|
||||
@@ -254,10 +259,9 @@ bool file_worker_write_internal(
|
||||
FileWorker* file_worker,
|
||||
const void* buffer,
|
||||
uint16_t bytes_to_write) {
|
||||
uint16_t write_count =
|
||||
file_worker->fs_api->file.write(&file_worker->file, buffer, bytes_to_write);
|
||||
uint16_t write_count = storage_file_write(file_worker->file, buffer, bytes_to_write);
|
||||
|
||||
if(file_worker->file.error_id != FSE_OK || write_count != bytes_to_write) {
|
||||
if(storage_file_get_error(file_worker->file) != FSE_OK || write_count != bytes_to_write) {
|
||||
file_worker_show_error_internal(file_worker, "Cannot write\nto file");
|
||||
return false;
|
||||
}
|
||||
@@ -266,9 +270,9 @@ bool file_worker_write_internal(
|
||||
}
|
||||
|
||||
bool file_worker_tell_internal(FileWorker* file_worker, uint64_t* position) {
|
||||
*position = file_worker->fs_api->file.tell(&file_worker->file);
|
||||
*position = storage_file_tell(file_worker->file);
|
||||
|
||||
if(file_worker->file.error_id != FSE_OK) {
|
||||
if(storage_file_get_error(file_worker->file) != FSE_OK) {
|
||||
file_worker_show_error_internal(file_worker, "Cannot tell\nfile offset");
|
||||
return false;
|
||||
}
|
||||
@@ -277,8 +281,8 @@ bool file_worker_tell_internal(FileWorker* file_worker, uint64_t* position) {
|
||||
}
|
||||
|
||||
bool file_worker_seek_internal(FileWorker* file_worker, uint64_t position, bool from_start) {
|
||||
file_worker->fs_api->file.seek(&file_worker->file, position, from_start);
|
||||
if(file_worker->file.error_id != FSE_OK) {
|
||||
storage_file_seek(file_worker->file, position, from_start);
|
||||
if(storage_file_get_error(file_worker->file) != FSE_OK) {
|
||||
file_worker_show_error_internal(file_worker, "Cannot seek\nfile");
|
||||
return false;
|
||||
}
|
||||
@@ -286,9 +290,17 @@ bool file_worker_seek_internal(FileWorker* file_worker, uint64_t position, bool
|
||||
return true;
|
||||
}
|
||||
|
||||
bool file_worker_read_until_buffered(FileWorker* file_worker, string_t str_result, char* file_buf, size_t* file_buf_cnt, size_t file_buf_size, char separator) {
|
||||
bool file_worker_read_until_buffered(
|
||||
FileWorker* file_worker,
|
||||
string_t str_result,
|
||||
char* file_buf,
|
||||
size_t* file_buf_cnt,
|
||||
size_t file_buf_size,
|
||||
char separator) {
|
||||
furi_assert(string_capacity(str_result) > 0);
|
||||
furi_assert(file_buf_size <= 512); /* fs_api->file.read now supports up to 512 bytes reading at a time */
|
||||
|
||||
// fs_api->file.read now supports up to 512 bytes reading at a time
|
||||
furi_assert(file_buf_size <= 512);
|
||||
|
||||
string_clean(str_result);
|
||||
size_t newline_index = 0;
|
||||
@@ -311,11 +323,11 @@ bool file_worker_read_until_buffered(FileWorker* file_worker, string_t str_resul
|
||||
furi_assert(0);
|
||||
}
|
||||
|
||||
if (max_length && (string_size(str_result) + end_index > max_length))
|
||||
if(max_length && (string_size(str_result) + end_index > max_length))
|
||||
max_length_exceeded = true;
|
||||
|
||||
if (!max_length_exceeded) {
|
||||
for (size_t i = 0; i < end_index; ++i) {
|
||||
if(!max_length_exceeded) {
|
||||
for(size_t i = 0; i < end_index; ++i) {
|
||||
string_push_back(str_result, file_buf[i]);
|
||||
}
|
||||
}
|
||||
@@ -325,9 +337,9 @@ bool file_worker_read_until_buffered(FileWorker* file_worker, string_t str_resul
|
||||
if(found_eol) break;
|
||||
}
|
||||
|
||||
*file_buf_cnt +=
|
||||
file_worker->fs_api->file.read(&file_worker->file, &file_buf[*file_buf_cnt], file_buf_size - *file_buf_cnt);
|
||||
if(file_worker->file.error_id != FSE_OK) {
|
||||
*file_buf_cnt += storage_file_read(
|
||||
file_worker->file, &file_buf[*file_buf_cnt], file_buf_size - *file_buf_cnt);
|
||||
if(storage_file_get_error(file_worker->file) != FSE_OK) {
|
||||
file_worker_show_error_internal(file_worker, "Cannot read\nfile");
|
||||
string_clear(str_result);
|
||||
*file_buf_cnt = 0;
|
||||
@@ -338,14 +350,13 @@ bool file_worker_read_until_buffered(FileWorker* file_worker, string_t str_resul
|
||||
}
|
||||
}
|
||||
|
||||
if (max_length_exceeded)
|
||||
string_clear(str_result);
|
||||
if(max_length_exceeded) string_clear(str_result);
|
||||
|
||||
return string_size(str_result) || *file_buf_cnt;
|
||||
}
|
||||
|
||||
bool file_worker_rename(FileWorker* file_worker, const char* old_path, const char* new_path) {
|
||||
FS_Error fs_result = file_worker->fs_api->common.rename(old_path, new_path);
|
||||
FS_Error fs_result = storage_common_rename(file_worker->api, old_path, new_path);
|
||||
|
||||
if(fs_result != FSE_OK && fs_result != FSE_EXIST) {
|
||||
file_worker_show_error_internal(file_worker, "Cannot rename\n file/directory");
|
||||
@@ -359,16 +370,12 @@ bool file_worker_check_errors(FileWorker* file_worker) {
|
||||
return file_worker_check_common_errors(file_worker);
|
||||
}
|
||||
|
||||
bool file_worker_is_file_exist(
|
||||
FileWorker* file_worker,
|
||||
const char* filename,
|
||||
bool* exist) {
|
||||
bool file_worker_is_file_exist(FileWorker* file_worker, const char* filename, bool* exist) {
|
||||
File* file = storage_file_alloc(file_worker->api);
|
||||
|
||||
File file;
|
||||
*exist = file_worker->fs_api->file.open(&file, filename, FSAM_READ, FSOM_OPEN_EXISTING);
|
||||
if (*exist)
|
||||
file_worker->fs_api->file.close(&file);
|
||||
*exist = storage_file_open(file, filename, FSAM_READ, FSOM_OPEN_EXISTING);
|
||||
storage_file_close(file);
|
||||
storage_file_free(file);
|
||||
|
||||
return file_worker_check_common_errors(file_worker);
|
||||
}
|
||||
|
||||
|
@@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
#include <m-string.h>
|
||||
#include <filesystem-api.h>
|
||||
#include <storage/storage.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
@@ -150,20 +150,20 @@ void file_worker_show_error(FileWorker* file_worker, const char* error_text);
|
||||
* @brief Show system file select widget
|
||||
*
|
||||
* @param file_worker FileWorker instance
|
||||
* @param path
|
||||
* @param extension
|
||||
* @param result
|
||||
* @param result_size
|
||||
* @param selected_filename
|
||||
* @return true if file was selected
|
||||
* @param path path to directory
|
||||
* @param extension file extension to be offered for selection
|
||||
* @param selected_filename buffer where the selected filename will be saved
|
||||
* @param selected_filename_size and the size of this buffer
|
||||
* @param preselected_filename filename to be preselected
|
||||
* @return bool whether a file was selected
|
||||
*/
|
||||
bool file_worker_file_select(
|
||||
FileWorker* file_worker,
|
||||
const char* path,
|
||||
const char* extension,
|
||||
char* result,
|
||||
uint8_t result_size,
|
||||
const char* selected_filename);
|
||||
char* selected_filename,
|
||||
uint8_t selected_filename_size,
|
||||
const char* preselected_filename);
|
||||
|
||||
/**
|
||||
* @brief Reads data from a file until separator or EOF is found.
|
||||
@@ -177,7 +177,13 @@ bool file_worker_file_select(
|
||||
* @param separator
|
||||
* @return true on success
|
||||
*/
|
||||
bool file_worker_read_until_buffered(FileWorker* file_worker, string_t str_result, char* file_buf, size_t* file_buf_cnt, size_t max_length, char separator);
|
||||
bool file_worker_read_until_buffered(
|
||||
FileWorker* file_worker,
|
||||
string_t str_result,
|
||||
char* file_buf,
|
||||
size_t* file_buf_cnt,
|
||||
size_t max_length,
|
||||
char separator);
|
||||
|
||||
/**
|
||||
* @brief Check whether file exist or not
|
||||
@@ -187,10 +193,7 @@ bool file_worker_read_until_buffered(FileWorker* file_worker, string_t str_resul
|
||||
* @param exist - flag to show file exist
|
||||
* @return true on success
|
||||
*/
|
||||
bool file_worker_is_file_exist(
|
||||
FileWorker* file_worker,
|
||||
const char* filename,
|
||||
bool* exist);
|
||||
bool file_worker_is_file_exist(FileWorker* file_worker, const char* filename, bool* exist);
|
||||
|
||||
/**
|
||||
* @brief Rename file or directory
|
||||
@@ -200,9 +203,7 @@ bool file_worker_is_file_exist(
|
||||
* @param new_filename
|
||||
* @return true on success
|
||||
*/
|
||||
bool file_worker_rename(FileWorker* file_worker,
|
||||
const char* old_path,
|
||||
const char* new_path);
|
||||
bool file_worker_rename(FileWorker* file_worker, const char* old_path, const char* new_path);
|
||||
|
||||
/**
|
||||
* @brief Check errors
|
||||
|
Reference in New Issue
Block a user