[FL-2499] Folders rename fix (#1190)
* Toolbox: dir_walk concept (like os.walk) * Storage CLI: tree command * Storage: fix folders copying, stage 1 * UnitTest: proper delays in subghz tests * Toolbox: dir_walk, recursive and filter options * dir_walk: unit tests * Merge: Fix unused param * SubGhz: cleaned up data parsing routine * SubGhz unit test: cleaned up logs, yield data load * SubGhz unit test: naming Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
This commit is contained in:
@@ -282,7 +282,7 @@ FS_Error storage_int_restore(Storage* api, const char* dstname);
|
||||
/***************** Simplified Functions ******************/
|
||||
|
||||
/**
|
||||
* Removes a file/directory from the repository, the directory must be empty and the file/directory must not be open
|
||||
* Removes a file/directory, the directory must be empty and the file/directory must not be open
|
||||
* @param storage pointer to the api
|
||||
* @param path
|
||||
* @return true on success or if file/dir is not exist
|
||||
@@ -290,7 +290,7 @@ FS_Error storage_int_restore(Storage* api, const char* dstname);
|
||||
bool storage_simply_remove(Storage* storage, const char* path);
|
||||
|
||||
/**
|
||||
* Removes a file/directory from the repository, the directory can be not empty
|
||||
* Recursively removes a file/directory, the directory can be not empty
|
||||
* @param storage pointer to the api
|
||||
* @param path
|
||||
* @return true on success or if file/dir is not exist
|
||||
|
@@ -4,6 +4,7 @@
|
||||
#include <cli/cli.h>
|
||||
#include <lib/toolbox/args.h>
|
||||
#include <lib/toolbox/md5.h>
|
||||
#include <lib/toolbox/dir_walk.h>
|
||||
#include <storage/storage.h>
|
||||
#include <storage/storage_sd_api.h>
|
||||
#include <power/power_service/power.h>
|
||||
@@ -18,6 +19,7 @@ static void storage_cli_print_usage() {
|
||||
printf("\tinfo\t - get FS info\r\n");
|
||||
printf("\tformat\t - format filesystem\r\n");
|
||||
printf("\tlist\t - list files and dirs\r\n");
|
||||
printf("\ttree\t - list files and dirs, recursive\r\n");
|
||||
printf("\tremove\t - delete the file or directory\r\n");
|
||||
printf("\tread\t - read text from file and print file size and content to cli\r\n");
|
||||
printf(
|
||||
@@ -138,6 +140,44 @@ static void storage_cli_list(Cli* cli, string_t path) {
|
||||
}
|
||||
}
|
||||
|
||||
static void storage_cli_tree(Cli* cli, string_t path) {
|
||||
if(string_cmp_str(path, "/") == 0) {
|
||||
string_set(path, "/int");
|
||||
storage_cli_tree(cli, path);
|
||||
string_set(path, "/ext");
|
||||
storage_cli_tree(cli, path);
|
||||
} else {
|
||||
Storage* api = furi_record_open("storage");
|
||||
DirWalk* dir_walk = dir_walk_alloc(api);
|
||||
string_t name;
|
||||
string_init(name);
|
||||
|
||||
if(dir_walk_open(dir_walk, string_get_cstr(path))) {
|
||||
FileInfo fileinfo;
|
||||
bool read_done = false;
|
||||
|
||||
while(dir_walk_read(dir_walk, name, &fileinfo) == DirWalkOK) {
|
||||
read_done = true;
|
||||
if(fileinfo.flags & FSF_DIRECTORY) {
|
||||
printf("\t[D] %s\r\n", string_get_cstr(name));
|
||||
} else {
|
||||
printf("\t[F] %s %lub\r\n", string_get_cstr(name), (uint32_t)(fileinfo.size));
|
||||
}
|
||||
}
|
||||
|
||||
if(!read_done) {
|
||||
printf("\tEmpty\r\n");
|
||||
}
|
||||
} else {
|
||||
storage_cli_print_error(dir_walk_get_error(dir_walk));
|
||||
}
|
||||
|
||||
string_clear(name);
|
||||
dir_walk_free(dir_walk);
|
||||
furi_record_close("storage");
|
||||
}
|
||||
}
|
||||
|
||||
static void storage_cli_read(Cli* cli, string_t path) {
|
||||
UNUSED(cli);
|
||||
Storage* api = furi_record_open("storage");
|
||||
@@ -474,6 +514,11 @@ void storage_cli(Cli* cli, string_t args, void* context) {
|
||||
break;
|
||||
}
|
||||
|
||||
if(string_cmp_str(cmd, "tree") == 0) {
|
||||
storage_cli_tree(cli, path);
|
||||
break;
|
||||
}
|
||||
|
||||
if(string_cmp_str(cmd, "read") == 0) {
|
||||
storage_cli_read(cli, path);
|
||||
break;
|
||||
|
@@ -4,6 +4,7 @@
|
||||
#include "storage_i.h"
|
||||
#include "storage_message.h"
|
||||
#include <toolbox/stream/file_stream.h>
|
||||
#include <toolbox/dir_walk.h>
|
||||
|
||||
#define MAX_NAME_LENGTH 256
|
||||
|
||||
@@ -332,12 +333,69 @@ 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) {
|
||||
error = storage_common_remove(storage, old_path);
|
||||
if(storage_simply_remove_recursive(storage, old_path)) {
|
||||
error = FSE_OK;
|
||||
} else {
|
||||
error = FSE_INTERNAL;
|
||||
}
|
||||
}
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
static FS_Error
|
||||
storage_copy_recursive(Storage* storage, const char* old_path, const char* new_path) {
|
||||
FS_Error error = storage_common_mkdir(storage, new_path);
|
||||
DirWalk* dir_walk = dir_walk_alloc(storage);
|
||||
string_t path;
|
||||
string_t tmp_new_path;
|
||||
string_t tmp_old_path;
|
||||
FileInfo fileinfo;
|
||||
string_init(path);
|
||||
string_init(tmp_new_path);
|
||||
string_init(tmp_old_path);
|
||||
|
||||
do {
|
||||
if(error != FSE_OK) break;
|
||||
|
||||
if(!dir_walk_open(dir_walk, old_path)) {
|
||||
error = dir_walk_get_error(dir_walk);
|
||||
break;
|
||||
}
|
||||
|
||||
while(1) {
|
||||
DirWalkResult res = dir_walk_read(dir_walk, path, &fileinfo);
|
||||
|
||||
if(res == DirWalkError) {
|
||||
error = dir_walk_get_error(dir_walk);
|
||||
break;
|
||||
} else if(res == DirWalkLast) {
|
||||
break;
|
||||
} else {
|
||||
string_set(tmp_old_path, path);
|
||||
string_right(path, strlen(old_path));
|
||||
string_printf(tmp_new_path, "%s%s", new_path, string_get_cstr(path));
|
||||
|
||||
if(fileinfo.flags & FSF_DIRECTORY) {
|
||||
error = storage_common_mkdir(storage, string_get_cstr(tmp_new_path));
|
||||
} else {
|
||||
error = storage_common_copy(
|
||||
storage, string_get_cstr(tmp_old_path), string_get_cstr(tmp_new_path));
|
||||
}
|
||||
|
||||
if(error != FSE_OK) break;
|
||||
}
|
||||
}
|
||||
|
||||
} while(false);
|
||||
|
||||
string_clear(tmp_new_path);
|
||||
string_clear(tmp_old_path);
|
||||
string_clear(path);
|
||||
dir_walk_free(dir_walk);
|
||||
return error;
|
||||
}
|
||||
|
||||
FS_Error storage_common_copy(Storage* storage, const char* old_path, const char* new_path) {
|
||||
FS_Error error;
|
||||
|
||||
@@ -346,7 +404,7 @@ FS_Error storage_common_copy(Storage* storage, const char* old_path, const char*
|
||||
|
||||
if(error == FSE_OK) {
|
||||
if(fileinfo.flags & FSF_DIRECTORY) {
|
||||
error = storage_common_mkdir(storage, new_path);
|
||||
error = storage_copy_recursive(storage, old_path, new_path);
|
||||
} else {
|
||||
Stream* stream_from = file_stream_alloc(storage);
|
||||
Stream* stream_to = file_stream_alloc(storage);
|
||||
|
Reference in New Issue
Block a user