[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:
SG
2021-07-23 22:20:19 +10:00
committed by GitHub
parent a81203941b
commit ad421a81bc
95 changed files with 6451 additions and 4349 deletions

View File

@@ -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);
}

View File

@@ -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

View File

@@ -28,6 +28,23 @@ bool args_read_string_and_trim(string_t args, string_t word) {
return true;
}
bool args_read_probably_quoted_string_and_trim(string_t args, string_t word) {
if(string_size(args) > 1 && string_get_char(args, 0) == '\"') {
size_t second_quote_pos = string_search_char(args, '\"', 1);
if(second_quote_pos == 0) {
return false;
}
string_set_n(word, args, 1, second_quote_pos - 1);
string_right(args, second_quote_pos + 1);
string_strim(args);
return true;
} else {
return args_read_string_and_trim(args, word);
}
}
bool args_char_to_hex(char hi_nibble, char low_nibble, uint8_t* byte) {
uint8_t hi_nibble_value = 0;
uint8_t low_nibble_value = 0;

View File

@@ -8,15 +8,25 @@ extern "C" {
#endif
/**
* @brief Extract first word from arguments string and trim arguments string
* @brief Extract first argument from arguments string and trim arguments string
*
* @param args arguments string
* @param word first word, output
* @param word first argument, output
* @return true - success
* @return false - arguments string does not contain anything
*/
bool args_read_string_and_trim(string_t args, string_t word);
/**
* @brief Extract the first quoted argument from the argument string and trim the argument string. If the argument is not quoted, calls args_read_string_and_trim.
*
* @param args arguments string
* @param word first argument, output, without quotes
* @return true - success
* @return false - arguments string does not contain anything
*/
bool args_read_probably_quoted_string_and_trim(string_t args, string_t word);
/**
* @brief Convert hex ASCII values to byte array
*

View File

@@ -1,329 +0,0 @@
#pragma once
#include <furi.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Access mode flags
*/
typedef enum {
FSAM_READ = (1 << 0), /**< Read access */
FSAM_WRITE = (1 << 1), /**< Write access */
} FS_AccessMode;
/**
* @brief Open mode flags
*/
typedef enum {
FSOM_OPEN_EXISTING = 1, /**< Open file, fail if file doesn't exist */
FSOM_OPEN_ALWAYS = 2, /**< Open file. Create new file if not exist */
FSOM_OPEN_APPEND = 4, /**< Open file. Create new file if not exist. Set R/W pointer to EOF */
FSOM_CREATE_NEW = 8, /**< Creates a new file. Fails if the file is exist */
FSOM_CREATE_ALWAYS = 16, /**< Creates a new file. If file exist, truncate to zero size */
} FS_OpenMode;
/**
* @brief API errors enumeration
*/
typedef enum {
FSE_OK, /**< No error */
FSE_NOT_READY, /**< FS not ready */
FSE_EXIST, /**< File/Dir alrady exist */
FSE_NOT_EXIST, /**< File/Dir does not exist */
FSE_INVALID_PARAMETER, /**< Invalid API parameter */
FSE_DENIED, /**< Access denied */
FSE_INVALID_NAME, /**< Invalid name/path */
FSE_INTERNAL, /**< Internal error */
FSE_NOT_IMPLEMENTED, /**< Functon not implemented */
} FS_Error;
/**
* @brief FileInfo flags
*/
typedef enum {
FSF_READ_ONLY = (1 << 0), /**< Readonly */
FSF_HIDDEN = (1 << 1), /**< Hidden */
FSF_SYSTEM = (1 << 2), /**< System */
FSF_DIRECTORY = (1 << 3), /**< Directory */
FSF_ARCHIVE = (1 << 4), /**< Archive */
} FS_Flags;
/**
* @brief Structure that hold file index and returned api errors
*/
typedef struct {
uint32_t file_id; /**< File ID for internal references */
FS_Error error_id; /**< Standart API error from FS_Error enum */
uint32_t internal_error_id; /**< Internal API error value */
} File;
// TODO: solve year 2107 problem
/**
* @brief Structure that hold packed date values
*/
typedef struct __attribute__((packed)) {
uint16_t month_day : 5; /**< month day */
uint16_t month : 4; /**< month index */
uint16_t year : 7; /**< year, year + 1980 to get actual value */
} FileDate;
/**
* @brief Structure that hold packed time values
*/
typedef struct __attribute__((packed)) {
uint16_t second : 5; /**< second, second * 2 to get actual value */
uint16_t minute : 6; /**< minute */
uint16_t hour : 5; /**< hour */
} FileTime;
/**
* @brief Union of simple date and real value
*/
typedef union {
FileDate simple; /**< simple access to date */
uint16_t value; /**< real date value */
} FileDateUnion;
/**
* @brief Union of simple time and real value
*/
typedef union {
FileTime simple; /**< simple access to time */
uint16_t value; /**< real time value */
} FileTimeUnion;
/**
* @brief Structure that hold file info
*/
typedef struct {
uint8_t flags; /**< flags from FS_Flags enum */
uint64_t size; /**< file size */
FileDateUnion date; /**< file date */
FileTimeUnion time; /**< file time */
} FileInfo;
/** @struct FS_File_Api
* @brief File api structure
*
* @var FS_File_Api::open
* @brief Open file
* @param file pointer to file object, filled by api
* @param path path to file
* @param access_mode access mode from FS_AccessMode
* @param open_mode open mode from FS_OpenMode
* @return success flag
*
* @var FS_File_Api::close
* @brief Close file
* @param file pointer to file object
* @return success flag
*
* @var FS_File_Api::read
* @brief Read bytes from file to buffer
* @param file pointer to file object
* @param buff pointer to buffer for reading
* @param bytes_to_read how many bytes to read, must be smaller or equal to buffer size
* @return how many bytes actually has been readed
*
* @var FS_File_Api::write
* @brief Write bytes from buffer to file
* @param file pointer to file object
* @param buff pointer to buffer for writing
* @param bytes_to_read how many bytes to write, must be smaller or equal to buffer size
* @return how many bytes actually has been writed
*
* @var FS_File_Api::seek
* @brief Move r/w pointer
* @param file pointer to file object
* @param offset offset to move r/w pointer
* @param from_start set offset from start, or from current position
* @return success flag
*
* @var FS_File_Api::tell
* @brief Get r/w pointer position
* @param file pointer to file object
* @return current r/w pointer position
*
* @var FS_File_Api::truncate
* @brief Truncate file size to current r/w pointer position
* @param file pointer to file object
* @return success flag
*
* @var FS_File_Api::size
* @brief Fet file size
* @param file pointer to file object
* @return file size
*
* @var FS_File_Api::sync
* @brief Write file cache to storage
* @param file pointer to file object
* @return success flag
*
* @var FS_File_Api::eof
* @brief Checks that the r/w pointer is at the end of the file
* @param file pointer to file object
* @return end of file flag
*/
/**
* @brief File api structure
*/
typedef struct {
bool (*open)(File* file, const char* path, FS_AccessMode access_mode, FS_OpenMode open_mode);
bool (*close)(File* file);
uint16_t (*read)(File* file, void* buff, uint16_t bytes_to_read);
uint16_t (*write)(File* file, const void* buff, uint16_t bytes_to_write);
bool (*seek)(File* file, uint32_t offset, bool from_start);
uint64_t (*tell)(File* file);
bool (*truncate)(File* file);
uint64_t (*size)(File* file);
bool (*sync)(File* file);
bool (*eof)(File* file);
} FS_File_Api;
/** @struct FS_Dir_Api
* @brief Dir api structure
*
* @var FS_Dir_Api::open
* @brief Open directory to get objects from
* @param file pointer to file object, filled by api
* @param path path to directory
* @return success flag
*
* @var FS_Dir_Api::close
* @brief Close directory
* @param file pointer to file object
* @return success flag
*
* @var FS_Dir_Api::read
* @brief Read next object info in directory
* @param file pointer to file object
* @param fileinfo pointer to readed FileInfo, can be NULL
* @param name pointer to name buffer, can be NULL
* @param name_length name buffer length
* @return success flag (if next object not exist also returns false and set error_id to FSE_NOT_EXIST)
*
* @var FS_Dir_Api::rewind
* @brief Rewind to first object info in directory
* @param file pointer to file object
* @return success flag
*/
/**
* @brief Dir api structure
*/
typedef struct {
bool (*open)(File* file, const char* path);
bool (*close)(File* file);
bool (*read)(File* file, FileInfo* fileinfo, char* name, uint16_t name_length);
bool (*rewind)(File* file);
} FS_Dir_Api;
/** @struct FS_Common_Api
* @brief Common api structure
*
* @var FS_Common_Api::info
* @brief Open directory to get objects from
* @param path path to file/directory
* @param fileinfo pointer to readed FileInfo, can be NULL
* @param name pointer to name buffer, can be NULL
* @param name_length name buffer length
* @return FS_Error error info
*
* @var FS_Common_Api::remove
* @brief Remove file/directory from storage,
* directory must be empty,
* file/directory must not be opened,
* file/directory must not have FSF_READ_ONLY flag
* @param path path to file/directory
* @return FS_Error error info
*
* @var FS_Common_Api::rename
* @brief Rename file/directory,
* file/directory must not be opened
* @param path path to file/directory
* @return FS_Error error info
*
* @var FS_Common_Api::set_attr
* @brief Set attributes of file/directory,
* for example:
* @code
* set "read only" flag and remove "hidden" flag
* set_attr("file.txt", FSF_READ_ONLY, FSF_READ_ONLY | FSF_HIDDEN);
* @endcode
* @param path path to file/directory
* @param attr attribute values consist of FS_Flags
* @param mask attribute mask consist of FS_Flags
* @return FS_Error error info
*
* @var FS_Common_Api::mkdir
* @brief Create new directory
* @param path path to new directory
* @return FS_Error error info
*
* @var FS_Common_Api::set_time
* @brief Set file/directory modification time
* @param path path to file/directory
* @param date modification date
* @param time modification time
* @see FileDateUnion
* @see FileTimeUnion
* @return FS_Error error info
*
* @var FS_Common_Api::get_fs_info
* @brief Get total and free space storage values
* @param total_space pointer to total space value
* @param free_space pointer to free space value
* @return FS_Error error info
*/
/**
* @brief Common api structure
*/
typedef struct {
FS_Error (*info)(const char* path, FileInfo* fileinfo, char* name, const uint16_t name_length);
FS_Error (*remove)(const char* path);
FS_Error (*rename)(const char* old_path, const char* new_path);
FS_Error (*set_attr)(const char* path, uint8_t attr, uint8_t mask);
FS_Error (*mkdir)(const char* path);
FS_Error (*set_time)(const char* path, FileDateUnion date, FileTimeUnion time);
FS_Error (*get_fs_info)(uint64_t* total_space, uint64_t* free_space);
} FS_Common_Api;
/** @struct FS_Error_Api
* @brief Errors api structure
*
* @var FS_Error_Api::get_desc
* @brief Get error description text
* @param error_id FS_Error error id (for fire/dir functions result can be obtained from File.error_id)
* @return pointer to description text
*
* @var FS_Error_Api::get_internal_desc
* @brief Get internal error description text
* @param internal_error_id error id (for fire/dir functions result can be obtained from File.internal_error_id)
* @return pointer to description text
*/
/**
* @brief Errors api structure
*/
typedef struct {
const char* (*get_desc)(FS_Error error_id);
const char* (*get_internal_desc)(uint32_t internal_error_id);
} FS_Error_Api;
/**
* @brief Full filesystem api structure
*/
typedef struct {
FS_File_Api file;
FS_Dir_Api dir;
FS_Common_Api common;
FS_Error_Api error;
} FS_Api;
#ifdef __cplusplus
}
#endif

View File

@@ -1,25 +0,0 @@
#pragma once
#include <furi.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct SdApp SdApp;
typedef struct {
SdApp* context;
bool (*file_select)(
SdApp* context,
const char* path,
const char* extension,
char* result,
uint8_t result_size,
const char* selected_filename);
void (*check_error)(SdApp* context);
void (*show_error)(SdApp* context, const char* error_text);
} SdCard_Api;
#ifdef __cplusplus
}
#endif

