2022-05-10 17:05:36 +03:00
|
|
|
#include "dir_walk.h"
|
|
|
|
#include <m-list.h>
|
|
|
|
|
|
|
|
LIST_DEF(DirIndexList, uint32_t);
|
|
|
|
|
|
|
|
struct DirWalk {
|
|
|
|
File* file;
|
2022-10-05 18:15:23 +03:00
|
|
|
FuriString* path;
|
2022-05-10 17:05:36 +03:00
|
|
|
DirIndexList_t index_list;
|
|
|
|
uint32_t current_index;
|
|
|
|
bool recursive;
|
|
|
|
DirWalkFilterCb filter_cb;
|
|
|
|
void* filter_context;
|
|
|
|
};
|
|
|
|
|
|
|
|
DirWalk* dir_walk_alloc(Storage* storage) {
|
2024-03-19 17:43:52 +03:00
|
|
|
furi_check(storage);
|
|
|
|
|
2022-05-10 17:05:36 +03:00
|
|
|
DirWalk* dir_walk = malloc(sizeof(DirWalk));
|
2022-10-05 18:15:23 +03:00
|
|
|
dir_walk->path = furi_string_alloc();
|
2022-05-10 17:05:36 +03:00
|
|
|
dir_walk->file = storage_file_alloc(storage);
|
|
|
|
DirIndexList_init(dir_walk->index_list);
|
|
|
|
dir_walk->recursive = true;
|
|
|
|
dir_walk->filter_cb = NULL;
|
|
|
|
return dir_walk;
|
|
|
|
}
|
|
|
|
|
|
|
|
void dir_walk_free(DirWalk* dir_walk) {
|
2024-03-19 17:43:52 +03:00
|
|
|
furi_check(dir_walk);
|
|
|
|
|
2022-05-10 17:05:36 +03:00
|
|
|
storage_file_free(dir_walk->file);
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_free(dir_walk->path);
|
2022-05-10 17:05:36 +03:00
|
|
|
DirIndexList_clear(dir_walk->index_list);
|
|
|
|
free(dir_walk);
|
|
|
|
}
|
|
|
|
|
|
|
|
void dir_walk_set_recursive(DirWalk* dir_walk, bool recursive) {
|
2024-03-19 17:43:52 +03:00
|
|
|
furi_check(dir_walk);
|
2022-05-10 17:05:36 +03:00
|
|
|
dir_walk->recursive = recursive;
|
|
|
|
}
|
|
|
|
|
|
|
|
void dir_walk_set_filter_cb(DirWalk* dir_walk, DirWalkFilterCb cb, void* context) {
|
2024-03-19 17:43:52 +03:00
|
|
|
furi_check(dir_walk);
|
2022-05-10 17:05:36 +03:00
|
|
|
dir_walk->filter_cb = cb;
|
|
|
|
dir_walk->filter_context = context;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool dir_walk_open(DirWalk* dir_walk, const char* path) {
|
2024-03-19 17:43:52 +03:00
|
|
|
furi_check(dir_walk);
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_set(dir_walk->path, path);
|
2022-05-10 17:05:36 +03:00
|
|
|
dir_walk->current_index = 0;
|
|
|
|
return storage_dir_open(dir_walk->file, path);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool dir_walk_filter(DirWalk* dir_walk, const char* name, FileInfo* fileinfo) {
|
|
|
|
if(dir_walk->filter_cb) {
|
|
|
|
return dir_walk->filter_cb(name, fileinfo, dir_walk->filter_context);
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
static DirWalkResult
|
|
|
|
dir_walk_iter(DirWalk* dir_walk, FuriString* return_path, FileInfo* fileinfo) {
|
2022-05-10 17:05:36 +03:00
|
|
|
DirWalkResult result = DirWalkError;
|
2022-07-26 15:21:51 +03:00
|
|
|
char* name = malloc(256); // FIXME: remove magic number
|
2022-05-10 17:05:36 +03:00
|
|
|
FileInfo info;
|
|
|
|
bool end = false;
|
|
|
|
|
|
|
|
while(!end) {
|
|
|
|
storage_dir_read(dir_walk->file, &info, name, 255);
|
|
|
|
|
|
|
|
if(storage_file_get_error(dir_walk->file) == FSE_OK) {
|
|
|
|
result = DirWalkOK;
|
|
|
|
dir_walk->current_index++;
|
|
|
|
|
|
|
|
if(dir_walk_filter(dir_walk, name, &info)) {
|
|
|
|
if(return_path != NULL) {
|
2022-12-26 15:13:30 +03:00
|
|
|
furi_string_printf( //-V576
|
|
|
|
return_path,
|
|
|
|
"%s/%s",
|
|
|
|
furi_string_get_cstr(dir_walk->path),
|
|
|
|
name);
|
2022-05-10 17:05:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if(fileinfo != NULL) {
|
|
|
|
memcpy(fileinfo, &info, sizeof(FileInfo));
|
|
|
|
}
|
|
|
|
|
|
|
|
end = true;
|
|
|
|
}
|
|
|
|
|
2023-03-01 20:57:27 +03:00
|
|
|
if(file_info_is_dir(&info) && dir_walk->recursive) {
|
2022-05-10 17:05:36 +03:00
|
|
|
// step into
|
|
|
|
DirIndexList_push_back(dir_walk->index_list, dir_walk->current_index);
|
|
|
|
dir_walk->current_index = 0;
|
|
|
|
storage_dir_close(dir_walk->file);
|
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_cat_printf(dir_walk->path, "/%s", name);
|
|
|
|
storage_dir_open(dir_walk->file, furi_string_get_cstr(dir_walk->path));
|
2022-05-10 17:05:36 +03:00
|
|
|
}
|
|
|
|
} else if(storage_file_get_error(dir_walk->file) == FSE_NOT_EXIST) {
|
|
|
|
if(DirIndexList_size(dir_walk->index_list) == 0) {
|
|
|
|
// last
|
|
|
|
result = DirWalkLast;
|
|
|
|
end = true;
|
|
|
|
} else {
|
|
|
|
// step out
|
|
|
|
uint32_t index;
|
|
|
|
DirIndexList_pop_back(&index, dir_walk->index_list);
|
|
|
|
dir_walk->current_index = 0;
|
|
|
|
|
|
|
|
storage_dir_close(dir_walk->file);
|
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
size_t last_char = furi_string_search_rchar(dir_walk->path, '/');
|
|
|
|
if(last_char != FURI_STRING_FAILURE) {
|
|
|
|
furi_string_left(dir_walk->path, last_char);
|
2022-05-10 17:05:36 +03:00
|
|
|
}
|
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
storage_dir_open(dir_walk->file, furi_string_get_cstr(dir_walk->path));
|
2022-05-10 17:05:36 +03:00
|
|
|
|
|
|
|
// rewind
|
|
|
|
while(true) {
|
|
|
|
if(index == dir_walk->current_index) {
|
|
|
|
result = DirWalkOK;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!storage_dir_read(dir_walk->file, &info, name, 255)) {
|
|
|
|
result = DirWalkError;
|
|
|
|
end = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
dir_walk->current_index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
result = DirWalkError;
|
|
|
|
end = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(name);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
FS_Error dir_walk_get_error(DirWalk* dir_walk) {
|
2024-03-19 17:43:52 +03:00
|
|
|
furi_check(dir_walk);
|
2022-05-10 17:05:36 +03:00
|
|
|
return storage_file_get_error(dir_walk->file);
|
|
|
|
}
|
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
DirWalkResult dir_walk_read(DirWalk* dir_walk, FuriString* return_path, FileInfo* fileinfo) {
|
2024-03-19 17:43:52 +03:00
|
|
|
furi_check(dir_walk);
|
2022-05-10 17:05:36 +03:00
|
|
|
return dir_walk_iter(dir_walk, return_path, fileinfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
void dir_walk_close(DirWalk* dir_walk) {
|
2024-03-19 17:43:52 +03:00
|
|
|
furi_check(dir_walk);
|
2022-05-10 17:05:36 +03:00
|
|
|
if(storage_file_is_open(dir_walk->file)) {
|
|
|
|
storage_dir_close(dir_walk->file);
|
|
|
|
}
|
|
|
|
|
|
|
|
DirIndexList_reset(dir_walk->index_list);
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_reset(dir_walk->path);
|
2022-05-10 17:05:36 +03:00
|
|
|
dir_walk->current_index = 0;
|
|
|
|
}
|