[FL-2415] Storage: blocking file open (#1078)

* Storage: correct replacement for "/any" path in path holder
* Unit tests: storage, blocking file open test
* File stream: error getter
* Storage: common copy and common remove now executes in external thread
* Filesystems: got rid of unused functions
* Storage: untangle dependencies, ram-frendly filesystem api
* iButton: context assertions
* Storage: pubsub messages
* Storage: wait for the file to close if it was open
* Storage: fix folder copying
* Storage: unit test
* Storage: pubsub documentation
* Fix merge error
* Fix memleak in storage test
* Storage: remove unused define

Co-authored-by: あく <alleteam@gmail.com>
This commit is contained in:
SG
2022-04-01 22:21:31 +10:00
committed by GitHub
parent cb7d43f7e1
commit 855f2584ab
37 changed files with 443 additions and 281 deletions

View File

@@ -0,0 +1,80 @@
#include "../minunit.h"
#include <furi.h>
#include <furi_hal_delay.h>
#include <storage/storage.h>
#define STORAGE_LOCKED_FILE "/ext/locked_file.test"
static void storage_file_open_lock_setup() {
Storage* storage = furi_record_open("storage");
File* file = storage_file_alloc(storage);
storage_simply_remove(storage, STORAGE_LOCKED_FILE);
mu_check(storage_file_open(file, STORAGE_LOCKED_FILE, FSAM_WRITE, FSOM_CREATE_NEW));
mu_check(storage_file_write(file, "0123", 4) == 4);
mu_check(storage_file_close(file));
storage_file_free(file);
furi_record_close("storage");
}
static void storage_file_open_lock_teardown() {
Storage* storage = furi_record_open("storage");
mu_check(storage_simply_remove(storage, STORAGE_LOCKED_FILE));
furi_record_close("storage");
}
static int32_t storage_file_locker(void* ctx) {
Storage* storage = furi_record_open("storage");
osSemaphoreId_t semaphore = ctx;
File* file = storage_file_alloc(storage);
furi_check(storage_file_open(file, STORAGE_LOCKED_FILE, FSAM_READ_WRITE, FSOM_OPEN_EXISTING));
osSemaphoreRelease(semaphore);
furi_hal_delay_ms(1000);
furi_check(storage_file_close(file));
furi_record_close("storage");
storage_file_free(file);
return 0;
}
MU_TEST(storage_file_open_lock) {
Storage* storage = furi_record_open("storage");
bool result = false;
osSemaphoreId_t semaphore = osSemaphoreNew(1, 0, NULL);
File* file = storage_file_alloc(storage);
// file_locker thread start
FuriThread* locker_thread = furi_thread_alloc();
furi_thread_set_name(locker_thread, "StorageFileLocker");
furi_thread_set_stack_size(locker_thread, 2048);
furi_thread_set_context(locker_thread, semaphore);
furi_thread_set_callback(locker_thread, storage_file_locker);
mu_check(furi_thread_start(locker_thread));
// wait for file lock
osSemaphoreAcquire(semaphore, osWaitForever);
osSemaphoreDelete(semaphore);
result = storage_file_open(file, STORAGE_LOCKED_FILE, FSAM_READ_WRITE, FSOM_OPEN_EXISTING);
storage_file_close(file);
// file_locker thread stop
mu_check(furi_thread_join(locker_thread) == osOK);
furi_thread_free(locker_thread);
// clean data
storage_file_free(file);
furi_record_close("storage");
mu_assert(result, "cannot open locked file");
}
MU_TEST_SUITE(storage_file) {
storage_file_open_lock_setup();
MU_RUN_TEST(storage_file_open_lock);
storage_file_open_lock_teardown();
}
int run_minunit_test_storage() {
MU_RUN_SUITE(storage_file);
return MU_EXIT_CODE;
}

View File

@@ -16,6 +16,7 @@ int run_minunit_test_rpc();
int run_minunit_test_flipper_format();
int run_minunit_test_flipper_format_string();
int run_minunit_test_stream();
int run_minunit_test_storage();
void minunit_print_progress(void) {
static char progress[] = {'\\', '|', '/', '-'};
@@ -53,11 +54,12 @@ void unit_tests_cli(Cli* cli, string_t args, void* context) {
uint32_t cycle_counter = DWT->CYCCNT;
test_result |= run_minunit();
test_result |= run_minunit_test_infrared_decoder_encoder();
test_result |= run_minunit_test_rpc();
test_result |= run_minunit_test_storage();
test_result |= run_minunit_test_stream();
test_result |= run_minunit_test_flipper_format();
test_result |= run_minunit_test_flipper_format_string();
test_result |= run_minunit_test_infrared_decoder_encoder();
test_result |= run_minunit_test_rpc();
cycle_counter = (DWT->CYCCNT - cycle_counter);
FURI_LOG_I(TAG, "Consumed: %0.2fs", (float)cycle_counter / (SystemCoreClock));