View File

@@ -1,48 +0,0 @@
#include <file_reader.h>
std::string FileReader::getline(File* file) {
std::string str;
size_t newline_index = 0;
bool found_eol = false;
bool max_length_exceeded = false;
while(1) {
if(file_buf_cnt > 0) {
size_t end_index = 0;
char* endline_ptr = (char*)memchr(file_buf, '\n', file_buf_cnt);
newline_index = endline_ptr - file_buf;
if(endline_ptr == 0) {
end_index = file_buf_cnt;
} else if(newline_index < file_buf_cnt) {
end_index = newline_index + 1;
found_eol = true;
} else {
furi_assert(0);
}
if (max_line_length && (str.size() + end_index > max_line_length))
max_length_exceeded = true;
if (!max_length_exceeded)
str.append(file_buf, end_index);
memmove(file_buf, &file_buf[end_index], file_buf_cnt - end_index);
file_buf_cnt = file_buf_cnt - end_index;
if(found_eol) break;
}
file_buf_cnt +=
fs_api->file.read(file, &file_buf[file_buf_cnt], sizeof(file_buf) - file_buf_cnt);
if(file_buf_cnt == 0) {
break; // end of reading
}
}
if (max_length_exceeded)
str.clear();
return str;
}

View File

@@ -1,44 +0,0 @@
#pragma once
#include <string>
#include <memory>
#include "sd-card-api.h"
#include "filesystem-api.h"
class FileReader {
private:
char file_buf[48];
size_t file_buf_cnt = 0;
size_t max_line_length = 0;
SdCard_Api* sd_ex_api;
FS_Api* fs_api;
public:
FileReader() {
sd_ex_api = static_cast<SdCard_Api*>(furi_record_open("sdcard-ex"));
fs_api = static_cast<FS_Api*>(furi_record_open("sdcard"));
reset();
}
~FileReader() {
furi_record_close("sdcard");
furi_record_close("sdcard-ex");
}
std::string getline(File* file);
void reset(void) {
file_buf_cnt = 0;
}
SdCard_Api& get_sd_api() {
return *sd_ex_api;
}
FS_Api& get_fs_api() {
return *fs_api;
}
void set_max_line_length(size_t value) {
max_line_length = value;
}
};

