blob: 61cc737892bbd79d29681799c4bba8e5cc492d59 [file] [log] [blame]
Brian Silverman41cdd3e2019-01-19 19:48:58 -08001/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to
5 * deal in the Software without restriction, including without limitation the
6 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 * sell copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
9 *
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19 * IN THE SOFTWARE.
20 */
21
22#include "uv.h"
23#include "uv-common.h"
24
25#include <assert.h>
26#include <stdlib.h>
27#include <string.h>
28
29struct poll_ctx {
30 uv_fs_poll_t* parent_handle; /* NULL if parent has been stopped or closed */
31 int busy_polling;
32 unsigned int interval;
33 uint64_t start_time;
34 uv_loop_t* loop;
35 uv_fs_poll_cb poll_cb;
36 uv_timer_t timer_handle;
37 uv_fs_t fs_req; /* TODO(bnoordhuis) mark fs_req internal */
38 uv_stat_t statbuf;
39 char path[1]; /* variable length */
40};
41
42static int statbuf_eq(const uv_stat_t* a, const uv_stat_t* b);
43static void poll_cb(uv_fs_t* req);
44static void timer_cb(uv_timer_t* timer);
45static void timer_close_cb(uv_handle_t* handle);
46
47static uv_stat_t zero_statbuf;
48
49
50int uv_fs_poll_init(uv_loop_t* loop, uv_fs_poll_t* handle) {
51 uv__handle_init(loop, (uv_handle_t*)handle, UV_FS_POLL);
52 return 0;
53}
54
55
56int uv_fs_poll_start(uv_fs_poll_t* handle,
57 uv_fs_poll_cb cb,
58 const char* path,
59 unsigned int interval) {
60 struct poll_ctx* ctx;
61 uv_loop_t* loop;
62 size_t len;
63 int err;
64
65 if (uv__is_active(handle))
66 return 0;
67
68 loop = handle->loop;
69 len = strlen(path);
70 ctx = (struct poll_ctx*)uv__calloc(1, sizeof(*ctx) + len);
71
72 if (ctx == NULL)
73 return UV_ENOMEM;
74
75 ctx->loop = loop;
76 ctx->poll_cb = cb;
77 ctx->interval = interval ? interval : 1;
78 ctx->start_time = uv_now(loop);
79 ctx->parent_handle = handle;
80 memcpy(ctx->path, path, len + 1);
81
82 err = uv_timer_init(loop, &ctx->timer_handle);
83 if (err < 0)
84 goto error;
85
86 ctx->timer_handle.flags |= UV__HANDLE_INTERNAL;
87 uv__handle_unref(&ctx->timer_handle);
88
89 err = uv_fs_stat(loop, &ctx->fs_req, ctx->path, poll_cb);
90 if (err < 0)
91 goto error;
92
93 handle->poll_ctx = ctx;
94 uv__handle_start(handle);
95
96 return 0;
97
98error:
99 uv__free(ctx);
100 return err;
101}
102
103
104int uv_fs_poll_stop(uv_fs_poll_t* handle) {
105 struct poll_ctx* ctx;
106
107 if (!uv__is_active(handle))
108 return 0;
109
110 ctx = (struct poll_ctx*)handle->poll_ctx;
111 assert(ctx != NULL);
112 assert(ctx->parent_handle != NULL);
113 ctx->parent_handle = NULL;
114 handle->poll_ctx = NULL;
115
116 /* Close the timer if it's active. If it's inactive, there's a stat request
117 * in progress and poll_cb will take care of the cleanup.
118 */
119 if (uv__is_active(&ctx->timer_handle))
120 uv_close((uv_handle_t*)&ctx->timer_handle, timer_close_cb);
121
122 uv__handle_stop(handle);
123
124 return 0;
125}
126
127
128int uv_fs_poll_getpath(uv_fs_poll_t* handle, char* buffer, size_t* size) {
129 struct poll_ctx* ctx;
130 size_t required_len;
131
132 if (!uv__is_active(handle)) {
133 *size = 0;
134 return UV_EINVAL;
135 }
136
137 ctx = (struct poll_ctx*)handle->poll_ctx;
138 assert(ctx != NULL);
139
140 required_len = strlen(ctx->path);
141 if (required_len >= *size) {
142 *size = required_len + 1;
143 return UV_ENOBUFS;
144 }
145
146 memcpy(buffer, ctx->path, required_len);
147 *size = required_len;
148 buffer[required_len] = '\0';
149
150 return 0;
151}
152
153
154void uv__fs_poll_close(uv_fs_poll_t* handle) {
155 uv_fs_poll_stop(handle);
156}
157
158
159static void timer_cb(uv_timer_t* timer) {
160 struct poll_ctx* ctx;
161
162 ctx = container_of(timer, struct poll_ctx, timer_handle);
163 assert(ctx->parent_handle != NULL);
164 assert(ctx->parent_handle->poll_ctx == ctx);
165 ctx->start_time = uv_now(ctx->loop);
166
167 if (uv_fs_stat(ctx->loop, &ctx->fs_req, ctx->path, poll_cb))
168 abort();
169}
170
171
172static void poll_cb(uv_fs_t* req) {
173 uv_stat_t* statbuf;
174 struct poll_ctx* ctx;
175 uint64_t interval;
176
177 ctx = container_of(req, struct poll_ctx, fs_req);
178
179 if (ctx->parent_handle == NULL) { /* handle has been stopped or closed */
180 uv_close((uv_handle_t*)&ctx->timer_handle, timer_close_cb);
181 uv_fs_req_cleanup(req);
182 return;
183 }
184
185 if (req->result != 0) {
186 if (ctx->busy_polling != req->result) {
187 ctx->poll_cb(ctx->parent_handle,
188 req->result,
189 &ctx->statbuf,
190 &zero_statbuf);
191 ctx->busy_polling = req->result;
192 }
193 goto out;
194 }
195
196 statbuf = &req->statbuf;
197
198 if (ctx->busy_polling != 0)
199 if (ctx->busy_polling < 0 || !statbuf_eq(&ctx->statbuf, statbuf))
200 ctx->poll_cb(ctx->parent_handle, 0, &ctx->statbuf, statbuf);
201
202 ctx->statbuf = *statbuf;
203 ctx->busy_polling = 1;
204
205out:
206 uv_fs_req_cleanup(req);
207
208 if (ctx->parent_handle == NULL) { /* handle has been stopped by callback */
209 uv_close((uv_handle_t*)&ctx->timer_handle, timer_close_cb);
210 return;
211 }
212
213 /* Reschedule timer, subtract the delay from doing the stat(). */
214 interval = ctx->interval;
215 interval -= (uv_now(ctx->loop) - ctx->start_time) % interval;
216
217 if (uv_timer_start(&ctx->timer_handle, timer_cb, interval, 0))
218 abort();
219}
220
221
222static void timer_close_cb(uv_handle_t* handle) {
223 uv__free(container_of(handle, struct poll_ctx, timer_handle));
224}
225
226
227static int statbuf_eq(const uv_stat_t* a, const uv_stat_t* b) {
228 return a->st_ctim.tv_nsec == b->st_ctim.tv_nsec
229 && a->st_mtim.tv_nsec == b->st_mtim.tv_nsec
230 && a->st_birthtim.tv_nsec == b->st_birthtim.tv_nsec
231 && a->st_ctim.tv_sec == b->st_ctim.tv_sec
232 && a->st_mtim.tv_sec == b->st_mtim.tv_sec
233 && a->st_birthtim.tv_sec == b->st_birthtim.tv_sec
234 && a->st_size == b->st_size
235 && a->st_mode == b->st_mode
236 && a->st_uid == b->st_uid
237 && a->st_gid == b->st_gid
238 && a->st_ino == b->st_ino
239 && a->st_dev == b->st_dev
240 && a->st_flags == b->st_flags
241 && a->st_gen == b->st_gen;
242}
243
244
245#if defined(_WIN32)
246
247#include "win/internal.h"
248#include "win/handle-inl.h"
249
250void uv__fs_poll_endgame(uv_loop_t* loop, uv_fs_poll_t* handle) {
251 assert(handle->flags & UV__HANDLE_CLOSING);
252 assert(!(handle->flags & UV_HANDLE_CLOSED));
253 uv__handle_close(handle);
254}
255
256#endif /* _WIN32 */