From 90bfaa847d606711f4697a489cc7b9a901f1729d Mon Sep 17 00:00:00 2001 From: "Pawel.Paprocki" Date: Wed, 7 Apr 2021 17:21:09 +0200 Subject: [PATCH] [EGD-6352] Add Factory reset mode Stop DB service Delete DB files Make reboot --- .../endpoints/factoryReset/FactoryReset.cpp | 51 ++++++++++++------- .../endpoints/factoryReset/FactoryReset.hpp | 6 +-- .../service-desktop/test_factory_reset.py | 4 +- 3 files changed, 38 insertions(+), 23 deletions(-) diff --git a/module-services/service-desktop/endpoints/factoryReset/FactoryReset.cpp b/module-services/service-desktop/endpoints/factoryReset/FactoryReset.cpp index 416e07dfe81a143d891e4bf4d2dfc5d246c0caf3..93ba35521a91ec7aa11b96a1f4a8142e8a2da23e 100644 --- a/module-services/service-desktop/endpoints/factoryReset/FactoryReset.cpp +++ b/module-services/service-desktop/endpoints/factoryReset/FactoryReset.cpp @@ -1,10 +1,9 @@ -// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved. +// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved. // For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md #include "FactoryReset.hpp" #include #include -#include #include #include @@ -38,38 +37,54 @@ namespace FactoryReset bool Run(sys::Service *ownerService) { - LOG_INFO("FactoryReset: restoring factory state started..."); + LOG_INFO("Restoring factory state started..."); recurseDepth = 0; - const auto factoryOSPath = purefs::dir::getFactoryOSPath(); + const auto userOSPath = purefs::dir::getUserDiskPath(); - if (std::filesystem::is_directory(factoryOSPath.c_str()) && std::filesystem::is_empty(factoryOSPath.c_str())) { - LOG_ERROR("FactoryReset: restoring factory state aborted"); - LOG_ERROR("FactoryReset: directory %s seems empty.", factoryOSPath.c_str()); + if (std::filesystem::is_directory(userOSPath.c_str()) && std::filesystem::is_empty(userOSPath.c_str())) { + LOG_ERROR("Restoring factory state aborted"); + LOG_ERROR("Directory %s seems empty.", userOSPath.c_str()); return false; } if (ownerService != nullptr) { - LOG_INFO("FactoryReset: closing ServiceDB..."); + LOG_INFO("Closing ServiceDB..."); std::string dbServiceName = service::name::db; sys::SystemManager::DestroySystemService(dbServiceName, ownerService); } - if (DeleteDirContent(purefs::dir::getRootDiskPath()) != true) { - LOG_ERROR("FactoryReset: restoring factory state aborted"); - return false; - } - - if (CopyDirContent(factoryOSPath, purefs::dir::getRootDiskPath()) != true) { - LOG_ERROR("FactoryReset: restoring factory state aborted"); - return false; - } + DeleteSelectedUserFiles(userOSPath); - LOG_INFO("FactoryReset: restoring factory state finished, rebooting..."); + LOG_INFO("Rebooting..."); sys::SystemManager::Reboot(ownerService); return true; } + bool DeleteSelectedUserFiles(const std::filesystem::path &userOSPath) + { + bool returnStatus = true; + std::vector selectedFileExt = {".db", ".db-journal", ".db-wal"}; + + LOG_INFO("Delete DB files which will be recreated with factory content after reboot:"); + for (const auto &f : std::filesystem::directory_iterator(userOSPath.c_str())) { + for (const auto &ext : selectedFileExt) { + if (f.path().extension() == ext) { + auto removeStatus = std::filesystem::remove(f.path()); + if (removeStatus == false) { + LOG_ERROR("Error deleting file %s, aborting...", f.path().c_str()); + returnStatus = false; + } + else { + LOG_INFO("%s deleted.", f.path().c_str()); + } + break; + } + } + } + return returnStatus; + } + bool DeleteDirContent(std::string dir) { for (auto &direntry : std::filesystem::directory_iterator(dir.c_str())) { diff --git a/module-services/service-desktop/endpoints/factoryReset/FactoryReset.hpp b/module-services/service-desktop/endpoints/factoryReset/FactoryReset.hpp index c01202d050f2f5624baf5cb32430db4f93623d04..336e208326fe21446fb819cd1e2c3324ad9a8858 100644 --- a/module-services/service-desktop/endpoints/factoryReset/FactoryReset.hpp +++ b/module-services/service-desktop/endpoints/factoryReset/FactoryReset.hpp @@ -1,10 +1,9 @@ -// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved. +// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved. // For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md #pragma once - +#include #include - #include namespace sys @@ -15,6 +14,7 @@ namespace sys namespace FactoryReset { bool Run(sys::Service *ownerService); + bool DeleteSelectedUserFiles(const std::filesystem::path &userOSPath); bool DeleteDirContent(std::string dir); bool CopyDirContent(std::string sourcedir, std::string targetdir); } // namespace FactoryReset diff --git a/test/pytest/service-desktop/test_factory_reset.py b/test/pytest/service-desktop/test_factory_reset.py index 87ac0c2059ff1ad9c467f086ebb25d74d720d0aa..a66afe15b3be2e436311609c652f31b562ce1f23 100644 --- a/test/pytest/service-desktop/test_factory_reset.py +++ b/test/pytest/service-desktop/test_factory_reset.py @@ -1,11 +1,11 @@ -# Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved. +# Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved. # For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md import pytest from harness.interface.defs import status @pytest.mark.service_desktop_test -@pytest.mark.skip(reason="This will make a factory reset") +@pytest.mark.rt1051 @pytest.mark.usefixtures("usb_unlocked") def test_factory_reset(harness): body = {"factoryRequest": True}