fix(color): Move mdls not in models.yml to /Models/Unused (#2397)

* Move models.yml that don't exist into /Models/Unused

* Add sdMove, Translations, move both models.yml

* Comments, break loop, rmv extra bool.

* Show Press_Any_Key on dialog

* Translations

* Always create /Models/UNUSED if models.yml exists.

* Close directories
This commit is contained in:
Cliff
2022-10-01 11:29:16 +10:00
committed by GitHub
parent c19f7eafd4
commit 5b6fac36c6
21 changed files with 327 additions and 40 deletions
+86 -15
View File
@@ -30,6 +30,7 @@ using std::list;
#include "storage/sdcard_yaml.h"
#include "yaml/yaml_datastructs.h"
#include "yaml/yaml_labelslist.h"
#include "yaml/yaml_modelslist.h"
#include "yaml/yaml_parser.h"
#endif
@@ -1024,6 +1025,84 @@ bool ModelsList::loadYaml()
f_closedir(&moddir);
}
// Check if models.yml exists
// Any files found above that are not listed in the file will be moved into
// /MDOELS/UNUSED and removed from the discovered file hash list
char line[LEN_MODELS_IDX_LINE + 1];
FILINFO fno;
FRESULT result;
bool foundInModels = f_stat(MODELSLIST_YAML_PATH, &fno) == FR_OK;
bool foundInRadio = f_stat(FALLBACK_MODELSLIST_YAML_PATH, &fno) == FR_OK;
if(foundInModels) { // Default to /Models copy
result = f_open(&file, MODELSLIST_YAML_PATH, FA_OPEN_EXISTING | FA_READ);
} else if (foundInRadio) {
result = f_open(&file, FALLBACK_MODELSLIST_YAML_PATH, FA_OPEN_EXISTING | FA_READ);
}
if((foundInModels || foundInRadio) && result == FR_OK) {
// Create /Models/Unused if it doesn't exist
bool moveRequired = false;
DIR unusedFolder;
FRESULT result = f_opendir(&unusedFolder, UNUSED_MODELS_PATH);
if (result != FR_OK) {
if (result == FR_NO_PATH) result = f_mkdir(UNUSED_MODELS_PATH);
if (result != FR_OK) {
TRACE("Unable to create unused models folder");
return false;
}
} else f_closedir(&unusedFolder);
YamlParser ymp;
std::vector<std::string> modfiles;
void *ctx = get_modelslist_iter(&modfiles);
ymp.init(get_modelslist_parser_calls(), ctx);
UINT bytes_read = 0;
while (f_read(&file, line, sizeof(line), &bytes_read) == FR_OK) {
if (bytes_read == 0) break;
if (f_eof(&file)) ymp.set_eof();
if (ymp.parse(line, bytes_read) != YamlParser::CONTINUE_PARSING) break;
}
f_close(&file);
// Loop through file hases, move any files found that don't exists to /unused
std::vector<filedat> newFileHash;
for(const auto &fhas: fileHashInfo) {
bool found = false;
for(const auto &file : modfiles) {
if (file == fhas.name) {
TRACE_LABELS("Found file %s in models.yml.. OK!", file);
found = true;
break;
}
}
if(!found) {
moveRequired = true;
TRACE_LABELS("Model %s not in models.yml, moving to /UNUSED", fhas.name.c_str());
// Move model into unused folder.
const char *warning = sdMoveFile(fhas.name.c_str(), MODELS_PATH, fhas.name.c_str(), UNUSED_MODELS_PATH);
if(warning)
POPUP_WARNING(warning);
} else {
newFileHash.push_back(fhas); // File exists, keep it
}
}
if(foundInRadio) {
const char *warning = sdMoveFile(MODELS_FILENAME, RADIO_PATH, MODELS_FILENAME, UNUSED_MODELS_PATH);
if(warning)
POPUP_WARNING(warning);
}
if(foundInModels) { // Will overwrite the copy from /radio if both existed, do last
const char *warning = sdMoveFile(MODELS_FILENAME, MODELS_PATH, MODELS_FILENAME, UNUSED_MODELS_PATH);
if(warning)
POPUP_WARNING(warning);
}
if(moveRequired) {
fileHashInfo = newFileHash; // Update the new file list
POPUP_WARNING(TR_MODELS_MOVED "\n" UNUSED_MODELS_PATH, "\n" TR_PRESS_ANY_KEY_TO_SKIP);
}
}
#if defined(DEBUG_TIMERS)
DEBUG_TIMER_SAMPLE(debugTimerYamlScan);
TRACE("Lables: Time to scan models folder %luus",
@@ -1031,9 +1110,7 @@ bool ModelsList::loadYaml()
#endif
// Scan labels.yml
char line[LEN_MODELS_IDX_LINE + 1];
FRESULT result =
f_open(&file, LABELSLIST_YAML_PATH, FA_OPEN_EXISTING | FA_READ);
result = f_open(&file, LABELSLIST_YAML_PATH, FA_OPEN_EXISTING | FA_READ);
if (result == FR_OK) {
YamlParser yp;
void *ctx = get_labelslist_iter();
@@ -1340,21 +1417,15 @@ bool ModelsList::removeModel(ModelCell *model)
TRACE("Unable to create deleted models folder");
return true;
}
}
} else f_closedir(&deletedFolder);
// Move model into deleted folder. If not moved will be re-added on next
// reboot
if (!sdCopyFile(model->modelFilename, MODELS_PATH, model->modelFilename,
DELETED_MODELS_PATH)) {
char curFilename[sizeof(MODELS_PATH) + LEN_MODEL_FILENAME + 2] = "";
strcat(curFilename, MODELS_PATH PATH_SEPARATOR);
strcat(curFilename, model->modelFilename);
TRACE_LABELS("Deleting Model %s", model->modelFilename);
if (f_unlink(curFilename) != FR_OK) {
TRACE("Labels: Unable to delete file");
return true;
}
TRACE_LABELS("Deleting Model %s", model->modelFilename);
const char *warning = sdMoveFile(model->modelFilename, MODELS_PATH, model->modelFilename, DELETED_MODELS_PATH);
if (warning) {
TRACE("Labels: Unable to move file");
return true;
}
// Free memory
+1
View File
@@ -7,6 +7,7 @@ set(SRC ${SRC} storage/yaml/yaml_tree_walker.cpp
if(STORAGE_MODELSLIST)
set(SRC ${SRC} storage/yaml/yaml_labelslist.cpp)
set(SRC ${SRC} storage/yaml/yaml_modelslist.cpp)
add_definitions(-DSTORAGE_MODELSLIST)
endif()
+127
View File
@@ -0,0 +1,127 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include "yaml_modelslist.h"
#include "yaml_parser.h"
#include "storage/modelslist.h"
#include <cstring>
using std::list;
struct modelslist_iter
{
enum Level {
Root=0,
Category=1,
Model=2
};
std::vector<std::string>* filelist;
uint8_t level;
char current_attr[16]; // set after find_node()
};
static modelslist_iter __modelslist_iter_inst;
void* get_modelslist_iter(std::vector<std::string>* filelist)
{
__modelslist_iter_inst.filelist = filelist;
__modelslist_iter_inst.level = 0;
return &__modelslist_iter_inst;
}
static bool to_parent(void* ctx)
{
modelslist_iter* mi = (modelslist_iter*)ctx;
if (mi->level == modelslist_iter::Root) {
return false;
}
mi->level--;
return true;
}
static bool to_child(void* ctx)
{
modelslist_iter* mi = (modelslist_iter*)ctx;
if (mi->level == modelslist_iter::Model) {
return false;
}
mi->level++;
return true;
}
static bool to_next_elmt(void* ctx)
{
modelslist_iter* mi = (modelslist_iter*)ctx;
if (mi->level == modelslist_iter::Root) {
return false;
}
return true;
}
static bool find_node(void* ctx, char* buf, uint8_t len)
{
modelslist_iter* mi = (modelslist_iter*)ctx;
if (len > sizeof(modelslist_iter::current_attr)-1)
len = sizeof(modelslist_iter::current_attr)-1;
memcpy(mi->current_attr, buf, len);
mi->current_attr[len] = '\0';
return true;
}
static void set_attr(void* ctx, char* buf, uint8_t len)
{
char fnamebuf[LEN_MODEL_FILENAME + 1];
modelslist_iter* mi = (modelslist_iter*)ctx;
switch (mi->level) {
case modelslist_iter::Model:
if (!strcmp(mi->current_attr, "filename")) {
if(len <= LEN_MODEL_FILENAME) {
memcpy(fnamebuf, buf, len);
fnamebuf[len] = '\0';
mi->filelist->push_back(fnamebuf);
}
}
break;
}
}
static const YamlParserCalls modelslistCalls = {
to_parent,
to_child,
to_next_elmt,
find_node,
set_attr
};
const YamlParserCalls* get_modelslist_parser_calls()
{
return &modelslistCalls;
}
+33
View File
@@ -0,0 +1,33 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#ifndef _YAML_MODELSLIST_H_
#define _YAML_MODELSLIST_H_
#include <vector>
#include <string>
struct YamlParserCalls;
void* get_modelslist_iter(std::vector<std::string>* filelist);
const YamlParserCalls* get_modelslist_parser_calls();
#endif