rbuv 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (233) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +5 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +5 -0
  5. data/Gemfile +0 -1
  6. data/README.md +6 -1
  7. data/Rakefile +42 -0
  8. data/deps/libuv/.gitignore +34 -0
  9. data/deps/libuv/.mailmap +16 -0
  10. data/deps/libuv/AUTHORS +81 -0
  11. data/deps/libuv/ChangeLog +45 -0
  12. data/deps/libuv/LICENSE +41 -0
  13. data/deps/libuv/Makefile +53 -0
  14. data/deps/libuv/README.md +118 -0
  15. data/deps/libuv/build.mk +164 -0
  16. data/deps/libuv/checksparse.sh +230 -0
  17. data/deps/libuv/common.gypi +197 -0
  18. data/deps/libuv/config-mingw.mk +48 -0
  19. data/deps/libuv/config-unix.mk +167 -0
  20. data/deps/libuv/gyp_uv +98 -0
  21. data/deps/libuv/include/uv-private/ngx-queue.h +129 -0
  22. data/deps/libuv/include/uv-private/stdint-msvc2008.h +247 -0
  23. data/deps/libuv/include/uv-private/tree.h +768 -0
  24. data/deps/libuv/include/uv-private/uv-bsd.h +34 -0
  25. data/deps/libuv/include/uv-private/uv-darwin.h +61 -0
  26. data/deps/libuv/include/uv-private/uv-linux.h +34 -0
  27. data/deps/libuv/include/uv-private/uv-sunos.h +44 -0
  28. data/deps/libuv/include/uv-private/uv-unix.h +332 -0
  29. data/deps/libuv/include/uv-private/uv-win.h +585 -0
  30. data/deps/libuv/include/uv.h +1987 -0
  31. data/deps/libuv/src/fs-poll.c +248 -0
  32. data/deps/libuv/src/inet.c +298 -0
  33. data/deps/libuv/src/unix/aix.c +393 -0
  34. data/deps/libuv/src/unix/async.c +281 -0
  35. data/deps/libuv/src/unix/core.c +714 -0
  36. data/deps/libuv/src/unix/cygwin.c +93 -0
  37. data/deps/libuv/src/unix/darwin-proctitle.m +78 -0
  38. data/deps/libuv/src/unix/darwin.c +431 -0
  39. data/deps/libuv/src/unix/dl.c +83 -0
  40. data/deps/libuv/src/unix/error.c +109 -0
  41. data/deps/libuv/src/unix/freebsd.c +343 -0
  42. data/deps/libuv/src/unix/fs.c +869 -0
  43. data/deps/libuv/src/unix/fsevents.c +299 -0
  44. data/deps/libuv/src/unix/getaddrinfo.c +159 -0
  45. data/deps/libuv/src/unix/internal.h +259 -0
  46. data/deps/libuv/src/unix/kqueue.c +347 -0
  47. data/deps/libuv/src/unix/linux-core.c +724 -0
  48. data/deps/libuv/src/unix/linux-inotify.c +236 -0
  49. data/deps/libuv/src/unix/linux-syscalls.c +388 -0
  50. data/deps/libuv/src/unix/linux-syscalls.h +150 -0
  51. data/deps/libuv/src/unix/loop-watcher.c +64 -0
  52. data/deps/libuv/src/unix/loop.c +114 -0
  53. data/deps/libuv/src/unix/netbsd.c +353 -0
  54. data/deps/libuv/src/unix/openbsd.c +304 -0
  55. data/deps/libuv/src/unix/pipe.c +261 -0
  56. data/deps/libuv/src/unix/poll.c +108 -0
  57. data/deps/libuv/src/unix/process.c +501 -0
  58. data/deps/libuv/src/unix/proctitle.c +103 -0
  59. data/deps/libuv/src/unix/signal.c +455 -0
  60. data/deps/libuv/src/unix/stream.c +1380 -0
  61. data/deps/libuv/src/unix/sunos.c +647 -0
  62. data/deps/libuv/src/unix/tcp.c +357 -0
  63. data/deps/libuv/src/unix/thread.c +431 -0
  64. data/deps/libuv/src/unix/threadpool.c +286 -0
  65. data/deps/libuv/src/unix/timer.c +153 -0
  66. data/deps/libuv/src/unix/tty.c +179 -0
  67. data/deps/libuv/src/unix/udp.c +715 -0
  68. data/deps/libuv/src/uv-common.c +431 -0
  69. data/deps/libuv/src/uv-common.h +204 -0
  70. data/deps/libuv/src/version.c +60 -0
  71. data/deps/libuv/src/win/async.c +99 -0
  72. data/deps/libuv/src/win/atomicops-inl.h +56 -0
  73. data/deps/libuv/src/win/core.c +310 -0
  74. data/deps/libuv/src/win/dl.c +86 -0
  75. data/deps/libuv/src/win/error.c +164 -0
  76. data/deps/libuv/src/win/fs-event.c +506 -0
  77. data/deps/libuv/src/win/fs.c +1951 -0
  78. data/deps/libuv/src/win/getaddrinfo.c +365 -0
  79. data/deps/libuv/src/win/handle-inl.h +164 -0
  80. data/deps/libuv/src/win/handle.c +153 -0
  81. data/deps/libuv/src/win/internal.h +346 -0
  82. data/deps/libuv/src/win/loop-watcher.c +124 -0
  83. data/deps/libuv/src/win/pipe.c +1656 -0
  84. data/deps/libuv/src/win/poll.c +615 -0
  85. data/deps/libuv/src/win/process-stdio.c +503 -0
  86. data/deps/libuv/src/win/process.c +1048 -0
  87. data/deps/libuv/src/win/req-inl.h +224 -0
  88. data/deps/libuv/src/win/req.c +25 -0
  89. data/deps/libuv/src/win/signal.c +354 -0
  90. data/deps/libuv/src/win/stream-inl.h +67 -0
  91. data/deps/libuv/src/win/stream.c +198 -0
  92. data/deps/libuv/src/win/tcp.c +1422 -0
  93. data/deps/libuv/src/win/thread.c +666 -0
  94. data/deps/libuv/src/win/threadpool.c +82 -0
  95. data/deps/libuv/src/win/timer.c +230 -0
  96. data/deps/libuv/src/win/tty.c +1857 -0
  97. data/deps/libuv/src/win/udp.c +744 -0
  98. data/deps/libuv/src/win/util.c +946 -0
  99. data/deps/libuv/src/win/winapi.c +152 -0
  100. data/deps/libuv/src/win/winapi.h +4476 -0
  101. data/deps/libuv/src/win/winsock.c +560 -0
  102. data/deps/libuv/src/win/winsock.h +171 -0
  103. data/deps/libuv/test/benchmark-async-pummel.c +119 -0
  104. data/deps/libuv/test/benchmark-async.c +139 -0
  105. data/deps/libuv/test/benchmark-fs-stat.c +136 -0
  106. data/deps/libuv/test/benchmark-getaddrinfo.c +91 -0
  107. data/deps/libuv/test/benchmark-list.h +163 -0
  108. data/deps/libuv/test/benchmark-loop-count.c +90 -0
  109. data/deps/libuv/test/benchmark-million-async.c +112 -0
  110. data/deps/libuv/test/benchmark-million-timers.c +77 -0
  111. data/deps/libuv/test/benchmark-multi-accept.c +432 -0
  112. data/deps/libuv/test/benchmark-ping-pongs.c +212 -0
  113. data/deps/libuv/test/benchmark-pound.c +325 -0
  114. data/deps/libuv/test/benchmark-pump.c +459 -0
  115. data/deps/libuv/test/benchmark-sizes.c +45 -0
  116. data/deps/libuv/test/benchmark-spawn.c +163 -0
  117. data/deps/libuv/test/benchmark-tcp-write-batch.c +141 -0
  118. data/deps/libuv/test/benchmark-thread.c +64 -0
  119. data/deps/libuv/test/benchmark-udp-pummel.c +238 -0
  120. data/deps/libuv/test/blackhole-server.c +118 -0
  121. data/deps/libuv/test/dns-server.c +329 -0
  122. data/deps/libuv/test/echo-server.c +384 -0
  123. data/deps/libuv/test/fixtures/empty_file +0 -0
  124. data/deps/libuv/test/fixtures/load_error.node +1 -0
  125. data/deps/libuv/test/run-benchmarks.c +64 -0
  126. data/deps/libuv/test/run-tests.c +159 -0
  127. data/deps/libuv/test/runner-unix.c +328 -0
  128. data/deps/libuv/test/runner-unix.h +36 -0
  129. data/deps/libuv/test/runner-win.c +318 -0
  130. data/deps/libuv/test/runner-win.h +43 -0
  131. data/deps/libuv/test/runner.c +394 -0
  132. data/deps/libuv/test/runner.h +165 -0
  133. data/deps/libuv/test/task.h +122 -0
  134. data/deps/libuv/test/test-active.c +83 -0
  135. data/deps/libuv/test/test-async.c +136 -0
  136. data/deps/libuv/test/test-barrier.c +98 -0
  137. data/deps/libuv/test/test-callback-order.c +77 -0
  138. data/deps/libuv/test/test-callback-stack.c +204 -0
  139. data/deps/libuv/test/test-condvar.c +173 -0
  140. data/deps/libuv/test/test-connection-fail.c +150 -0
  141. data/deps/libuv/test/test-cwd-and-chdir.c +64 -0
  142. data/deps/libuv/test/test-delayed-accept.c +189 -0
  143. data/deps/libuv/test/test-dlerror.c +58 -0
  144. data/deps/libuv/test/test-embed.c +136 -0
  145. data/deps/libuv/test/test-error.c +59 -0
  146. data/deps/libuv/test/test-fail-always.c +29 -0
  147. data/deps/libuv/test/test-fs-event.c +504 -0
  148. data/deps/libuv/test/test-fs-poll.c +148 -0
  149. data/deps/libuv/test/test-fs.c +1899 -0
  150. data/deps/libuv/test/test-get-currentexe.c +63 -0
  151. data/deps/libuv/test/test-get-loadavg.c +36 -0
  152. data/deps/libuv/test/test-get-memory.c +38 -0
  153. data/deps/libuv/test/test-getaddrinfo.c +120 -0
  154. data/deps/libuv/test/test-getsockname.c +344 -0
  155. data/deps/libuv/test/test-hrtime.c +54 -0
  156. data/deps/libuv/test/test-idle.c +82 -0
  157. data/deps/libuv/test/test-ipc-send-recv.c +218 -0
  158. data/deps/libuv/test/test-ipc.c +625 -0
  159. data/deps/libuv/test/test-list.h +492 -0
  160. data/deps/libuv/test/test-loop-handles.c +337 -0
  161. data/deps/libuv/test/test-loop-stop.c +73 -0
  162. data/deps/libuv/test/test-multiple-listen.c +103 -0
  163. data/deps/libuv/test/test-mutexes.c +63 -0
  164. data/deps/libuv/test/test-pass-always.c +28 -0
  165. data/deps/libuv/test/test-ping-pong.c +250 -0
  166. data/deps/libuv/test/test-pipe-bind-error.c +144 -0
  167. data/deps/libuv/test/test-pipe-connect-error.c +98 -0
  168. data/deps/libuv/test/test-platform-output.c +87 -0
  169. data/deps/libuv/test/test-poll-close.c +73 -0
  170. data/deps/libuv/test/test-poll.c +575 -0
  171. data/deps/libuv/test/test-process-title.c +49 -0
  172. data/deps/libuv/test/test-ref.c +415 -0
  173. data/deps/libuv/test/test-run-nowait.c +46 -0
  174. data/deps/libuv/test/test-run-once.c +49 -0
  175. data/deps/libuv/test/test-semaphore.c +111 -0
  176. data/deps/libuv/test/test-shutdown-close.c +105 -0
  177. data/deps/libuv/test/test-shutdown-eof.c +184 -0
  178. data/deps/libuv/test/test-signal-multiple-loops.c +270 -0
  179. data/deps/libuv/test/test-signal.c +152 -0
  180. data/deps/libuv/test/test-spawn.c +938 -0
  181. data/deps/libuv/test/test-stdio-over-pipes.c +250 -0
  182. data/deps/libuv/test/test-tcp-bind-error.c +198 -0
  183. data/deps/libuv/test/test-tcp-bind6-error.c +159 -0
  184. data/deps/libuv/test/test-tcp-close-while-connecting.c +81 -0
  185. data/deps/libuv/test/test-tcp-close.c +130 -0
  186. data/deps/libuv/test/test-tcp-connect-error-after-write.c +96 -0
  187. data/deps/libuv/test/test-tcp-connect-error.c +71 -0
  188. data/deps/libuv/test/test-tcp-connect-timeout.c +86 -0
  189. data/deps/libuv/test/test-tcp-connect6-error.c +69 -0
  190. data/deps/libuv/test/test-tcp-flags.c +52 -0
  191. data/deps/libuv/test/test-tcp-open.c +175 -0
  192. data/deps/libuv/test/test-tcp-read-stop.c +73 -0
  193. data/deps/libuv/test/test-tcp-shutdown-after-write.c +132 -0
  194. data/deps/libuv/test/test-tcp-unexpected-read.c +114 -0
  195. data/deps/libuv/test/test-tcp-write-to-half-open-connection.c +136 -0
  196. data/deps/libuv/test/test-tcp-writealot.c +171 -0
  197. data/deps/libuv/test/test-thread.c +183 -0
  198. data/deps/libuv/test/test-threadpool-cancel.c +311 -0
  199. data/deps/libuv/test/test-threadpool.c +77 -0
  200. data/deps/libuv/test/test-timer-again.c +142 -0
  201. data/deps/libuv/test/test-timer.c +266 -0
  202. data/deps/libuv/test/test-tty.c +111 -0
  203. data/deps/libuv/test/test-udp-dgram-too-big.c +87 -0
  204. data/deps/libuv/test/test-udp-ipv6.c +158 -0
  205. data/deps/libuv/test/test-udp-multicast-join.c +140 -0
  206. data/deps/libuv/test/test-udp-multicast-ttl.c +87 -0
  207. data/deps/libuv/test/test-udp-open.c +154 -0
  208. data/deps/libuv/test/test-udp-options.c +87 -0
  209. data/deps/libuv/test/test-udp-send-and-recv.c +210 -0
  210. data/deps/libuv/test/test-util.c +97 -0
  211. data/deps/libuv/test/test-walk-handles.c +78 -0
  212. data/deps/libuv/uv.gyp +431 -0
  213. data/deps/libuv/vcbuild.bat +128 -0
  214. data/ext/rbuv/debug.h +27 -0
  215. data/ext/rbuv/error.c +7 -0
  216. data/ext/rbuv/error.h +10 -0
  217. data/ext/rbuv/extconf.rb +35 -0
  218. data/ext/rbuv/handle.c +40 -0
  219. data/ext/rbuv/handle.h +14 -0
  220. data/ext/rbuv/libuv.mk +12 -0
  221. data/ext/rbuv/loop.c +50 -0
  222. data/ext/rbuv/loop.h +13 -0
  223. data/ext/rbuv/rbuv.c +15 -0
  224. data/ext/rbuv/rbuv.h +27 -0
  225. data/ext/rbuv/timer.c +133 -0
  226. data/ext/rbuv/timer.h +13 -0
  227. data/lib/rbuv/timer.rb +7 -0
  228. data/lib/rbuv/version.rb +1 -1
  229. data/lib/rbuv.rb +24 -2
  230. data/rbuv.gemspec +5 -1
  231. data/spec/spec_helper.rb +22 -0
  232. data/spec/timer_spec.rb +144 -0
  233. metadata +278 -9
