foolio 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (261) hide show
  1. data/.gitignore +22 -0
  2. data/Gemfile +4 -0
  3. data/LICENSE +22 -0
  4. data/README.md +29 -0
  5. data/Rakefile +21 -0
  6. data/examples/timer.rb +20 -0
  7. data/ext/foolio/extconf.rb +34 -0
  8. data/ext/foolio/foolio_ext.c +921 -0
  9. data/ext/foolio/gen.rb +50 -0
  10. data/ext/foolio/make_table.rb +12 -0
  11. data/ext/foolio/templ +243 -0
  12. data/ext/libuv/.gitignore +33 -0
  13. data/ext/libuv/.mailmap +13 -0
  14. data/ext/libuv/.travis.yml +9 -0
  15. data/ext/libuv/AUTHORS +61 -0
  16. data/ext/libuv/LICENSE +44 -0
  17. data/ext/libuv/Makefile +71 -0
  18. data/ext/libuv/README.md +90 -0
  19. data/ext/libuv/common.gypi +178 -0
  20. data/ext/libuv/gyp_uv +73 -0
  21. data/ext/libuv/include/uv-private/eio.h +403 -0
  22. data/ext/libuv/include/uv-private/ev.h +838 -0
  23. data/ext/libuv/include/uv-private/ngx-queue.h +108 -0
  24. data/ext/libuv/include/uv-private/tree.h +768 -0
  25. data/ext/libuv/include/uv-private/uv-unix.h +324 -0
  26. data/ext/libuv/include/uv-private/uv-win.h +517 -0
  27. data/ext/libuv/include/uv.h +1838 -0
  28. data/ext/libuv/src/fs-poll.c +235 -0
  29. data/ext/libuv/src/inet.c +293 -0
  30. data/ext/libuv/src/unix/async.c +148 -0
  31. data/ext/libuv/src/unix/core.c +696 -0
  32. data/ext/libuv/src/unix/cygwin.c +83 -0
  33. data/ext/libuv/src/unix/darwin.c +342 -0
  34. data/ext/libuv/src/unix/dl.c +83 -0
  35. data/ext/libuv/src/unix/eio/Changes +63 -0
  36. data/ext/libuv/src/unix/eio/LICENSE +36 -0
  37. data/ext/libuv/src/unix/eio/Makefile.am +15 -0
  38. data/ext/libuv/src/unix/eio/aclocal.m4 +8957 -0
  39. data/ext/libuv/src/unix/eio/autogen.sh +3 -0
  40. data/ext/libuv/src/unix/eio/config.h.in +86 -0
  41. data/ext/libuv/src/unix/eio/config_cygwin.h +80 -0
  42. data/ext/libuv/src/unix/eio/config_darwin.h +141 -0
  43. data/ext/libuv/src/unix/eio/config_freebsd.h +81 -0
  44. data/ext/libuv/src/unix/eio/config_linux.h +94 -0
  45. data/ext/libuv/src/unix/eio/config_netbsd.h +81 -0
  46. data/ext/libuv/src/unix/eio/config_openbsd.h +137 -0
  47. data/ext/libuv/src/unix/eio/config_sunos.h +84 -0
  48. data/ext/libuv/src/unix/eio/configure.ac +22 -0
  49. data/ext/libuv/src/unix/eio/demo.c +194 -0
  50. data/ext/libuv/src/unix/eio/ecb.h +370 -0
  51. data/ext/libuv/src/unix/eio/eio.3 +3428 -0
  52. data/ext/libuv/src/unix/eio/eio.c +2593 -0
  53. data/ext/libuv/src/unix/eio/eio.pod +969 -0
  54. data/ext/libuv/src/unix/eio/libeio.m4 +195 -0
  55. data/ext/libuv/src/unix/eio/xthread.h +164 -0
  56. data/ext/libuv/src/unix/error.c +105 -0
  57. data/ext/libuv/src/unix/ev/Changes +388 -0
  58. data/ext/libuv/src/unix/ev/LICENSE +36 -0
  59. data/ext/libuv/src/unix/ev/Makefile.am +18 -0
  60. data/ext/libuv/src/unix/ev/Makefile.in +771 -0
  61. data/ext/libuv/src/unix/ev/README +58 -0
  62. data/ext/libuv/src/unix/ev/aclocal.m4 +8957 -0
  63. data/ext/libuv/src/unix/ev/autogen.sh +6 -0
  64. data/ext/libuv/src/unix/ev/config.guess +1526 -0
  65. data/ext/libuv/src/unix/ev/config.h.in +125 -0
  66. data/ext/libuv/src/unix/ev/config.sub +1658 -0
  67. data/ext/libuv/src/unix/ev/config_cygwin.h +123 -0
  68. data/ext/libuv/src/unix/ev/config_darwin.h +122 -0
  69. data/ext/libuv/src/unix/ev/config_freebsd.h +120 -0
  70. data/ext/libuv/src/unix/ev/config_linux.h +141 -0
  71. data/ext/libuv/src/unix/ev/config_netbsd.h +120 -0
  72. data/ext/libuv/src/unix/ev/config_openbsd.h +126 -0
  73. data/ext/libuv/src/unix/ev/config_sunos.h +122 -0
  74. data/ext/libuv/src/unix/ev/configure +13037 -0
  75. data/ext/libuv/src/unix/ev/configure.ac +18 -0
  76. data/ext/libuv/src/unix/ev/depcomp +630 -0
  77. data/ext/libuv/src/unix/ev/ev++.h +816 -0
  78. data/ext/libuv/src/unix/ev/ev.3 +5311 -0
  79. data/ext/libuv/src/unix/ev/ev.c +3925 -0
  80. data/ext/libuv/src/unix/ev/ev.pod +5243 -0
  81. data/ext/libuv/src/unix/ev/ev_epoll.c +266 -0
  82. data/ext/libuv/src/unix/ev/ev_kqueue.c +235 -0
  83. data/ext/libuv/src/unix/ev/ev_poll.c +148 -0
  84. data/ext/libuv/src/unix/ev/ev_port.c +179 -0
  85. data/ext/libuv/src/unix/ev/ev_select.c +310 -0
  86. data/ext/libuv/src/unix/ev/ev_vars.h +203 -0
  87. data/ext/libuv/src/unix/ev/ev_win32.c +153 -0
  88. data/ext/libuv/src/unix/ev/ev_wrap.h +196 -0
  89. data/ext/libuv/src/unix/ev/event.c +402 -0
  90. data/ext/libuv/src/unix/ev/event.h +170 -0
  91. data/ext/libuv/src/unix/ev/install-sh +294 -0
  92. data/ext/libuv/src/unix/ev/libev.m4 +39 -0
  93. data/ext/libuv/src/unix/ev/ltmain.sh +8413 -0
  94. data/ext/libuv/src/unix/ev/missing +336 -0
  95. data/ext/libuv/src/unix/ev/mkinstalldirs +111 -0
  96. data/ext/libuv/src/unix/freebsd.c +326 -0
  97. data/ext/libuv/src/unix/fs.c +739 -0
  98. data/ext/libuv/src/unix/internal.h +188 -0
  99. data/ext/libuv/src/unix/kqueue.c +120 -0
  100. data/ext/libuv/src/unix/linux/inotify.c +239 -0
  101. data/ext/libuv/src/unix/linux/linux-core.c +557 -0
  102. data/ext/libuv/src/unix/linux/syscalls.c +388 -0
  103. data/ext/libuv/src/unix/linux/syscalls.h +124 -0
  104. data/ext/libuv/src/unix/loop-watcher.c +62 -0
  105. data/ext/libuv/src/unix/loop.c +94 -0
  106. data/ext/libuv/src/unix/netbsd.c +108 -0
  107. data/ext/libuv/src/unix/openbsd.c +295 -0
  108. data/ext/libuv/src/unix/pipe.c +259 -0
  109. data/ext/libuv/src/unix/poll.c +114 -0
  110. data/ext/libuv/src/unix/process.c +495 -0
  111. data/ext/libuv/src/unix/signal.c +269 -0
  112. data/ext/libuv/src/unix/stream.c +990 -0
  113. data/ext/libuv/src/unix/sunos.c +481 -0
  114. data/ext/libuv/src/unix/tcp.c +393 -0
  115. data/ext/libuv/src/unix/thread.c +251 -0
  116. data/ext/libuv/src/unix/timer.c +136 -0
  117. data/ext/libuv/src/unix/tty.c +145 -0
  118. data/ext/libuv/src/unix/udp.c +659 -0
  119. data/ext/libuv/src/unix/uv-eio.c +107 -0
  120. data/ext/libuv/src/unix/uv-eio.h +13 -0
  121. data/ext/libuv/src/uv-common.c +380 -0
  122. data/ext/libuv/src/uv-common.h +170 -0
  123. data/ext/libuv/src/win/async.c +100 -0
  124. data/ext/libuv/src/win/atomicops-inl.h +56 -0
  125. data/ext/libuv/src/win/core.c +278 -0
  126. data/ext/libuv/src/win/dl.c +86 -0
  127. data/ext/libuv/src/win/error.c +155 -0
  128. data/ext/libuv/src/win/fs-event.c +510 -0
  129. data/ext/libuv/src/win/fs.c +1948 -0
  130. data/ext/libuv/src/win/getaddrinfo.c +365 -0
  131. data/ext/libuv/src/win/handle-inl.h +149 -0
  132. data/ext/libuv/src/win/handle.c +154 -0
  133. data/ext/libuv/src/win/internal.h +343 -0
  134. data/ext/libuv/src/win/loop-watcher.c +122 -0
  135. data/ext/libuv/src/win/pipe.c +1672 -0
  136. data/ext/libuv/src/win/poll.c +616 -0
  137. data/ext/libuv/src/win/process-stdio.c +500 -0
  138. data/ext/libuv/src/win/process.c +1013 -0
  139. data/ext/libuv/src/win/req-inl.h +220 -0
  140. data/ext/libuv/src/win/req.c +25 -0
  141. data/ext/libuv/src/win/signal.c +57 -0
  142. data/ext/libuv/src/win/stream-inl.h +67 -0
  143. data/ext/libuv/src/win/stream.c +167 -0
  144. data/ext/libuv/src/win/tcp.c +1394 -0
  145. data/ext/libuv/src/win/thread.c +372 -0
  146. data/ext/libuv/src/win/threadpool.c +74 -0
  147. data/ext/libuv/src/win/timer.c +224 -0
  148. data/ext/libuv/src/win/tty.c +1799 -0
  149. data/ext/libuv/src/win/udp.c +716 -0
  150. data/ext/libuv/src/win/util.c +864 -0
  151. data/ext/libuv/src/win/winapi.c +132 -0
  152. data/ext/libuv/src/win/winapi.h +4452 -0
  153. data/ext/libuv/src/win/winsock.c +557 -0
  154. data/ext/libuv/src/win/winsock.h +171 -0
  155. data/ext/libuv/test/benchmark-async-pummel.c +97 -0
  156. data/ext/libuv/test/benchmark-async.c +137 -0
  157. data/ext/libuv/test/benchmark-fs-stat.c +135 -0
  158. data/ext/libuv/test/benchmark-getaddrinfo.c +94 -0
  159. data/ext/libuv/test/benchmark-list.h +127 -0
  160. data/ext/libuv/test/benchmark-loop-count.c +88 -0
  161. data/ext/libuv/test/benchmark-million-timers.c +65 -0
  162. data/ext/libuv/test/benchmark-ping-pongs.c +213 -0
  163. data/ext/libuv/test/benchmark-pound.c +324 -0
  164. data/ext/libuv/test/benchmark-pump.c +462 -0
  165. data/ext/libuv/test/benchmark-sizes.c +44 -0
  166. data/ext/libuv/test/benchmark-spawn.c +162 -0
  167. data/ext/libuv/test/benchmark-tcp-write-batch.c +140 -0
  168. data/ext/libuv/test/benchmark-thread.c +64 -0
  169. data/ext/libuv/test/benchmark-udp-packet-storm.c +247 -0
  170. data/ext/libuv/test/blackhole-server.c +118 -0
  171. data/ext/libuv/test/dns-server.c +321 -0
  172. data/ext/libuv/test/echo-server.c +378 -0
  173. data/ext/libuv/test/fixtures/empty_file +0 -0
  174. data/ext/libuv/test/fixtures/load_error.node +1 -0
  175. data/ext/libuv/test/run-benchmarks.c +64 -0
  176. data/ext/libuv/test/run-tests.c +138 -0
  177. data/ext/libuv/test/runner-unix.c +295 -0
  178. data/ext/libuv/test/runner-unix.h +36 -0
  179. data/ext/libuv/test/runner-win.c +285 -0
  180. data/ext/libuv/test/runner-win.h +42 -0
  181. data/ext/libuv/test/runner.c +355 -0
  182. data/ext/libuv/test/runner.h +159 -0
  183. data/ext/libuv/test/task.h +112 -0
  184. data/ext/libuv/test/test-async.c +118 -0
  185. data/ext/libuv/test/test-callback-order.c +76 -0
  186. data/ext/libuv/test/test-callback-stack.c +203 -0
  187. data/ext/libuv/test/test-connection-fail.c +148 -0
  188. data/ext/libuv/test/test-cwd-and-chdir.c +64 -0
  189. data/ext/libuv/test/test-delayed-accept.c +188 -0
  190. data/ext/libuv/test/test-dlerror.c +58 -0
  191. data/ext/libuv/test/test-error.c +59 -0
  192. data/ext/libuv/test/test-fail-always.c +29 -0
  193. data/ext/libuv/test/test-fs-event.c +474 -0
  194. data/ext/libuv/test/test-fs-poll.c +146 -0
  195. data/ext/libuv/test/test-fs.c +1843 -0
  196. data/ext/libuv/test/test-get-currentexe.c +63 -0
  197. data/ext/libuv/test/test-get-loadavg.c +36 -0
  198. data/ext/libuv/test/test-get-memory.c +38 -0
  199. data/ext/libuv/test/test-getaddrinfo.c +122 -0
  200. data/ext/libuv/test/test-getsockname.c +342 -0
  201. data/ext/libuv/test/test-hrtime.c +54 -0
  202. data/ext/libuv/test/test-idle.c +81 -0
  203. data/ext/libuv/test/test-ipc-send-recv.c +209 -0
  204. data/ext/libuv/test/test-ipc.c +620 -0
  205. data/ext/libuv/test/test-list.h +427 -0
  206. data/ext/libuv/test/test-loop-handles.c +336 -0
  207. data/ext/libuv/test/test-multiple-listen.c +102 -0
  208. data/ext/libuv/test/test-mutexes.c +63 -0
  209. data/ext/libuv/test/test-pass-always.c +28 -0
  210. data/ext/libuv/test/test-ping-pong.c +253 -0
  211. data/ext/libuv/test/test-pipe-bind-error.c +140 -0
  212. data/ext/libuv/test/test-pipe-connect-error.c +96 -0
  213. data/ext/libuv/test/test-platform-output.c +87 -0
  214. data/ext/libuv/test/test-poll-close.c +72 -0
  215. data/ext/libuv/test/test-poll.c +573 -0
  216. data/ext/libuv/test/test-process-title.c +49 -0
  217. data/ext/libuv/test/test-ref.c +338 -0
  218. data/ext/libuv/test/test-run-once.c +48 -0
  219. data/ext/libuv/test/test-semaphore.c +111 -0
  220. data/ext/libuv/test/test-shutdown-close.c +103 -0
  221. data/ext/libuv/test/test-shutdown-eof.c +183 -0
  222. data/ext/libuv/test/test-signal.c +162 -0
  223. data/ext/libuv/test/test-spawn.c +863 -0
  224. data/ext/libuv/test/test-stdio-over-pipes.c +246 -0
  225. data/ext/libuv/test/test-tcp-bind-error.c +191 -0
  226. data/ext/libuv/test/test-tcp-bind6-error.c +154 -0
  227. data/ext/libuv/test/test-tcp-close-while-connecting.c +80 -0
  228. data/ext/libuv/test/test-tcp-close.c +129 -0
  229. data/ext/libuv/test/test-tcp-connect-error-after-write.c +95 -0
  230. data/ext/libuv/test/test-tcp-connect-error.c +70 -0
  231. data/ext/libuv/test/test-tcp-connect-timeout.c +85 -0
  232. data/ext/libuv/test/test-tcp-connect6-error.c +68 -0
  233. data/ext/libuv/test/test-tcp-flags.c +51 -0
  234. data/ext/libuv/test/test-tcp-shutdown-after-write.c +131 -0
  235. data/ext/libuv/test/test-tcp-unexpected-read.c +113 -0
  236. data/ext/libuv/test/test-tcp-write-error.c +168 -0
  237. data/ext/libuv/test/test-tcp-write-to-half-open-connection.c +135 -0
  238. data/ext/libuv/test/test-tcp-writealot.c +170 -0
  239. data/ext/libuv/test/test-thread.c +183 -0
  240. data/ext/libuv/test/test-threadpool.c +57 -0
  241. data/ext/libuv/test/test-timer-again.c +141 -0
  242. data/ext/libuv/test/test-timer.c +152 -0
  243. data/ext/libuv/test/test-tty.c +110 -0
  244. data/ext/libuv/test/test-udp-dgram-too-big.c +86 -0
  245. data/ext/libuv/test/test-udp-ipv6.c +156 -0
  246. data/ext/libuv/test/test-udp-multicast-join.c +139 -0
  247. data/ext/libuv/test/test-udp-multicast-ttl.c +86 -0
  248. data/ext/libuv/test/test-udp-options.c +86 -0
  249. data/ext/libuv/test/test-udp-send-and-recv.c +208 -0
  250. data/ext/libuv/test/test-util.c +97 -0
  251. data/ext/libuv/test/test-walk-handles.c +77 -0
  252. data/ext/libuv/uv.gyp +375 -0
  253. data/ext/libuv/vcbuild.bat +105 -0
  254. data/foolio.gemspec +18 -0
  255. data/lib/foolio.rb +9 -0
  256. data/lib/foolio/handle.rb +27 -0
  257. data/lib/foolio/listener.rb +26 -0
  258. data/lib/foolio/loop.rb +79 -0
  259. data/lib/foolio/stream.rb +109 -0
  260. data/lib/foolio/version.rb +3 -0
  261. metadata +309 -0
