summaryrefslogtreecommitdiff
path: root/src/util/file_watcher.cc
blob: 22eb824703ff62a2b3a2ba4255d6a36e80b7aec0 (plain)
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
// file_watcher.cc - Hot-reload file change detection (debug only)

#include "file_watcher.h"

#if !defined(STRIP_ALL)

#include <sys/stat.h>

void FileWatcher::add_file(const char* path) {
  WatchEntry entry;
  entry.path = path;
  entry.last_mtime = get_mtime(path);
  entry.changed = false;
  files_.push_back(entry);
}

bool FileWatcher::check_changes() {
  bool any_changed = false;
  for (auto& entry : files_) {
    time_t current_mtime = get_mtime(entry.path.c_str());
    if (current_mtime != entry.last_mtime && current_mtime != 0) {
      entry.changed = true;
      entry.last_mtime = current_mtime;
      any_changed = true;
    }
  }
  return any_changed;
}

void FileWatcher::reset() {
  for (auto& entry : files_) {
    entry.changed = false;
  }
}

time_t FileWatcher::get_mtime(const char* path) {
  struct stat st;
  if (stat(path, &st) == 0) {
    return st.st_mtime;
  }
  return 0;
}

#endif  // !defined(STRIP_ALL)