1*6777b538SAndroid Build Coastguard Worker // Copyright 2012 The Chromium Authors 2*6777b538SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be 3*6777b538SAndroid Build Coastguard Worker // found in the LICENSE file. 4*6777b538SAndroid Build Coastguard Worker 5*6777b538SAndroid Build Coastguard Worker // This module provides a way to monitor a file or directory for changes. 6*6777b538SAndroid Build Coastguard Worker 7*6777b538SAndroid Build Coastguard Worker #ifndef BASE_FILES_FILE_PATH_WATCHER_H_ 8*6777b538SAndroid Build Coastguard Worker #define BASE_FILES_FILE_PATH_WATCHER_H_ 9*6777b538SAndroid Build Coastguard Worker 10*6777b538SAndroid Build Coastguard Worker #include <memory> 11*6777b538SAndroid Build Coastguard Worker #include <optional> 12*6777b538SAndroid Build Coastguard Worker #include <string> 13*6777b538SAndroid Build Coastguard Worker #include <utility> 14*6777b538SAndroid Build Coastguard Worker 15*6777b538SAndroid Build Coastguard Worker #include "base/base_export.h" 16*6777b538SAndroid Build Coastguard Worker #include "base/containers/enum_set.h" 17*6777b538SAndroid Build Coastguard Worker #include "base/files/file_path.h" 18*6777b538SAndroid Build Coastguard Worker #include "base/functional/callback_forward.h" 19*6777b538SAndroid Build Coastguard Worker #include "base/memory/scoped_refptr.h" 20*6777b538SAndroid Build Coastguard Worker #include "base/sequence_checker.h" 21*6777b538SAndroid Build Coastguard Worker #include "base/task/sequenced_task_runner.h" 22*6777b538SAndroid Build Coastguard Worker #include "build/build_config.h" 23*6777b538SAndroid Build Coastguard Worker 24*6777b538SAndroid Build Coastguard Worker namespace base { 25*6777b538SAndroid Build Coastguard Worker 26*6777b538SAndroid Build Coastguard Worker // This class lets you register interest in changes on a FilePath. 27*6777b538SAndroid Build Coastguard Worker // The callback will get called whenever the file or directory referenced by the 28*6777b538SAndroid Build Coastguard Worker // FilePath is changed, including created or deleted. Due to limitations in the 29*6777b538SAndroid Build Coastguard Worker // underlying OS APIs, FilePathWatcher has slightly different semantics on OS X 30*6777b538SAndroid Build Coastguard Worker // than on Windows or Linux. FilePathWatcher on Linux and Windows will detect 31*6777b538SAndroid Build Coastguard Worker // modifications to files in a watched directory. FilePathWatcher on Mac will 32*6777b538SAndroid Build Coastguard Worker // detect the creation and deletion of files in a watched directory, but will 33*6777b538SAndroid Build Coastguard Worker // not detect modifications to those files. See file_path_watcher_kqueue.cc for 34*6777b538SAndroid Build Coastguard Worker // details. 35*6777b538SAndroid Build Coastguard Worker // 36*6777b538SAndroid Build Coastguard Worker // Must be destroyed on the sequence that invokes Watch(). 37*6777b538SAndroid Build Coastguard Worker class BASE_EXPORT FilePathWatcher { 38*6777b538SAndroid Build Coastguard Worker public: 39*6777b538SAndroid Build Coastguard Worker // Type of change which occurred on the affected. Note that this may differ 40*6777b538SAndroid Build Coastguard Worker // from the watched path, e.g. in the case of recursive watches. 41*6777b538SAndroid Build Coastguard Worker enum class ChangeType { 42*6777b538SAndroid Build Coastguard Worker kUnsupported, // The implementation does not support change types. 43*6777b538SAndroid Build Coastguard Worker kCreated, 44*6777b538SAndroid Build Coastguard Worker kDeleted, 45*6777b538SAndroid Build Coastguard Worker kModified, // Includes modifications to either file contents or attributes. 46*6777b538SAndroid Build Coastguard Worker kMoved 47*6777b538SAndroid Build Coastguard Worker }; 48*6777b538SAndroid Build Coastguard Worker 49*6777b538SAndroid Build Coastguard Worker // Path type of the affected path. Note that this may differ from the watched 50*6777b538SAndroid Build Coastguard Worker // path, e.g. in the case of recursive watches. 51*6777b538SAndroid Build Coastguard Worker enum class FilePathType { 52*6777b538SAndroid Build Coastguard Worker kUnknown, // The implementation could not determine the path type or does 53*6777b538SAndroid Build Coastguard Worker // not support path types. 54*6777b538SAndroid Build Coastguard Worker kDirectory, 55*6777b538SAndroid Build Coastguard Worker kFile, 56*6777b538SAndroid Build Coastguard Worker }; 57*6777b538SAndroid Build Coastguard Worker 58*6777b538SAndroid Build Coastguard Worker // Information about the file system change. This information should be as 59*6777b538SAndroid Build Coastguard Worker // specific as the underlying platform allows. For example, when watching 60*6777b538SAndroid Build Coastguard Worker // directory foo/, creating file foo/bar.txt should be reported as a change 61*6777b538SAndroid Build Coastguard Worker // with a `kCreated` change type and a `kFile` path type rather than as a 62*6777b538SAndroid Build Coastguard Worker // modification to directory foo/. Due to limitations on some platforms, this 63*6777b538SAndroid Build Coastguard Worker // is not always possible. Callers should treat this information a strong 64*6777b538SAndroid Build Coastguard Worker // hint, but still be capable of handling events where this information is not 65*6777b538SAndroid Build Coastguard Worker // known given the limitations on some platforms. 66*6777b538SAndroid Build Coastguard Worker struct ChangeInfo { 67*6777b538SAndroid Build Coastguard Worker FilePathType file_path_type = FilePathType::kUnknown; 68*6777b538SAndroid Build Coastguard Worker ChangeType change_type = ChangeType::kUnsupported; 69*6777b538SAndroid Build Coastguard Worker // Can be used to associate related events. For example, renaming a file may 70*6777b538SAndroid Build Coastguard Worker // trigger separate "moved from" and "moved to" events with the same 71*6777b538SAndroid Build Coastguard Worker // `cookie` value. 72*6777b538SAndroid Build Coastguard Worker // 73*6777b538SAndroid Build Coastguard Worker // TODO(https://crbug.com/1425601): This is currently only used to associate 74*6777b538SAndroid Build Coastguard Worker // `kMoved` events, and requires all consumers to implement the same logic 75*6777b538SAndroid Build Coastguard Worker // to coalesce these events. Consider upstreaming this event coalesing logic 76*6777b538SAndroid Build Coastguard Worker // to the platform-specific implementations and then replacing `cookie` with 77*6777b538SAndroid Build Coastguard Worker // the file path that the file was moved from, if this is known. 78*6777b538SAndroid Build Coastguard Worker std::optional<uint32_t> cookie; 79*6777b538SAndroid Build Coastguard Worker }; 80*6777b538SAndroid Build Coastguard Worker 81*6777b538SAndroid Build Coastguard Worker // TODO(https://crbug.com/1425601): Rename this now that this class declares 82*6777b538SAndroid Build Coastguard Worker // other types of Types. 83*6777b538SAndroid Build Coastguard Worker enum class Type { 84*6777b538SAndroid Build Coastguard Worker // Indicates that the watcher should watch the given path and its 85*6777b538SAndroid Build Coastguard Worker // ancestors for changes. If the path does not exist, its ancestors will 86*6777b538SAndroid Build Coastguard Worker // be watched in anticipation of it appearing later. If the path names a 87*6777b538SAndroid Build Coastguard Worker // directory, changes within the directory are not watched. 88*6777b538SAndroid Build Coastguard Worker kNonRecursive, 89*6777b538SAndroid Build Coastguard Worker 90*6777b538SAndroid Build Coastguard Worker // Indicates that the watcher should watch the given path, its ancestors, 91*6777b538SAndroid Build Coastguard Worker // and its descendants for changes. If the path names a directory, changes 92*6777b538SAndroid Build Coastguard Worker // within the directory are watched. 93*6777b538SAndroid Build Coastguard Worker kRecursive, 94*6777b538SAndroid Build Coastguard Worker 95*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_APPLE) 96*6777b538SAndroid Build Coastguard Worker // Indicates that the watcher should watch the given path only (neither 97*6777b538SAndroid Build Coastguard Worker // ancestors nor descendants). The watch fails if the path does not exist. 98*6777b538SAndroid Build Coastguard Worker kTrivial, 99*6777b538SAndroid Build Coastguard Worker #endif // BUILDFLAG(IS_APPLE) 100*6777b538SAndroid Build Coastguard Worker }; 101*6777b538SAndroid Build Coastguard Worker 102*6777b538SAndroid Build Coastguard Worker // WatchOptions are a generalization of |Type|. They are used in the new 103*6777b538SAndroid Build Coastguard Worker // PlatformDelegate::WatchWithOptions. 104*6777b538SAndroid Build Coastguard Worker struct WatchOptions { 105*6777b538SAndroid Build Coastguard Worker Type type = Type::kNonRecursive; 106*6777b538SAndroid Build Coastguard Worker 107*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID) || \ 108*6777b538SAndroid Build Coastguard Worker BUILDFLAG(IS_FUCHSIA) 109*6777b538SAndroid Build Coastguard Worker // The callback will return the full path to a changed file instead of 110*6777b538SAndroid Build Coastguard Worker // the watched path supplied as |path| when Watch is called. 111*6777b538SAndroid Build Coastguard Worker // So the full path can be different from the watched path when a folder is 112*6777b538SAndroid Build Coastguard Worker // watched. In case of any error, it behaves as the original Watch. 113*6777b538SAndroid Build Coastguard Worker bool report_modified_path = false; 114*6777b538SAndroid Build Coastguard Worker #endif // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || 115*6777b538SAndroid Build Coastguard Worker // BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_FUCHSIA) 116*6777b538SAndroid Build Coastguard Worker }; 117*6777b538SAndroid Build Coastguard Worker 118*6777b538SAndroid Build Coastguard Worker // Callback type for Watch(). |path| points to the file that was updated, 119*6777b538SAndroid Build Coastguard Worker // and |error| is true if the platform specific code detected an error. In 120*6777b538SAndroid Build Coastguard Worker // that case, the callback won't be invoked again. 121*6777b538SAndroid Build Coastguard Worker using Callback = 122*6777b538SAndroid Build Coastguard Worker base::RepeatingCallback<void(const FilePath& path, bool error)>; 123*6777b538SAndroid Build Coastguard Worker // Same as above, but includes more information about the change, if known. 124*6777b538SAndroid Build Coastguard Worker using CallbackWithChangeInfo = RepeatingCallback< 125*6777b538SAndroid Build Coastguard Worker void(const ChangeInfo&, const FilePath& path, bool error)>; 126*6777b538SAndroid Build Coastguard Worker 127*6777b538SAndroid Build Coastguard Worker // Used internally to encapsulate different members on different platforms. 128*6777b538SAndroid Build Coastguard Worker class PlatformDelegate { 129*6777b538SAndroid Build Coastguard Worker public: 130*6777b538SAndroid Build Coastguard Worker using Type = FilePathWatcher::Type; 131*6777b538SAndroid Build Coastguard Worker using WatchOptions = FilePathWatcher::WatchOptions; 132*6777b538SAndroid Build Coastguard Worker 133*6777b538SAndroid Build Coastguard Worker PlatformDelegate(); 134*6777b538SAndroid Build Coastguard Worker PlatformDelegate(const PlatformDelegate&) = delete; 135*6777b538SAndroid Build Coastguard Worker PlatformDelegate& operator=(const PlatformDelegate&) = delete; 136*6777b538SAndroid Build Coastguard Worker virtual ~PlatformDelegate(); 137*6777b538SAndroid Build Coastguard Worker 138*6777b538SAndroid Build Coastguard Worker // Start watching for the given |path| and notify |delegate| about changes. 139*6777b538SAndroid Build Coastguard Worker [[nodiscard]] virtual bool Watch(const FilePath& path, 140*6777b538SAndroid Build Coastguard Worker Type type, 141*6777b538SAndroid Build Coastguard Worker const Callback& callback) = 0; 142*6777b538SAndroid Build Coastguard Worker 143*6777b538SAndroid Build Coastguard Worker // A more general API which can deal with multiple options. 144*6777b538SAndroid Build Coastguard Worker [[nodiscard]] virtual bool WatchWithOptions(const FilePath& path, 145*6777b538SAndroid Build Coastguard Worker const WatchOptions& options, 146*6777b538SAndroid Build Coastguard Worker const Callback& callback); 147*6777b538SAndroid Build Coastguard Worker 148*6777b538SAndroid Build Coastguard Worker // Watches the specified `path` according to the given `options`. 149*6777b538SAndroid Build Coastguard Worker // `callback` is invoked for each subsequent modification, with a 150*6777b538SAndroid Build Coastguard Worker // `ChangeInfo` populated with the fields supported by the implementation. 151*6777b538SAndroid Build Coastguard Worker [[nodiscard]] virtual bool WatchWithChangeInfo( 152*6777b538SAndroid Build Coastguard Worker const FilePath& path, 153*6777b538SAndroid Build Coastguard Worker const WatchOptions& options, 154*6777b538SAndroid Build Coastguard Worker const CallbackWithChangeInfo& callback); 155*6777b538SAndroid Build Coastguard Worker 156*6777b538SAndroid Build Coastguard Worker // Stop watching. This is called from FilePathWatcher's dtor in order to 157*6777b538SAndroid Build Coastguard Worker // allow to shut down properly while the object is still alive. 158*6777b538SAndroid Build Coastguard Worker virtual void Cancel() = 0; 159*6777b538SAndroid Build Coastguard Worker 160*6777b538SAndroid Build Coastguard Worker protected: 161*6777b538SAndroid Build Coastguard Worker friend class FilePathWatcher; 162*6777b538SAndroid Build Coastguard Worker task_runner()163*6777b538SAndroid Build Coastguard Worker scoped_refptr<SequencedTaskRunner> task_runner() const { 164*6777b538SAndroid Build Coastguard Worker return task_runner_; 165*6777b538SAndroid Build Coastguard Worker } 166*6777b538SAndroid Build Coastguard Worker set_task_runner(scoped_refptr<SequencedTaskRunner> runner)167*6777b538SAndroid Build Coastguard Worker void set_task_runner(scoped_refptr<SequencedTaskRunner> runner) { 168*6777b538SAndroid Build Coastguard Worker task_runner_ = std::move(runner); 169*6777b538SAndroid Build Coastguard Worker } 170*6777b538SAndroid Build Coastguard Worker 171*6777b538SAndroid Build Coastguard Worker // Must be called before the PlatformDelegate is deleted. set_cancelled()172*6777b538SAndroid Build Coastguard Worker void set_cancelled() { cancelled_ = true; } 173*6777b538SAndroid Build Coastguard Worker is_cancelled()174*6777b538SAndroid Build Coastguard Worker bool is_cancelled() const { return cancelled_; } 175*6777b538SAndroid Build Coastguard Worker 176*6777b538SAndroid Build Coastguard Worker private: 177*6777b538SAndroid Build Coastguard Worker scoped_refptr<SequencedTaskRunner> task_runner_; 178*6777b538SAndroid Build Coastguard Worker bool cancelled_ = false; 179*6777b538SAndroid Build Coastguard Worker }; 180*6777b538SAndroid Build Coastguard Worker 181*6777b538SAndroid Build Coastguard Worker FilePathWatcher(); 182*6777b538SAndroid Build Coastguard Worker FilePathWatcher(const FilePathWatcher&) = delete; 183*6777b538SAndroid Build Coastguard Worker FilePathWatcher& operator=(const FilePathWatcher&) = delete; 184*6777b538SAndroid Build Coastguard Worker ~FilePathWatcher(); 185*6777b538SAndroid Build Coastguard Worker 186*6777b538SAndroid Build Coastguard Worker // Returns true if the platform and OS version support recursive watches. 187*6777b538SAndroid Build Coastguard Worker static bool RecursiveWatchAvailable(); 188*6777b538SAndroid Build Coastguard Worker 189*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) 190*6777b538SAndroid Build Coastguard Worker // Whether there are outstanding inotify watches. 191*6777b538SAndroid Build Coastguard Worker static bool HasWatchesForTest(); 192*6777b538SAndroid Build Coastguard Worker #endif // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) 193*6777b538SAndroid Build Coastguard Worker 194*6777b538SAndroid Build Coastguard Worker // Starts watching |path| (and its descendants if |type| is kRecursive) for 195*6777b538SAndroid Build Coastguard Worker // changes. |callback| will be run on the caller's sequence to report such 196*6777b538SAndroid Build Coastguard Worker // changes. Returns true if the watch was started successfully and |callback| 197*6777b538SAndroid Build Coastguard Worker // may one day be run, or false in case of failure (e.g., a recursive watch on 198*6777b538SAndroid Build Coastguard Worker // platforms that do not support such). 199*6777b538SAndroid Build Coastguard Worker // 200*6777b538SAndroid Build Coastguard Worker // On POSIX, this must be called from a thread that supports 201*6777b538SAndroid Build Coastguard Worker // FileDescriptorWatcher. 202*6777b538SAndroid Build Coastguard Worker bool Watch(const FilePath& path, Type type, const Callback& callback); 203*6777b538SAndroid Build Coastguard Worker 204*6777b538SAndroid Build Coastguard Worker // A more general API which can deal with multiple options. 205*6777b538SAndroid Build Coastguard Worker bool WatchWithOptions(const FilePath& path, 206*6777b538SAndroid Build Coastguard Worker const WatchOptions& options, 207*6777b538SAndroid Build Coastguard Worker const Callback& callback); 208*6777b538SAndroid Build Coastguard Worker 209*6777b538SAndroid Build Coastguard Worker // Same as above, but `callback` includes more information about the change, 210*6777b538SAndroid Build Coastguard Worker // if known. On platforms for which change information is not supported, 211*6777b538SAndroid Build Coastguard Worker // `callback` is called with a dummy `ChangeInfo`. 212*6777b538SAndroid Build Coastguard Worker bool WatchWithChangeInfo(const FilePath& path, 213*6777b538SAndroid Build Coastguard Worker const WatchOptions& options, 214*6777b538SAndroid Build Coastguard Worker const CallbackWithChangeInfo& callback); 215*6777b538SAndroid Build Coastguard Worker 216*6777b538SAndroid Build Coastguard Worker private: 217*6777b538SAndroid Build Coastguard Worker explicit FilePathWatcher(std::unique_ptr<PlatformDelegate> delegate); 218*6777b538SAndroid Build Coastguard Worker 219*6777b538SAndroid Build Coastguard Worker std::unique_ptr<PlatformDelegate> impl_; 220*6777b538SAndroid Build Coastguard Worker 221*6777b538SAndroid Build Coastguard Worker SEQUENCE_CHECKER(sequence_checker_); 222*6777b538SAndroid Build Coastguard Worker }; 223*6777b538SAndroid Build Coastguard Worker 224*6777b538SAndroid Build Coastguard Worker } // namespace base 225*6777b538SAndroid Build Coastguard Worker 226*6777b538SAndroid Build Coastguard Worker #endif // BASE_FILES_FILE_PATH_WATCHER_H_ 227