File: FilesystemWatcher.cpp

package info (click to toggle)
dolphin-emu 2512%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 76,328 kB
  • sloc: cpp: 499,023; ansic: 119,674; python: 6,547; sh: 2,338; makefile: 1,093; asm: 726; pascal: 257; javascript: 183; perl: 97; objc: 75; xml: 30
file content (67 lines) | stat: -rw-r--r-- 2,108 bytes parent folder | download
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
// Copyright 2025 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include "Common/FilesystemWatcher.h"

#include <wtr/watcher.hpp>

#include "Common/Logging/Log.h"
#include "Common/StringUtil.h"

namespace Common
{
FilesystemWatcher::FilesystemWatcher() = default;
FilesystemWatcher::~FilesystemWatcher() = default;

void FilesystemWatcher::Watch(const std::string& path)
{
  const auto [iter, inserted] = m_watched_paths.try_emplace(path, nullptr);
  if (inserted)
  {
    iter->second = std::make_unique<wtr::watch>(path, [this](wtr::event e) {
      const auto watched_path = PathToString(e.path_name);
      if (e.path_type == wtr::event::path_type::watcher)
      {
        if (watched_path.starts_with('e'))
          ERROR_LOG_FMT(COMMON, "Filesystem watcher: '{}'", watched_path);
        else if (watched_path.starts_with('w'))
          WARN_LOG_FMT(COMMON, "Filesystem watcher: '{}'", watched_path);
        return;
      }

      if (e.effect_type == wtr::event::effect_type::create)
      {
        const auto unified_path = WithUnifiedPathSeparators(watched_path);
        PathAdded(unified_path);
      }
      else if (e.effect_type == wtr::event::effect_type::modify)
      {
        const auto unified_path = WithUnifiedPathSeparators(watched_path);
        PathModified(unified_path);
      }
      else if (e.effect_type == wtr::event::effect_type::rename)
      {
        if (!e.associated)
        {
          WARN_LOG_FMT(COMMON, "Rename on path '{}' seen without association!", watched_path);
          return;
        }

        const auto old_path = WithUnifiedPathSeparators(watched_path);
        const auto new_path = WithUnifiedPathSeparators(PathToString(e.associated->path_name));
        PathRenamed(old_path, new_path);
      }
      else if (e.effect_type == wtr::event::effect_type::destroy)
      {
        const auto unified_path = WithUnifiedPathSeparators(watched_path);
        PathDeleted(unified_path);
      }
    });
  }
}

void FilesystemWatcher::Unwatch(const std::string& path)
{
  m_watched_paths.erase(path);
}
}  // namespace Common