1 /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
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:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
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
23 #include "uv-common.h"
26 #include "win/internal.h"
27 #include "win/handle-inl.h"
28 #define uv__make_close_pending(h) uv_want_endgame((h)->loop, (h))
30 #include "unix/internal.h"
38 uv_fs_poll_t* parent_handle;
40 unsigned int interval;
43 uv_fs_poll_cb poll_cb;
44 uv_timer_t timer_handle;
45 uv_fs_t fs_req; /* TODO(bnoordhuis) mark fs_req internal */
47 struct poll_ctx* previous; /* context from previous start()..stop() period */
48 char path[1]; /* variable length */
51 static int statbuf_eq(const uv_stat_t* a, const uv_stat_t* b);
52 static void poll_cb(uv_fs_t* req);
53 static void timer_cb(uv_timer_t* timer);
54 static void timer_close_cb(uv_handle_t* handle);
56 static uv_stat_t zero_statbuf;
59 int uv_fs_poll_init(uv_loop_t* loop, uv_fs_poll_t* handle) {
60 uv__handle_init(loop, (uv_handle_t*)handle, UV_FS_POLL);
61 handle->poll_ctx = NULL;
66 int uv_fs_poll_start(uv_fs_poll_t* handle,
69 unsigned int interval) {
75 if (uv_is_active((uv_handle_t*)handle))
80 ctx = uv__calloc(1, sizeof(*ctx) + len);
87 ctx->interval = interval ? interval : 1;
88 ctx->start_time = uv_now(loop);
89 ctx->parent_handle = handle;
90 memcpy(ctx->path, path, len + 1);
92 err = uv_timer_init(loop, &ctx->timer_handle);
96 ctx->timer_handle.flags |= UV_HANDLE_INTERNAL;
97 uv__handle_unref(&ctx->timer_handle);
99 err = uv_fs_stat(loop, &ctx->fs_req, ctx->path, poll_cb);
103 if (handle->poll_ctx != NULL)
104 ctx->previous = handle->poll_ctx;
105 handle->poll_ctx = ctx;
106 uv__handle_start(handle);
116 int uv_fs_poll_stop(uv_fs_poll_t* handle) {
117 struct poll_ctx* ctx;
119 if (!uv_is_active((uv_handle_t*)handle))
122 ctx = handle->poll_ctx;
124 assert(ctx->parent_handle == handle);
126 /* Close the timer if it's active. If it's inactive, there's a stat request
127 * in progress and poll_cb will take care of the cleanup.
129 if (uv_is_active((uv_handle_t*)&ctx->timer_handle))
130 uv_close((uv_handle_t*)&ctx->timer_handle, timer_close_cb);
132 uv__handle_stop(handle);
138 int uv_fs_poll_getpath(uv_fs_poll_t* handle, char* buffer, size_t* size) {
139 struct poll_ctx* ctx;
142 if (!uv_is_active((uv_handle_t*)handle)) {
147 ctx = handle->poll_ctx;
150 required_len = strlen(ctx->path);
151 if (required_len >= *size) {
152 *size = required_len + 1;
156 memcpy(buffer, ctx->path, required_len);
157 *size = required_len;
158 buffer[required_len] = '\0';
164 void uv__fs_poll_close(uv_fs_poll_t* handle) {
165 uv_fs_poll_stop(handle);
167 if (handle->poll_ctx == NULL)
168 uv__make_close_pending((uv_handle_t*)handle);
172 static void timer_cb(uv_timer_t* timer) {
173 struct poll_ctx* ctx;
175 ctx = container_of(timer, struct poll_ctx, timer_handle);
176 assert(ctx->parent_handle != NULL);
177 assert(ctx->parent_handle->poll_ctx == ctx);
178 ctx->start_time = uv_now(ctx->loop);
180 if (uv_fs_stat(ctx->loop, &ctx->fs_req, ctx->path, poll_cb))
185 static void poll_cb(uv_fs_t* req) {
187 struct poll_ctx* ctx;
189 uv_fs_poll_t* handle;
191 ctx = container_of(req, struct poll_ctx, fs_req);
192 handle = ctx->parent_handle;
194 if (!uv_is_active((uv_handle_t*)handle) || uv__is_closing(handle))
197 if (req->result != 0) {
198 if (ctx->busy_polling != req->result) {
199 ctx->poll_cb(ctx->parent_handle,
203 ctx->busy_polling = req->result;
208 statbuf = &req->statbuf;
210 if (ctx->busy_polling != 0)
211 if (ctx->busy_polling < 0 || !statbuf_eq(&ctx->statbuf, statbuf))
212 ctx->poll_cb(ctx->parent_handle, 0, &ctx->statbuf, statbuf);
214 ctx->statbuf = *statbuf;
215 ctx->busy_polling = 1;
218 uv_fs_req_cleanup(req);
220 if (!uv_is_active((uv_handle_t*)handle) || uv__is_closing(handle)) {
221 uv_close((uv_handle_t*)&ctx->timer_handle, timer_close_cb);
225 /* Reschedule timer, subtract the delay from doing the stat(). */
226 interval = ctx->interval;
227 interval -= (uv_now(ctx->loop) - ctx->start_time) % interval;
229 if (uv_timer_start(&ctx->timer_handle, timer_cb, interval, 0))
234 static void timer_close_cb(uv_handle_t* timer) {
235 struct poll_ctx* ctx;
237 struct poll_ctx* last;
238 uv_fs_poll_t* handle;
240 ctx = container_of(timer, struct poll_ctx, timer_handle);
241 handle = ctx->parent_handle;
242 if (ctx == handle->poll_ctx) {
243 handle->poll_ctx = ctx->previous;
244 if (handle->poll_ctx == NULL && uv__is_closing(handle))
245 uv__make_close_pending((uv_handle_t*)handle);
247 for (last = handle->poll_ctx, it = last->previous;
249 last = it, it = it->previous) {
250 assert(last->previous != NULL);
252 last->previous = ctx->previous;
258 static int statbuf_eq(const uv_stat_t* a, const uv_stat_t* b) {
259 return a->st_ctim.tv_nsec == b->st_ctim.tv_nsec
260 && a->st_mtim.tv_nsec == b->st_mtim.tv_nsec
261 && a->st_birthtim.tv_nsec == b->st_birthtim.tv_nsec
262 && a->st_ctim.tv_sec == b->st_ctim.tv_sec
263 && a->st_mtim.tv_sec == b->st_mtim.tv_sec
264 && a->st_birthtim.tv_sec == b->st_birthtim.tv_sec
265 && a->st_size == b->st_size
266 && a->st_mode == b->st_mode
267 && a->st_uid == b->st_uid
268 && a->st_gid == b->st_gid
269 && a->st_ino == b->st_ino
270 && a->st_dev == b->st_dev
271 && a->st_flags == b->st_flags
272 && a->st_gen == b->st_gen;
278 #include "win/internal.h"
279 #include "win/handle-inl.h"
281 void uv__fs_poll_endgame(uv_loop_t* loop, uv_fs_poll_t* handle) {
282 assert(handle->flags & UV_HANDLE_CLOSING);
283 assert(!(handle->flags & UV_HANDLE_CLOSED));
284 uv__handle_close(handle);