OFW be like rEnAmE = cOpY + dElEtE, bro stfu :/

This commit is contained in:
Willy-JL
2023-05-15 01:31:02 +01:00
parent c5bf0763a5
commit c6eff58890
6 changed files with 84 additions and 7 deletions

View File

@@ -169,6 +169,7 @@ typedef struct {
typedef struct {
FS_Error (*const stat)(void* context, const char* path, FileInfo* fileinfo);
FS_Error (*const remove)(void* context, const char* path);
FS_Error (*const rename)(void* context, const char* old, const char* new);
FS_Error (*const mkdir)(void* context, const char* path);
FS_Error (*const fs_info)(
void* context,

View File

@@ -422,14 +422,17 @@ 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 = storage_common_copy(storage, old_path, new_path);
if(error == FSE_OK) {
if(!storage_simply_remove_recursive(storage, old_path)) {
error = FSE_INTERNAL;
}
}
S_API_PROLOGUE;
SAData data = {
.rename = {
.old = old_path,
.new = new_path,
.thread_id = furi_thread_get_current_id(),
}};
return error;
S_API_MESSAGE(StorageCommandCommonRename);
S_API_EPILOGUE;
return S_RETURN_ERROR;
}
static FS_Error

View File

@@ -78,6 +78,12 @@ typedef struct {
FuriThreadId thread_id;
} SADataPath;
typedef struct {
const char* old;
const char* new;
FuriThreadId thread_id;
} SADataRename;
typedef struct {
File* file;
} SADataFile;
@@ -104,6 +110,7 @@ typedef union {
SADataFile file;
SADataPath path;
SADataRename rename;
SAInfo sdinfo;
} SAData;
@@ -134,6 +141,7 @@ typedef enum {
StorageCommandCommonTimestamp,
StorageCommandCommonStat,
StorageCommandCommonRemove,
StorageCommandCommonRename,
StorageCommandCommonMkDir,
StorageCommandCommonFSInfo,
StorageCommandSDFormat,

View File

@@ -369,6 +369,39 @@ static FS_Error storage_process_common_remove(Storage* app, FuriString* path) {
return ret;
}
static FS_Error storage_process_common_rename(Storage* app, FuriString* old, FuriString* new) {
FS_Error ret;
// Paths are already resolved, no aliases
if(strncmp(furi_string_get_cstr(old), furi_string_get_cstr(new), STORAGE_PATH_PREFIX_LEN)) {
// Different filesystems, use copy + remove
ret = storage_common_copy(app, furi_string_get_cstr(old), furi_string_get_cstr(new));
if(ret == FSE_OK) {
if(!storage_simply_remove_recursive(app, furi_string_get_cstr(old))) {
ret = FSE_INTERNAL;
}
}
} else {
// Same filesystem, use rename
StorageData* storage;
ret = storage_get_data(app, old, &storage);
do {
if(storage_path_already_open(old, storage)) {
ret = FSE_ALREADY_OPEN;
break;
}
storage_data_timestamp(storage);
FS_CALL(
storage,
common.rename(
storage, cstr_path_without_vfs_prefix(old), cstr_path_without_vfs_prefix(new)));
} while(false);
}
return ret;
}
static FS_Error storage_process_common_mkdir(Storage* app, FuriString* path) {
StorageData* storage;
FS_Error ret = storage_get_data(app, path, &storage);
@@ -514,6 +547,7 @@ void storage_process_alias(
void storage_process_message_internal(Storage* app, StorageMessage* message) {
FuriString* path = NULL;
FuriString* opath = NULL;
switch(message->command) {
// File operations
@@ -614,6 +648,13 @@ void storage_process_message_internal(Storage* app, StorageMessage* message) {
storage_process_alias(app, path, message->data->path.thread_id, false);
message->return_data->error_value = storage_process_common_remove(app, path);
break;
case StorageCommandCommonRename:
opath = furi_string_alloc_set(message->data->rename.old);
storage_process_alias(app, opath, message->data->rename.thread_id, false);
path = furi_string_alloc_set(message->data->rename.new);
storage_process_alias(app, path, message->data->rename.thread_id, false);
message->return_data->error_value = storage_process_common_rename(app, opath, path);
break;
case StorageCommandCommonMkDir:
path = furi_string_alloc_set(message->data->path.path);
storage_process_alias(app, path, message->data->path.thread_id, true);
@@ -649,6 +690,9 @@ void storage_process_message_internal(Storage* app, StorageMessage* message) {
if(path != NULL) { //-V547
furi_string_free(path);
}
if(opath != NULL) { //-V547
furi_string_free(opath);
}
api_lock_unlock(message->lock);
}

View File

@@ -535,6 +535,18 @@ static FS_Error storage_ext_common_remove(void* ctx, const char* path) {
#endif
}
static FS_Error storage_ext_common_rename(void* ctx, const char* old, const char* new) {
UNUSED(ctx);
#ifdef FURI_RAM_EXEC
UNUSED(old);
UNUSED(new);
return FSE_NOT_READY;
#else
SDError result = f_rename(old, new);
return storage_ext_parse_error(result);
#endif
}
static FS_Error storage_ext_common_mkdir(void* ctx, const char* path) {
UNUSED(ctx);
#ifdef FURI_RAM_EXEC
@@ -614,6 +626,7 @@ static const FS_Api fs_api = {
.stat = storage_ext_common_stat,
.mkdir = storage_ext_common_mkdir,
.remove = storage_ext_common_remove,
.rename = storage_ext_common_rename,
.fs_info = storage_ext_common_fs_info,
},
};

View File

@@ -689,6 +689,13 @@ static FS_Error storage_int_common_remove(void* ctx, const char* path) {
return storage_int_parse_error(result);
}
static FS_Error storage_int_common_rename(void* ctx, const char* old, const char* new) { // FIXME
StorageData* storage = ctx;
lfs_t* lfs = lfs_get_from_storage(storage);
int result = lfs_rename(lfs, old, new);
return storage_int_parse_error(result);
}
static FS_Error storage_int_common_mkdir(void* ctx, const char* path) {
StorageData* storage = ctx;
lfs_t* lfs = lfs_get_from_storage(storage);
@@ -746,6 +753,7 @@ static const FS_Api fs_api = {
.stat = storage_int_common_stat,
.mkdir = storage_int_common_mkdir,
.remove = storage_int_common_remove,
.rename = storage_int_common_rename,
.fs_info = storage_int_common_fs_info,
},
};