~aleteoryx/muditaos

ref: 80c5d5b4ef5ff35886b503afe026d135c675015c muditaos/module-services/service-fileindexer/StartupIndexer.cpp -rw-r--r-- 2.8 KiB
80c5d5b4 — Radoslaw Wicik [EGD-6642] Fix googletest gui 4 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
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "StartupIndexer.hpp"
#include "messages/FileChangeMessage.hpp"
#include <Timers/TimerFactory.hpp>
#include <filesystem>
//#include <ff_stdio_listdir_recursive.h>
#include <purefs/filesystem_paths.hpp>
#include "Constants.hpp"

namespace service::detail
{

    namespace fs = std::filesystem;
    namespace
    {
        using namespace std::string_literals;
        // File extensions indexing allow list
        const std::vector<std::pair<std::string_view, mimeType>> allowed_exts{
            {".txt", mimeType::text}, {".wav", mimeType::audio}, {".mp3", mimeType::audio}, {".flac", mimeType::audio}};

        // List of initial dirs for scan
        const std::vector<std::string> start_dirs{purefs::dir::getUserDiskPath(), purefs::dir::getCurrentOSPath()};
    } // namespace

    auto StartupIndexer::getFileType(std::string_view path) -> mimeType
    {
        for (const auto &ext : allowed_exts) {
            if (fs::path(path).extension() == ext.first) {
                return ext.second;
            }
        }
        return mimeType::_none_;
    }

    // Collect startup files when service starts
    auto StartupIndexer::collectStartupFiles() -> void
    {
        /*
        using namespace std::string_literals;
        auto searcher_cb = [](void *ctx, const char *path, bool isDir) {
            auto _this = reinterpret_cast<StartupIndexer *>(ctx);
            if (!isDir) {
                for (const auto &ext : allowed_exts) {
                    if (fs::path(path).extension() == ext.first) {
                        _this->mMsgs.emplace_back(std::make_shared<msg::FileChangeMessage>(
                            path, msg::FileChangeMessage::evt_t::modified, ""s));
                        LOG_DEBUG("Initial indexing file added %s", path);
                    }
                }
            }
        };
        for (const auto &path : start_dirs) {
            ff_stdio_listdir_recursive(path.c_str(), searcher_cb, this);
        }
        */
    }
    // Setup timers for notification
    auto StartupIndexer::setupTimers(std::shared_ptr<sys::Service> svc, std::string_view svc_name) -> void
    {
        if (!mIdxTimer.isValid()) {
            mIdxTimer = sys::TimerFactory::createPeriodicTimer(
                svc.get(), "file_indexing", std::chrono::milliseconds{timer_indexing_time}, [this, svc](sys::Timer &) {
                    if (!mMsgs.empty()) {
                        svc->bus.sendUnicast(mMsgs.front(), std::string(service::name::file_indexer));
                        mMsgs.pop_front();
                    }
                    else {
                        mIdxTimer.stop();
                    }
                });
            mIdxTimer.start();
        }
    }
} // namespace service::detail