Skip to content

Commit bbb3154

Browse files
committed
make the file watchers throw early if a file path doesnt exist
1 parent c7e65a1 commit bbb3154

File tree

3 files changed

+36
-27
lines changed

3 files changed

+36
-27
lines changed

src/filewatcher/linux_filewatcher.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@ void LinuxFileWatcher::startWatching(const std::string &filepath,
7474
// created and recreated, or else we'd lose
7575
// track of the inode
7676
std::filesystem::path path(std::filesystem::absolute(filepath));
77+
std::error_code ec;
78+
if (!std::filesystem::exists(path, ec) || ec) {
79+
throw std::runtime_error("File does not exist: " + path.string());
80+
}
81+
if (!std::filesystem::is_regular_file(path, ec) || ec) {
82+
throw std::runtime_error("Path is not a regular file: " +
83+
path.string());
84+
}
7785
std::string dirPath = path.parent_path();
7886
filename = path.filename();
7987
spdlog::info("Watching dirPath: {} for file path {}", dirPath,

src/filewatcher/mac_filewatcher.cpp

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <dispatch/dispatch.h>
44
#include <filesystem>
55
#include <spdlog/spdlog.h>
6+
#include <stdexcept>
67

78
// LATENCY
89
// ### function `FSEventStreamCreate`
@@ -47,36 +48,28 @@ void MacFileWatcher::fsEventsCallback(
4748
void MacFileWatcher::startWatching(const std::string &path,
4849
FileChangeCallback cb) {
4950
this->callback = cb;
50-
running = true;
5151
std::filesystem::path abspath(std::filesystem::absolute(path));
52-
53-
// Use canonical path if file exists to resolve symlinks (e.g., /tmp -> /private/tmp)
54-
// Otherwise use absolute path to support watching files that don't exist yet
55-
std::filesystem::path resolvedPath;
5652
std::error_code ec;
57-
if (std::filesystem::exists(abspath, ec)) {
58-
resolvedPath = std::filesystem::canonical(abspath, ec);
59-
if (ec) {
60-
// If canonical fails, fall back to absolute
61-
resolvedPath = abspath;
62-
}
63-
} else {
64-
// File doesn't exist yet, use absolute and try to canonicalize parent
65-
auto parentPath = abspath.parent_path();
66-
if (std::filesystem::exists(parentPath, ec)) {
67-
auto canonicalParent = std::filesystem::canonical(parentPath, ec);
68-
if (!ec) {
69-
resolvedPath = canonicalParent / abspath.filename();
70-
} else {
71-
resolvedPath = abspath;
72-
}
73-
} else {
74-
resolvedPath = abspath;
75-
}
53+
54+
if (!std::filesystem::exists(abspath, ec) || ec) {
55+
throw std::runtime_error("File does not exist: " + abspath.string());
7656
}
77-
78-
std::string dirPath = resolvedPath.parent_path();
79-
filename = resolvedPath.string();
57+
if (!std::filesystem::is_regular_file(abspath, ec) || ec) {
58+
throw std::runtime_error("Path is not a regular file: " +
59+
abspath.string());
60+
}
61+
62+
// Use canonical path to resolve symlinks (e.g., /tmp -> /private/tmp).
63+
std::filesystem::path canonicalPath =
64+
std::filesystem::canonical(abspath, ec);
65+
if (ec) {
66+
throw std::runtime_error("Failed to canonicalize path: " +
67+
abspath.string());
68+
}
69+
70+
std::string dirPath = canonicalPath.parent_path();
71+
filename = canonicalPath.string();
72+
running = true;
8073
watchThread = std::thread([this, dirPath]() {
8174
spdlog::info("Watching file: {}", dirPath);
8275
CFStringRef pathToWatch = CFStringCreateWithCString(

src/filewatcher/windows_filewatcher.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,14 @@ void WindowsFileWatcher::startWatching(const std::string &filepath,
143143
this->callback = cb;
144144

145145
std::filesystem::path path(std::filesystem::absolute(filepath));
146+
std::error_code ec;
147+
if (!std::filesystem::exists(path, ec) || ec) {
148+
throw std::runtime_error("File does not exist: " + path.string());
149+
}
150+
if (!std::filesystem::is_regular_file(path, ec) || ec) {
151+
throw std::runtime_error("Path is not a regular file: " +
152+
path.string());
153+
}
146154
dirPath = path.parent_path().string();
147155
filename = path.filename().string();
148156

0 commit comments

Comments
 (0)