Seastar
High performance C++ framework for concurrent servers
fsnotify.hh
1/*
2 * This file is open source software, licensed to you under the terms
3 * of the Apache License, Version 2.0 (the "License"). See the NOTICE file
4 * distributed with this work for additional information regarding copyright
5 * ownership. You may not use this file except in compliance with the License.
6 *
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing,
12 * software distributed under the License is distributed on an
13 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 * KIND, either express or implied. See the License for the
15 * specific language governing permissions and limitations
16 * under the License.
17 */
18/*
19 * Copyright 2020 ScyllaDB Ltd.
20 */
21
22#pragma once
23
24#ifndef SEASTAR_MODULE
25#include <sys/inotify.h>
26
27#include <seastar/core/future.hh>
28#include <seastar/core/sstring.hh>
29#include <seastar/core/shared_ptr.hh>
30#include <seastar/util/modules.hh>
31#endif
32
33namespace seastar::experimental {
34
35SEASTAR_MODULE_EXPORT_BEGIN
36
43
58 class impl;
59 shared_ptr<impl> _impl;
60public:
61 class watch;
62 friend class watch;
63
70 enum class flags : uint32_t {
71 access = IN_ACCESS, // File was accessed (e.g., read(2), execve(2)).
72 attrib = IN_ATTRIB, // Metadata changed—for example, permissions, timestamps, extended attributes
73 close_write = IN_CLOSE_WRITE, // File opened for writing was closed.
74 close_nowrite = IN_CLOSE_NOWRITE,// File or directory not opened for writing was closed.
75 create_child = IN_CREATE, // File/directory created in watched directory
76 delete_child = IN_DELETE, // File/directory deleted from watched directory.
77 delete_self = IN_DELETE_SELF, // Watched file/directory was itself deleted. (This event
78 // also occurs if an object is moved to another filesystem)
79 modify = IN_MODIFY, // File was modified (e.g., write(2), truncate(2)).
80 move_self = IN_MOVE_SELF, // Watched file/directory was itself moved.
81 move_from = IN_MOVED_FROM, // Generated for the directory containing the old filename
82 // when a file is renamed.
83 move_to = IN_MOVED_TO, // Generated for the directory containing the new filename
84 // when a file is renamed.
85 open = IN_OPEN, // File was opened
86 close = IN_CLOSE, // close_write|close_nowrite
87 move = IN_MOVE, // move_from|move_to
88 oneshot = IN_ONESHOT, // listen for only a single notification, after which the
89 // token will be invalid
90 ignored = IN_IGNORED, // generated when a token or the file being watched is deleted
91 onlydir = IN_ONLYDIR, // Watch pathname only if it is a directory; the error ENOT‐
92 // DIR results if pathname is not a directory. Using this
93 // flag provides an application with a race-free way of
94 // ensuring that the monitored object is a directory.
95 };
96
98 using watch_token = int32_t;
104 using sequence_no = uint32_t;
105
110 class watch {
111 public:
112 ~watch();
113 watch(watch&&) noexcept;
114 watch& operator=(watch&&) noexcept;
115
121
123 operator watch_token() const {
124 return _token;
125 }
126
129 return _token;
130 }
131
132 private:
133 friend class fsnotifier;
135 watch_token _token;
136 shared_ptr<impl> _impl;
137 };
138
139 fsnotifier();
140 ~fsnotifier();
141
143 fsnotifier& operator=(fsnotifier&&);
144
151 future<watch> create_watch(const sstring& path, flags mask);
152
154 struct event {
155 // matches source watch
156 watch_token id;
157 // event(s) generated
158 flags mask;
159 sequence_no seq; // event correlation -> move_from+move_to
160 sstring name; // optional file name, in case of move_from/to
161 };
162
167
172 void shutdown();
173
175 bool active() const;
176
178 operator bool() const {
179 return active();
180 }
181};
182
185 return fsnotifier::flags(std::underlying_type_t<fsnotifier::flags>(a) | std::underlying_type_t<fsnotifier::flags>(b));
186}
187
189inline void operator|=(fsnotifier::flags& a, fsnotifier::flags b) {
190 a = (a | b);
191}
192
195 return fsnotifier::flags(std::underlying_type_t<fsnotifier::flags>(a) & std::underlying_type_t<fsnotifier::flags>(b));
196}
197
199inline void operator&=(fsnotifier::flags& a, fsnotifier::flags b) {
200 a = (a & b);
201}
202
204
205SEASTAR_MODULE_EXPORT_END
206
207}
Simple RAII wrapper around a fsnotifier::watch_token.
Definition: fsnotify.hh:110
watch_token token() const
Get the token of this watch point.
Definition: fsnotify.hh:128
Filesystem modification notifier.
Definition: fsnotify.hh:57
bool active() const
Check if the notifier is activated.
int32_t watch_token
Token of a watch point.
Definition: fsnotify.hh:98
uint32_t sequence_no
Unique sequence number of associating related events.
Definition: fsnotify.hh:104
future< watch > create_watch(const sstring &path, flags mask)
Monitor events specified in mask for the give path.
flags
Flags of events supported by FileSystem Notifier.
Definition: fsnotify.hh:70
future< std::vector< event > > wait() const
A wrapper around inotify_event.
Definition: fsnotify.hh:154
A representation of a possibly not-yet-computed value.
Definition: future.hh:1240
holds the implementation parts of the metrics layer, do not use directly.