M module-bluetooth/Bluetooth/BtKeysStorage.cpp => module-bluetooth/Bluetooth/BtKeysStorage.cpp +40 -3
@@ 2,6 2,11 @@
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md
#include <algorithm>
+#include <cstdio>
+#include <filesystem>
+#include <purefs/filesystem_paths.hpp>
+#include <gsl/gsl_util>
+
#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
M module-vfs/CMakeLists.txt => module-vfs/CMakeLists.txt +0 -1
@@ 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
)
M module-vfs/include/user/deprecated/vfs.hpp => module-vfs/include/user/deprecated/vfs.hpp +0 -3
@@ 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);
D module-vfs/src/deprecated/vfs-utils.cpp => module-vfs/src/deprecated/vfs-utils.cpp +0 -44
@@ 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 <vfs.hpp>
-#include <time/time_conversion.hpp>
-#include <random>
-#include <ticks.hpp>
-#include <source/version.hpp>
-
-#include <purefs/filesystem_paths.hpp>
-
-
-std::string vfs::loadFileAsString(const fs::path &fileToLoad)
-{
- auto lamb = [](vfs::FILE *stream) { ::vfs.fclose(stream); };
- std::unique_ptr<char[]> readBuf(new char[purefs::buffer::tar_buf]);
- std::unique_ptr<vfs::FILE, decltype(lamb)> fp(fopen(fileToLoad.c_str(), "r"), lamb);
- std::string contents;
- size_t readSize;
-
- if (fp.get() != nullptr) {
- while (!eof(fp.get())) {
- readSize = fread(readBuf.get(), 1, purefs::buffer::tar_buf, fp.get());
- contents.append(static_cast<const char *>(readBuf.get()), readSize);
- }
- }
-
- return contents;
-}
-
-bool vfs::replaceWithString(const fs::path &fileToModify, const std::string &stringToWrite)
-{
- auto lamb = [](vfs::FILE *stream) { ::vfs.fclose(stream); };
- std::unique_ptr<vfs::FILE, decltype(lamb)> fp(::vfs.fopen(fileToModify.c_str(), "w"), lamb);
-
- if (fp.get() != nullptr) {
- size_t dataWritten = fprintf(fp.get(), stringToWrite.c_str());
- return dataWritten == stringToWrite.length();
- }
- else {
- return false;
- }
-}
-