fs-poll.c 7.43 KB
Newer Older
libuv upstream's avatar
libuv upstream committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

#include "uv.h"
#include "uv-common.h"

libuv upstream's avatar
libuv upstream committed
25 26 27 28 29 30 31 32
#ifdef _WIN32
#include "win/internal.h"
#include "win/handle-inl.h"
#define uv__make_close_pending(h) uv_want_endgame((h)->loop, (h))
#else
#include "unix/internal.h"
#endif

libuv upstream's avatar
libuv upstream committed
33 34 35 36 37
#include <assert.h>
#include <stdlib.h>
#include <string.h>

struct poll_ctx {
libuv upstream's avatar
libuv upstream committed
38
  uv_fs_poll_t* parent_handle;
libuv upstream's avatar
libuv upstream committed
39 40 41 42 43 44 45 46
  int busy_polling;
  unsigned int interval;
  uint64_t start_time;
  uv_loop_t* loop;
  uv_fs_poll_cb poll_cb;
  uv_timer_t timer_handle;
  uv_fs_t fs_req; /* TODO(bnoordhuis) mark fs_req internal */
  uv_stat_t statbuf;
libuv upstream's avatar
libuv upstream committed
47
  struct poll_ctx* previous; /* context from previous start()..stop() period */
libuv upstream's avatar
libuv upstream committed
48 49 50 51 52 53 54 55 56 57 58 59 60
  char path[1]; /* variable length */
};

static int statbuf_eq(const uv_stat_t* a, const uv_stat_t* b);
static void poll_cb(uv_fs_t* req);
static void timer_cb(uv_timer_t* timer);
static void timer_close_cb(uv_handle_t* handle);

static uv_stat_t zero_statbuf;


int uv_fs_poll_init(uv_loop_t* loop, uv_fs_poll_t* handle) {
  uv__handle_init(loop, (uv_handle_t*)handle, UV_FS_POLL);
libuv upstream's avatar
libuv upstream committed
61
  handle->poll_ctx = NULL;
libuv upstream's avatar
libuv upstream committed
62 63 64 65 66 67 68 69 70 71 72 73 74
  return 0;
}


int uv_fs_poll_start(uv_fs_poll_t* handle,
                     uv_fs_poll_cb cb,
                     const char* path,
                     unsigned int interval) {
  struct poll_ctx* ctx;
  uv_loop_t* loop;
  size_t len;
  int err;

libuv upstream's avatar
libuv upstream committed
75
  if (uv_is_active((uv_handle_t*)handle))
libuv upstream's avatar
libuv upstream committed
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    return 0;

  loop = handle->loop;
  len = strlen(path);
  ctx = uv__calloc(1, sizeof(*ctx) + len);

  if (ctx == NULL)
    return UV_ENOMEM;

  ctx->loop = loop;
  ctx->poll_cb = cb;
  ctx->interval = interval ? interval : 1;
  ctx->start_time = uv_now(loop);
  ctx->parent_handle = handle;
  memcpy(ctx->path, path, len + 1);

  err = uv_timer_init(loop, &ctx->timer_handle);
  if (err < 0)
    goto error;

libuv upstream's avatar
libuv upstream committed
96
  ctx->timer_handle.flags |= UV_HANDLE_INTERNAL;
libuv upstream's avatar
libuv upstream committed
97 98 99 100 101 102
  uv__handle_unref(&ctx->timer_handle);

  err = uv_fs_stat(loop, &ctx->fs_req, ctx->path, poll_cb);
  if (err < 0)
    goto error;

libuv upstream's avatar
libuv upstream committed
103 104
  if (handle->poll_ctx != NULL)
    ctx->previous = handle->poll_ctx;
libuv upstream's avatar
libuv upstream committed
105 106 107 108 109 110 111 112 113 114 115 116 117 118
  handle->poll_ctx = ctx;
  uv__handle_start(handle);

  return 0;

error:
  uv__free(ctx);
  return err;
}


int uv_fs_poll_stop(uv_fs_poll_t* handle) {
  struct poll_ctx* ctx;

libuv upstream's avatar
libuv upstream committed
119
  if (!uv_is_active((uv_handle_t*)handle))
libuv upstream's avatar
libuv upstream committed
120 121 122 123
    return 0;

  ctx = handle->poll_ctx;
  assert(ctx != NULL);
libuv upstream's avatar
libuv upstream committed
124
  assert(ctx->parent_handle == handle);
libuv upstream's avatar
libuv upstream committed
125 126 127 128

  /* Close the timer if it's active. If it's inactive, there's a stat request
   * in progress and poll_cb will take care of the cleanup.
   */
libuv upstream's avatar
libuv upstream committed
129
  if (uv_is_active((uv_handle_t*)&ctx->timer_handle))
libuv upstream's avatar
libuv upstream committed
130 131 132 133 134 135 136 137 138 139 140 141
    uv_close((uv_handle_t*)&ctx->timer_handle, timer_close_cb);

  uv__handle_stop(handle);

  return 0;
}


int uv_fs_poll_getpath(uv_fs_poll_t* handle, char* buffer, size_t* size) {
  struct poll_ctx* ctx;
  size_t required_len;

libuv upstream's avatar
libuv upstream committed
142
  if (!uv_is_active((uv_handle_t*)handle)) {
libuv upstream's avatar
libuv upstream committed
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
    *size = 0;
    return UV_EINVAL;
  }

  ctx = handle->poll_ctx;
  assert(ctx != NULL);

  required_len = strlen(ctx->path);
  if (required_len >= *size) {
    *size = required_len + 1;
    return UV_ENOBUFS;
  }

  memcpy(buffer, ctx->path, required_len);
  *size = required_len;
  buffer[required_len] = '\0';

  return 0;
}


