2021-09-10 03:57:43 +03:00
|
|
|
#include "archive_files.h"
|
2021-09-21 13:56:33 +03:00
|
|
|
#include "archive_browser.h"
|
2021-09-10 03:57:43 +03:00
|
|
|
|
|
|
|
bool filter_by_extension(FileInfo* file_info, const char* tab_ext, const char* name) {
|
|
|
|
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) {
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-09-21 13:56:33 +03: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 03:57:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_file_type(ArchiveFile_t* file, FileInfo* file_info) {
|
|
|
|
furi_assert(file);
|
|
|
|
furi_assert(file_info);
|
|
|
|
|
|
|
|
for(size_t i = 0; i < SIZEOF_ARRAY(known_ext); i++) {
|
|
|
|
if(string_search_str(file->name, known_ext[i], 0) != STRING_FAILURE) {
|
|
|
|
file->type = i;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(file_info->flags & FSF_DIRECTORY) {
|
|
|
|
file->type = ArchiveFileTypeFolder;
|
|
|
|
} else {
|
|
|
|
file->type = ArchiveFileTypeUnknown;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-21 13:56:33 +03:00
|
|
|
bool archive_get_filenames(void* context, const char* path) {
|
2021-09-10 03:57:43 +03:00
|
|
|
furi_assert(context);
|
|
|
|
|
2021-09-21 13:56:33 +03:00
|
|
|
bool res;
|
|
|
|
ArchiveBrowserView* browser = context;
|
|
|
|
archive_file_array_rm_all(browser);
|
2021-09-10 03:57:43 +03:00
|
|
|
|
2021-09-21 13:56:33 +03:00
|
|
|
if(archive_get_tab(browser) != ArchiveTabFavorites) {
|
|
|
|
res = archive_read_dir(browser, path);
|
2021-09-10 03:57:43 +03:00
|
|
|
} else {
|
2021-09-21 13:56:33 +03:00
|
|
|
res = archive_favorites_read(browser);
|
2021-09-10 03:57:43 +03:00
|
|
|
}
|
2021-09-21 13:56:33 +03:00
|
|
|
return res;
|
2021-09-10 03:57:43 +03:00
|
|
|
}
|
|
|
|
|
2021-09-21 13:56:33 +03:00
|
|
|
bool archive_dir_empty(void* context, const char* path) { // can be simpler?
|
2021-09-10 03:57:43 +03:00
|
|
|
furi_assert(context);
|
|
|
|
|
|
|
|
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);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-09-21 13:56:33 +03:00
|
|
|
bool files_found = false;
|
2021-09-10 03:57:43 +03:00
|
|
|
while(1) {
|
|
|
|
if(!storage_dir_read(directory, &file_info, name, MAX_NAME_LEN)) {
|
|
|
|
break;
|
|
|
|
}
|
2021-09-21 13:56:33 +03:00
|
|
|
if(files_found) {
|
|
|
|
break;
|
|
|
|
} else if(storage_file_get_error(directory) == FSE_OK) {
|
|
|
|
files_found = name[0];
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
storage_dir_close(directory);
|
|
|
|
storage_file_free(directory);
|
|
|
|
|
|
|
|
furi_record_close("storage");
|
|
|
|
|
|
|
|
return files_found;
|
|
|
|
}
|
2021-09-10 03:57:43 +03:00
|
|
|
|
2021-09-21 13:56:33 +03:00
|
|
|
bool archive_read_dir(void* context, const char* path) {
|
|
|
|
furi_assert(context);
|
2021-09-10 03:57:43 +03:00
|
|
|
|
2021-09-21 13:56:33 +03: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];
|
|
|
|
size_t files_cnt = 0;
|
|
|
|
|
|
|
|
if(!storage_dir_open(directory, path)) {
|
|
|
|
storage_dir_close(directory);
|
|
|
|
storage_file_free(directory);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
while(1) {
|
|
|
|
if(!storage_dir_read(directory, &file_info, name, MAX_NAME_LEN)) {
|
|
|
|
break;
|
|
|
|
}
|
2021-09-10 03:57:43 +03:00
|
|
|
if(files_cnt > MAX_FILES) {
|
|
|
|
break;
|
|
|
|
} else if(storage_file_get_error(directory) == FSE_OK) {
|
2021-09-21 13:56:33 +03:00
|
|
|
archive_add_item(browser, &file_info, name);
|
|
|
|
++files_cnt;
|
2021-09-10 03:57:43 +03:00
|
|
|
} else {
|
|
|
|
storage_dir_close(directory);
|
|
|
|
storage_file_free(directory);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
storage_dir_close(directory);
|
|
|
|
storage_file_free(directory);
|
|
|
|
|
|
|
|
furi_record_close("storage");
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-09-21 13:56:33 +03:00
|
|
|
void archive_file_append(const char* path, const char* format, ...) {
|
2021-09-10 03:57:43 +03:00
|
|
|
furi_assert(path);
|
2021-09-21 13:56:33 +03:00
|
|
|
|
2021-10-12 16:09:34 +03:00
|
|
|
string_t string;
|
2021-09-21 13:56:33 +03:00
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
2021-10-12 16:09:34 +03:00
|
|
|
string_init_vprintf(string, format, args);
|
2021-09-21 13:56:33 +03:00
|
|
|
va_end(args);
|
2021-09-10 03:57:43 +03:00
|
|
|
|
|
|
|
FileWorker* file_worker = file_worker_alloc(false);
|
|
|
|
|
|
|
|
if(!file_worker_open(file_worker, path, FSAM_WRITE, FSOM_OPEN_APPEND)) {
|
|
|
|
FURI_LOG_E("Archive", "Append open error");
|
|
|
|
}
|
|
|
|
|
2021-10-12 16:09:34 +03:00
|
|
|
if(!file_worker_write(file_worker, string_get_cstr(string), string_size(string))) {
|
2021-09-10 03:57:43 +03:00
|
|
|
FURI_LOG_E("Archive", "Append write error");
|
|
|
|
}
|
|
|
|
|
|
|
|
file_worker_close(file_worker);
|
|
|
|
file_worker_free(file_worker);
|
|
|
|
}
|
|
|
|
|
2021-10-12 16:09:34 +03:00
|
|
|
void archive_delete_file(void* context, const char* format, ...) {
|
2021-09-10 03:57:43 +03:00
|
|
|
furi_assert(context);
|
2021-10-12 16:09:34 +03:00
|
|
|
|
|
|
|
string_t filename;
|
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
|
|
|
string_init_vprintf(filename, format, args);
|
|
|
|
va_end(args);
|
|
|
|
|
2021-09-21 13:56:33 +03:00
|
|
|
ArchiveBrowserView* browser = context;
|
|
|
|
FileWorker* file_worker = file_worker_alloc(true);
|
2021-09-10 03:57:43 +03:00
|
|
|
|
2021-10-12 16:09:34 +03:00
|
|
|
bool res = file_worker_remove(file_worker, string_get_cstr(filename));
|
2021-09-10 03:57:43 +03:00
|
|
|
file_worker_free(file_worker);
|
|
|
|
|
2021-10-12 16:09:34 +03:00
|
|
|
if(archive_is_favorite("%s", string_get_cstr(filename))) {
|
|
|
|
archive_favorites_delete("%s", string_get_cstr(filename));
|
2021-09-21 13:56:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if(res) {
|
|
|
|
archive_file_array_rm_selected(browser);
|
2021-09-10 03:57:43 +03:00
|
|
|
}
|
|
|
|
|
2021-10-12 16:09:34 +03:00
|
|
|
string_clear(filename);
|
|
|
|
}
|