From f60b9c0013d4e81de64aeb3bca7631c820eb32f9 Mon Sep 17 00:00:00 2001 From: Lucjan Bryndza Date: Mon, 25 Jan 2021 12:52:47 +0100 Subject: [PATCH] [EGD-5392] Add switch vfat to RO mode VFAT partition should be mounted in RO mode by default because writing to FAT filesystem is unsafe. Writing is allowed only in the LFS filesystem. If upgrade is needed FAT partition can be temporary switched to RW mode using mount() syscall with REMOUNT flag but it should be switched to RO mode again after upgrade. --- module-db/Database/Database.cpp | 5 +++-- module-db/Database/Database.hpp | 2 +- module-db/Databases/CountryCodesDB.cpp | 4 ++-- module-vfs/src/purefs/vfs_subsystem.cpp | 5 ++++- 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/module-db/Database/Database.cpp b/module-db/Database/Database.cpp index 665a7786fe2d3d338c5c64a6d20e08864548ef69..c1182c92bf9e960f9fe0ea60e72df8855a7ecbb1 100644 --- a/module-db/Database/Database.cpp +++ b/module-db/Database/Database.cpp @@ -64,12 +64,13 @@ extern "C" } } -Database::Database(const char *name) +Database::Database(const char *name, bool readOnly) : dbConnection(nullptr), dbName(name), queryStatementBuffer{nullptr}, isInitialized_(false), initializer(std::make_unique(this)) { + const int flags = (readOnly) ? (SQLITE_OPEN_READONLY) : (SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE); LOG_INFO("Creating database: %s", dbName.c_str()); - if (const auto rc = sqlite3_open(name, &dbConnection); rc != SQLITE_OK) { + if (const auto rc = sqlite3_open_v2(name, &dbConnection, flags, nullptr); rc != SQLITE_OK) { LOG_ERROR("SQLITE INITIALIZATION ERROR! rc=%d dbName=%s", rc, name); throw DatabaseInitialisationError{"Failed to initialize the sqlite db"}; } diff --git a/module-db/Database/Database.hpp b/module-db/Database/Database.hpp index fa75e5f9ae7fda510c697deb4da3327b5280352e..4909d65c1ce3408fe211ff990a71f946aeb9b517 100644 --- a/module-db/Database/Database.hpp +++ b/module-db/Database/Database.hpp @@ -20,7 +20,7 @@ class DatabaseInitialisationError : public std::runtime_error class Database { public: - explicit Database(const char *name); + explicit Database(const char *name, bool readOnly = false); virtual ~Database(); std::unique_ptr query(const char *format, ...); diff --git a/module-db/Databases/CountryCodesDB.cpp b/module-db/Databases/CountryCodesDB.cpp index 9eb706a40e59958bab066d3f1b18fd7e1d1803e7..9028628ca2b6b3369c59c9c7ddc600b77b09dbc4 100644 --- a/module-db/Databases/CountryCodesDB.cpp +++ b/module-db/Databases/CountryCodesDB.cpp @@ -3,7 +3,7 @@ #include "CountryCodesDB.hpp" -CountryCodesDB::CountryCodesDB(const char *name) : Database(name), countryCodes(this) +CountryCodesDB::CountryCodesDB(const char *name) : Database(name, true), countryCodes(this) { if (countryCodes.create() == false) return; @@ -11,4 +11,4 @@ CountryCodesDB::CountryCodesDB(const char *name) : Database(name), countryCodes( } CountryCodesDB::~CountryCodesDB() -{} \ No newline at end of file +{} diff --git a/module-vfs/src/purefs/vfs_subsystem.cpp b/module-vfs/src/purefs/vfs_subsystem.cpp index 6f328043b694265eee3672f9d0b95cf935795814..d8eef58898906e40a7b6c8ed8ec5c5fdbc5828d6 100644 --- a/module-vfs/src/purefs/vfs_subsystem.cpp +++ b/module-vfs/src/purefs/vfs_subsystem.cpp @@ -150,7 +150,9 @@ namespace purefs::subsystem lfs_it = it; } } + bool vfat_ro = true; if (lfs_it == std::end(parts) && parts.size() == old_layout_part_count) { + vfat_ro = false; LOG_ERROR("!!!! Caution !!!! eMMC is formated with vFAT old layout scheme. Filesystem may be currupted on " "power loss."); LOG_WARN("Please upgrade to new partition scheme based on the LittleFS filesystem."); @@ -164,7 +166,8 @@ namespace purefs::subsystem LOG_FATAL("Unable to lock vfs core"); return -EIO; } - auto err = vfs->mount(boot_it->name, purefs::dir::getRootDiskPath().string(), "vfat"); + auto err = vfs->mount( + boot_it->name, purefs::dir::getRootDiskPath().string(), "vfat", vfat_ro ? fs::mount_flags::read_only : 0); if (err) { return err; }