fs-poll.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. #include "uv.h"
  22. #include "uv-common.h"
  23. #ifdef _WIN32
  24. #include "win/internal.h"
  25. #include "win/handle-inl.h"
  26. #define uv__make_close_pending(h) uv_want_endgame((h)->loop, (h))
  27. #else
  28. #include "unix/internal.h"
  29. #endif
  30. #include <assert.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. struct poll_ctx {
  34. uv_fs_poll_t* parent_handle;
  35. int busy_polling;
  36. unsigned int interval;
  37. uint64_t start_time;
  38. uv_loop_t* loop;
  39. uv_fs_poll_cb poll_cb;
  40. uv_timer_t timer_handle;
  41. uv_fs_t fs_req; /* TODO(bnoordhuis) mark fs_req internal */
  42. uv_stat_t statbuf;
  43. struct poll_ctx* previous; /* context from previous start()..stop() period */
  44. char path[1]; /* variable length */
  45. };
  46. static int statbuf_eq(const uv_stat_t* a, const uv_stat_t* b);
  47. static void poll_cb(uv_fs_t* req);
  48. static void timer_cb(uv_timer_t* timer);
  49. static void timer_close_cb(uv_handle_t* handle);
  50. static uv_stat_t zero_statbuf;
  51. int uv_fs_poll_init(uv_loop_t* loop, uv_fs_poll_t* handle) {
  52. uv__handle_init(loop, (uv_handle_t*)handle, UV_FS_POLL);
  53. handle->poll_ctx = NULL;
  54. return 0;
  55. }
  56. int 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. if (uv_is_active((uv_handle_t*)handle))
  65. return 0;
  66. loop = handle->loop;
  67. len = strlen(path);
  68. ctx = uv__calloc(1, sizeof(*ctx) + len);
  69. if (ctx == NULL)
  70. return UV_ENOMEM;
  71. ctx->loop = loop;
  72. ctx->poll_cb = cb;
  73. ctx->interval = interval ? interval : 1;
  74. ctx->start_time = uv_now(loop);
  75. ctx->parent_handle = handle;
  76. memcpy(ctx->path, path, len + 1);
  77. err = uv_timer_init(loop, &ctx->timer_handle);
  78. if (err < 0)
  79. goto error;
  80. ctx->timer_handle.flags |= UV_HANDLE_INTERNAL;
  81. uv__handle_unref(&ctx->timer_handle);
  82. err = uv_fs_stat(loop, &ctx->fs_req, ctx->path, poll_cb);
  83. if (err < 0)
  84. goto error;
  85. if (handle->poll_ctx != NULL)
  86. ctx->previous = handle->poll_ctx;
  87. handle->poll_ctx = ctx;
  88. uv__handle_start(handle);
  89. return 0;
  90. error:
  91. uv__free(ctx);
  92. return err;
  93. }
  94. int uv_fs_poll_stop(uv_fs_poll_t* handle) {
  95. struct poll_ctx* ctx;
  96. if (!uv_is_active((uv_handle_t*)handle))
  97. return 0;
  98. ctx = handle->poll_ctx;
  99. assert(ctx != NULL);
  100. assert(ctx->parent_handle == handle);
  101. /* Close the timer if it's active. If it's inactive, there's a stat request
  102. * in progress and poll_cb will take care of the cleanup.
  103. */
  104. if (uv_is_active((uv_handle_t*)&ctx->timer_handle))
  105. uv_close((uv_handle_t*)&ctx->timer_handle, timer_close_cb);
  106. uv__handle_stop(handle);
  107. return 0;
  108. }
  109. int uv_fs_poll_getpath(uv_fs_poll_t* handle, char* buffer, size_t* size) {
  110. struct poll_ctx* ctx;
  111. size_t required_len;
  112. if (!uv_is_active((uv_handle_t*)handle)) {
  113. *size = 0;
  114. return UV_EINVAL;
  115. }
  116. ctx = handle->poll_ctx;
  117. assert(ctx != NULL);
  118. required_len = strlen(ctx->path);
  119. if (required_len >= *size) {
  120. *size = required_len + 1;
  121. return UV_ENOBUFS;
  122. }
  123. memcpy(buffer, ctx->path, required_len);
  124. *size = required_len;
  125. buffer[required_len] = '\0';
  126. return 0;
  127. }
  128. void uv__fs_poll_close(uv_fs_poll_t* handle) {
  129. uv_fs_poll_stop(handle);
  130. if (handle->poll_ctx == NULL)
  131. uv__make_close_pending((uv_handle_t*)handle);
  132. }
  133. static void timer_cb(uv_timer_t* timer) {
  134. struct poll_ctx* ctx;
  135. ctx = container_of(timer, struct poll_ctx, timer_handle);
  136. assert(ctx->parent_handle != NULL);
  137. assert(ctx->parent_handle->poll_ctx == ctx);
  138. ctx->start_time = uv_now(ctx->loop);
  139. if (uv_fs_stat(ctx->loop, &ctx->fs_req, ctx->path, poll_cb))
  140. abort();
  141. }
  142. static void poll_cb(uv_fs_t* req) {
  143. uv_stat_t* statbuf;
  144. struct poll_ctx* ctx;
  145. uint64_t interval;
  146. uv_fs_poll_t* handle;
  147. ctx = container_of(req, struct poll_ctx, fs_req);
  148. handle = ctx->parent_handle;
  149. if (!uv_is_active((uv_handle_t*)handle) || uv__is_closing(handle))
  150. goto out;
  151. if (req->result != 0) {
  152. if (ctx->busy_polling != req->result) {
  153. ctx->poll_cb(ctx->parent_handle,
  154. req->result,
  155. &ctx->statbuf,
  156. &zero_statbuf);
  157. ctx->busy_polling = req->result;
  158. }
  159. goto out;
  160. }
  161. statbuf = &req->statbuf;
  162. if (ctx->busy_polling != 0)
  163. if (ctx->busy_polling < 0 || !statbuf_eq(&ctx->statbuf, statbuf))
  164. ctx->poll_cb(ctx->parent_handle, 0, &ctx->statbuf, statbuf);
  165. ctx->statbuf = *statbuf;
  166. ctx->busy_polling = 1;
  167. out:
  168. uv_fs_req_cleanup(req);
  169. if (!uv_is_active((uv_handle_t*)handle) || uv__is_closing(handle)) {
  170. uv_close((uv_handle_t*)&ctx->timer_handle, timer_close_cb);
  171. return;
  172. }
  173. /* Reschedule timer, subtract the delay from doing the stat(). */
  174. interval = ctx->interval;
  175. interval -= (uv_now(ctx->loop) - ctx->start_time) % interval;
  176. if (uv_timer_start(&ctx->timer_handle, timer_cb, interval, 0))
  177. abort();
  178. }
  179. static void timer_close_cb(uv_handle_t* timer) {
  180. struct poll_ctx* ctx;
  181. struct poll_ctx* it;
  182. struct poll_ctx* last;
  183. uv_fs_poll_t* handle;
  184. ctx = container_of(timer, struct poll_ctx, timer_handle);
  185. handle = ctx->parent_handle;
  186. if (ctx == handle->poll_ctx) {
  187. handle->poll_ctx = ctx->previous;
  188. if (handle->poll_ctx == NULL && uv__is_closing(handle))
  189. uv__make_close_pending((uv_handle_t*)handle);
  190. } else {
  191. for (last = handle->poll_ctx, it = last->previous;
  192. it != ctx;
  193. last = it, it = it->previous) {
  194. assert(last->previous != NULL);
  195. }
  196. last->previous = ctx->previous;
  197. }
  198. uv__free(ctx);
  199. }
  200. static int statbuf_eq(const uv_stat_t* a, const uv_stat_t* b) {
  201. return a->st_ctim.tv_nsec == b->st_ctim.tv_nsec
  202. && a->st_mtim.tv_nsec == b->st_mtim.tv_nsec
  203. && a->st_birthtim.tv_nsec == b->st_birthtim.tv_nsec
  204. && a->st_ctim.tv_sec == b->st_ctim.tv_sec
  205. && a->st_mtim.tv_sec == b->st_mtim.tv_sec
  206. && a->st_birthtim.tv_sec == b->st_birthtim.tv_sec
  207. && a->st_size == b->st_size
  208. && a->st_mode == b->st_mode
  209. && a->st_uid == b->st_uid
  210. && a->st_gid == b->st_gid
  211. && a->st_ino == b->st_ino
  212. && a->st_dev == b->st_dev
  213. && a->st_flags == b->st_flags
  214. && a->st_gen == b->st_gen;
  215. }
  216. #if defined(_WIN32)
  217. #include "win/internal.h"
  218. #include "win/handle-inl.h"
  219. void uv__fs_poll_endgame(uv_loop_t* loop, uv_fs_poll_t* handle) {
  220. assert(handle->flags & UV_HANDLE_CLOSING);
  221. assert(!(handle->flags & UV_HANDLE_CLOSED));
  222. uv__handle_close(handle);
  223. }
  224. #endif /* _WIN32 */