[FL-2423] Storage: blocking dir open (#1083)
* Storage: more unit tests * Storage: blocking dir open, differentiate file and dir when freed. Co-authored-by: あく <alleteam@gmail.com>
This commit is contained in:
@@ -24,6 +24,7 @@ typedef enum {
|
||||
StorageEventTypeCardUnmount,
|
||||
StorageEventTypeCardMountError,
|
||||
StorageEventTypeFileClose,
|
||||
StorageEventTypeDirClose,
|
||||
} StorageEventType;
|
||||
|
||||
typedef struct {
|
||||
@@ -65,6 +66,12 @@ bool storage_file_close(File* file);
|
||||
*/
|
||||
bool storage_file_is_open(File* file);
|
||||
|
||||
/** Tells if the file is a directory
|
||||
* @param file pointer to a file object
|
||||
* @return bool true if file is a directory
|
||||
*/
|
||||
bool storage_file_is_dir(File* file);
|
||||
|
||||
/** Reads bytes from a file into a buffer
|
||||
* @param file pointer to file object.
|
||||
* @param buff pointer to a buffer, for reading
|
||||
|
@@ -47,7 +47,8 @@
|
||||
#define S_RETURN_ERROR (return_data.error_value);
|
||||
#define S_RETURN_CSTRING (return_data.cstring_value);
|
||||
|
||||
#define FILE_OPENED 1
|
||||
#define FILE_OPENED_FILE 1
|
||||
#define FILE_OPENED_DIR 2
|
||||
#define FILE_CLOSED 0
|
||||
|
||||
typedef enum {
|
||||
@@ -71,7 +72,7 @@ static bool storage_file_open_internal(
|
||||
.open_mode = open_mode,
|
||||
}};
|
||||
|
||||
file->file_id = FILE_OPENED;
|
||||
file->file_id = FILE_OPENED_FILE;
|
||||
|
||||
S_API_MESSAGE(StorageCommandFileOpen);
|
||||
S_API_EPILOGUE;
|
||||
@@ -82,7 +83,8 @@ static bool storage_file_open_internal(
|
||||
static void storage_file_close_callback(const void* message, void* context) {
|
||||
const StorageEvent* storage_event = message;
|
||||
|
||||
if(storage_event->type == StorageEventTypeFileClose) {
|
||||
if(storage_event->type == StorageEventTypeFileClose ||
|
||||
storage_event->type == StorageEventTypeDirClose) {
|
||||
furi_assert(context);
|
||||
osEventFlagsId_t event = context;
|
||||
osEventFlagsSet(event, StorageEventFlagFileClose);
|
||||
@@ -222,7 +224,7 @@ bool storage_file_eof(File* file) {
|
||||
|
||||
/****************** DIR ******************/
|
||||
|
||||
bool storage_dir_open(File* file, const char* path) {
|
||||
static bool storage_dir_open_internal(File* file, const char* path) {
|
||||
S_FILE_API_PROLOGUE;
|
||||
S_API_PROLOGUE;
|
||||
|
||||
@@ -232,13 +234,34 @@ bool storage_dir_open(File* file, const char* path) {
|
||||
.path = path,
|
||||
}};
|
||||
|
||||
file->file_id = FILE_OPENED;
|
||||
file->file_id = FILE_OPENED_DIR;
|
||||
|
||||
S_API_MESSAGE(StorageCommandDirOpen);
|
||||
S_API_EPILOGUE;
|
||||
return S_RETURN_BOOL;
|
||||
}
|
||||
|
||||
bool storage_dir_open(File* file, const char* path) {
|
||||
bool result;
|
||||
osEventFlagsId_t event = osEventFlagsNew(NULL);
|
||||
FuriPubSubSubscription* subscription = furi_pubsub_subscribe(
|
||||
storage_get_pubsub(file->storage), storage_file_close_callback, event);
|
||||
|
||||
do {
|
||||
result = storage_dir_open_internal(file, path);
|
||||
|
||||
if(!result && file->error_id == FSE_ALREADY_OPEN) {
|
||||
osEventFlagsWait(event, StorageEventFlagFileClose, osFlagsWaitAny, osWaitForever);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
} while(true);
|
||||
|
||||
furi_pubsub_unsubscribe(storage_get_pubsub(file->storage), subscription);
|
||||
osEventFlagsDelete(event);
|
||||
return result;
|
||||
}
|
||||
|
||||
bool storage_dir_close(File* file) {
|
||||
S_FILE_API_PROLOGUE;
|
||||
S_API_PROLOGUE;
|
||||
@@ -435,9 +458,17 @@ bool storage_file_is_open(File* file) {
|
||||
return (file->file_id != FILE_CLOSED);
|
||||
}
|
||||
|
||||
bool storage_file_is_dir(File* file) {
|
||||
return (file->file_id == FILE_OPENED_DIR);
|
||||
}
|
||||
|
||||
void storage_file_free(File* file) {
|
||||
if(storage_file_is_open(file)) {
|
||||
storage_file_close(file);
|
||||
if(storage_file_is_dir(file)) {
|
||||
storage_dir_close(file);
|
||||
} else {
|
||||
storage_file_close(file);
|
||||
}
|
||||
}
|
||||
|
||||
free(file);
|
||||
|
@@ -285,6 +285,9 @@ bool storage_process_dir_close(Storage* app, File* file) {
|
||||
} else {
|
||||
FS_CALL(storage, dir.close(storage, file));
|
||||
storage_pop_storage_file(file, storage);
|
||||
|
||||
StorageEvent event = {.type = StorageEventTypeDirClose};
|
||||
furi_pubsub_publish(app->pubsub, &event);
|
||||
}
|
||||
|
||||
return ret;
|
||||
|
Reference in New Issue
Block a user