@@ -0,0 +1,869 @@
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 "internal.h"
24
+
25
+ #include <errno.h>
26
+ #include <stdio.h>
27
+ #include <stdlib.h>
28
+ #include <string.h>
29
+
30
+ #include <sys/types.h>
31
+ #include <sys/stat.h>
32
+ #include <sys/time.h>
33
+ #include <pthread.h>
34
+ #include <dirent.h>
35
+ #include <unistd.h>
36
+ #include <fcntl.h>
37
+ #include <utime.h>
38
+ #include <poll.h>
39
+
40
+ #if defined(__linux__) || defined(__sun)
41
+ # include <sys/sendfile.h>
42
+ #elif defined(__APPLE__) || defined(__FreeBSD__)
43
+ # include <sys/socket.h>
44
+ # include <sys/uio.h>
45
+ #endif
46
+
47
+ #define INIT(type) \
48
+ do { \
49
+ uv__req_init((loop), (req), UV_FS); \
50
+ (req)->fs_type = UV_FS_ ## type; \
51
+ (req)->errorno = 0; \
52
+ (req)->result = 0; \
53
+ (req)->ptr = NULL; \
54
+ (req)->loop = loop; \
55
+ (req)->path = NULL; \
56
+ (req)->new_path = NULL; \
57
+ (req)->cb = (cb); \
58
+ } \
59
+ while (0)
60
+
61
+ #define PATH \
62
+ do { \
63
+ if (NULL == ((req)->path = strdup((path)))) \
64
+ return uv__set_sys_error((loop), ENOMEM); \
65
+ } \
66
+ while (0)
67
+
68
+ #define PATH2 \
69
+ do { \
70
+ size_t path_len; \
71
+ size_t new_path_len; \
72
+ \
73
+ path_len = strlen((path)) + 1; \
74
+ new_path_len = strlen((new_path)) + 1; \
75
+ \
76
+ if (NULL == ((req)->path = malloc(path_len + new_path_len))) \
77
+ return uv__set_sys_error((loop), ENOMEM); \
78
+ \
79
+ (req)->new_path = (req)->path + path_len; \
80
+ memcpy((void*) (req)->path, (path), path_len); \
81
+ memcpy((void*) (req)->new_path, (new_path), new_path_len); \
82
+ } \
83
+ while (0)
84
+
85
+ #define POST \
86
+ do { \
87
+ if ((cb) != NULL) { \
88
+ uv__work_submit((loop), &(req)->work_req, uv__fs_work, uv__fs_done); \
89
+ return 0; \
90
+ } \
91
+ else { \
92
+ uv__fs_work(&(req)->work_req); \
93
+ uv__fs_done(&(req)->work_req, 0); \
94
+ return (req)->result; \
95
+ } \
96
+ } \
97
+ while (0)
98
+
99
+
100
+ static ssize_t uv__fs_fdatasync(uv_fs_t* req) {
101
+ #if defined(__linux__) || defined(__sun) || defined(__NetBSD__)
102
+ return fdatasync(req->file);
103
+ #elif defined(__APPLE__) && defined(F_FULLFSYNC)
104
+ return fcntl(req->file, F_FULLFSYNC);
105
+ #else
106
+ return fsync(req->file);
107
+ #endif
108
+ }
109
+
110
+
111
+ static ssize_t uv__fs_futime(uv_fs_t* req) {
112
+ #if defined(__linux__)
113
+ /* utimesat() has nanosecond resolution but we stick to microseconds
114
+ * for the sake of consistency with other platforms.
115
+ */
116
+ static int no_utimesat;
117
+ struct timespec ts[2];
118
+ struct timeval tv[2];
119
+ char path[sizeof("/proc/self/fd/") + 3 * sizeof(int)];
120
+ int r;
121
+
122
+ if (no_utimesat)
123
+ goto skip;
124
+
125
+ ts[0].tv_sec = req->atime;
126
+ ts[0].tv_nsec = (unsigned long)(req->atime * 1000000) % 1000000 * 1000;
127
+ ts[1].tv_sec = req->mtime;
128
+ ts[1].tv_nsec = (unsigned long)(req->mtime * 1000000) % 1000000 * 1000;
129
+
130
+ r = uv__utimesat(req->file, NULL, ts, 0);
131
+ if (r == 0)
132
+ return r;
133
+
134
+ if (errno != ENOSYS)
135
+ return r;
136
+
137
+ no_utimesat = 1;
138
+
139
+ skip:
140
+
141
+ tv[0].tv_sec = req->atime;
142
+ tv[0].tv_usec = (unsigned long)(req->atime * 1000000) % 1000000;
143
+ tv[1].tv_sec = req->mtime;
144
+ tv[1].tv_usec = (unsigned long)(req->mtime * 1000000) % 1000000;
145
+ snprintf(path, sizeof(path), "/proc/self/fd/%d", (int) req->file);
146
+
147
+ r = utimes(path, tv);
148
+ if (r == 0)
149
+ return r;
150
+
151
+ switch (errno) {
152
+ case ENOENT:
153
+ if (fcntl(req->file, F_GETFL) == -1 && errno == EBADF)
154
+ break;
155
+ /* Fall through. */
156
+
157
+ case EACCES:
158
+ case ENOTDIR:
159
+ errno = ENOSYS;
160
+ break;
161
+ }
162
+
163
+ return r;
164
+
165
+ #elif defined(__APPLE__) \
166
+ || defined(__DragonFly__) \
167
+ || defined(__FreeBSD__) \
168
+ || defined(__sun)
169
+ struct timeval tv[2];
170
+ tv[0].tv_sec = req->atime;
171
+ tv[0].tv_usec = (unsigned long)(req->atime * 1000000) % 1000000;
172
+ tv[1].tv_sec = req->mtime;
173
+ tv[1].tv_usec = (unsigned long)(req->mtime * 1000000) % 1000000;
174
+ return futimes(req->file, tv);
175
+ #else
176
+ errno = ENOSYS;
177
+ return -1;
178
+ #endif
179
+ }
180
+
181
+
182
+ static ssize_t uv__fs_read(uv_fs_t* req) {
183
+ if (req->off < 0)
184
+ return read(req->file, req->buf, req->len);
185
+ else
186
+ return pread(req->file, req->buf, req->len, req->off);
187
+ }
188
+
189
+
190
+ static int uv__fs_readdir_filter(const struct dirent* dent) {
191
+ return strcmp(dent->d_name, ".") != 0 && strcmp(dent->d_name, "..") != 0;
192
+ }
193
+
194
+
195
+ /* This should have been called uv__fs_scandir(). */
196
+ static ssize_t uv__fs_readdir(uv_fs_t* req) {
197
+ struct dirent **dents;
198
+ int saved_errno;
199
+ size_t off;
200
+ size_t len;
201
+ char *buf;
202
+ int i;
203
+ int n;
204
+
205
+ n = scandir(req->path, &dents, uv__fs_readdir_filter, alphasort);
206
+
207
+ if (n == -1 || n == 0)
208
+ return n;
209
+
210
+ len = 0;
211
+
212
+ for (i = 0; i < n; i++)
213
+ len += strlen(dents[i]->d_name) + 1;
214
+
215
+ buf = malloc(len);
216
+
217
+ if (buf == NULL) {
218
+ errno = ENOMEM;
219
+ n = -1;
220
+ goto out;
221
+ }
222
+
223
+ off = 0;
224
+
225
+ for (i = 0; i < n; i++) {
226
+ len = strlen(dents[i]->d_name) + 1;
227
+ memcpy(buf + off, dents[i]->d_name, len);
228
+ off += len;
229
+ }
230
+
231
+ req->ptr = buf;
232
+
233
+ out:
234
+ saved_errno = errno;
235
+ {
236
+ for (i = 0; i < n; i++)
237
+ free(dents[i]);
238
+ free(dents);
239
+ }
240
+ errno = saved_errno;
241
+
242
+ return n;
243
+ }
244
+
245
+
246
+ static ssize_t uv__fs_readlink(uv_fs_t* req) {
247
+ ssize_t len;
248
+ char* buf;
249
+
250
+ len = pathconf(req->path, _PC_PATH_MAX);
251
+
252
+ if (len == -1) {
253
+ #if defined(PATH_MAX)
254
+ len = PATH_MAX;
255
+ #else
256
+ len = 4096;
257
+ #endif
258
+ }
259
+
260
+ buf = malloc(len + 1);
261
+
262
+ if (buf == NULL) {
263
+ errno = ENOMEM;
264
+ return -1;
265
+ }
266
+
267
+ len = readlink(req->path, buf, len);
268
+
269
+ if (len == -1) {
270
+ free(buf);
271
+ return -1;
272
+ }
273
+
274
+ buf[len] = '\0';
275
+ req->ptr = buf;
276
+
277
+ return 0;
278
+ }
279
+
280
+
281
+ static ssize_t uv__fs_sendfile_emul(uv_fs_t* req) {
282
+ struct pollfd pfd;
283
+ int use_pread;
284
+ off_t offset;
285
+ ssize_t nsent;
286
+ ssize_t nread;
287
+ ssize_t nwritten;
288
+ size_t buflen;
289
+ size_t len;
290
+ ssize_t n;
291
+ int in_fd;
292
+ int out_fd;
293
+ char buf[8192];
294
+
295
+ len = req->len;
296
+ in_fd = req->flags;
297
+ out_fd = req->file;
298
+ offset = req->off;
299
+ use_pread = 1;
300
+
301
+ /* Here are the rules regarding errors:
302
+ *
303
+ * 1. Read errors are reported only if nsent==0, otherwise we return nsent.
304
+ * The user needs to know that some data has already been sent, to stop
305
+ * him from sending it twice.
306
+ *
307
+ * 2. Write errors are always reported. Write errors are bad because they
308
+ * mean data loss: we've read data but now we can't write it out.
309
+ *
310
+ * We try to use pread() and fall back to regular read() if the source fd
311
+ * doesn't support positional reads, for example when it's a pipe fd.
312
+ *
313
+ * If we get EAGAIN when writing to the target fd, we poll() on it until
314
+ * it becomes writable again.
315
+ *
316
+ * FIXME: If we get a write error when use_pread==1, it should be safe to
317
+ * return the number of sent bytes instead of an error because pread()
318
+ * is, in theory, idempotent. However, special files in /dev or /proc
319
+ * may support pread() but not necessarily return the same data on
320
+ * successive reads.
321
+ *
322
+ * FIXME: There is no way now to signal that we managed to send *some* data
323
+ * before a write error.
324
+ */
325
+ for (nsent = 0; (size_t) nsent < len; ) {
326
+ buflen = len - nsent;
327
+
328
+ if (buflen > sizeof(buf))
329
+ buflen = sizeof(buf);
330
+
331
+ do
332
+ if (use_pread)
333
+ nread = pread(in_fd, buf, buflen, offset);
334
+ else
335
+ nread = read(in_fd, buf, buflen);
336
+ while (nread == -1 && errno == EINTR);
337
+
338
+ if (nread == 0)
339
+ goto out;
340
+
341
+ if (nread == -1) {
342
+ if (use_pread && nsent == 0 && (errno == EIO || errno == ESPIPE)) {
343
+ use_pread = 0;
344
+ continue;
345
+ }
346
+
347
+ if (nsent == 0)
348
+ nsent = -1;
349
+
350
+ goto out;
351
+ }
352
+
353
+ for (nwritten = 0; nwritten < nread; ) {
354
+ do
355
+ n = write(out_fd, buf + nwritten, nread - nwritten);
356
+ while (n == -1 && errno == EINTR);
357
+
358
+ if (n != -1) {
359
+ nwritten += n;
360
+ continue;
361
+ }
362
+
363
+ if (errno != EAGAIN && errno != EWOULDBLOCK) {
364
+ nsent = -1;
365
+ goto out;
366
+ }
367
+
368
+ pfd.fd = out_fd;
369
+ pfd.events = POLLOUT;
370
+ pfd.revents = 0;
371
+
372
+ do
373
+ n = poll(&pfd, 1, -1);
374
+ while (n == -1 && errno == EINTR);
375
+
376
+ if (n == -1 || (pfd.revents & ~POLLOUT) != 0) {
377
+ errno = EIO;
378
+ nsent = -1;
379
+ goto out;
380
+ }
381
+ }
382
+
383
+ offset += nread;
384
+ nsent += nread;
385
+ }
386
+
387
+ out:
388
+ if (nsent != -1)
389
+ req->off = offset;
390
+
391
+ return nsent;
392
+ }
393
+
394
+
395
+ static ssize_t uv__fs_sendfile(uv_fs_t* req) {
396
+ int in_fd;
397
+ int out_fd;
398
+
399
+ in_fd = req->flags;
400
+ out_fd = req->file;
401
+
402
+ #if defined(__linux__) || defined(__sun)
403
+ {
404
+ off_t off;
405
+ ssize_t r;
406
+
407
+ off = req->off;
408
+ r = sendfile(out_fd, in_fd, &off, req->len);
409
+
410
+ /* sendfile() on SunOS returns EINVAL if the target fd is not a socket but
411
+ * it still writes out data. Fortunately, we can detect it by checking if
412
+ * the offset has been updated.
413
+ */
414
+ if (r != -1 || off > req->off) {
415
+ r = off - req->off;
416
+ req->off = off;
417
+ return r;
418
+ }
419
+
420
+ if (errno == EINVAL ||
421
+ errno == EIO ||
422
+ errno == ENOTSOCK ||
423
+ errno == EXDEV) {
424
+ errno = 0;
425
+ return uv__fs_sendfile_emul(req);
426
+ }
427
+
428
+ return -1;
429
+ }
430
+ #elif defined(__FreeBSD__) || defined(__APPLE__)
431
+ {
432
+ off_t len;
433
+ ssize_t r;
434
+
435
+ /* sendfile() on FreeBSD and Darwin returns EAGAIN if the target fd is in
436
+ * non-blocking mode and not all data could be written. If a non-zero
437
+ * number of bytes have been sent, we don't consider it an error.
438
+ */
439
+ len = 0;
440
+
441
+ #if defined(__FreeBSD__)
442
+ r = sendfile(in_fd, out_fd, req->off, req->len, NULL, &len, 0);
443
+ #else
444
+ r = sendfile(in_fd, out_fd, req->off, &len, NULL, 0);
445
+ #endif
446
+
447
+ if (r != -1 || len != 0) {
448
+ req->off += len;
449
+ return (ssize_t) len;
450
+ }
451
+
452
+ if (errno == EINVAL ||
453
+ errno == EIO ||
454
+ errno == ENOTSOCK ||
455
+ errno == EXDEV) {
456
+ errno = 0;
457
+ return uv__fs_sendfile_emul(req);
458
+ }
459
+
460
+ return -1;
461
+ }
462
+ #else
463
+ return uv__fs_sendfile_emul(req);
464
+ #endif
465
+ }
466
+
467
+
468
+ static ssize_t uv__fs_utime(uv_fs_t* req) {
469
+ struct utimbuf buf;
470
+ buf.actime = req->atime;
471
+ buf.modtime = req->mtime;
472
+ return utime(req->path, &buf); /* TODO use utimes() where available */
473
+ }
474
+
475
+
476
+ static ssize_t uv__fs_write(uv_fs_t* req) {
477
+ ssize_t r;
478
+
479
+ /* Serialize writes on OS X, concurrent write() and pwrite() calls result in
480
+ * data loss. We can't use a per-file descriptor lock, the descriptor may be
481
+ * a dup().
482
+ */
483
+ #if defined(__APPLE__)
484
+ static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
485
+ pthread_mutex_lock(&lock);
486
+ #endif
487
+
488
+ if (req->off < 0)
489
+ r = write(req->file, req->buf, req->len);
490
+ else
491
+ r = pwrite(req->file, req->buf, req->len, req->off);
492
+
493
+ #if defined(__APPLE__)
494
+ pthread_mutex_unlock(&lock);
495
+ #endif
496
+
497
+ return r;
498
+ }
499
+
500
+
501
+ static void uv__fs_work(struct uv__work* w) {
502
+ int retry_on_eintr;
503
+ uv_fs_t* req;
504
+ ssize_t r;
505
+
506
+ req = container_of(w, uv_fs_t, work_req);
507
+ retry_on_eintr = !(req->fs_type == UV_FS_CLOSE);
508
+
509
+ do {
510
+ errno = 0;
511
+
512
+ #define X(type, action) \
513
+ case UV_FS_ ## type: \
514
+ r = action; \
515
+ break;
516
+
517
+ switch (req->fs_type) {
518
+ X(CHMOD, chmod(req->path, req->mode));
519
+ X(CHOWN, chown(req->path, req->uid, req->gid));
520
+ X(CLOSE, close(req->file));
521
+ X(FCHMOD, fchmod(req->file, req->mode));
522
+ X(FCHOWN, fchown(req->file, req->uid, req->gid));
523
+ X(FDATASYNC, uv__fs_fdatasync(req));
524
+ X(FSTAT, fstat(req->file, &req->statbuf));
525
+ X(FSYNC, fsync(req->file));
526
+ X(FTRUNCATE, ftruncate(req->file, req->off));
527
+ X(FUTIME, uv__fs_futime(req));
528
+ X(LSTAT, lstat(req->path, &req->statbuf));
529
+ X(LINK, link(req->path, req->new_path));
530
+ X(MKDIR, mkdir(req->path, req->mode));
531
+ X(OPEN, open(req->path, req->flags, req->mode));
532
+ X(READ, uv__fs_read(req));
533
+ X(READDIR, uv__fs_readdir(req));
534
+ X(READLINK, uv__fs_readlink(req));
535
+ X(RENAME, rename(req->path, req->new_path));
536
+ X(RMDIR, rmdir(req->path));
537
+ X(SENDFILE, uv__fs_sendfile(req));
538
+ X(STAT, stat(req->path, &req->statbuf));
539
+ X(SYMLINK, symlink(req->path, req->new_path));
540
+ X(UNLINK, unlink(req->path));
541
+ X(UTIME, uv__fs_utime(req));
542
+ X(WRITE, uv__fs_write(req));
543
+ default: abort();
544
+ }
545
+
546
+ #undef X
547
+ }
548
+ while (r == -1 && errno == EINTR && retry_on_eintr);
549
+
550
+ req->errorno = errno;
551
+ req->result = r;
552
+
553
+ if (r == 0 && (req->fs_type == UV_FS_STAT ||
554
+ req->fs_type == UV_FS_FSTAT ||
555
+ req->fs_type == UV_FS_LSTAT)) {
556
+ req->ptr = &req->statbuf;
557
+ }
558
+ }
559
+
560
+
561
+ static void uv__fs_done(struct uv__work* w, int status) {
562
+ uv_fs_t* req;
563
+
564
+ req = container_of(w, uv_fs_t, work_req);
565
+ uv__req_unregister(req->loop, req);
566
+
567
+ if (req->errorno != 0) {
568
+ req->errorno = uv_translate_sys_error(req->errorno);
569
+ uv__set_artificial_error(req->loop, req->errorno);
570
+ }
571
+
572
+ if (status == -UV_ECANCELED) {
573
+ assert(req->errorno == 0);
574
+ req->errorno = UV_ECANCELED;
575
+ uv__set_artificial_error(req->loop, UV_ECANCELED);
576
+ }
577
+
578
+ if (req->cb != NULL)
579
+ req->cb(req);
580
+ }
581
+
582
+
583
+ int uv_fs_chmod(uv_loop_t* loop,
584
+ uv_fs_t* req,
585
+ const char* path,
586
+ int mode,
587
+ uv_fs_cb cb) {
588
+ INIT(CHMOD);
589
+ PATH;
590
+ req->mode = mode;
591
+ POST;
592
+ }
593
+
594
+
595
+ int uv_fs_chown(uv_loop_t* loop,
596
+ uv_fs_t* req,
597
+ const char* path,
598
+ int uid,
599
+ int gid,
600
+ uv_fs_cb cb) {
601
+ INIT(CHOWN);
602
+ PATH;
603
+ req->uid = uid;
604
+ req->gid = gid;
605
+ POST;
606
+ }
607
+
608
+
609
+ int uv_fs_close(uv_loop_t* loop, uv_fs_t* req, uv_file file, uv_fs_cb cb) {
610
+ INIT(CLOSE);
611
+ req->file = file;
612
+ POST;
613
+ }
614
+
615
+
616
+ int uv_fs_fchmod(uv_loop_t* loop,
617
+ uv_fs_t* req,
618
+ uv_file file,
619
+ int mode,
620
+ uv_fs_cb cb) {
621
+ INIT(FCHMOD);
622
+ req->file = file;
623
+ req->mode = mode;
624
+ POST;
625
+ }
626
+
627
+
628
+ int uv_fs_fchown(uv_loop_t* loop,
629
+ uv_fs_t* req,
630
+ uv_file file,
631
+ int uid,
632
+ int gid,
633
+ uv_fs_cb cb) {
634
+ INIT(FCHOWN);
635
+ req->file = file;
636
+ req->uid = uid;
637
+ req->gid = gid;
638
+ POST;
639
+ }
640
+
641
+
642
+ int uv_fs_fdatasync(uv_loop_t* loop, uv_fs_t* req, uv_file file, uv_fs_cb cb) {
643
+ INIT(FDATASYNC);
644
+ req->file = file;
645
+ POST;
646
+ }
647
+
648
+
649
+ int uv_fs_fstat(uv_loop_t* loop, uv_fs_t* req, uv_file file, uv_fs_cb cb) {
650
+ INIT(FSTAT);
651
+ req->file = file;
652
+ POST;
653
+ }
654
+
655
+
656
+ int uv_fs_fsync(uv_loop_t* loop, uv_fs_t* req, uv_file file, uv_fs_cb cb) {
657
+ INIT(FSYNC);
658
+ req->file = file;
659
+ POST;
660
+ }
661
+
662
+
663
+ int uv_fs_ftruncate(uv_loop_t* loop,
664
+ uv_fs_t* req,
665
+ uv_file file,
666
+ int64_t off,
667
+ uv_fs_cb cb) {
668
+ INIT(FTRUNCATE);
669
+ req->file = file;
670
+ req->off = off;
671
+ POST;
672
+ }
673
+
674
+
675
+ int uv_fs_futime(uv_loop_t* loop,
676
+ uv_fs_t* req,
677
+ uv_file file,
678
+ double atime,
679
+ double mtime,
680
+ uv_fs_cb cb) {
681
+ INIT(FUTIME);
682
+ req->file = file;
683
+ req->atime = atime;
684
+ req->mtime = mtime;
685
+ POST;
686
+ }
687
+
688
+
689
+ int uv_fs_lstat(uv_loop_t* loop, uv_fs_t* req, const char* path, uv_fs_cb cb) {
690
+ INIT(LSTAT);
691
+ PATH;
692
+ POST;
693
+ }
694
+
695
+
696
+ int uv_fs_link(uv_loop_t* loop,
697
+ uv_fs_t* req,
698
+ const char* path,
699
+ const char* new_path,
700
+ uv_fs_cb cb) {
701
+ INIT(LINK);
702
+ PATH2;
703
+ POST;
704
+ }
705
+
706
+
707
+ int uv_fs_mkdir(uv_loop_t* loop,
708
+ uv_fs_t* req,
709
+ const char* path,
710
+ int mode,
711
+ uv_fs_cb cb) {
712
+ INIT(MKDIR);
713
+ PATH;
714
+ req->mode = mode;
715
+ POST;
716
+ }
717
+
718
+
719
+ int uv_fs_open(uv_loop_t* loop,
720
+ uv_fs_t* req,
721
+ const char* path,
722
+ int flags,
723
+ int mode,
724
+ uv_fs_cb cb) {
725
+ INIT(OPEN);
726
+ PATH;
727
+ req->flags = flags;
728
+ req->mode = mode;
729
+ POST;
730
+ }
731
+
732
+
733
+ int uv_fs_read(uv_loop_t* loop, uv_fs_t* req,
734
+ uv_file file,
735
+ void* buf,
736
+ size_t len,
737
+ int64_t off,
738
+ uv_fs_cb cb) {
739
+ INIT(READ);
740
+ req->file = file;
741
+ req->buf = buf;
742
+ req->len = len;
743
+ req->off = off;
744
+ POST;
745
+ }
746
+
747
+
748
+ int uv_fs_readdir(uv_loop_t* loop,
749
+ uv_fs_t* req,
750
+ const char* path,
751
+ int flags,
752
+ uv_fs_cb cb) {
753
+ INIT(READDIR);
754
+ PATH;
755
+ req->flags = flags;
756
+ POST;
757
+ }
758
+
759
+
760
+ int uv_fs_readlink(uv_loop_t* loop,
761
+ uv_fs_t* req,
762
+ const char* path,
763
+ uv_fs_cb cb) {
764
+ INIT(READLINK);
765
+ PATH;
766
+ POST;
767
+ }
768
+
769
+
770
+ int uv_fs_rename(uv_loop_t* loop,
771
+ uv_fs_t* req,
772
+ const char* path,
773
+ const char* new_path,
774
+ uv_fs_cb cb) {
775
+ INIT(RENAME);
776
+ PATH2;
777
+ POST;
778
+ }
779
+
780
+
781
+ int uv_fs_rmdir(uv_loop_t* loop, uv_fs_t* req, const char* path, uv_fs_cb cb) {
782
+ INIT(RMDIR);
783
+ PATH;
784
+ POST;
785
+ }
786
+
787
+
788
+ int uv_fs_sendfile(uv_loop_t* loop,
789
+ uv_fs_t* req,
790
+ uv_file out_fd,
791
+ uv_file in_fd,
792
+ int64_t off,
793
+ size_t len,
794
+ uv_fs_cb cb) {
795
+ INIT(SENDFILE);
796
+ req->flags = in_fd; /* hack */
797
+ req->file = out_fd;
798
+ req->off = off;
799
+ req->len = len;
800
+ POST;
801
+ }
802
+
803
+
804
+ int uv_fs_stat(uv_loop_t* loop, uv_fs_t* req, const char* path, uv_fs_cb cb) {
805
+ INIT(STAT);
806
+ PATH;
807
+ POST;
808
+ }
809
+
810
+
811
+ int uv_fs_symlink(uv_loop_t* loop,
812
+ uv_fs_t* req,
813
+ const char* path,
814
+ const char* new_path,
815
+ int flags,
816
+ uv_fs_cb cb) {
817
+ INIT(SYMLINK);
818
+ PATH2;
819
+ req->flags = flags;
820
+ POST;
821
+ }
822
+
823
+
824
+ int uv_fs_unlink(uv_loop_t* loop, uv_fs_t* req, const char* path, uv_fs_cb cb) {
825
+ INIT(UNLINK);
826
+ PATH;
827
+ POST;
828
+ }
829
+
830
+
831
+ int uv_fs_utime(uv_loop_t* loop,
832
+ uv_fs_t* req,
833
+ const char* path,
834
+ double atime,
835
+ double mtime,
836
+ uv_fs_cb cb) {
837
+ INIT(UTIME);
838
+ PATH;
839
+ req->atime = atime;
840
+ req->mtime = mtime;
841
+ POST;
842
+ }
843
+
844
+
845
+ int uv_fs_write(uv_loop_t* loop,
846
+ uv_fs_t* req,
847
+ uv_file file,
848
+ void* buf,
849
+ size_t len,
850
+ int64_t off,
851
+ uv_fs_cb cb) {
852
+ INIT(WRITE);
853
+ req->file = file;
854
+ req->buf = buf;
855
+ req->len = len;
856
+ req->off = off;
857
+ POST;
858
+ }
859
+
860
+
861
+ void uv_fs_req_cleanup(uv_fs_t* req) {
862
+ free((void*) req->path);
863
+ req->path = NULL;
864
+ req->new_path = NULL;
865
+
866
+ if (req->ptr != &req->statbuf)
867
+ free(req->ptr);
868
+ req->ptr = NULL;
869
+ }