View File

@@ -1,7 +1,7 @@
#include "subghz_keystore.h"
#include <furi.h>
#include <filesystem-api.h>
#include <storage/storage.h>
#define FILE_BUFFER_SIZE 64
@@ -52,17 +52,15 @@ static void subghz_keystore_process_line(SubGhzKeystore* instance, string_t line
}
void subghz_keystore_load(SubGhzKeystore* instance, const char* file_name) {
File manufacture_keys_file;
FS_Api* fs_api = furi_record_open("sdcard");
fs_api->file.open(&manufacture_keys_file, file_name, FSAM_READ, FSOM_OPEN_EXISTING);
File* manufacture_keys_file = storage_file_alloc(furi_record_open("storage"));
string_t line;
string_init(line);
if(manufacture_keys_file.error_id == FSE_OK) {
if(storage_file_open(manufacture_keys_file, file_name, FSAM_READ, FSOM_OPEN_EXISTING)) {
printf("Loading manufacture keys file %s\r\n", file_name);
char buffer[FILE_BUFFER_SIZE];
uint16_t ret;
do {
ret = fs_api->file.read(&manufacture_keys_file, buffer, FILE_BUFFER_SIZE);
ret = storage_file_read(manufacture_keys_file, buffer, FILE_BUFFER_SIZE);
for (uint16_t i=0; i < ret; i++) {
if (buffer[i] == '\n' && string_size(line) > 0) {
subghz_keystore_process_line(instance, line);
@@ -76,8 +74,9 @@ void subghz_keystore_load(SubGhzKeystore* instance, const char* file_name) {
printf("Manufacture keys file is not found: %s\r\n", file_name);
}
string_clear(line);
fs_api->file.close(&manufacture_keys_file);
furi_record_close("sdcard");
storage_file_close(manufacture_keys_file);
storage_file_free(manufacture_keys_file);
furi_record_close("storage");
}
SubGhzKeyArray_t* subghz_keystore_get_data(SubGhzKeystore* instance) {