Welcome to mirror list, hosted at ThFree Co, Russian Federation.

archive_files.c « helpers « archive « applications - github.com/ClusterM/flipperzero-firmware.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7ff54fedb0f52f3f77ae2e1684f3764151bdd706 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include "archive_files.h"
#include "archive_apps.h"
#include "archive_browser.h"

#define TAG "Archive"

#define ASSETS_DIR "assets"

void archive_set_file_type(ArchiveFile_t* file, const char* path, bool is_folder, bool is_app) {
    furi_assert(file);

    file->is_app = is_app;
    if(is_app) {
        file->type = archive_get_app_filetype(archive_get_app_type(path));
    } else {
        for(size_t i = 0; i < COUNT_OF(known_ext); i++) {
            if((known_ext[i][0] == '?') || (known_ext[i][0] == '*')) continue;
            if(string_search_str(file->path, known_ext[i], 0) != STRING_FAILURE) {
                if(i == ArchiveFileTypeBadUsb) {
                    if(string_search_str(file->path, 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;
                }
            }
        }

        if(is_folder) {
            file->type = ArchiveFileTypeFolder;
        } else {
            file->type = ArchiveFileTypeUnknown;
        }
    }
}

bool archive_get_items(void* context, const char* path) {
    furi_assert(context);

    bool res = false;
    ArchiveBrowserView* browser = context;

    if(archive_get_tab(browser) == ArchiveTabFavorites) {
        res = archive_favorites_read(browser);
    } else if(strncmp(path, "/app:", 5) == 0) {
        res = archive_app_read_dir(browser, path);
    }
    return res;
}

void archive_file_append(const char* path, const char* format, ...) {
    furi_assert(path);

    string_t string;
    va_list args;
    va_start(args, format);
    string_init_vprintf(string, format, args);
    va_end(args);

    Storage* fs_api = furi_record_open("storage");
    File* file = storage_file_alloc(fs_api);

    bool res = storage_file_open(file, path, FSAM_WRITE, FSOM_OPEN_APPEND);

    if(res) {
        storage_file_write(file, string_get_cstr(string), string_size(string));
    }

    storage_file_close(file);
    storage_file_free(file);
    furi_record_close("storage");
}

void archive_delete_file(void* context, const char* format, ...) {
    furi_assert(context);

    string_t filename;
    va_list args;
    va_start(args, format);
    string_init_vprintf(filename, format, args);
    va_end(args);

    ArchiveBrowserView* browser = context;
    Storage* fs_api = furi_record_open("storage");

    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");

    if(archive_is_favorite("%s", string_get_cstr(filename))) {
        archive_favorites_delete("%s", string_get_cstr(filename));
    }

    if(res) {
        archive_file_array_rm_selected(browser);
    }

    string_clear(filename);
}