~aleteoryx/muditaos

90bfaa847d606711f4697a489cc7b9a901f1729d — Pawel.Paprocki 4 years ago d6cae88
[EGD-6352] Add Factory reset mode

Stop DB service
Delete DB files
Make reboot
M module-services/service-desktop/endpoints/factoryReset/FactoryReset.cpp => module-services/service-desktop/endpoints/factoryReset/FactoryReset.cpp +33 -18
@@ 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 <SystemManager/SystemManager.hpp>
#include <log/log.hpp>
#include <purefs/filesystem_paths.hpp>
#include <service-db/DBServiceName.hpp>
#include <Utils.hpp>



@@ 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<std::string> 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())) {

M module-services/service-desktop/endpoints/factoryReset/FactoryReset.hpp => module-services/service-desktop/endpoints/factoryReset/FactoryReset.hpp +3 -3
@@ 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 <purefs/filesystem_paths.hpp>
#include <Service/Service.hpp>

#include <string>

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

M test/pytest/service-desktop/test_factory_reset.py => test/pytest/service-desktop/test_factory_reset.py +2 -2
@@ 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}