[FL-2356] Infrared: Fix opening files outside app folder #1050

This commit is contained in:
Nikolay Minaylov 2022-03-23 21:51:40 +03:00 committed by GitHub
parent c4a0847c99
commit 46a894bc5c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 24 deletions

View File

@ -12,10 +12,11 @@ int32_t InfraredApp::run(void* args) {
bool exit = false; bool exit = false;
if(args) { if(args) {
std::string full_name = static_cast<const char*>(args); std::string path = static_cast<const char*>(args);
std::string remote_name(full_name, full_name.find_last_of('/') + 1, full_name.size()); std::string remote_name(path, path.find_last_of('/') + 1, path.size());
remote_name.erase(remote_name.find_last_of('.')); remote_name.erase(remote_name.find_last_of('.'));
bool result = remote_manager.load(remote_name); path.erase(path.find_last_of('/'));
bool result = remote_manager.load(path, remote_name);
if(result) { if(result) {
current_scene = InfraredApp::Scene::Remote; current_scene = InfraredApp::Scene::Remote;
} else { } else {

View File

@ -15,16 +15,18 @@
static const std::string default_remote_name = "remote"; static const std::string default_remote_name = "remote";
std::string InfraredAppRemoteManager::make_full_name(const std::string& remote_name) const { std::string InfraredAppRemoteManager::make_full_name(
return std::string("") + InfraredApp::infrared_directory + "/" + remote_name + const std::string& path,
InfraredApp::infrared_extension; const std::string& remote_name) const {
return std::string("") + path + "/" + remote_name + InfraredApp::infrared_extension;
} }
std::string InfraredAppRemoteManager::find_vacant_remote_name(const std::string& name) { std::string InfraredAppRemoteManager::find_vacant_remote_name(const std::string& name) {
bool exist = true; bool exist = true;
FileWorkerCpp file_worker; FileWorkerCpp file_worker;
if(!file_worker.is_file_exist(make_full_name(name).c_str(), &exist)) { if(!file_worker.is_file_exist(
make_full_name(InfraredApp::infrared_directory, name).c_str(), &exist)) {
return std::string(); return std::string();
} else if(!exist) { } else if(!exist) {
return name; return name;
@ -35,7 +37,7 @@ std::string InfraredAppRemoteManager::find_vacant_remote_name(const std::string&
bool file_worker_result = false; bool file_worker_result = false;
std::string new_name; std::string new_name;
do { do {
new_name = make_full_name(name + std::to_string(++i)); new_name = make_full_name(InfraredApp::infrared_directory, name + std::to_string(++i));
file_worker_result = file_worker.is_file_exist(new_name.c_str(), &exist); file_worker_result = file_worker.is_file_exist(new_name.c_str(), &exist);
} while(file_worker_result && exist); } while(file_worker_result && exist);
@ -57,7 +59,7 @@ bool InfraredAppRemoteManager::add_remote_with_button(
return false; return false;
} }
remote = std::make_unique<InfraredAppRemote>(new_name); remote = std::make_unique<InfraredAppRemote>(InfraredApp::infrared_directory, new_name);
return add_button(button_name, signal); return add_button(button_name, signal);
} }
@ -84,7 +86,7 @@ const InfraredAppSignal& InfraredAppRemoteManager::get_button_data(size_t index)
bool InfraredAppRemoteManager::delete_remote() { bool InfraredAppRemoteManager::delete_remote() {
bool result; bool result;
FileWorkerCpp file_worker; FileWorkerCpp file_worker;
result = file_worker.remove(make_full_name(remote->name).c_str()); result = file_worker.remove(make_full_name(remote->path, remote->name).c_str());
reset_remote(); reset_remote();
return result; return result;
@ -128,8 +130,8 @@ bool InfraredAppRemoteManager::rename_remote(const char* str) {
} }
FileWorkerCpp file_worker; FileWorkerCpp file_worker;
std::string old_filename = make_full_name(remote->name); std::string old_filename = make_full_name(remote->path, remote->name);
std::string new_filename = make_full_name(new_name); std::string new_filename = make_full_name(remote->path, new_name);
bool result = file_worker.rename(old_filename.c_str(), new_filename.c_str()); bool result = file_worker.rename(old_filename.c_str(), new_filename.c_str());
remote->name = new_name; remote->name = new_name;
@ -160,8 +162,10 @@ bool InfraredAppRemoteManager::store(void) {
Storage* storage = static_cast<Storage*>(furi_record_open("storage")); Storage* storage = static_cast<Storage*>(furi_record_open("storage"));
FlipperFormat* ff = flipper_format_file_alloc(storage); FlipperFormat* ff = flipper_format_file_alloc(storage);
FURI_LOG_I("RemoteManager", "store file: \'%s\'", make_full_name(remote->name).c_str()); FURI_LOG_I(
result = flipper_format_file_open_always(ff, make_full_name(remote->name).c_str()); "RemoteManager", "store file: \'%s\'", make_full_name(remote->path, remote->name).c_str());
result =
flipper_format_file_open_always(ff, make_full_name(remote->path, remote->name).c_str());
if(result) { if(result) {
result = flipper_format_write_header_cstr(ff, "IR signals file", 1); result = flipper_format_write_header_cstr(ff, "IR signals file", 1);
} }
@ -179,13 +183,13 @@ bool InfraredAppRemoteManager::store(void) {
return result; return result;
} }
bool InfraredAppRemoteManager::load(const std::string& remote_name) { bool InfraredAppRemoteManager::load(const std::string& path, const std::string& remote_name) {
bool result = false; bool result = false;
Storage* storage = static_cast<Storage*>(furi_record_open("storage")); Storage* storage = static_cast<Storage*>(furi_record_open("storage"));
FlipperFormat* ff = flipper_format_file_alloc(storage); FlipperFormat* ff = flipper_format_file_alloc(storage);
FURI_LOG_I("RemoteManager", "load file: \'%s\'", make_full_name(remote_name).c_str()); FURI_LOG_I("RemoteManager", "load file: \'%s\'", make_full_name(path, remote_name).c_str());
result = flipper_format_file_open_existing(ff, make_full_name(remote_name).c_str()); result = flipper_format_file_open_existing(ff, make_full_name(path, remote_name).c_str());
if(result) { if(result) {
string_t header; string_t header;
string_init(header); string_init(header);
@ -197,7 +201,7 @@ bool InfraredAppRemoteManager::load(const std::string& remote_name) {
string_clear(header); string_clear(header);
} }
if(result) { if(result) {
remote = std::make_unique<InfraredAppRemote>(remote_name); remote = std::make_unique<InfraredAppRemote>(path, remote_name);
InfraredAppSignal signal; InfraredAppSignal signal;
std::string signal_name; std::string signal_name;
while(infrared_parser_read_signal(ff, signal, signal_name)) { while(infrared_parser_read_signal(ff, signal, signal_name)) {

View File

@ -59,14 +59,18 @@ class InfraredAppRemote {
std::vector<InfraredAppRemoteButton> buttons; std::vector<InfraredAppRemoteButton> buttons;
/** Name of remote */ /** Name of remote */
std::string name; std::string name;
/** Path to remote file */
std::string path;
public: public:
/** Initialize new remote /** Initialize new remote
* *
* @param path - remote file path
* @param name - new remote name * @param name - new remote name
*/ */
InfraredAppRemote(const std::string& name) InfraredAppRemote(const std::string& path, const std::string& name)
: name(name) { : name(name)
, path(path) {
} }
}; };
@ -79,7 +83,7 @@ class InfraredAppRemoteManager {
* @param remote_name name of remote * @param remote_name name of remote
* @retval full name of remote on disk * @retval full name of remote on disk
*/ */
std::string make_full_name(const std::string& remote_name) const; std::string make_full_name(const std::string& path, const std::string& remote_name) const;
public: public:
/** Restriction to button name length. Buttons larger are ignored. */ /** Restriction to button name length. Buttons larger are ignored. */
@ -184,5 +188,5 @@ public:
* @param name - name of remote to load * @param name - name of remote to load
* @retval true if success, false otherwise * @retval true if success, false otherwise
*/ */
bool load(const std::string& name); bool load(const std::string& path, const std::string& name);
}; };

View File

@ -29,7 +29,7 @@ void InfraredAppSceneRemoteList::on_enter(InfraredApp* app) {
last_selected_remote_name); last_selected_remote_name);
if(file_select_result) { if(file_select_result) {
if(remote_manager->load(std::string(filename_ts->text))) { if(remote_manager->load(InfraredApp::infrared_directory, std::string(filename_ts->text))) {
app->switch_to_next_scene(InfraredApp::Scene::Remote); app->switch_to_next_scene(InfraredApp::Scene::Remote);
result = true; result = true;
} }