From 7a942e2b92dd8fb4894b599c9b48d5a67a37f32b Mon Sep 17 00:00:00 2001 From: Jakub Pyszczak Date: Wed, 23 Dec 2020 10:30:46 +0100 Subject: [PATCH] [EGD-5070] Change new filesystem in module bt Due to vfs deprecation there is need to remove all vfs calls from code. This PR covers module bluetooth. [EGD-5070] Review changes. [EGD-5070] Change new filesystem in module bt Due to vfs deprecation there is need to remove all vfs calls from code. This PR covers module bluetooth --- module-bluetooth/Bluetooth/BtKeysStorage.cpp | 43 +++++++++++++++++-- module-vfs/CMakeLists.txt | 1 - module-vfs/include/user/deprecated/vfs.hpp | 3 -- module-vfs/src/deprecated/vfs-utils.cpp | 44 -------------------- 4 files changed, 40 insertions(+), 51 deletions(-) delete mode 100644 module-vfs/src/deprecated/vfs-utils.cpp diff --git a/module-bluetooth/Bluetooth/BtKeysStorage.cpp b/module-bluetooth/Bluetooth/BtKeysStorage.cpp index fc5d1c2b593a3aa414b6f770d42dbf170269881d..b34c73e6cd3ee0f1f9912296bf4950d58ddae318 100644 --- a/module-bluetooth/Bluetooth/BtKeysStorage.cpp +++ b/module-bluetooth/Bluetooth/BtKeysStorage.cpp @@ -2,6 +2,11 @@ // For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md #include +#include +#include +#include +#include + #include "BtKeysStorage.hpp" json11::Json Bt::KeyStorage::fileJson = json11::Json(); @@ -11,9 +16,41 @@ std::string Bt::KeyStorage::fileContent; namespace Bt { + namespace + { + std::string loadFilesAsString(const std::filesystem::path &fileToLoad) + { + using namespace std::string_literals; + static constexpr auto file_size_limit = 512LU * 1024LU; + std::error_code ec; + auto filesize = std::filesystem::file_size(fileToLoad, ec); + if (ec || filesize > file_size_limit) { + return ""s; + } + std::string contents(filesize, '\0'); + auto fp = fopen(fileToLoad.c_str(), "r"); + if (!fp) { + return ""s; + } + auto cleanup = gsl::finally([fp] { fclose(fp); }); + const auto nitems = std::fread(contents.data(), contents.size(), 1, fp); + return (nitems == 1) ? contents : ""s; + } + + bool replaceWithString(const std::filesystem::path &fileToModify, const std::string &stringToWrite) + { + auto fp = std::fopen(fileToModify.c_str(), "w"); + if (!fp) + return false; + auto cleanup = gsl::finally([fp] { fclose(fp); }); + size_t dataWritten = std::fwrite(stringToWrite.data(), stringToWrite.size(), 1, fp); + return dataWritten == 1; + } + } // namespace + namespace strings { - inline std::string keysFilename = USER_PATH("btkeys.json"); + inline std::string keysFilename = purefs::dir::getUserDiskPath() / "btkeys.json"; inline std::string keys = "keys"; inline std::string link_key = "link_key"; inline std::string bd_addr = "bd_addr"; @@ -39,7 +76,7 @@ namespace Bt { LOG_INFO("opening storage from API"); fileContent.clear(); - fileContent = vfs.loadFileAsString(strings::keysFilename); + fileContent = loadFilesAsString(strings::keysFilename); if (fileContent.empty()) { LOG_WARN("opening empty key file!"); return; @@ -131,7 +168,7 @@ namespace Bt { json11::Json finalJson = json11::Json::object{{strings::keys, keys}}; fileContent = finalJson.dump(); - vfs.replaceWithString(strings::keysFilename, fileContent); + replaceWithString(strings::keysFilename, fileContent); } } // namespace Bt diff --git a/module-vfs/CMakeLists.txt b/module-vfs/CMakeLists.txt index 156460627d778ea6da5850b6c5e15efb0e5dd019..a0418182df48395ffae5c652481124a6d015a73f 100644 --- a/module-vfs/CMakeLists.txt +++ b/module-vfs/CMakeLists.txt @@ -76,7 +76,6 @@ set(SOURCES "" drivers/src/purefs/fs/filesystem_vfat.cpp drivers/src/purefs/fs/filesystem_littlefs.cpp src/deprecated/vfs.cpp - src/deprecated/vfs-utils.cpp src/deprecated/vfsNotifier.cpp ) diff --git a/module-vfs/include/user/deprecated/vfs.hpp b/module-vfs/include/user/deprecated/vfs.hpp index 39aa55de13fb5b1d0dd988238def639b67c390fa..ce08cd7b66b77bef378c815f9737cbd5b99fa57b 100644 --- a/module-vfs/include/user/deprecated/vfs.hpp +++ b/module-vfs/include/user/deprecated/vfs.hpp @@ -122,9 +122,6 @@ class vfs [[deprecated]] int deltree(const char *path); [[deprecated]] int mkdir(const char *dir); [[deprecated]] int rename(const char *oldname, const char *newname); - [[deprecated]] std::string loadFileAsString(const fs::path &fileToLoad); - [[deprecated]] bool replaceWithString(const fs::path &fileToModify, const std::string &stringToWrite); - [[deprecated]] void updateTimestamp(); [[deprecated]] void registerNotificationHandler(vfsn::utility::vfsNotifier::NotifyHandler handler) { chnNotifier.registerNotificationHandler(handler); diff --git a/module-vfs/src/deprecated/vfs-utils.cpp b/module-vfs/src/deprecated/vfs-utils.cpp deleted file mode 100644 index fc8921906bcf4af8fdb04cd943588f0b1b8323f5..0000000000000000000000000000000000000000 --- a/module-vfs/src/deprecated/vfs-utils.cpp +++ /dev/null @@ -1,44 +0,0 @@ -// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved. -// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md - -#include -#include