This commit is contained in:
ClaraCrazy
2023-07-18 01:55:25 +02:00
2 changed files with 70 additions and 26 deletions
@@ -29,6 +29,7 @@ static void storage_cli_print_usage() {
"\twrite_chunk\t - read data from cli and append it to file, <args> should contain how many bytes you want to write\r\n");
printf("\tcopy\t - copy file to new file, <args> must contain new path\r\n");
printf("\trename\t - move file to new file, <args> must contain new path\r\n");
printf("\tmigrate\t - \r\n");
printf("\tmkdir\t - creates a new directory\r\n");
printf("\tmd5\t - md5 hash of the file\r\n");
printf("\tstat\t - info about file or dir\r\n");
@@ -466,6 +467,27 @@ static void storage_cli_rename(Cli* cli, FuriString* old_path, FuriString* args)
furi_record_close(RECORD_STORAGE);
}
static void storage_cli_migrate(Cli* cli, FuriString* old_path, FuriString* args) {
UNUSED(cli);
Storage* api = furi_record_open(RECORD_STORAGE);
FuriString* new_path;
new_path = furi_string_alloc();
if(!args_read_probably_quoted_string_and_trim(args, new_path)) {
storage_cli_print_usage();
} else {
FS_Error error = storage_common_migrate(
api, furi_string_get_cstr(old_path), furi_string_get_cstr(new_path));
if(error != FSE_OK) {
storage_cli_print_error(error);
}
}
furi_string_free(new_path);
furi_record_close(RECORD_STORAGE);
}
static void storage_cli_mkdir(Cli* cli, FuriString* path) {
UNUSED(cli);
Storage* api = furi_record_open(RECORD_STORAGE);
@@ -589,6 +611,11 @@ void storage_cli(Cli* cli, FuriString* args, void* context) {
break;
}
if(furi_string_cmp_str(cmd, "migrate") == 0) {
storage_cli_migrate(cli, path, args);
break;
}
if(furi_string_cmp_str(cmd, "mkdir") == 0) {
storage_cli_mkdir(cli, path);
break;
@@ -593,8 +593,13 @@ FS_Error storage_common_copy(Storage* storage, const char* old_path, const char*
return error;
}
static FS_Error
storage_merge_recursive(Storage* storage, const char* old_path, const char* new_path) {
static FS_Error _storage_common_merge(Storage*, const char*, const char*, bool);
static FS_Error storage_merge_recursive(
Storage* storage,
const char* old_path,
const char* new_path,
bool copy) {
FS_Error error = FSE_OK;
DirWalk* dir_walk = dir_walk_alloc(storage);
FuriString *path, *file_basename, *tmp_new_path;
@@ -636,8 +641,8 @@ static FS_Error
}
}
}
error = storage_common_merge(
storage, furi_string_get_cstr(path), furi_string_get_cstr(tmp_new_path));
error = _storage_common_merge(
storage, furi_string_get_cstr(path), furi_string_get_cstr(tmp_new_path), copy);
if(error != FSE_OK) {
break;
@@ -654,7 +659,8 @@ static FS_Error
return error;
}
FS_Error storage_common_merge(Storage* storage, const char* old_path, const char* new_path) {
static FS_Error
_storage_common_merge(Storage* storage, const char* old_path, const char* new_path, bool copy) {
FS_Error error;
const char* new_path_tmp = NULL;
FuriString* new_path_next = NULL;
@@ -665,7 +671,12 @@ FS_Error storage_common_merge(Storage* storage, const char* old_path, const char
if(error == FSE_OK) {
if(file_info_is_dir(&fileinfo)) {
error = storage_merge_recursive(storage, old_path, new_path);
if(!copy) {
error = storage_common_rename(storage, old_path, new_path);
}
if(copy || error != FSE_OK) {
error = storage_merge_recursive(storage, old_path, new_path, copy);
}
} else {
error = storage_common_stat(storage, new_path, &fileinfo);
if(error == FSE_OK) {
@@ -699,22 +710,28 @@ FS_Error storage_common_merge(Storage* storage, const char* old_path, const char
} else {
new_path_tmp = new_path;
}
Stream* stream_from = file_stream_alloc(storage);
Stream* stream_to = file_stream_alloc(storage);
if(copy) {
Stream* stream_from = file_stream_alloc(storage);
Stream* stream_to = file_stream_alloc(storage);
do {
if(!file_stream_open(stream_from, old_path, FSAM_READ, FSOM_OPEN_EXISTING)) break;
if(!file_stream_open(stream_to, new_path_tmp, FSAM_WRITE, FSOM_CREATE_NEW)) break;
stream_copy_full(stream_from, stream_to);
} while(false);
do {
if(!file_stream_open(stream_from, old_path, FSAM_READ, FSOM_OPEN_EXISTING))
break;
if(!file_stream_open(stream_to, new_path_tmp, FSAM_WRITE, FSOM_CREATE_NEW))
break;
stream_copy_full(stream_from, stream_to);
} while(false);
error = file_stream_get_error(stream_from);
if(error == FSE_OK) {
error = file_stream_get_error(stream_to);
error = file_stream_get_error(stream_from);
if(error == FSE_OK) {
error = file_stream_get_error(stream_to);
}
stream_free(stream_from);
stream_free(stream_to);
} else {
error = storage_common_rename(storage, old_path, new_path_tmp);
}
stream_free(stream_from);
stream_free(stream_to);
}
}
@@ -723,6 +740,10 @@ FS_Error storage_common_merge(Storage* storage, const char* old_path, const char
return error;
}
FS_Error storage_common_merge(Storage* storage, const char* old_path, const char* new_path) {
return _storage_common_merge(storage, old_path, new_path, true);
}
FS_Error storage_common_mkdir(Storage* storage, const char* path) {
S_API_PROLOGUE;
SAData data = {
@@ -774,14 +795,10 @@ FS_Error storage_common_migrate(Storage* storage, const char* source, const char
return FSE_OK;
}
FS_Error error = storage_common_rename(storage, source, dest);
FS_Error error = _storage_common_merge(storage, source, dest, false);
if(error != FSE_OK) {
error = storage_common_merge(storage, source, dest);
if(error == FSE_OK) {
storage_simply_remove_recursive(storage, source);
}
if(error == FSE_OK) {
storage_simply_remove_recursive(storage, source);
}
return error;