rbuv 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +5 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +0 -1
- data/README.md +6 -1
- data/Rakefile +42 -0
- data/deps/libuv/.gitignore +34 -0
- data/deps/libuv/.mailmap +16 -0
- data/deps/libuv/AUTHORS +81 -0
- data/deps/libuv/ChangeLog +45 -0
- data/deps/libuv/LICENSE +41 -0
- data/deps/libuv/Makefile +53 -0
- data/deps/libuv/README.md +118 -0
- data/deps/libuv/build.mk +164 -0
- data/deps/libuv/checksparse.sh +230 -0
- data/deps/libuv/common.gypi +197 -0
- data/deps/libuv/config-mingw.mk +48 -0
- data/deps/libuv/config-unix.mk +167 -0
- data/deps/libuv/gyp_uv +98 -0
- data/deps/libuv/include/uv-private/ngx-queue.h +129 -0
- data/deps/libuv/include/uv-private/stdint-msvc2008.h +247 -0
- data/deps/libuv/include/uv-private/tree.h +768 -0
- data/deps/libuv/include/uv-private/uv-bsd.h +34 -0
- data/deps/libuv/include/uv-private/uv-darwin.h +61 -0
- data/deps/libuv/include/uv-private/uv-linux.h +34 -0
- data/deps/libuv/include/uv-private/uv-sunos.h +44 -0
- data/deps/libuv/include/uv-private/uv-unix.h +332 -0
- data/deps/libuv/include/uv-private/uv-win.h +585 -0
- data/deps/libuv/include/uv.h +1987 -0
- data/deps/libuv/src/fs-poll.c +248 -0
- data/deps/libuv/src/inet.c +298 -0
- data/deps/libuv/src/unix/aix.c +393 -0
- data/deps/libuv/src/unix/async.c +281 -0
- data/deps/libuv/src/unix/core.c +714 -0
- data/deps/libuv/src/unix/cygwin.c +93 -0
- data/deps/libuv/src/unix/darwin-proctitle.m +78 -0
- data/deps/libuv/src/unix/darwin.c +431 -0
- data/deps/libuv/src/unix/dl.c +83 -0
- data/deps/libuv/src/unix/error.c +109 -0
- data/deps/libuv/src/unix/freebsd.c +343 -0
- data/deps/libuv/src/unix/fs.c +869 -0
- data/deps/libuv/src/unix/fsevents.c +299 -0
- data/deps/libuv/src/unix/getaddrinfo.c +159 -0
- data/deps/libuv/src/unix/internal.h +259 -0
- data/deps/libuv/src/unix/kqueue.c +347 -0
- data/deps/libuv/src/unix/linux-core.c +724 -0
- data/deps/libuv/src/unix/linux-inotify.c +236 -0
- data/deps/libuv/src/unix/linux-syscalls.c +388 -0
- data/deps/libuv/src/unix/linux-syscalls.h +150 -0
- data/deps/libuv/src/unix/loop-watcher.c +64 -0
- data/deps/libuv/src/unix/loop.c +114 -0
- data/deps/libuv/src/unix/netbsd.c +353 -0
- data/deps/libuv/src/unix/openbsd.c +304 -0
- data/deps/libuv/src/unix/pipe.c +261 -0
- data/deps/libuv/src/unix/poll.c +108 -0
- data/deps/libuv/src/unix/process.c +501 -0
- data/deps/libuv/src/unix/proctitle.c +103 -0
- data/deps/libuv/src/unix/signal.c +455 -0
- data/deps/libuv/src/unix/stream.c +1380 -0
- data/deps/libuv/src/unix/sunos.c +647 -0
- data/deps/libuv/src/unix/tcp.c +357 -0
- data/deps/libuv/src/unix/thread.c +431 -0
- data/deps/libuv/src/unix/threadpool.c +286 -0
- data/deps/libuv/src/unix/timer.c +153 -0
- data/deps/libuv/src/unix/tty.c +179 -0
- data/deps/libuv/src/unix/udp.c +715 -0
- data/deps/libuv/src/uv-common.c +431 -0
- data/deps/libuv/src/uv-common.h +204 -0
- data/deps/libuv/src/version.c +60 -0
- data/deps/libuv/src/win/async.c +99 -0
- data/deps/libuv/src/win/atomicops-inl.h +56 -0
- data/deps/libuv/src/win/core.c +310 -0
- data/deps/libuv/src/win/dl.c +86 -0
- data/deps/libuv/src/win/error.c +164 -0
- data/deps/libuv/src/win/fs-event.c +506 -0
- data/deps/libuv/src/win/fs.c +1951 -0
- data/deps/libuv/src/win/getaddrinfo.c +365 -0
- data/deps/libuv/src/win/handle-inl.h +164 -0
- data/deps/libuv/src/win/handle.c +153 -0
- data/deps/libuv/src/win/internal.h +346 -0
- data/deps/libuv/src/win/loop-watcher.c +124 -0
- data/deps/libuv/src/win/pipe.c +1656 -0
- data/deps/libuv/src/win/poll.c +615 -0
- data/deps/libuv/src/win/process-stdio.c +503 -0
- data/deps/libuv/src/win/process.c +1048 -0
- data/deps/libuv/src/win/req-inl.h +224 -0
- data/deps/libuv/src/win/req.c +25 -0
- data/deps/libuv/src/win/signal.c +354 -0
- data/deps/libuv/src/win/stream-inl.h +67 -0
- data/deps/libuv/src/win/stream.c +198 -0
- data/deps/libuv/src/win/tcp.c +1422 -0
- data/deps/libuv/src/win/thread.c +666 -0
- data/deps/libuv/src/win/threadpool.c +82 -0
- data/deps/libuv/src/win/timer.c +230 -0
- data/deps/libuv/src/win/tty.c +1857 -0
- data/deps/libuv/src/win/udp.c +744 -0
- data/deps/libuv/src/win/util.c +946 -0
- data/deps/libuv/src/win/winapi.c +152 -0
- data/deps/libuv/src/win/winapi.h +4476 -0
- data/deps/libuv/src/win/winsock.c +560 -0
- data/deps/libuv/src/win/winsock.h +171 -0
- data/deps/libuv/test/benchmark-async-pummel.c +119 -0
- data/deps/libuv/test/benchmark-async.c +139 -0
- data/deps/libuv/test/benchmark-fs-stat.c +136 -0
- data/deps/libuv/test/benchmark-getaddrinfo.c +91 -0
- data/deps/libuv/test/benchmark-list.h +163 -0
- data/deps/libuv/test/benchmark-loop-count.c +90 -0
- data/deps/libuv/test/benchmark-million-async.c +112 -0
- data/deps/libuv/test/benchmark-million-timers.c +77 -0
- data/deps/libuv/test/benchmark-multi-accept.c +432 -0
- data/deps/libuv/test/benchmark-ping-pongs.c +212 -0
- data/deps/libuv/test/benchmark-pound.c +325 -0
- data/deps/libuv/test/benchmark-pump.c +459 -0
- data/deps/libuv/test/benchmark-sizes.c +45 -0
- data/deps/libuv/test/benchmark-spawn.c +163 -0
- data/deps/libuv/test/benchmark-tcp-write-batch.c +141 -0
- data/deps/libuv/test/benchmark-thread.c +64 -0
- data/deps/libuv/test/benchmark-udp-pummel.c +238 -0
- data/deps/libuv/test/blackhole-server.c +118 -0
- data/deps/libuv/test/dns-server.c +329 -0
- data/deps/libuv/test/echo-server.c +384 -0
- data/deps/libuv/test/fixtures/empty_file +0 -0
- data/deps/libuv/test/fixtures/load_error.node +1 -0
- data/deps/libuv/test/run-benchmarks.c +64 -0
- data/deps/libuv/test/run-tests.c +159 -0
- data/deps/libuv/test/runner-unix.c +328 -0
- data/deps/libuv/test/runner-unix.h +36 -0
- data/deps/libuv/test/runner-win.c +318 -0
- data/deps/libuv/test/runner-win.h +43 -0
- data/deps/libuv/test/runner.c +394 -0
- data/deps/libuv/test/runner.h +165 -0
- data/deps/libuv/test/task.h +122 -0
- data/deps/libuv/test/test-active.c +83 -0
- data/deps/libuv/test/test-async.c +136 -0
- data/deps/libuv/test/test-barrier.c +98 -0
- data/deps/libuv/test/test-callback-order.c +77 -0
- data/deps/libuv/test/test-callback-stack.c +204 -0
- data/deps/libuv/test/test-condvar.c +173 -0
- data/deps/libuv/test/test-connection-fail.c +150 -0
- data/deps/libuv/test/test-cwd-and-chdir.c +64 -0
- data/deps/libuv/test/test-delayed-accept.c +189 -0
- data/deps/libuv/test/test-dlerror.c +58 -0
- data/deps/libuv/test/test-embed.c +136 -0
- data/deps/libuv/test/test-error.c +59 -0
- data/deps/libuv/test/test-fail-always.c +29 -0
- data/deps/libuv/test/test-fs-event.c +504 -0
- data/deps/libuv/test/test-fs-poll.c +148 -0
- data/deps/libuv/test/test-fs.c +1899 -0
- data/deps/libuv/test/test-get-currentexe.c +63 -0
- data/deps/libuv/test/test-get-loadavg.c +36 -0
- data/deps/libuv/test/test-get-memory.c +38 -0
- data/deps/libuv/test/test-getaddrinfo.c +120 -0
- data/deps/libuv/test/test-getsockname.c +344 -0
- data/deps/libuv/test/test-hrtime.c +54 -0
- data/deps/libuv/test/test-idle.c +82 -0
- data/deps/libuv/test/test-ipc-send-recv.c +218 -0
- data/deps/libuv/test/test-ipc.c +625 -0
- data/deps/libuv/test/test-list.h +492 -0
- data/deps/libuv/test/test-loop-handles.c +337 -0
- data/deps/libuv/test/test-loop-stop.c +73 -0
- data/deps/libuv/test/test-multiple-listen.c +103 -0
- data/deps/libuv/test/test-mutexes.c +63 -0
- data/deps/libuv/test/test-pass-always.c +28 -0
- data/deps/libuv/test/test-ping-pong.c +250 -0
- data/deps/libuv/test/test-pipe-bind-error.c +144 -0
- data/deps/libuv/test/test-pipe-connect-error.c +98 -0
- data/deps/libuv/test/test-platform-output.c +87 -0
- data/deps/libuv/test/test-poll-close.c +73 -0
- data/deps/libuv/test/test-poll.c +575 -0
- data/deps/libuv/test/test-process-title.c +49 -0
- data/deps/libuv/test/test-ref.c +415 -0
- data/deps/libuv/test/test-run-nowait.c +46 -0
- data/deps/libuv/test/test-run-once.c +49 -0
- data/deps/libuv/test/test-semaphore.c +111 -0
- data/deps/libuv/test/test-shutdown-close.c +105 -0
- data/deps/libuv/test/test-shutdown-eof.c +184 -0
- data/deps/libuv/test/test-signal-multiple-loops.c +270 -0
- data/deps/libuv/test/test-signal.c +152 -0
- data/deps/libuv/test/test-spawn.c +938 -0
- data/deps/libuv/test/test-stdio-over-pipes.c +250 -0
- data/deps/libuv/test/test-tcp-bind-error.c +198 -0
- data/deps/libuv/test/test-tcp-bind6-error.c +159 -0
- data/deps/libuv/test/test-tcp-close-while-connecting.c +81 -0
- data/deps/libuv/test/test-tcp-close.c +130 -0
- data/deps/libuv/test/test-tcp-connect-error-after-write.c +96 -0
- data/deps/libuv/test/test-tcp-connect-error.c +71 -0
- data/deps/libuv/test/test-tcp-connect-timeout.c +86 -0
- data/deps/libuv/test/test-tcp-connect6-error.c +69 -0
- data/deps/libuv/test/test-tcp-flags.c +52 -0
- data/deps/libuv/test/test-tcp-open.c +175 -0
- data/deps/libuv/test/test-tcp-read-stop.c +73 -0
- data/deps/libuv/test/test-tcp-shutdown-after-write.c +132 -0
- data/deps/libuv/test/test-tcp-unexpected-read.c +114 -0
- data/deps/libuv/test/test-tcp-write-to-half-open-connection.c +136 -0
- data/deps/libuv/test/test-tcp-writealot.c +171 -0
- data/deps/libuv/test/test-thread.c +183 -0
- data/deps/libuv/test/test-threadpool-cancel.c +311 -0
- data/deps/libuv/test/test-threadpool.c +77 -0
- data/deps/libuv/test/test-timer-again.c +142 -0
- data/deps/libuv/test/test-timer.c +266 -0
- data/deps/libuv/test/test-tty.c +111 -0
- data/deps/libuv/test/test-udp-dgram-too-big.c +87 -0
- data/deps/libuv/test/test-udp-ipv6.c +158 -0
- data/deps/libuv/test/test-udp-multicast-join.c +140 -0
- data/deps/libuv/test/test-udp-multicast-ttl.c +87 -0
- data/deps/libuv/test/test-udp-open.c +154 -0
- data/deps/libuv/test/test-udp-options.c +87 -0
- data/deps/libuv/test/test-udp-send-and-recv.c +210 -0
- data/deps/libuv/test/test-util.c +97 -0
- data/deps/libuv/test/test-walk-handles.c +78 -0
- data/deps/libuv/uv.gyp +431 -0
- data/deps/libuv/vcbuild.bat +128 -0
- data/ext/rbuv/debug.h +27 -0
- data/ext/rbuv/error.c +7 -0
- data/ext/rbuv/error.h +10 -0
- data/ext/rbuv/extconf.rb +35 -0
- data/ext/rbuv/handle.c +40 -0
- data/ext/rbuv/handle.h +14 -0
- data/ext/rbuv/libuv.mk +12 -0
- data/ext/rbuv/loop.c +50 -0
- data/ext/rbuv/loop.h +13 -0
- data/ext/rbuv/rbuv.c +15 -0
- data/ext/rbuv/rbuv.h +27 -0
- data/ext/rbuv/timer.c +133 -0
- data/ext/rbuv/timer.h +13 -0
- data/lib/rbuv/timer.rb +7 -0
- data/lib/rbuv/version.rb +1 -1
- data/lib/rbuv.rb +24 -2
- data/rbuv.gemspec +5 -1
- data/spec/spec_helper.rb +22 -0
- data/spec/timer_spec.rb +144 -0
- metadata +278 -9
@@ -0,0 +1,248 @@
|
|
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
|
+
#include <assert.h>
|
26
|
+
#include <stdlib.h>
|
27
|
+
#include <string.h>
|
28
|
+
|
29
|
+
struct 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_statbuf_t statbuf;
|
39
|
+
char path[1]; /* variable length */
|
40
|
+
};
|
41
|
+
|
42
|
+
static int statbuf_eq(const uv_statbuf_t* a, const uv_statbuf_t* b);
|
43
|
+
static void poll_cb(uv_fs_t* req);
|
44
|
+
static void timer_cb(uv_timer_t* timer, int status);
|
45
|
+
static void timer_close_cb(uv_handle_t* handle);
|
46
|
+
|
47
|
+
static uv_statbuf_t zero_statbuf;
|
48
|
+
|
49
|
+
|
50
|
+
int 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
|
+
|
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
|
+
|
64
|
+
if (uv__is_active(handle))
|
65
|
+
return 0;
|
66
|
+
|
67
|
+
loop = handle->loop;
|
68
|
+
len = strlen(path);
|
69
|
+
ctx = calloc(1, sizeof(*ctx) + len);
|
70
|
+
|
71
|
+
if (ctx == NULL)
|
72
|
+
return uv__set_artificial_error(loop, UV_ENOMEM);
|
73
|
+
|
74
|
+
ctx->loop = loop;
|
75
|
+
ctx->poll_cb = cb;
|
76
|
+
ctx->interval = interval ? interval : 1;
|
77
|
+
ctx->start_time = uv_now(loop);
|
78
|
+
ctx->parent_handle = handle;
|
79
|
+
memcpy(ctx->path, path, len + 1);
|
80
|
+
|
81
|
+
if (uv_timer_init(loop, &ctx->timer_handle))
|
82
|
+
abort();
|
83
|
+
|
84
|
+
ctx->timer_handle.flags |= UV__HANDLE_INTERNAL;
|
85
|
+
uv__handle_unref(&ctx->timer_handle);
|
86
|
+
|
87
|
+
if (uv_fs_stat(loop, &ctx->fs_req, ctx->path, poll_cb))
|
88
|
+
abort();
|
89
|
+
|
90
|
+
handle->poll_ctx = ctx;
|
91
|
+
uv__handle_start(handle);
|
92
|
+
|
93
|
+
return 0;
|
94
|
+
}
|
95
|
+
|
96
|
+
|
97
|
+
int uv_fs_poll_stop(uv_fs_poll_t* handle) {
|
98
|
+
struct poll_ctx* ctx;
|
99
|
+
|
100
|
+
if (!uv__is_active(handle))
|
101
|
+
return 0;
|
102
|
+
|
103
|
+
ctx = handle->poll_ctx;
|
104
|
+
assert(ctx != NULL);
|
105
|
+
assert(ctx->parent_handle != NULL);
|
106
|
+
ctx->parent_handle = NULL;
|
107
|
+
handle->poll_ctx = NULL;
|
108
|
+
|
109
|
+
/* Close the timer if it's active. If it's inactive, there's a stat request
|
110
|
+
* in progress and poll_cb will take care of the cleanup.
|
111
|
+
*/
|
112
|
+
if (uv__is_active(&ctx->timer_handle))
|
113
|
+
uv_close((uv_handle_t*)&ctx->timer_handle, timer_close_cb);
|
114
|
+
|
115
|
+
uv__handle_stop(handle);
|
116
|
+
|
117
|
+
return 0;
|
118
|
+
}
|
119
|
+
|
120
|
+
|
121
|
+
void uv__fs_poll_close(uv_fs_poll_t* handle) {
|
122
|
+
uv_fs_poll_stop(handle);
|
123
|
+
}
|
124
|
+
|
125
|
+
|
126
|
+
static void timer_cb(uv_timer_t* timer, int status) {
|
127
|
+
struct poll_ctx* ctx;
|
128
|
+
|
129
|
+
ctx = container_of(timer, struct poll_ctx, timer_handle);
|
130
|
+
assert(ctx->parent_handle != NULL);
|
131
|
+
assert(ctx->parent_handle->poll_ctx == ctx);
|
132
|
+
ctx->start_time = uv_now(ctx->loop);
|
133
|
+
|
134
|
+
if (uv_fs_stat(ctx->loop, &ctx->fs_req, ctx->path, poll_cb))
|
135
|
+
abort();
|
136
|
+
}
|
137
|
+
|
138
|
+
|
139
|
+
static void poll_cb(uv_fs_t* req) {
|
140
|
+
uv_statbuf_t* statbuf;
|
141
|
+
struct poll_ctx* ctx;
|
142
|
+
uint64_t interval;
|
143
|
+
|
144
|
+
ctx = container_of(req, struct poll_ctx, fs_req);
|
145
|
+
|
146
|
+
if (ctx->parent_handle == NULL) { /* handle has been stopped or closed */
|
147
|
+
uv_close((uv_handle_t*)&ctx->timer_handle, timer_close_cb);
|
148
|
+
uv_fs_req_cleanup(req);
|
149
|
+
return;
|
150
|
+
}
|
151
|
+
|
152
|
+
if (req->result != 0) {
|
153
|
+
if (ctx->busy_polling != -req->errorno) {
|
154
|
+
uv__set_artificial_error(ctx->loop, req->errorno);
|
155
|
+
ctx->poll_cb(ctx->parent_handle, -1, &ctx->statbuf, &zero_statbuf);
|
156
|
+
ctx->busy_polling = -req->errorno;
|
157
|
+
}
|
158
|
+
goto out;
|
159
|
+
}
|
160
|
+
|
161
|
+
statbuf = &req->statbuf;
|
162
|
+
|
163
|
+
if (ctx->busy_polling != 0)
|
164
|
+
if (ctx->busy_polling < 0 || !statbuf_eq(&ctx->statbuf, statbuf))
|
165
|
+
ctx->poll_cb(ctx->parent_handle, 0, &ctx->statbuf, statbuf);
|
166
|
+
|
167
|
+
ctx->statbuf = *statbuf;
|
168
|
+
ctx->busy_polling = 1;
|
169
|
+
|
170
|
+
out:
|
171
|
+
uv_fs_req_cleanup(req);
|
172
|
+
|
173
|
+
if (ctx->parent_handle == NULL) { /* handle has been stopped by callback */
|
174
|
+
uv_close((uv_handle_t*)&ctx->timer_handle, timer_close_cb);
|
175
|
+
return;
|
176
|
+
}
|
177
|
+
|
178
|
+
/* Reschedule timer, subtract the delay from doing the stat(). */
|
179
|
+
interval = ctx->interval;
|
180
|
+
interval -= (uv_now(ctx->loop) - ctx->start_time) % interval;
|
181
|
+
|
182
|
+
if (uv_timer_start(&ctx->timer_handle, timer_cb, interval, 0))
|
183
|
+
abort();
|
184
|
+
}
|
185
|
+
|
186
|
+
|
187
|
+
static void timer_close_cb(uv_handle_t* handle) {
|
188
|
+
free(container_of(handle, struct poll_ctx, timer_handle));
|
189
|
+
}
|
190
|
+
|
191
|
+
|
192
|
+
static int statbuf_eq(const uv_statbuf_t* a, const uv_statbuf_t* b) {
|
193
|
+
#if defined(_WIN32)
|
194
|
+
return a->st_mtime == b->st_mtime
|
195
|
+
&& a->st_size == b->st_size
|
196
|
+
&& a->st_mode == b->st_mode;
|
197
|
+
#else
|
198
|
+
|
199
|
+
/* Jump through a few hoops to get sub-second granularity on Linux. */
|
200
|
+
# if defined(__linux__)
|
201
|
+
# if defined(__USE_MISC) /* _BSD_SOURCE || _SVID_SOURCE */
|
202
|
+
if (a->st_ctim.tv_nsec != b->st_ctim.tv_nsec) return 0;
|
203
|
+
if (a->st_mtim.tv_nsec != b->st_mtim.tv_nsec) return 0;
|
204
|
+
# else
|
205
|
+
if (a->st_ctimensec != b->st_ctimensec) return 0;
|
206
|
+
if (a->st_mtimensec != b->st_mtimensec) return 0;
|
207
|
+
# endif
|
208
|
+
# endif
|
209
|
+
|
210
|
+
/* Jump through different hoops on OS X. */
|
211
|
+
# if defined(__APPLE__)
|
212
|
+
# if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
|
213
|
+
if (a->st_ctimespec.tv_nsec != b->st_ctimespec.tv_nsec) return 0;
|
214
|
+
if (a->st_mtimespec.tv_nsec != b->st_mtimespec.tv_nsec) return 0;
|
215
|
+
# else
|
216
|
+
if (a->st_ctimensec != b->st_ctimensec) return 0;
|
217
|
+
if (a->st_mtimensec != b->st_mtimensec) return 0;
|
218
|
+
# endif
|
219
|
+
# endif
|
220
|
+
|
221
|
+
/* TODO(bnoordhuis) Other Unices have st_ctim and friends too, provided
|
222
|
+
* the stars and compiler flags are right...
|
223
|
+
*/
|
224
|
+
|
225
|
+
return a->st_ctime == b->st_ctime
|
226
|
+
&& a->st_mtime == b->st_mtime
|
227
|
+
&& a->st_size == b->st_size
|
228
|
+
&& a->st_mode == b->st_mode
|
229
|
+
&& a->st_uid == b->st_uid
|
230
|
+
&& a->st_gid == b->st_gid
|
231
|
+
&& a->st_ino == b->st_ino
|
232
|
+
&& a->st_dev == b->st_dev;
|
233
|
+
#endif
|
234
|
+
}
|
235
|
+
|
236
|
+
|
237
|
+
#if defined(_WIN32)
|
238
|
+
|
239
|
+
#include "win/internal.h"
|
240
|
+
#include "win/handle-inl.h"
|
241
|
+
|
242
|
+
void uv__fs_poll_endgame(uv_loop_t* loop, uv_fs_poll_t* handle) {
|
243
|
+
assert(handle->flags & UV__HANDLE_CLOSING);
|
244
|
+
assert(!(handle->flags & UV_HANDLE_CLOSED));
|
245
|
+
uv__handle_close(handle);
|
246
|
+
}
|
247
|
+
|
248
|
+
#endif /* _WIN32 */
|
@@ -0,0 +1,298 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
|
3
|
+
* Copyright (c) 1996-1999 by Internet Software Consortium.
|
4
|
+
*
|
5
|
+
* Permission to use, copy, modify, and distribute this software for any
|
6
|
+
* purpose with or without fee is hereby granted, provided that the above
|
7
|
+
* copyright notice and this permission notice appear in all copies.
|
8
|
+
*
|
9
|
+
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
|
10
|
+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
11
|
+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
|
12
|
+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
13
|
+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
14
|
+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
|
15
|
+
* OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
16
|
+
*/
|
17
|
+
|
18
|
+
#include <stdio.h>
|
19
|
+
#include <string.h>
|
20
|
+
|
21
|
+
#if defined(_MSC_VER) && _MSC_VER < 1600
|
22
|
+
# include "uv-private/stdint-msvc2008.h"
|
23
|
+
#else
|
24
|
+
# include <stdint.h>
|
25
|
+
#endif
|
26
|
+
|
27
|
+
#include "uv.h"
|
28
|
+
#include "uv-common.h"
|
29
|
+
|
30
|
+
|
31
|
+
static const uv_err_t uv_eafnosupport_ = { UV_EAFNOSUPPORT, 0 };
|
32
|
+
static const uv_err_t uv_enospc_ = { UV_ENOSPC, 0 };
|
33
|
+
static const uv_err_t uv_einval_ = { UV_EINVAL, 0 };
|
34
|
+
|
35
|
+
static uv_err_t inet_ntop4(const unsigned char *src, char *dst, size_t size);
|
36
|
+
static uv_err_t inet_ntop6(const unsigned char *src, char *dst, size_t size);
|
37
|
+
static uv_err_t inet_pton4(const char *src, unsigned char *dst);
|
38
|
+
static uv_err_t inet_pton6(const char *src, unsigned char *dst);
|
39
|
+
|
40
|
+
|
41
|
+
uv_err_t uv_inet_ntop(int af, const void* src, char* dst, size_t size) {
|
42
|
+
switch (af) {
|
43
|
+
case AF_INET:
|
44
|
+
return (inet_ntop4(src, dst, size));
|
45
|
+
case AF_INET6:
|
46
|
+
return (inet_ntop6(src, dst, size));
|
47
|
+
default:
|
48
|
+
return uv_eafnosupport_;
|
49
|
+
}
|
50
|
+
/* NOTREACHED */
|
51
|
+
}
|
52
|
+
|
53
|
+
|
54
|
+
static uv_err_t inet_ntop4(const unsigned char *src, char *dst, size_t size) {
|
55
|
+
static const char fmt[] = "%u.%u.%u.%u";
|
56
|
+
char tmp[sizeof "255.255.255.255"];
|
57
|
+
size_t l;
|
58
|
+
|
59
|
+
#ifndef _WIN32
|
60
|
+
l = snprintf(tmp, sizeof(tmp), fmt, src[0], src[1], src[2], src[3]);
|
61
|
+
#else
|
62
|
+
l = _snprintf(tmp, sizeof(tmp), fmt, src[0], src[1], src[2], src[3]);
|
63
|
+
#endif
|
64
|
+
if (l <= 0 || l >= size) {
|
65
|
+
return uv_enospc_;
|
66
|
+
}
|
67
|
+
strncpy(dst, tmp, size);
|
68
|
+
dst[size - 1] = '\0';
|
69
|
+
return uv_ok_;
|
70
|
+
}
|
71
|
+
|
72
|
+
|
73
|
+
static uv_err_t inet_ntop6(const unsigned char *src, char *dst, size_t size) {
|
74
|
+
/*
|
75
|
+
* Note that int32_t and int16_t need only be "at least" large enough
|
76
|
+
* to contain a value of the specified size. On some systems, like
|
77
|
+
* Crays, there is no such thing as an integer variable with 16 bits.
|
78
|
+
* Keep this in mind if you think this function should have been coded
|
79
|
+
* to use pointer overlays. All the world's not a VAX.
|
80
|
+
*/
|
81
|
+
char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp;
|
82
|
+
struct { int base, len; } best, cur;
|
83
|
+
unsigned int words[sizeof(struct in6_addr) / sizeof(uint16_t)];
|
84
|
+
int i;
|
85
|
+
|
86
|
+
/*
|
87
|
+
* Preprocess:
|
88
|
+
* Copy the input (bytewise) array into a wordwise array.
|
89
|
+
* Find the longest run of 0x00's in src[] for :: shorthanding.
|
90
|
+
*/
|
91
|
+
memset(words, '\0', sizeof words);
|
92
|
+
for (i = 0; i < (int) sizeof(struct in6_addr); i++)
|
93
|
+
words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
|
94
|
+
best.base = -1;
|
95
|
+
best.len = 0;
|
96
|
+
cur.base = -1;
|
97
|
+
cur.len = 0;
|
98
|
+
for (i = 0; i < (int) ARRAY_SIZE(words); i++) {
|
99
|
+
if (words[i] == 0) {
|
100
|
+
if (cur.base == -1)
|
101
|
+
cur.base = i, cur.len = 1;
|
102
|
+
else
|
103
|
+
cur.len++;
|
104
|
+
} else {
|
105
|
+
if (cur.base != -1) {
|
106
|
+
if (best.base == -1 || cur.len > best.len)
|
107
|
+
best = cur;
|
108
|
+
cur.base = -1;
|
109
|
+
}
|
110
|
+
}
|
111
|
+
}
|
112
|
+
if (cur.base != -1) {
|
113
|
+
if (best.base == -1 || cur.len > best.len)
|
114
|
+
best = cur;
|
115
|
+
}
|
116
|
+
if (best.base != -1 && best.len < 2)
|
117
|
+
best.base = -1;
|
118
|
+
|
119
|
+
/*
|
120
|
+
* Format the result.
|
121
|
+
*/
|
122
|
+
tp = tmp;
|
123
|
+
for (i = 0; i < (int) ARRAY_SIZE(words); i++) {
|
124
|
+
/* Are we inside the best run of 0x00's? */
|
125
|
+
if (best.base != -1 && i >= best.base &&
|
126
|
+
i < (best.base + best.len)) {
|
127
|
+
if (i == best.base)
|
128
|
+
*tp++ = ':';
|
129
|
+
continue;
|
130
|
+
}
|
131
|
+
/* Are we following an initial run of 0x00s or any real hex? */
|
132
|
+
if (i != 0)
|
133
|
+
*tp++ = ':';
|
134
|
+
/* Is this address an encapsulated IPv4? */
|
135
|
+
if (i == 6 && best.base == 0 && (best.len == 6 ||
|
136
|
+
(best.len == 7 && words[7] != 0x0001) ||
|
137
|
+
(best.len == 5 && words[5] == 0xffff))) {
|
138
|
+
uv_err_t err = inet_ntop4(src+12, tp, sizeof tmp - (tp - tmp));
|
139
|
+
if (err.code != UV_OK)
|
140
|
+
return err;
|
141
|
+
tp += strlen(tp);
|
142
|
+
break;
|
143
|
+
}
|
144
|
+
tp += sprintf(tp, "%x", words[i]);
|
145
|
+
}
|
146
|
+
/* Was it a trailing run of 0x00's? */
|
147
|
+
if (best.base != -1 && (best.base + best.len) == ARRAY_SIZE(words))
|
148
|
+
*tp++ = ':';
|
149
|
+
*tp++ = '\0';
|
150
|
+
|
151
|
+
/*
|
152
|
+
* Check for overflow, copy, and we're done.
|
153
|
+
*/
|
154
|
+
if ((size_t)(tp - tmp) > size) {
|
155
|
+
return uv_enospc_;
|
156
|
+
}
|
157
|
+
strcpy(dst, tmp);
|
158
|
+
return uv_ok_;
|
159
|
+
}
|
160
|
+
|
161
|
+
|
162
|
+
uv_err_t uv_inet_pton(int af, const char* src, void* dst) {
|
163
|
+
switch (af) {
|
164
|
+
case AF_INET:
|
165
|
+
return (inet_pton4(src, dst));
|
166
|
+
case AF_INET6:
|
167
|
+
return (inet_pton6(src, dst));
|
168
|
+
default:
|
169
|
+
return uv_eafnosupport_;
|
170
|
+
}
|
171
|
+
/* NOTREACHED */
|
172
|
+
}
|
173
|
+
|
174
|
+
|
175
|
+
static uv_err_t inet_pton4(const char *src, unsigned char *dst) {
|
176
|
+
static const char digits[] = "0123456789";
|
177
|
+
int saw_digit, octets, ch;
|
178
|
+
unsigned char tmp[sizeof(struct in_addr)], *tp;
|
179
|
+
|
180
|
+
saw_digit = 0;
|
181
|
+
octets = 0;
|
182
|
+
*(tp = tmp) = 0;
|
183
|
+
while ((ch = *src++) != '\0') {
|
184
|
+
const char *pch;
|
185
|
+
|
186
|
+
if ((pch = strchr(digits, ch)) != NULL) {
|
187
|
+
unsigned int nw = *tp * 10 + (pch - digits);
|
188
|
+
|
189
|
+
if (saw_digit && *tp == 0)
|
190
|
+
return uv_einval_;
|
191
|
+
if (nw > 255)
|
192
|
+
return uv_einval_;
|
193
|
+
*tp = nw;
|
194
|
+
if (!saw_digit) {
|
195
|
+
if (++octets > 4)
|
196
|
+
return uv_einval_;
|
197
|
+
saw_digit = 1;
|
198
|
+
}
|
199
|
+
} else if (ch == '.' && saw_digit) {
|
200
|
+
if (octets == 4)
|
201
|
+
return uv_einval_;
|
202
|
+
*++tp = 0;
|
203
|
+
saw_digit = 0;
|
204
|
+
} else
|
205
|
+
return uv_einval_;
|
206
|
+
}
|
207
|
+
if (octets < 4)
|
208
|
+
return uv_einval_;
|
209
|
+
memcpy(dst, tmp, sizeof(struct in_addr));
|
210
|
+
return uv_ok_;
|
211
|
+
}
|
212
|
+
|
213
|
+
|
214
|
+
static uv_err_t inet_pton6(const char *src, unsigned char *dst) {
|
215
|
+
static const char xdigits_l[] = "0123456789abcdef",
|
216
|
+
xdigits_u[] = "0123456789ABCDEF";
|
217
|
+
unsigned char tmp[sizeof(struct in6_addr)], *tp, *endp, *colonp;
|
218
|
+
const char *xdigits, *curtok;
|
219
|
+
int ch, seen_xdigits;
|
220
|
+
unsigned int val;
|
221
|
+
|
222
|
+
memset((tp = tmp), '\0', sizeof tmp);
|
223
|
+
endp = tp + sizeof tmp;
|
224
|
+
colonp = NULL;
|
225
|
+
/* Leading :: requires some special handling. */
|
226
|
+
if (*src == ':')
|
227
|
+
if (*++src != ':')
|
228
|
+
return uv_einval_;
|
229
|
+
curtok = src;
|
230
|
+
seen_xdigits = 0;
|
231
|
+
val = 0;
|
232
|
+
while ((ch = *src++) != '\0') {
|
233
|
+
const char *pch;
|
234
|
+
|
235
|
+
if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
|
236
|
+
pch = strchr((xdigits = xdigits_u), ch);
|
237
|
+
if (pch != NULL) {
|
238
|
+
val <<= 4;
|
239
|
+
val |= (pch - xdigits);
|
240
|
+
if (++seen_xdigits > 4)
|
241
|
+
return uv_einval_;
|
242
|
+
continue;
|
243
|
+
}
|
244
|
+
if (ch == ':') {
|
245
|
+
curtok = src;
|
246
|
+
if (!seen_xdigits) {
|
247
|
+
if (colonp)
|
248
|
+
return uv_einval_;
|
249
|
+
colonp = tp;
|
250
|
+
continue;
|
251
|
+
} else if (*src == '\0') {
|
252
|
+
return uv_einval_;
|
253
|
+
}
|
254
|
+
if (tp + sizeof(uint16_t) > endp)
|
255
|
+
return uv_einval_;
|
256
|
+
*tp++ = (unsigned char) (val >> 8) & 0xff;
|
257
|
+
*tp++ = (unsigned char) val & 0xff;
|
258
|
+
seen_xdigits = 0;
|
259
|
+
val = 0;
|
260
|
+
continue;
|
261
|
+
}
|
262
|
+
if (ch == '.' && ((tp + sizeof(struct in_addr)) <= endp)) {
|
263
|
+
uv_err_t err = inet_pton4(curtok, tp);
|
264
|
+
if (err.code == 0) {
|
265
|
+
tp += sizeof(struct in_addr);
|
266
|
+
seen_xdigits = 0;
|
267
|
+
break; /*%< '\\0' was seen by inet_pton4(). */
|
268
|
+
}
|
269
|
+
}
|
270
|
+
return uv_einval_;
|
271
|
+
}
|
272
|
+
if (seen_xdigits) {
|
273
|
+
if (tp + sizeof(uint16_t) > endp)
|
274
|
+
return uv_einval_;
|
275
|
+
*tp++ = (unsigned char) (val >> 8) & 0xff;
|
276
|
+
*tp++ = (unsigned char) val & 0xff;
|
277
|
+
}
|
278
|
+
if (colonp != NULL) {
|
279
|
+
/*
|
280
|
+
* Since some memmove()'s erroneously fail to handle
|
281
|
+
* overlapping regions, we'll do the shift by hand.
|
282
|
+
*/
|
283
|
+
const int n = tp - colonp;
|
284
|
+
int i;
|
285
|
+
|
286
|
+
if (tp == endp)
|
287
|
+
return uv_einval_;
|
288
|
+
for (i = 1; i <= n; i++) {
|
289
|
+
endp[- i] = colonp[n - i];
|
290
|
+
colonp[n - i] = 0;
|
291
|
+
}
|
292
|
+
tp = endp;
|
293
|
+
}
|
294
|
+
if (tp != endp)
|
295
|
+
return uv_einval_;
|
296
|
+
memcpy(dst, tmp, sizeof tmp);
|
297
|
+
return uv_ok_;
|
298
|
+
}
|