|
3 | 3 | #include <dispatch/dispatch.h> |
4 | 4 | #include <filesystem> |
5 | 5 | #include <spdlog/spdlog.h> |
| 6 | +#include <stdexcept> |
6 | 7 |
|
7 | 8 | // LATENCY |
8 | 9 | // ### function `FSEventStreamCreate` |
@@ -47,36 +48,28 @@ void MacFileWatcher::fsEventsCallback( |
47 | 48 | void MacFileWatcher::startWatching(const std::string &path, |
48 | 49 | FileChangeCallback cb) { |
49 | 50 | this->callback = cb; |
50 | | - running = true; |
51 | 51 | 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; |
56 | 52 | 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()); |
76 | 56 | } |
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; |
80 | 73 | watchThread = std::thread([this, dirPath]() { |
81 | 74 | spdlog::info("Watching file: {}", dirPath); |
82 | 75 | CFStringRef pathToWatch = CFStringCreateWithCString( |
|
0 commit comments