1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3 
4 typedef struct Path Path;
5 typedef struct PathSpec PathSpec;
6 
7 #include "unit.h"
8 
9 typedef enum PathType {
10         PATH_EXISTS,
11         PATH_EXISTS_GLOB,
12         PATH_DIRECTORY_NOT_EMPTY,
13         PATH_CHANGED,
14         PATH_MODIFIED,
15         _PATH_TYPE_MAX,
16         _PATH_TYPE_INVALID = -EINVAL,
17 } PathType;
18 
19 typedef struct PathSpec {
20         Unit *unit;
21 
22         char *path;
23 
24         sd_event_source *event_source;
25 
26         LIST_FIELDS(struct PathSpec, spec);
27 
28         PathType type;
29         int inotify_fd;
30         int primary_wd;
31 
32         bool previous_exists;
33 } PathSpec;
34 
35 int path_spec_watch(PathSpec *s, sd_event_io_handler_t handler);
36 void path_spec_unwatch(PathSpec *s);
37 int path_spec_fd_event(PathSpec *s, uint32_t events);
38 void path_spec_done(PathSpec *s);
39 
path_spec_owns_inotify_fd(PathSpec * s,int fd)40 static inline bool path_spec_owns_inotify_fd(PathSpec *s, int fd) {
41         return s->inotify_fd == fd;
42 }
43 
44 typedef enum PathResult {
45         PATH_SUCCESS,
46         PATH_FAILURE_RESOURCES,
47         PATH_FAILURE_START_LIMIT_HIT,
48         PATH_FAILURE_UNIT_START_LIMIT_HIT,
49         PATH_FAILURE_TRIGGER_LIMIT_HIT,
50         _PATH_RESULT_MAX,
51         _PATH_RESULT_INVALID = -EINVAL,
52 } PathResult;
53 
54 struct Path {
55         Unit meta;
56 
57         LIST_HEAD(PathSpec, specs);
58 
59         PathState state, deserialized_state;
60 
61         bool make_directory;
62         mode_t directory_mode;
63 
64         PathResult result;
65 
66         RateLimit trigger_limit;
67 };
68 
69 void path_free_specs(Path *p);
70 
71 extern const UnitVTable path_vtable;
72 
73 const char* path_type_to_string(PathType i) _const_;
74 PathType path_type_from_string(const char *s) _pure_;
75 
76 const char* path_result_to_string(PathResult i) _const_;
77 PathResult path_result_from_string(const char *s) _pure_;
78 
79 DEFINE_CAST(PATH, Path);
80