Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:36:52

0001 //===- DirectoryWatcher.h - Listens for directory file changes --*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLVM_CLANG_DIRECTORYWATCHER_DIRECTORYWATCHER_H
0010 #define LLVM_CLANG_DIRECTORYWATCHER_DIRECTORYWATCHER_H
0011 
0012 #include "llvm/ADT/ArrayRef.h"
0013 #include "llvm/ADT/StringRef.h"
0014 #include "llvm/Support/Error.h"
0015 #include <functional>
0016 #include <memory>
0017 #include <string>
0018 
0019 namespace clang {
0020 /// Provides notifications for file changes in a directory.
0021 ///
0022 /// Invokes client-provided function on every filesystem event in the watched
0023 /// directory. Initially the watched directory is scanned and for every file
0024 /// found, an event is synthesized as if the file was added.
0025 ///
0026 /// This is not a general purpose directory monitoring tool - list of
0027 /// limitations follows.
0028 ///
0029 /// Only flat directories with no subdirectories are supported. In case
0030 /// subdirectories are present the behavior is unspecified - events *might* be
0031 /// passed to Receiver on macOS (due to FSEvents being used) while they
0032 /// *probably* won't be passed on Linux (due to inotify being used).
0033 ///
0034 /// Known potential inconsistencies
0035 /// - For files that are deleted befor the initial scan processed them, clients
0036 /// might receive Removed notification without any prior Added notification.
0037 /// - Multiple notifications might be produced when a file is added to the
0038 /// watched directory during the initial scan. We are choosing the lesser evil
0039 /// here as the only known alternative strategy would be to invalidate the
0040 /// watcher instance and force user to create a new one whenever filesystem
0041 /// event occurs during the initial scan but that would introduce continuous
0042 /// restarting failure mode (watched directory is not always "owned" by the same
0043 /// process that is consuming it). Since existing clients can handle duplicate
0044 /// events well, we decided for simplicity.
0045 ///
0046 /// Notifications are provided only for changes done through local user-space
0047 /// filesystem interface. Specifically, it's unspecified if notification would
0048 /// be provided in case of a:
0049 /// - a file mmap-ed and changed
0050 /// - a file changed via remote (NFS) or virtual (/proc) FS access to monitored
0051 /// directory
0052 /// - another filesystem mounted to the watched directory
0053 ///
0054 /// No support for LLVM VFS.
0055 ///
0056 /// It is unspecified whether notifications for files being deleted are sent in
0057 /// case the whole watched directory is sent.
0058 ///
0059 /// Directories containing "too many" files and/or receiving events "too
0060 /// frequently" are not supported - if the initial scan can't be finished before
0061 /// the watcher instance gets invalidated (see WatcherGotInvalidated) there's no
0062 /// good error handling strategy - the only option for client is to destroy the
0063 /// watcher, restart watching with new instance and hope it won't repeat.
0064 class DirectoryWatcher {
0065 public:
0066   struct Event {
0067     enum class EventKind {
0068       Removed,
0069       /// Content of a file was modified.
0070       Modified,
0071       /// The watched directory got deleted.
0072       WatchedDirRemoved,
0073       /// The DirectoryWatcher that originated this event is no longer valid and
0074       /// its behavior is unspecified.
0075       ///
0076       /// The prime case is kernel signalling to OS-specific implementation of
0077       /// DirectoryWatcher some resource limit being hit.
0078       /// *Usually* kernel starts dropping or squashing events together after
0079       /// that and so would DirectoryWatcher. This means that *some* events
0080       /// might still be passed to Receiver but this behavior is unspecified.
0081       ///
0082       /// Another case is after the watched directory itself is deleted.
0083       /// WatcherGotInvalidated will be received at least once during
0084       /// DirectoryWatcher instance lifetime - when handling errors this is done
0085       /// on best effort basis, when an instance is being destroyed then this is
0086       /// guaranteed.
0087       ///
0088       /// The only proper response to this kind of event is to destruct the
0089       /// originating DirectoryWatcher instance and create a new one.
0090       WatcherGotInvalidated
0091     };
0092 
0093     EventKind Kind;
0094     /// Filename that this event is related to or an empty string in
0095     /// case this event is related to the watched directory itself.
0096     std::string Filename;
0097 
0098     Event(EventKind Kind, llvm::StringRef Filename)
0099         : Kind(Kind), Filename(Filename) {}
0100   };
0101 
0102   /// llvm fatal_error if \param Path doesn't exist or isn't a directory.
0103   /// Returns llvm::Expected Error if OS kernel API told us we can't start
0104   /// watching. In such case it's unclear whether just retrying has any chance
0105   /// to succeed.
0106   static llvm::Expected<std::unique_ptr<DirectoryWatcher>>
0107   create(llvm::StringRef Path,
0108          std::function<void(llvm::ArrayRef<DirectoryWatcher::Event> Events,
0109                             bool IsInitial)>
0110              Receiver,
0111          bool WaitForInitialSync);
0112 
0113   virtual ~DirectoryWatcher() = default;
0114   DirectoryWatcher(const DirectoryWatcher &) = delete;
0115   DirectoryWatcher &operator=(const DirectoryWatcher &) = delete;
0116   DirectoryWatcher(DirectoryWatcher &&) = default;
0117 
0118 protected:
0119   DirectoryWatcher() = default;
0120 };
0121 
0122 } // namespace clang
0123 
0124 #endif // LLVM_CLANG_DIRECTORYWATCHER_DIRECTORYWATCHER_H