@@ -0,0 +1,739 @@
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
+ #include "eio.h"
25
+
26
+ #include <stdio.h>
27
+ #include <stdlib.h>
28
+ #include <string.h>
29
+ #include <dirent.h>
30
+ #include <assert.h>
31
+ #include <errno.h>
32
+ #include <fcntl.h>
33
+ #include <unistd.h>
34
+ #include <utime.h>
35
+ #include <sys/time.h>
36
+
37
+
38
+ #define ARGS1(a) (a)
39
+ #define ARGS2(a,b) (a), (b)
40
+ #define ARGS3(a,b,c) (a), (b), (c)
41
+ #define ARGS4(a,b,c,d) (a), (b), (c), (d)
42
+
43
+ #define WRAP_EIO(type, eiofunc, func, args) \
44
+ uv_fs_req_init(loop, req, type, path, cb); \
45
+ if (cb) { \
46
+ /* async */ \
47
+ req->eio = eiofunc(args, EIO_PRI_DEFAULT, uv__fs_after, req, &loop->uv_eio_channel); \
48
+ if (!req->eio) { \
49
+ uv__set_sys_error(loop, ENOMEM); \
50
+ return -1; \
51
+ } \
52
+ } else { \
53
+ /* sync */ \
54
+ req->result = func(args); \
55
+ if (req->result) { \
56
+ uv__set_sys_error(loop, errno); \
57
+ } \
58
+ return req->result; \
59
+ } \
60
+ return 0;
61
+
62
+
63
+ static void uv_fs_req_init(uv_loop_t* loop, uv_fs_t* req, uv_fs_type fs_type,
64
+ const char* path, uv_fs_cb cb) {
65
+ /* Make sure the thread pool is initialized. */
66
+ uv_eio_init(loop);
67
+
68
+ uv__req_init(loop, req, UV_FS);
69
+ req->loop = loop;
70
+ req->fs_type = fs_type;
71
+ req->cb = cb;
72
+ req->result = 0;
73
+ req->ptr = NULL;
74
+ req->path = path ? strdup(path) : NULL;
75
+ req->file = -1;
76
+ req->errorno = 0;
77
+ req->eio = NULL;
78
+
79
+ /* synchronous requests don't increase the reference count */
80
+ if (!req->cb)
81
+ uv__req_unregister(req->loop, req);
82
+ }
83
+
84
+
85
+ void uv_fs_req_cleanup(uv_fs_t* req) {
86
+ if (req->cb)
87
+ uv__req_unregister(req->loop, req);
88
+
89
+ free((void*)req->path);
90
+ req->path = NULL;
91
+
92
+ switch (req->fs_type) {
93
+ case UV_FS_READDIR:
94
+ assert(req->result > 0 ? (req->ptr != NULL) : (req->ptr == NULL));
95
+ free(req->ptr);
96
+ req->ptr = NULL;
97
+ break;
98
+
99
+ case UV_FS_STAT:
100
+ case UV_FS_LSTAT:
101
+ req->ptr = NULL;
102
+ break;
103
+
104
+ default:
105
+ break;
106
+ }
107
+ }
108
+
109
+
110
+ static int uv__fs_after(eio_req* eio) {
111
+ char* name;
112
+ int namelen;
113
+ int buflen = 0;
114
+ uv_fs_t* req = eio->data;
115
+ int i;
116
+
117
+ assert(req->cb);
118
+
119
+ req->result = req->eio->result;
120
+ req->errorno = uv_translate_sys_error(req->eio->errorno);
121
+
122
+ switch (req->fs_type) {
123
+ case UV_FS_READDIR:
124
+ /*
125
+ * XXX This is pretty bad.
126
+ * We alloc and copy the large null terminated string list from libeio.
127
+ * This is done because libeio is going to free eio->ptr2 after this
128
+ * callback. We must keep it until uv_fs_req_cleanup. If we get rid of
129
+ * libeio this can be avoided.
130
+ */
131
+ buflen = 0;
132
+ name = req->eio->ptr2;
133
+
134
+ for (i = 0; i < req->result; i++) {
135
+ namelen = strlen(name);
136
+ buflen += namelen + 1;
137
+ name += namelen;
138
+ assert(*name == '\0');
139
+ name++;
140
+ }
141
+
142
+ if (buflen) {
143
+ if ((req->ptr = malloc(buflen)))
144
+ memcpy(req->ptr, req->eio->ptr2, buflen);
145
+ else
146
+ uv__set_sys_error(req->loop, ENOMEM);
147
+ }
148
+ break;
149
+
150
+ case UV_FS_STAT:
151
+ case UV_FS_LSTAT:
152
+ case UV_FS_FSTAT:
153
+ req->ptr = req->eio->ptr2;
154
+ break;
155
+
156
+ case UV_FS_READLINK:
157
+ if (req->result == -1) {
158
+ req->ptr = NULL;
159
+ break;
160
+ }
161
+ assert(req->result > 0);
162
+
163
+ /* Make zero-terminated copy of req->eio->ptr2 */
164
+ if ((req->ptr = name = malloc(req->result + 1))) {
165
+ memcpy(name, req->eio->ptr2, req->result);
166
+ name[req->result] = '\0';
167
+ req->result = 0;
168
+ }
169
+ else {
170
+ req->errorno = ENOMEM;
171
+ req->result = -1;
172
+ }
173
+ break;
174
+
175
+ default:
176
+ break;
177
+ }
178
+
179
+ req->eio = NULL; /* Freed by libeio */
180
+ req->cb(req);
181
+
182
+ return 0;
183
+ }
184
+
185
+
186
+ int uv_fs_close(uv_loop_t* loop, uv_fs_t* req, uv_file file, uv_fs_cb cb) {
187
+ char* path = NULL;
188
+ WRAP_EIO(UV_FS_CLOSE, eio_close, close, ARGS1(file));
189
+ }
190
+
191
+
192
+ int uv_fs_open(uv_loop_t* loop, uv_fs_t* req, const char* path, int flags,
193
+ int mode, uv_fs_cb cb) {
194
+ uv_fs_req_init(loop, req, UV_FS_OPEN, path, cb);
195
+
196
+ if (cb) {
197
+ /* async */
198
+ req->eio = eio_open(path, flags, mode, EIO_PRI_DEFAULT, uv__fs_after, req, &loop->uv_eio_channel);
199
+ if (!req->eio) {
200
+ uv__set_sys_error(loop, ENOMEM);
201
+ return -1;
202
+ }
203
+
204
+ } else {
205
+ /* sync */
206
+ req->result = open(path, flags, mode);
207
+ if (req->result < 0) {
208
+ uv__set_sys_error(loop, errno);
209
+ return -1;
210
+ }
211
+
212
+ uv__cloexec(req->result, 1);
213
+
214
+ return req->result;
215
+ }
216
+
217
+ return 0;
218
+ }
219
+
220
+
221
+ int uv_fs_read(uv_loop_t* loop, uv_fs_t* req, uv_file fd, void* buf,
222
+ size_t length, int64_t offset, uv_fs_cb cb) {
223
+ uv_fs_req_init(loop, req, UV_FS_READ, NULL, cb);
224
+
225
+ if (cb) {
226
+ /* async */
227
+ req->eio = eio_read(fd, buf, length, offset, EIO_PRI_DEFAULT,
228
+ uv__fs_after, req, &loop->uv_eio_channel);
229
+
230
+ if (!req->eio) {
231
+ uv__set_sys_error(loop, ENOMEM);
232
+ return -1;
233
+ }
234
+
235
+ } else {
236
+ /* sync */
237
+ req->result = offset < 0 ?
238
+ read(fd, buf, length) :
239
+ pread(fd, buf, length, offset);
240
+
241
+ if (req->result < 0) {
242
+ uv__set_sys_error(loop, errno);
243
+ return -1;
244
+ }
245
+
246
+ return req->result;
247
+ }
248
+
249
+ return 0;
250
+ }
251
+
252
+
253
+ int uv_fs_unlink(uv_loop_t* loop, uv_fs_t* req, const char* path, uv_fs_cb cb) {
254
+ WRAP_EIO(UV_FS_UNLINK, eio_unlink, unlink, ARGS1(path))
255
+ }
256
+
257
+
258
+ int uv_fs_write(uv_loop_t* loop, uv_fs_t* req, uv_file file, void* buf,
259
+ size_t length, int64_t offset, uv_fs_cb cb) {
260
+ uv_fs_req_init(loop, req, UV_FS_WRITE, NULL, cb);
261
+
262
+ if (cb) {
263
+ /* async */
264
+ req->eio = eio_write(file, buf, length, offset, EIO_PRI_DEFAULT,
265
+ uv__fs_after, req, &loop->uv_eio_channel);
266
+ if (!req->eio) {
267
+ uv__set_sys_error(loop, ENOMEM);
268
+ return -1;
269
+ }
270
+
271
+ } else {
272
+ /* sync */
273
+ req->result = offset < 0 ?
274
+ write(file, buf, length) :
275
+ pwrite(file, buf, length, offset);
276
+
277
+ if (req->result < 0) {
278
+ uv__set_sys_error(loop, errno);
279
+ return -1;
280
+ }
281
+
282
+ return req->result;
283
+ }
284
+
285
+ return 0;
286
+ }
287
+
288
+
289
+ int uv_fs_mkdir(uv_loop_t* loop, uv_fs_t* req, const char* path, int mode,
290
+ uv_fs_cb cb) {
291
+ WRAP_EIO(UV_FS_MKDIR, eio_mkdir, mkdir, ARGS2(path, mode))
292
+ }
293
+
294
+
295
+ int uv_fs_rmdir(uv_loop_t* loop, uv_fs_t* req, const char* path, uv_fs_cb cb) {
296
+ WRAP_EIO(UV_FS_RMDIR, eio_rmdir, rmdir, ARGS1(path))
297
+ }
298
+
299
+
300
+ int uv_fs_readdir(uv_loop_t* loop, uv_fs_t* req, const char* path, int flags,
301
+ uv_fs_cb cb) {
302
+ int r;
303
+ struct dirent* entry;
304
+ size_t size = 0;
305
+ size_t d_namlen = 0;
306
+
307
+ uv_fs_req_init(loop, req, UV_FS_READDIR, path, cb);
308
+
309
+ if (cb) {
310
+ /* async */
311
+ req->eio = eio_readdir(path, flags, EIO_PRI_DEFAULT, uv__fs_after, req, &loop->uv_eio_channel);
312
+ if (!req->eio) {
313
+ uv__set_sys_error(loop, ENOMEM);
314
+ return -1;
315
+ }
316
+
317
+ } else {
318
+ /* sync */
319
+ DIR* dir = opendir(path);
320
+ if (!dir) {
321
+ uv__set_sys_error(loop, errno);
322
+ req->result = -1;
323
+ return -1;
324
+ }
325
+
326
+ /* req->result stores number of entries */
327
+ req->result = 0;
328
+
329
+ while ((entry = readdir(dir))) {
330
+ d_namlen = strlen(entry->d_name);
331
+
332
+ /* Skip . and .. */
333
+ if ((d_namlen == 1 && entry->d_name[0] == '.') ||
334
+ (d_namlen == 2 && entry->d_name[0] == '.' &&
335
+ entry->d_name[1] == '.')) {
336
+ continue;
337
+ }
338
+
339
+ req->ptr = realloc(req->ptr, size + d_namlen + 1);
340
+ /* TODO check ENOMEM */
341
+ memcpy((char*)req->ptr + size, entry->d_name, d_namlen);
342
+ size += d_namlen;
343
+ ((char*)req->ptr)[size] = '\0';
344
+ size++;
345
+ req->result++;
346
+ }
347
+
348
+ r = closedir(dir);
349
+ if (r) {
350
+ uv__set_sys_error(loop, errno);
351
+ req->result = -1;
352
+ return -1;
353
+ }
354
+
355
+ return req->result;
356
+ }
357
+
358
+ return 0;
359
+ }
360
+
361
+
362
+ int uv_fs_stat(uv_loop_t* loop, uv_fs_t* req, const char* path, uv_fs_cb cb) {
363
+ char* pathdup;
364
+ int pathlen;
365
+
366
+ uv_fs_req_init(loop, req, UV_FS_STAT, path, cb);
367
+
368
+ /* TODO do this without duplicating the string. */
369
+ /* TODO security */
370
+ pathdup = strdup(path);
371
+ pathlen = strlen(path);
372
+
373
+ if (pathlen > 0 && path[pathlen - 1] == '\\') {
374
+ /* TODO do not modify input string */
375
+ pathdup[pathlen - 1] = '\0';
376
+ }
377
+
378
+ if (cb) {
379
+ /* async */
380
+ req->eio = eio_stat(pathdup, EIO_PRI_DEFAULT, uv__fs_after, req, &loop->uv_eio_channel);
381
+
382
+ free(pathdup);
383
+
384
+ if (!req->eio) {
385
+ uv__set_sys_error(loop, ENOMEM);
386
+ return -1;
387
+ }
388
+
389
+ } else {
390
+ /* sync */
391
+ req->result = stat(pathdup, &req->statbuf);
392
+
393
+ free(pathdup);
394
+
395
+ if (req->result < 0) {
396
+ uv__set_sys_error(loop, errno);
397
+ return -1;
398
+ }
399
+
400
+ req->ptr = &req->statbuf;
401
+ return req->result;
402
+ }
403
+
404
+ return 0;
405
+ }
406
+
407
+
408
+ int uv_fs_fstat(uv_loop_t* loop, uv_fs_t* req, uv_file file, uv_fs_cb cb) {
409
+ uv_fs_req_init(loop, req, UV_FS_FSTAT, NULL, cb);
410
+
411
+ if (cb) {
412
+ /* async */
413
+ req->eio = eio_fstat(file, EIO_PRI_DEFAULT, uv__fs_after, req, &loop->uv_eio_channel);
414
+
415
+ if (!req->eio) {
416
+ uv__set_sys_error(loop, ENOMEM);
417
+ return -1;
418
+ }
419
+
420
+ } else {
421
+ /* sync */
422
+ req->result = fstat(file, &req->statbuf);
423
+
424
+ if (req->result < 0) {
425
+ uv__set_sys_error(loop, errno);
426
+ return -1;
427
+ }
428
+
429
+ req->ptr = &req->statbuf;
430
+ return req->result;
431
+ }
432
+
433
+ return 0;
434
+ }
435
+
436
+
437
+ int uv_fs_rename(uv_loop_t* loop, uv_fs_t* req, const char* path, const char* new_path,
438
+ uv_fs_cb cb) {
439
+ WRAP_EIO(UV_FS_RENAME, eio_rename, rename, ARGS2(path, new_path))
440
+ }
441
+
442
+
443
+ int uv_fs_fsync(uv_loop_t* loop, uv_fs_t* req, uv_file file, uv_fs_cb cb) {
444
+ char* path = NULL;
445
+ WRAP_EIO(UV_FS_FSYNC, eio_fsync, fsync, ARGS1(file))
446
+ }
447
+
448
+
449
+ #if defined(__APPLE__) && defined(F_FULLFSYNC)
450
+ ssize_t uv__fs_fdatasync(uv_file file) {
451
+ return fcntl(file, F_FULLFSYNC);
452
+ }
453
+
454
+
455
+ void uv__fs_fdatasync_work(eio_req* eio) {
456
+ uv_fs_t* req = eio->data;
457
+
458
+ eio->result = uv__fs_fdatasync(req->file);
459
+ }
460
+ #endif
461
+
462
+
463
+ int uv_fs_fdatasync(uv_loop_t* loop, uv_fs_t* req, uv_file file, uv_fs_cb cb) {
464
+ char* path = NULL;
465
+ #if defined(__FreeBSD__) \
466
+ || (__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 1060)
467
+ /* freebsd and pre-10.6 darwin don't have fdatasync,
468
+ * do a full fsync instead.
469
+ */
470
+ WRAP_EIO(UV_FS_FDATASYNC, eio_fdatasync, fsync, ARGS1(file))
471
+ #elif defined(__APPLE__) && defined(F_FULLFSYNC)
472
+ /* OSX >= 10.6 does have fdatasync, but better use fcntl anyway */
473
+ uv_fs_req_init(loop, req, UV_FS_FDATASYNC, path, cb);
474
+ req->file = file;
475
+
476
+ if (cb) {
477
+ /* async */
478
+ req->eio = eio_custom(uv__fs_fdatasync_work,
479
+ EIO_PRI_DEFAULT,
480
+ uv__fs_after,
481
+ req,
482
+ &loop->uv_eio_channel);
483
+ if (req->eio == NULL) {
484
+ uv__set_sys_error(loop, ENOMEM);
485
+ return -1;
486
+ }
487
+ } else {
488
+ /* sync */
489
+ req->result = uv__fs_fdatasync(file);
490
+ if (req->result) {
491
+ uv__set_sys_error(loop, errno);
492
+ }
493
+ return req->result;
494
+ }
495
+ return 0;
496
+ #else
497
+ WRAP_EIO(UV_FS_FDATASYNC, eio_fdatasync, fdatasync, ARGS1(file))
498
+ #endif
499
+ }
500
+
501
+
502
+ int uv_fs_ftruncate(uv_loop_t* loop, uv_fs_t* req, uv_file file, int64_t offset,
503
+ uv_fs_cb cb) {
504
+ char* path = NULL;
505
+ WRAP_EIO(UV_FS_FTRUNCATE, eio_ftruncate, ftruncate, ARGS2(file, offset))
506
+ }
507
+
508
+
509
+ int uv_fs_sendfile(uv_loop_t* loop, uv_fs_t* req, uv_file out_fd, uv_file in_fd,
510
+ int64_t in_offset, size_t length, uv_fs_cb cb) {
511
+ char* path = NULL;
512
+ WRAP_EIO(UV_FS_SENDFILE, eio_sendfile, eio_sendfile_sync,
513
+ ARGS4(out_fd, in_fd, in_offset, length))
514
+ }
515
+
516
+
517
+ int uv_fs_chmod(uv_loop_t* loop, uv_fs_t* req, const char* path, int mode,
518
+ uv_fs_cb cb) {
519
+ WRAP_EIO(UV_FS_CHMOD, eio_chmod, chmod, ARGS2(path, mode))
520
+ }
521
+
522
+
523
+ static int _utime(const char* path, double atime, double mtime) {
524
+ struct utimbuf buf;
525
+ buf.actime = atime;
526
+ buf.modtime = mtime;
527
+ return utime(path, &buf);
528
+ }
529
+
530
+
531
+ int uv_fs_utime(uv_loop_t* loop, uv_fs_t* req, const char* path, double atime,
532
+ double mtime, uv_fs_cb cb) {
533
+ WRAP_EIO(UV_FS_UTIME, eio_utime, _utime, ARGS3(path, atime, mtime))
534
+ }
535
+
536
+
537
+ static int _futime(const uv_file fd, double atime, double mtime) {
538
+ #if __linux__
539
+ /* utimesat() has nanosecond resolution but we stick to microseconds
540
+ * for the sake of consistency with other platforms.
541
+ */
542
+ struct timespec ts[2];
543
+ ts[0].tv_sec = atime;
544
+ ts[0].tv_nsec = (unsigned long)(atime * 1000000) % 1000000 * 1000;
545
+ ts[1].tv_sec = mtime;
546
+ ts[1].tv_nsec = (unsigned long)(mtime * 1000000) % 1000000 * 1000;
547
+ return uv__utimesat(fd, NULL, ts, 0);
548
+ #elif HAVE_FUTIMES
549
+ struct timeval tv[2];
550
+ tv[0].tv_sec = atime;
551
+ tv[0].tv_usec = (unsigned long)(atime * 1000000) % 1000000;
552
+ tv[1].tv_sec = mtime;
553
+ tv[1].tv_usec = (unsigned long)(mtime * 1000000) % 1000000;
554
+ return futimes(fd, tv);
555
+ #else /* !HAVE_FUTIMES */
556
+ errno = ENOSYS;
557
+ return -1;
558
+ #endif
559
+ }
560
+
561
+
562
+ int uv_fs_futime(uv_loop_t* loop, uv_fs_t* req, uv_file file, double atime,
563
+ double mtime, uv_fs_cb cb) {
564
+ const char* path = NULL;
565
+ WRAP_EIO(UV_FS_FUTIME, eio_futime, _futime, ARGS3(file, atime, mtime))
566
+ }
567
+
568
+
569
+ int uv_fs_lstat(uv_loop_t* loop, uv_fs_t* req, const char* path, uv_fs_cb cb) {
570
+ char* pathdup;
571
+ int pathlen;
572
+
573
+ uv_fs_req_init(loop, req, UV_FS_LSTAT, path, cb);
574
+
575
+ /* TODO do this without duplicating the string. */
576
+ /* TODO security */
577
+ pathdup = strdup(path);
578
+ pathlen = strlen(path);
579
+
580
+ if (pathlen > 0 && path[pathlen - 1] == '\\') {
581
+ /* TODO do not modify input string */
582
+ pathdup[pathlen - 1] = '\0';
583
+ }
584
+
585
+ if (cb) {
586
+ /* async */
587
+ req->eio = eio_lstat(pathdup, EIO_PRI_DEFAULT, uv__fs_after, req, &loop->uv_eio_channel);
588
+
589
+ free(pathdup);
590
+
591
+ if (!req->eio) {
592
+ uv__set_sys_error(loop, ENOMEM);
593
+ return -1;
594
+ }
595
+
596
+ } else {
597
+ /* sync */
598
+ req->result = lstat(pathdup, &req->statbuf);
599
+
600
+ free(pathdup);
601
+
602
+ if (req->result < 0) {
603
+ uv__set_sys_error(loop, errno);
604
+ return -1;
605
+ }
606
+
607
+ req->ptr = &req->statbuf;
608
+ return req->result;
609
+ }
610
+
611
+ return 0;
612
+ }
613
+
614
+
615
+ int uv_fs_link(uv_loop_t* loop, uv_fs_t* req, const char* path,
616
+ const char* new_path, uv_fs_cb cb) {
617
+ WRAP_EIO(UV_FS_LINK, eio_link, link, ARGS2(path, new_path))
618
+ }
619
+
620
+
621
+ int uv_fs_symlink(uv_loop_t* loop, uv_fs_t* req, const char* path,
622
+ const char* new_path, int flags, uv_fs_cb cb) {
623
+ WRAP_EIO(UV_FS_SYMLINK, eio_symlink, symlink, ARGS2(path, new_path))
624
+ }
625
+
626
+
627
+ int uv_fs_readlink(uv_loop_t* loop, uv_fs_t* req, const char* path,
628
+ uv_fs_cb cb) {
629
+ ssize_t size;
630
+ char* buf;
631
+
632
+ uv_fs_req_init(loop, req, UV_FS_READLINK, path, cb);
633
+
634
+ if (cb) {
635
+ if ((req->eio = eio_readlink(path, EIO_PRI_DEFAULT, uv__fs_after, req, &loop->uv_eio_channel))) {
636
+ return 0;
637
+ } else {
638
+ uv__set_sys_error(loop, ENOMEM);
639
+ return -1;
640
+ }
641
+ } else {
642
+ /* pathconf(_PC_PATH_MAX) may return -1 to signify that path
643
+ * lengths have no upper limit or aren't suitable for malloc'ing.
644
+ */
645
+ if ((size = pathconf(path, _PC_PATH_MAX)) == -1) {
646
+ #if defined(PATH_MAX)
647
+ size = PATH_MAX;
648
+ #else
649
+ size = 4096;
650
+ #endif
651
+ }
652
+
653
+ if ((buf = malloc(size + 1)) == NULL) {
654
+ uv__set_sys_error(loop, ENOMEM);
655
+ return -1;
656
+ }
657
+
658
+ if ((size = readlink(path, buf, size)) == -1) {
659
+ req->errorno = errno;
660
+ req->result = -1;
661
+ free(buf);
662
+ } else {
663
+ /* Cannot conceivably fail since it shrinks the buffer. */
664
+ buf = realloc(buf, size + 1);
665
+ buf[size] = '\0';
666
+ req->result = 0;
667
+ req->ptr = buf;
668
+ }
669
+
670
+ return req->result;
671
+ }
672
+
673
+ assert(0 && "unreachable");
674
+ }
675
+
676
+
677
+ int uv_fs_fchmod(uv_loop_t* loop, uv_fs_t* req, uv_file file, int mode,
678
+ uv_fs_cb cb) {
679
+ char* path = NULL;
680
+ WRAP_EIO(UV_FS_FCHMOD, eio_fchmod, fchmod, ARGS2(file, mode))
681
+ }
682
+
683
+
684
+ int uv_fs_chown(uv_loop_t* loop, uv_fs_t* req, const char* path, int uid,
685
+ int gid, uv_fs_cb cb) {
686
+ WRAP_EIO(UV_FS_CHOWN, eio_chown, chown, ARGS3(path, uid, gid))
687
+ }
688
+
689
+
690
+ int uv_fs_fchown(uv_loop_t* loop, uv_fs_t* req, uv_file file, int uid, int gid,
691
+ uv_fs_cb cb) {
692
+ char* path = NULL;
693
+ WRAP_EIO(UV_FS_FCHOWN, eio_fchown, fchown, ARGS3(file, uid, gid))
694
+ }
695
+
696
+
697
+ static void uv__work(eio_req* eio) {
698
+ uv_work_t* req = eio->data;
699
+ if (req->work_cb) {
700
+ req->work_cb(req);
701
+ }
702
+ }
703
+
704
+
705
+ static int uv__after_work(eio_req *eio) {
706
+ uv_work_t* req = eio->data;
707
+ uv__req_unregister(req->loop, req);
708
+ if (req->after_work_cb) {
709
+ req->after_work_cb(req);
710
+ }
711
+ return 0;
712
+ }
713
+
714
+
715
+ int uv_queue_work(uv_loop_t* loop, uv_work_t* req, uv_work_cb work_cb,
716
+ uv_after_work_cb after_work_cb) {
717
+ void* data = req->data;
718
+
719
+ uv_eio_init(loop);
720
+
721
+ uv__req_init(loop, req, UV_WORK);
722
+ req->loop = loop;
723
+ req->data = data;
724
+ req->work_cb = work_cb;
725
+ req->after_work_cb = after_work_cb;
726
+
727
+ req->eio = eio_custom(uv__work,
728
+ EIO_PRI_DEFAULT,
729
+ uv__after_work,
730
+ req,
731
+ &loop->uv_eio_channel);
732
+
733
+ if (!req->eio) {
734
+ uv__set_sys_error(loop, ENOMEM);
735
+ return -1;
736
+ }
737
+
738
+ return 0;
739
+ }