~aleteoryx/muditaos

ref: 7eb1278f0b19713264cf4401475d215ad11a220f muditaos/module-vfs/src/purefs/fs/fsnotify.cpp -rw-r--r-- 2.6 KiB
7eb1278f — Maciej Gibowicz [BH-1476] Add greetings in all languages 1 year, 6 months 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
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include <purefs/fs/fsnotify.hpp>
#include <purefs/fs/notifier.hpp>
#include <log/log.hpp>
#include <mutex.hpp>
#include <cerrno>

namespace purefs::fs
{
    namespace internal
    {
        struct inotify_container
        {
            std::map<std::string, internal::notifier::item_it> items;
        };
    } // namespace internal
    inotify::inotify(std::shared_ptr<sys::Service> svc, std::shared_ptr<internal::notifier> notifier)
        : m_svc(svc), m_notify(notifier), m_evlist(std::make_unique<internal::inotify_container>()),
          m_lock(std::make_unique<cpp_freertos::MutexRecursive>())
    {}

    inotify::~inotify()
    {
        const auto notifier = m_notify.lock();
        if (!notifier) {
            LOG_ERROR("Unable lock notifier");
        }
        const auto svc = m_svc.lock();
        if (!svc) {
            LOG_ERROR("Unable lock service");
        }
        for (const auto &[_, val] : m_evlist->items) {
            notifier->unregister_path(val);
        }
    }

    int inotify::add_watch(std::string_view monitored_path, inotify_flags event_mask)
    {
        const auto notifier = m_notify.lock();
        if (!notifier) {
            LOG_ERROR("Unable lock notifier");
            return -ENXIO;
        }
        const auto svc = m_svc.lock();
        if (!svc) {
            LOG_ERROR("Unable lock service");
            return -ENXIO;
        }
        auto it = notifier->register_path(monitored_path, svc, event_mask);
        if (!it) {
            LOG_ERROR("Unable to register path");
            return -EIO;
        }
        cpp_freertos::LockGuard _lck(*m_lock);
        m_evlist->items.emplace(std::make_pair(std::string(monitored_path), *it));
        return {};
    }

    int inotify::rm_watch(std::string_view monitored_path)
    {
        const auto notifier = m_notify.lock();
        if (!notifier) {
            LOG_ERROR("Unable lock notifier");
            return -ENXIO;
        }
        const auto svc = m_svc.lock();
        if (!svc) {
            LOG_ERROR("Unable lock service");
            return -ENXIO;
        }
        cpp_freertos::LockGuard _lck(*m_lock);
        auto it = m_evlist->items.find(std::string(monitored_path));
        if (it == std::end(m_evlist->items)) {
            LOG_ERROR("Unable to find registered path %s", std::string(monitored_path).c_str());
            return -ENOENT;
        }
        notifier->unregister_path(it->second);
        m_evlist->items.erase(it);
        return {};
    }
} // namespace purefs::fs