~aleteoryx/muditaos

ref: 2276ceed679b93a3a891e4f5739ade9e13991c5a muditaos/module-services/service-desktop/endpoints/factoryReset/FactoryReset.cpp -rw-r--r-- 7.7 KiB
2276ceed — Radoslaw Wicik [EGD-3743] Update copyrights in fies 5 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "FactoryReset.hpp"
#include "vfs.hpp"
#include "service-db/includes/DBServiceName.hpp"
#include "SystemManager/SystemManager.hpp"

namespace FactoryReset
{
    static bool CopyFile(std::string sourcefile, std::string targetfile);

    static int recurseDepth             = 0;
    static const int empty_dirlist_size = 2;   /* vfs.listdir lists "." and ".." by default also for empty dir */
    static const int max_recurse_depth  = 120; /* 120 is just an arbitrary value of max number of recursive calls.
                                                * If more then error is assumed, not the real depth of directories."
                                                */
#ifdef TARGET_Linux
    static const int max_filepath_length = 4096;
#else
    static const int max_filepath_length = ffconfigMAX_FILENAME;
#endif

    bool Run(sys::Service *ownerService)
    {
        LOG_INFO("FactoryReset: restoring factory state started...");

        recurseDepth = 0;

        std::vector<vfs::DirectoryEntry> dirlist = vfs.listdir(purefs::dir::os_factory.c_str(), "");

        if (dirlist.size() <= empty_dirlist_size) {
            LOG_ERROR("FactoryReset: restoring factory state aborted");
            LOG_ERROR("FactoryReset: directory %s seems empty.", purefs::dir::os_factory.c_str());
            return false;
        }

        if (ownerService != nullptr) {
            LOG_INFO("FactoryReset: closing ServiceDB...");
            std::string dbServiceName = service::name::db;
            (void)sys::SystemManager::DestroyService(dbServiceName, ownerService);
        }

        if (DeleteDirContent(purefs::dir::eMMC_disk) != true) {
            LOG_ERROR("FactoryReset: restoring factory state aborted");
            return false;
        }

        if (CopyDirContent(purefs::dir::os_factory, purefs::dir::eMMC_disk) != true) {
            LOG_ERROR("FactoryReset: restoring factory state aborted");
            return false;
        }

        LOG_INFO("FactoryReset: restoring factory state finished, rebooting...");
        sys::SystemManager::Reboot(ownerService);
        return true;
    }

    bool DeleteDirContent(std::string dir)
    {
        std::vector<vfs::DirectoryEntry> dirlist = vfs.listdir(dir.c_str(), "");

        for (auto &direntry : dirlist) {
            if ((direntry.fileName.compare(".") != 0) && (direntry.fileName.compare("..") != 0) &&
                (direntry.fileName.compare("...") != 0)) {

                std::string delpath = dir;
                delpath += "/";
                delpath += direntry.fileName.c_str();

                if (direntry.attributes == vfs::FileAttributes::Directory) {
                    if (direntry.fileName.compare(PATH_FACTORY) != 0) {
                        LOG_INFO("FactoryReset: recursively deleting dir %s...", delpath.c_str());
                        if (vfs.deltree(delpath.c_str()) != 0) {
                            LOG_ERROR("FactoryReset: error deleting dir %s, aborting...", delpath.c_str());
                            return false;
                        }
                    }
                }
                else {
                    LOG_INFO("FactoryReset: deleting file %s...", delpath.c_str());
                    if (vfs.remove(delpath.c_str()) != 0) {
                        LOG_ERROR("FactoryReset: error deleting file %s, aborting...", delpath.c_str());
                        return false;
                    }
                }
            }
        }

        return true;
    }

    bool CopyDirContent(std::string sourcedir, std::string targetdir)
    {
        if (recurseDepth >= max_recurse_depth) {
            LOG_ERROR("FactoryReset: recurse level %d (too high), error assumed, skipping restore of dir %s",
                      recurseDepth,
                      sourcedir.c_str());
            return false;
        }

        std::vector<vfs::DirectoryEntry> dirlist = vfs.listdir(sourcedir.c_str(), "");

        for (auto &direntry : dirlist) {
            if ((direntry.fileName.compare(".") == 0) || (direntry.fileName.compare("..") == 0) ||
                (direntry.fileName.compare("...") == 0)) {
                continue;
            }

            std::string sourcepath = sourcedir;
            sourcepath += "/";
            sourcepath += direntry.fileName.c_str();

            std::string targetpath = targetdir;
            targetpath += "/";
            targetpath += direntry.fileName.c_str();

            if ((sourcepath.size() >= max_filepath_length) || (targetpath.size() >= max_filepath_length)) {
                LOG_ERROR("FactoryReset: path length (source or target) exceeds system limit of %d",
                          max_filepath_length);
                LOG_ERROR("FactoryReset: skipping restore of dir %s into %s", sourcepath.c_str(), targetpath.c_str());
                return false;
            }

            if (direntry.attributes == vfs::FileAttributes::Directory) {
                if (targetpath.compare(purefs::dir::os_factory.c_str()) == 0) {
                    continue;
                }

                LOG_INFO("FactoryReset: restoring dir  %s into %s...", sourcepath.c_str(), targetpath.c_str());

                if (vfs.mkdir(targetpath.c_str()) != 0) {
                    LOG_ERROR("FactoryReset: create dir %s failed", targetpath.c_str());
                    return false;
                }

                recurseDepth++;

                if (CopyDirContent(sourcepath, targetpath) != true) {
                    recurseDepth--;
                    return false;
                }

                recurseDepth--;
            }
            else {
                LOG_INFO("FactoryReset: restoring file %s into %s...", sourcepath.c_str(), targetpath.c_str());

                if (CopyFile(sourcepath, targetpath) != true) {
                    return false;
                }
            }
        }

        return true;
    }

    static bool CopyFile(std::string sourcefile, std::string targetfile)
    {
        bool ret  = true;
        auto lamb = [](vfs::FILE *stream) { vfs.fclose(stream); };

        std::unique_ptr<vfs::FILE, decltype(lamb)> sf(vfs.fopen(sourcefile.c_str(), "r"), lamb);
        std::unique_ptr<vfs::FILE, decltype(lamb)> tf(vfs.fopen(targetfile.c_str(), "w"), lamb);

        if ((sf.get() != nullptr) && (tf.get() != nullptr)) {
            std::unique_ptr<unsigned char[]> buffer(new unsigned char[purefs::buffer::copy_buf]);

            if (buffer.get() != nullptr) {
                uint32_t loopcount = (vfs.filelength(sf.get()) / purefs::buffer::copy_buf) + 1u;
                uint32_t readsize  = purefs::buffer::copy_buf;

                for (uint32_t i = 0u; i < loopcount; i++) {
                    if (i + 1u == loopcount) {
                        readsize = vfs.filelength(sf.get()) % purefs::buffer::copy_buf;
                    }

                    if (vfs.fread(buffer.get(), 1, readsize, sf.get()) != readsize) {
                        LOG_ERROR("FactoryReset: read from sourcefile %s failed", sourcefile.c_str());
                        ret = false;
                        break;
                    }

                    if (vfs.fwrite(buffer.get(), 1, readsize, tf.get()) != readsize) {
                        LOG_ERROR("FactoryReset: write to targetfile %s failed", targetfile.c_str());
                        ret = false;
                        break;
                    }
                }
            }
            else {
                LOG_ERROR("FactoryReset: unable to open copy buffer");
                ret = false;
            }
        }
        else {
            LOG_ERROR("FactoryReset: unable to open source or target file");
            ret = false;
        }

        return ret;
    }
} // namespace FactoryReset