void uv__fs_poll_close(uv_fs_poll_t* handle) {
  uv_fs_poll_stop(handle);
libuv upstream's avatar
libuv upstream committed
166 167 168

  if (handle->poll_ctx == NULL)
    uv__make_close_pending((uv_handle_t*)handle);
libuv upstream's avatar
libuv upstream committed
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
}


static void timer_cb(uv_timer_t* timer) {
  struct poll_ctx* ctx;

  ctx = container_of(timer, struct poll_ctx, timer_handle);
  assert(ctx->parent_handle != NULL);
  assert(ctx->parent_handle->poll_ctx == ctx);
  ctx->start_time = uv_now(ctx->loop);

  if (uv_fs_stat(ctx->loop, &ctx->fs_req, ctx->path, poll_cb))
    abort();
}


static void poll_cb(uv_fs_t* req) {
  uv_stat_t* statbuf;
  struct poll_ctx* ctx;
  uint64_t interval;
libuv upstream's avatar
libuv upstream committed
189
  uv_fs_poll_t* handle;
libuv upstream's avatar
libuv upstream committed
190 191

  ctx = container_of(req, struct poll_ctx, fs_req);
libuv upstream's avatar
libuv upstream committed
192
  handle = ctx->parent_handle;
libuv upstream's avatar
libuv upstream committed
193

libuv upstream's avatar
libuv upstream committed
194 195
  if (!uv_is_active((uv_handle_t*)handle) || uv__is_closing(handle))
    goto out;
libuv upstream's avatar
libuv upstream committed
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219

  if (req->result != 0) {
    if (ctx->busy_polling != req->result) {
      ctx->poll_cb(ctx->parent_handle,
                   req->result,
                   &ctx->statbuf,
                   &zero_statbuf);
      ctx->busy_polling = req->result;
    }
    goto out;
  }

  statbuf = &req->statbuf;

  if (ctx->busy_polling != 0)
    if (ctx->busy_polling < 0 || !statbuf_eq(&ctx->statbuf, statbuf))
      ctx->poll_cb(ctx->parent_handle, 0, &ctx->statbuf, statbuf);

  ctx->statbuf = *statbuf;
  ctx->busy_polling = 1;

out:
  uv_fs_req_cleanup(req);

libuv upstream's avatar
libuv upstream committed
220
  if (!uv_is_active((uv_handle_t*)handle) || uv__is_closing(handle)) {
libuv upstream's avatar
libuv upstream committed
221 222 223 224 225 226 227 228 229 230 231 232 233
    uv_close((uv_handle_t*)&ctx->timer_handle, timer_close_cb);
    return;
  }

  /* Reschedule timer, subtract the delay from doing the stat(). */
  interval = ctx->interval;
  interval -= (uv_now(ctx->loop) - ctx->start_time) % interval;

  if (uv_timer_start(&ctx->timer_handle, timer_cb, interval, 0))
    abort();
}


libuv upstream's avatar
libuv upstream committed
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
static void timer_close_cb(uv_handle_t* timer) {
  struct poll_ctx* ctx;
  struct poll_ctx* it;
  struct poll_ctx* last;
  uv_fs_poll_t* handle;

  ctx = container_of(timer, struct poll_ctx, timer_handle);
  handle = ctx->parent_handle;
  if (ctx == handle->poll_ctx) {
    handle->poll_ctx = ctx->previous;
    if (handle->poll_ctx == NULL && uv__is_closing(handle))
      uv__make_close_pending((uv_handle_t*)handle);
  } else {
    for (last = handle->poll_ctx, it = last->previous;
         it != ctx;
         last = it, it = it->previous) {
      assert(last->previous != NULL);
    }
    last->previous = ctx->previous;
  }
  uv__free(ctx);
libuv upstream's avatar
libuv upstream committed
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
}


static int statbuf_eq(const uv_stat_t* a, const uv_stat_t* b) {
  return a->st_ctim.tv_nsec == b->st_ctim.tv_nsec
      && a->st_mtim.tv_nsec == b->st_mtim.tv_nsec
      && a->st_birthtim.tv_nsec == b->st_birthtim.tv_nsec
      && a->st_ctim.tv_sec == b->st_ctim.tv_sec
      && a->st_mtim.tv_sec == b->st_mtim.tv_sec
      && a->st_birthtim.tv_sec == b->st_birthtim.tv_sec
      && a->st_size == b->st_size
      && a->st_mode == b->st_mode
      && a->st_uid == b->st_uid
      && a->st_gid == b->st_gid
      && a->st_ino == b->st_ino
      && a->st_dev == b->st_dev
      && a->st_flags == b->st_flags
      && a->st_gen == b->st_gen;
}


#if defined(_WIN32)

#include "win/internal.h"
#include "win/handle-inl.h"

void uv__fs_poll_endgame(uv_loop_t* loop, uv_fs_poll_t* handle) {
libuv upstream's avatar
libuv upstream committed
282
  assert(handle->flags & UV_HANDLE_CLOSING);
libuv upstream's avatar
libuv upstream committed
283 284 285 286 287
  assert(!(handle->flags & UV_HANDLE_CLOSED));
  uv__handle_close(handle);
}

#endif /* _WIN32 */