From 88f0b635774afe76f15bb72a469e22d4449d7e35 Mon Sep 17 00:00:00 2001 From: Sergey Gavrilov Date: Thu, 25 May 2023 06:44:32 -0700 Subject: [PATCH] Storage, common_rename: check that old path is exists (#2698) * Storage, common_rename: check that old path is exists * Storage, common_rename: return correct status --- .../services/storage/storage_external_api.c | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/applications/services/storage/storage_external_api.c b/applications/services/storage/storage_external_api.c index 549397c87..5fcaa5921 100644 --- a/applications/services/storage/storage_external_api.c +++ b/applications/services/storage/storage_external_api.c @@ -424,19 +424,25 @@ FS_Error storage_common_remove(Storage* storage, const char* path) { FS_Error storage_common_rename(Storage* storage, const char* old_path, const char* new_path) { FS_Error error; - if(storage_file_exists(storage, new_path)) { - error = storage_common_remove(storage, new_path); - if(error != FSE_OK) { - return error; + do { + if(!storage_common_exists(storage, old_path)) { + error = FSE_INVALID_NAME; + break; + } + + if(storage_file_exists(storage, new_path)) { + storage_common_remove(storage, new_path); + } + + error = storage_common_copy(storage, old_path, new_path); + if(error != FSE_OK) { + break; } - } - error = storage_common_copy(storage, old_path, new_path); - if(error == FSE_OK) { if(!storage_simply_remove_recursive(storage, old_path)) { error = FSE_INTERNAL; } - } + } while(false); return error; }