Coverage Report

Created: 2026-06-15 07:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Utilities/cmlibuv/src/fs-poll.c
Line
Count
Source
1
/* 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
#ifdef _WIN32
26
#include "win/internal.h"
27
#include "win/handle-inl.h"
28
#define uv__make_close_pending(h) uv__want_endgame((h)->loop, (h))
29
#else
30
#include "unix/internal.h"
31
#endif
32
33
#include <assert.h>
34
#include <stdlib.h>
35
#include <string.h>
36
37
struct poll_ctx {
38
  uv_fs_poll_t* parent_handle;
39
  int busy_polling;
40
  unsigned int interval;
41
  uint64_t start_time;
42
  uv_loop_t* loop;
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 */
46
  uv_stat_t statbuf;
47
  struct poll_ctx* previous; /* context from previous start()..stop() period */
48
  char path[1]; /* variable length */
49
};
50
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* timer);
55
56
static uv_stat_t zero_statbuf;
57
58
59
0
int uv_fs_poll_init(uv_loop_t* loop, uv_fs_poll_t* handle) {
60
0
  uv__handle_init(loop, (uv_handle_t*)handle, UV_FS_POLL);
61
0
  handle->poll_ctx = NULL;
62
0
  return 0;
63
0
}
64
65
66
int uv_fs_poll_start(uv_fs_poll_t* handle,
67
                     uv_fs_poll_cb cb,
68
                     const char* path,
69
0
                     unsigned int interval) {
70
0
  struct poll_ctx* ctx;
71
0
  uv_loop_t* loop;
72
0
  size_t len;
73
0
  int err;
74
75
0
  if (uv_is_active((uv_handle_t*)handle))
76
0
    return 0;
77
78
0
  loop = handle->loop;
79
0
  len = strlen(path);
80
0
  ctx = uv__calloc(1, sizeof(*ctx) + len);
81
82
0
  if (ctx == NULL)
83
0
    return UV_ENOMEM;
84
85
0
  ctx->loop = loop;
86
0
  ctx->poll_cb = cb;
87
0
  ctx->interval = interval ? interval : 1;
88
0
  ctx->start_time = uv_now(loop);
89
0
  ctx->parent_handle = handle;
90
0
  memcpy(ctx->path, path, len + 1);
91
92
0
  err = uv_timer_init(loop, &ctx->timer_handle);
93
0
  if (err < 0)
94
0
    goto error;
95
96
0
  ctx->timer_handle.flags |= UV_HANDLE_INTERNAL;
97
0
  uv__handle_unref(&ctx->timer_handle);
98
99
0
  err = uv_fs_stat(loop, &ctx->fs_req, ctx->path, poll_cb);
100
0
  if (err < 0)
101
0
    goto error;
102
103
0
  if (handle->poll_ctx != NULL)
104
0
    ctx->previous = handle->poll_ctx;
105
0
  handle->poll_ctx = ctx;
106
0
  uv__handle_start(handle);
107
108
0
  return 0;
109
110
0
error:
111
0
  uv__free(ctx);
112
0
  return err;
113
0
}
114
115
116
0
int uv_fs_poll_stop(uv_fs_poll_t* handle) {
117
0
  struct poll_ctx* ctx;
118
119
0
  if (!uv_is_active((uv_handle_t*)handle))
120
0
    return 0;
121
122
0
  ctx = handle->poll_ctx;
123
0
  assert(ctx != NULL);
124
0
  assert(ctx->parent_handle == handle);
125
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.
128
   */
129
0
  if (uv_is_active((uv_handle_t*)&ctx->timer_handle))
130
0
    uv_close((uv_handle_t*)&ctx->timer_handle, timer_close_cb);
131
132
0
  uv__handle_stop(handle);
133
134
0
  return 0;
135
0
}
136
137
138
0
int uv_fs_poll_getpath(uv_fs_poll_t* handle, char* buffer, size_t* size) {
139
0
  struct poll_ctx* ctx;
140
0
  size_t required_len;
141
142
0
  if (buffer == NULL || size == NULL || *size == 0)
143
0
    return UV_EINVAL;
144
145
0
  if (!uv_is_active((uv_handle_t*)handle)) {
146
0
    *size = 0;
147
0
    return UV_EINVAL;
148
0
  }
149
150
0
  ctx = handle->poll_ctx;
151
0
  assert(ctx != NULL);
152
153
0
  required_len = strlen(ctx->path);
154
0
  if (required_len >= *size) {
155
0
    *size = required_len + 1;
156
0
    return UV_ENOBUFS;
157
0
  }
158
159
0
  memcpy(buffer, ctx->path, required_len);
160
0
  *size = required_len;
161
0
  buffer[required_len] = '\0';
162
163
0
  return 0;
164
0
}
165
166
167
0
void uv__fs_poll_close(uv_fs_poll_t* handle) {
168
0
  uv_fs_poll_stop(handle);
169
170
0
  if (handle->poll_ctx == NULL)
171
0
    uv__make_close_pending((uv_handle_t*)handle);
172
0
}
173
174
175
0
static void timer_cb(uv_timer_t* timer) {
176
0
  struct poll_ctx* ctx;
177
178
0
  ctx = container_of(timer, struct poll_ctx, timer_handle);
179
0
  assert(ctx->parent_handle != NULL);
180
0
  assert(ctx->parent_handle->poll_ctx == ctx);
181
0
  ctx->start_time = uv_now(ctx->loop);
182
183
0
  if (uv_fs_stat(ctx->loop, &ctx->fs_req, ctx->path, poll_cb))
184
0
    abort();
185
0
}
186
187
188
0
static void poll_cb(uv_fs_t* req) {
189
0
  uv_stat_t* statbuf;
190
0
  struct poll_ctx* ctx;
191
0
  uint64_t interval;
192
0
  uv_fs_poll_t* handle;
193
194
0
  ctx = container_of(req, struct poll_ctx, fs_req);
195
0
  handle = ctx->parent_handle;
196
197
0
  if (!uv_is_active((uv_handle_t*)handle) || uv__is_closing(handle))
198
0
    goto out;
199
200
0
  if (req->result != 0) {
201
0
    if (ctx->busy_polling != req->result) {
202
0
      ctx->poll_cb(ctx->parent_handle,
203
0
                   req->result,
204
0
                   &ctx->statbuf,
205
0
                   &zero_statbuf);
206
0
      ctx->busy_polling = req->result;
207
0
    }
208
0
    goto out;
209
0
  }
210
211
0
  statbuf = &req->statbuf;
212
213
0
  if (ctx->busy_polling != 0)
214
0
    if (ctx->busy_polling < 0 || !statbuf_eq(&ctx->statbuf, statbuf))
215
0
      ctx->poll_cb(ctx->parent_handle, 0, &ctx->statbuf, statbuf);
216
217
0
  ctx->statbuf = *statbuf;
218
0
  ctx->busy_polling = 1;
219
220
0
out:
221
0
  uv_fs_req_cleanup(req);
222
223
0
  if (!uv_is_active((uv_handle_t*)handle) || uv__is_closing(handle)) {
224
0
    uv_close((uv_handle_t*)&ctx->timer_handle, timer_close_cb);
225
0
    return;
226
0
  }
227
228
  /* Reschedule timer, subtract the delay from doing the stat(). */
229
0
  interval = ctx->interval;
230
0
  interval -= (uv_now(ctx->loop) - ctx->start_time) % interval;
231
232
0
  if (uv_timer_start(&ctx->timer_handle, timer_cb, interval, 0))
233
0
    abort();
234
0
}
235
236
237
0
static void timer_close_cb(uv_handle_t* timer) {
238
0
  struct poll_ctx* ctx;
239
0
  struct poll_ctx* it;
240
0
  struct poll_ctx* last;
241
0
  uv_fs_poll_t* handle;
242
243
0
  ctx = container_of(timer, struct poll_ctx, timer_handle);
244
0
  handle = ctx->parent_handle;
245
0
  if (ctx == handle->poll_ctx) {
246
0
    handle->poll_ctx = ctx->previous;
247
0
    if (handle->poll_ctx == NULL && uv__is_closing(handle))
248
0
      uv__make_close_pending((uv_handle_t*)handle);
249
0
  } else {
250
0
    for (last = handle->poll_ctx, it = last->previous;
251
0
         it != ctx;
252
0
         last = it, it = it->previous) {
253
0
      assert(last->previous != NULL);
254
0
    }
255
0
    last->previous = ctx->previous;
256
0
  }
257
0
  uv__free(ctx);
258
0
}
259
260
261
0
static int statbuf_eq(const uv_stat_t* a, const uv_stat_t* b) {
262
0
  return a->st_ctim.tv_nsec == b->st_ctim.tv_nsec
263
0
      && a->st_mtim.tv_nsec == b->st_mtim.tv_nsec
264
0
      && a->st_birthtim.tv_nsec == b->st_birthtim.tv_nsec
265
0
      && a->st_ctim.tv_sec == b->st_ctim.tv_sec
266
0
      && a->st_mtim.tv_sec == b->st_mtim.tv_sec
267
0
      && a->st_birthtim.tv_sec == b->st_birthtim.tv_sec
268
0
      && a->st_size == b->st_size
269
0
      && a->st_mode == b->st_mode
270
0
      && a->st_uid == b->st_uid
271
0
      && a->st_gid == b->st_gid
272
0
      && a->st_ino == b->st_ino
273
0
      && a->st_dev == b->st_dev
274
0
      && a->st_flags == b->st_flags
275
0
      && a->st_gen == b->st_gen;
276
0
}
277
278
279
#if defined(_WIN32)
280
281
#include "win/internal.h"
282
#include "win/handle-inl.h"
283
284
void uv__fs_poll_endgame(uv_loop_t* loop, uv_fs_poll_t* handle) {
285
  assert(handle->flags & UV_HANDLE_CLOSING);
286
  assert(!(handle->flags & UV_HANDLE_CLOSED));
287
  uv__handle_close(handle);
288
}
289
290
#endif /* _WIN32 */