2021-09-10 00:57:43 +00:00
|
|
|
#include "archive_files.h"
|
2022-02-10 13:01:49 +00:00
|
|
|
#include "archive_apps.h"
|
2021-09-21 10:56:33 +00:00
|
|
|
#include "archive_browser.h"
|
2021-09-10 00:57:43 +00:00
|
|
|
|
2021-11-12 13:04:35 +00:00
|
|
|
#define TAG "Archive"
|
|
|
|
|
2022-02-10 13:01:49 +00:00
|
|
|
#define ASSETS_DIR "assets"
|
|
|
|
|
2022-04-06 17:44:06 +00:00
|
|
|
bool archive_filter_by_extension(FileInfo* file_info, const char* tab_ext, const char* name) {
|
2021-09-10 00:57:43 +00:00
|
|
|
furi_assert(file_info);
|
|
|
|
furi_assert(tab_ext);
|
|
|
|
furi_assert(name);
|
|
|
|
|
|
|
|
bool result = false;
|
|
|
|
|
|
|
|
if(strcmp(tab_ext, "*") == 0) {
|
|
|
|
result = true;
|
|
|
|
} else if(strstr(name, tab_ext) != NULL) {
|
|
|
|
result = true;
|
|
|
|
} else if(file_info->flags & FSF_DIRECTORY) {
|
2022-02-10 13:01:49 +00:00
|
|
|
if(strstr(name, ASSETS_DIR) != NULL) {
|
|
|
|
result = false; // Skip assets folder in all tabs except browser
|
|
|
|
} else {
|
|
|
|
result = true;
|
|
|
|
}
|
2021-09-10 00:57:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-09-21 10:56:33 +00:00
|
|
|
void archive_trim_file_path(char* name, bool ext) {
|
|
|
|
char* slash = strrchr(name, '/') + 1;
|
|
|
|
if(strlen(slash)) strlcpy(name, slash, strlen(slash) + 1);
|
|
|
|
if(ext) {
|
|
|
|
char* dot = strrchr(name, '.');
|
|
|
|
if(strlen(dot)) *dot = '\0';
|
2021-09-10 00:57:43 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-29 09:39:10 +00:00
|
|
|
|
|
|
|
void archive_get_file_extension(char* name, char* ext) {
|
|
|
|
char* dot = strrchr(name, '.');
|
|
|
|
if(dot == NULL)
|
|
|
|
*ext = '\0';
|
|
|
|
else
|
|
|
|
strncpy(ext, dot, MAX_EXT_LEN);
|
|
|
|
}
|
2021-09-10 00:57:43 +00:00
|
|
|
|
2022-04-06 17:44:06 +00:00
|
|
|
void archive_set_file_type(ArchiveFile_t* file, FileInfo* file_info, const char* path, bool is_app) {
|
2021-09-10 00:57:43 +00:00
|
|
|
furi_assert(file);
|
|
|
|
|
2022-02-10 13:01:49 +00:00
|
|
|
file->is_app = is_app;
|
|
|
|
if(is_app) {
|
|
|
|
file->type = archive_get_app_filetype(archive_get_app_type(path));
|
|
|
|
} else {
|
|
|
|
furi_assert(file_info);
|
|
|
|
|
|
|
|
for(size_t i = 0; i < SIZEOF_ARRAY(known_ext); i++) {
|
|
|
|
if((known_ext[i][0] == '?') || (known_ext[i][0] == '*')) continue;
|
|
|
|
if(string_search_str(file->name, known_ext[i], 0) != STRING_FAILURE) {
|
|
|
|
if(i == ArchiveFileTypeBadUsb) {
|
|
|
|
if(string_search_str(file->name, archive_get_default_path(ArchiveTabBadUsb)) ==
|
|
|
|
0) {
|
|
|
|
file->type = i;
|
|
|
|
return; // *.txt file is a BadUSB script only if it is in BadUSB folder
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
file->type = i;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2021-09-10 00:57:43 +00:00
|
|
|
}
|
|
|
|
|
2022-02-10 13:01:49 +00:00
|
|
|
if(file_info->flags & FSF_DIRECTORY) {
|
|
|
|
file->type = ArchiveFileTypeFolder;
|
|
|
|
} else {
|
|
|
|
file->type = ArchiveFileTypeUnknown;
|
|
|
|
}
|
2021-09-10 00:57:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-21 10:56:33 +00:00
|
|
|
bool archive_get_filenames(void* context, const char* path) {
|
2021-09-10 00:57:43 +00:00
|
|
|
furi_assert(context);
|
|
|
|
|
2021-09-21 10:56:33 +00:00
|
|
|
bool res;
|
|
|
|
ArchiveBrowserView* browser = context;
|
2021-09-10 00:57:43 +00:00
|
|
|
|
2022-02-10 13:01:49 +00:00
|
|
|
if(archive_get_tab(browser) == ArchiveTabFavorites) {
|
2021-09-21 10:56:33 +00:00
|
|
|
res = archive_favorites_read(browser);
|
2022-02-10 13:01:49 +00:00
|
|
|
} else if(strncmp(path, "/app:", 5) == 0) {
|
|
|
|
res = archive_app_read_dir(browser, path);
|
|
|
|
} else {
|
2022-04-06 17:44:06 +00:00
|
|
|
res = archive_file_array_load(browser, 0);
|
2021-09-10 00:57:43 +00:00
|
|
|
}
|
2021-09-21 10:56:33 +00:00
|
|
|
return res;
|
2021-09-10 00:57:43 +00:00
|
|
|
}
|
|
|
|
|
2022-04-06 17:44:06 +00:00
|
|
|
uint32_t archive_dir_count_items(void* context, const char* path) {
|
2021-09-10 00:57:43 +00:00
|
|
|
furi_assert(context);
|
2022-02-10 13:01:49 +00:00
|
|
|
ArchiveBrowserView* browser = context;
|
|
|
|
|
2021-09-10 00:57:43 +00:00
|
|
|
FileInfo file_info;
|
|
|
|
Storage* fs_api = furi_record_open("storage");
|
|
|
|
File* directory = storage_file_alloc(fs_api);
|
|
|
|
char name[MAX_NAME_LEN];
|
|
|
|
|
|
|
|
if(!storage_dir_open(directory, path)) {
|
|
|
|
storage_dir_close(directory);
|
|
|
|
storage_file_free(directory);
|
2022-04-06 17:44:06 +00:00
|
|
|
return 0;
|
2021-09-10 00:57:43 +00:00
|
|
|
}
|
|
|
|
|
2022-04-06 17:44:06 +00:00
|
|
|
uint32_t files_found = 0;
|
2021-09-10 00:57:43 +00:00
|
|
|
while(1) {
|
|
|
|
if(!storage_dir_read(directory, &file_info, name, MAX_NAME_LEN)) {
|
|
|
|
break;
|
|
|
|
}
|
2022-04-06 17:44:06 +00:00
|
|
|
if((storage_file_get_error(directory) == FSE_OK) && (name[0])) {
|
|
|
|
if(archive_filter_by_extension(
|
2022-02-10 13:01:49 +00:00
|
|
|
&file_info, archive_get_tab_ext(archive_get_tab(browser)), name)) {
|
2022-04-06 17:44:06 +00:00
|
|
|
files_found++;
|
2022-02-10 13:01:49 +00:00
|
|
|
}
|
2021-09-21 10:56:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
storage_dir_close(directory);
|
|
|
|
storage_file_free(directory);
|
|
|
|
|
|
|
|
furi_record_close("storage");
|
|
|
|
|
2022-04-06 17:44:06 +00:00
|
|
|
archive_set_item_count(browser, files_found);
|
|
|
|
|
2021-09-21 10:56:33 +00:00
|
|
|
return files_found;
|
|
|
|
}
|
2021-09-10 00:57:43 +00:00
|
|
|
|
2022-04-06 17:44:06 +00:00
|
|
|
uint32_t archive_dir_read_items(void* context, const char* path, uint32_t offset, uint32_t count) {
|
2021-09-21 10:56:33 +00:00
|
|
|
furi_assert(context);
|
2021-09-10 00:57:43 +00:00
|
|
|
|
2021-09-21 10:56:33 +00:00
|
|
|
ArchiveBrowserView* browser = context;
|
|
|
|
FileInfo file_info;
|
|
|
|
Storage* fs_api = furi_record_open("storage");
|
|
|
|
File* directory = storage_file_alloc(fs_api);
|
|
|
|
char name[MAX_NAME_LEN];
|
2022-02-10 13:01:49 +00:00
|
|
|
snprintf(name, MAX_NAME_LEN, "%s/", path);
|
|
|
|
size_t path_len = strlen(name);
|
2021-09-21 10:56:33 +00:00
|
|
|
|
|
|
|
if(!storage_dir_open(directory, path)) {
|
|
|
|
storage_dir_close(directory);
|
|
|
|
storage_file_free(directory);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-04-06 17:44:06 +00:00
|
|
|
// Skip items before offset
|
|
|
|
uint32_t items_cnt = 0;
|
|
|
|
while(items_cnt < offset) {
|
|
|
|
if(!storage_dir_read(directory, &file_info, &name[path_len], MAX_NAME_LEN)) {
|
2021-09-21 10:56:33 +00:00
|
|
|
break;
|
|
|
|
}
|
2022-04-06 17:44:06 +00:00
|
|
|
if(storage_file_get_error(directory) == FSE_OK) {
|
|
|
|
if(archive_filter_by_extension(
|
|
|
|
&file_info, archive_get_tab_ext(archive_get_tab(browser)), name)) {
|
|
|
|
items_cnt++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(items_cnt != offset) {
|
|
|
|
storage_dir_close(directory);
|
|
|
|
storage_file_free(directory);
|
|
|
|
furi_record_close("storage");
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2022-02-10 13:01:49 +00:00
|
|
|
|
2022-04-06 17:44:06 +00:00
|
|
|
items_cnt = 0;
|
|
|
|
archive_file_array_rm_all(browser);
|
|
|
|
while(items_cnt < count) {
|
|
|
|
if(!storage_dir_read(directory, &file_info, &name[path_len], MAX_NAME_LEN - path_len)) {
|
2021-09-10 00:57:43 +00:00
|
|
|
break;
|
2022-04-06 17:44:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(storage_file_get_error(directory) == FSE_OK) {
|
2022-02-10 13:01:49 +00:00
|
|
|
archive_add_file_item(browser, &file_info, name);
|
2022-04-06 17:44:06 +00:00
|
|
|
items_cnt++;
|
2021-09-10 00:57:43 +00:00
|
|
|
} else {
|
2022-04-06 17:44:06 +00:00
|
|
|
break;
|
2021-09-10 00:57:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
storage_dir_close(directory);
|
|
|
|
storage_file_free(directory);
|
|
|
|
furi_record_close("storage");
|
|
|
|
|
2022-04-06 17:44:06 +00:00
|
|
|
return (items_cnt == count);
|
2021-09-10 00:57:43 +00:00
|
|
|
}
|
|
|
|
|
2021-09-21 10:56:33 +00:00
|
|
|
void archive_file_append(const char* path, const char* format, ...) {
|
2021-09-10 00:57:43 +00:00
|
|
|
furi_assert(path);
|
2021-09-21 10:56:33 +00:00
|
|
|
|
2021-10-12 13:09:34 +00:00
|
|
|
string_t string;
|
2021-09-21 10:56:33 +00:00
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
2021-10-12 13:09:34 +00:00
|
|
|
string_init_vprintf(string, format, args);
|
2021-09-21 10:56:33 +00:00
|
|
|
va_end(args);
|
2021-09-10 00:57:43 +00:00
|
|
|
|
|
|
|
FileWorker* file_worker = file_worker_alloc(false);
|
|
|
|
|
|
|
|
if(!file_worker_open(file_worker, path, FSAM_WRITE, FSOM_OPEN_APPEND)) {
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_E(TAG, "Append open error");
|
2021-09-10 00:57:43 +00:00
|
|
|
}
|
|
|
|
|
2021-10-12 13:09:34 +00:00
|
|
|
if(!file_worker_write(file_worker, string_get_cstr(string), string_size(string))) {
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_E(TAG, "Append write error");
|
2021-09-10 00:57:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
file_worker_close(file_worker);
|
|
|
|
file_worker_free(file_worker);
|
|
|
|
}
|
|
|
|
|
2021-10-12 13:09:34 +00:00
|
|
|
void archive_delete_file(void* context, const char* format, ...) {
|
2021-09-10 00:57:43 +00:00
|
|
|
furi_assert(context);
|
2021-10-12 13:09:34 +00:00
|
|
|
|
|
|
|
string_t filename;
|
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
|
|
|
string_init_vprintf(filename, format, args);
|
|
|
|
va_end(args);
|
|
|
|
|
2021-09-21 10:56:33 +00:00
|
|
|
ArchiveBrowserView* browser = context;
|
2022-04-06 17:44:06 +00:00
|
|
|
Storage* fs_api = furi_record_open("storage");
|
2021-09-10 00:57:43 +00:00
|
|
|
|
2022-04-06 17:44:06 +00:00
|
|
|
FileInfo fileinfo;
|
|
|
|
storage_common_stat(fs_api, string_get_cstr(filename), &fileinfo);
|
|
|
|
|
|
|
|
bool res = false;
|
|
|
|
|
|
|
|
if(fileinfo.flags & FSF_DIRECTORY) {
|
|
|
|
res = storage_simply_remove_recursive(fs_api, string_get_cstr(filename));
|
|
|
|
} else {
|
|
|
|
res = (storage_common_remove(fs_api, string_get_cstr(filename)) == FSE_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
furi_record_close("storage");
|
2021-09-10 00:57:43 +00:00
|
|
|
|
2021-10-12 13:09:34 +00:00
|
|
|
if(archive_is_favorite("%s", string_get_cstr(filename))) {
|
|
|
|
archive_favorites_delete("%s", string_get_cstr(filename));
|
2021-09-21 10:56:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(res) {
|
|
|
|
archive_file_array_rm_selected(browser);
|
2021-09-10 00:57:43 +00:00
|
|
|
}
|
|
|
|
|
2021-10-12 13:09:34 +00:00
|
|
|
string_clear(filename);
|
2022-04-06 17:44:06 +00:00
|
|
|
}
|