racer 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (336) hide show
  1. data/LICENSE +22 -0
  2. data/bin/racer +33 -0
  3. data/ext/libuv/AUTHORS +45 -0
  4. data/ext/libuv/LICENSE +42 -0
  5. data/ext/libuv/Makefile +119 -0
  6. data/ext/libuv/README.md +88 -0
  7. data/ext/libuv/build/gcc_version.py +20 -0
  8. data/ext/libuv/common.gypi +176 -0
  9. data/ext/libuv/config-mingw.mk +61 -0
  10. data/ext/libuv/config-unix.mk +171 -0
  11. data/ext/libuv/gyp_uv +64 -0
  12. data/ext/libuv/include/ares.h +591 -0
  13. data/ext/libuv/include/ares_version.h +24 -0
  14. data/ext/libuv/include/uv-private/eio.h +403 -0
  15. data/ext/libuv/include/uv-private/ev.h +838 -0
  16. data/ext/libuv/include/uv-private/ngx-queue.h +108 -0
  17. data/ext/libuv/include/uv-private/tree.h +768 -0
  18. data/ext/libuv/include/uv-private/uv-unix.h +310 -0
  19. data/ext/libuv/include/uv-private/uv-win.h +510 -0
  20. data/ext/libuv/include/uv.h +1796 -0
  21. data/ext/libuv/src/ares/AUTHORS +37 -0
  22. data/ext/libuv/src/ares/CHANGES +1218 -0
  23. data/ext/libuv/src/ares/CMakeLists.txt +22 -0
  24. data/ext/libuv/src/ares/NEWS +21 -0
  25. data/ext/libuv/src/ares/README +60 -0
  26. data/ext/libuv/src/ares/README.cares +13 -0
  27. data/ext/libuv/src/ares/README.msvc +142 -0
  28. data/ext/libuv/src/ares/README.node +21 -0
  29. data/ext/libuv/src/ares/RELEASE-NOTES +26 -0
  30. data/ext/libuv/src/ares/TODO +23 -0
  31. data/ext/libuv/src/ares/ares__close_sockets.c +66 -0
  32. data/ext/libuv/src/ares/ares__get_hostent.c +263 -0
  33. data/ext/libuv/src/ares/ares__read_line.c +71 -0
  34. data/ext/libuv/src/ares/ares__timeval.c +111 -0
  35. data/ext/libuv/src/ares/ares_cancel.c +63 -0
  36. data/ext/libuv/src/ares/ares_data.c +190 -0
  37. data/ext/libuv/src/ares/ares_data.h +65 -0
  38. data/ext/libuv/src/ares/ares_destroy.c +105 -0
  39. data/ext/libuv/src/ares/ares_dns.h +90 -0
  40. data/ext/libuv/src/ares/ares_expand_name.c +200 -0
  41. data/ext/libuv/src/ares/ares_expand_string.c +75 -0
  42. data/ext/libuv/src/ares/ares_fds.c +63 -0
  43. data/ext/libuv/src/ares/ares_free_hostent.c +42 -0
  44. data/ext/libuv/src/ares/ares_free_string.c +25 -0
  45. data/ext/libuv/src/ares/ares_getenv.c +30 -0
  46. data/ext/libuv/src/ares/ares_getenv.h +26 -0
  47. data/ext/libuv/src/ares/ares_gethostbyaddr.c +301 -0
  48. data/ext/libuv/src/ares/ares_gethostbyname.c +523 -0
  49. data/ext/libuv/src/ares/ares_getnameinfo.c +427 -0
  50. data/ext/libuv/src/ares/ares_getopt.c +122 -0
  51. data/ext/libuv/src/ares/ares_getopt.h +53 -0
  52. data/ext/libuv/src/ares/ares_getsock.c +72 -0
  53. data/ext/libuv/src/ares/ares_init.c +1809 -0
  54. data/ext/libuv/src/ares/ares_iphlpapi.h +221 -0
  55. data/ext/libuv/src/ares/ares_ipv6.h +78 -0
  56. data/ext/libuv/src/ares/ares_library_init.c +142 -0
  57. data/ext/libuv/src/ares/ares_library_init.h +42 -0
  58. data/ext/libuv/src/ares/ares_llist.c +86 -0
  59. data/ext/libuv/src/ares/ares_llist.h +42 -0
  60. data/ext/libuv/src/ares/ares_mkquery.c +195 -0
  61. data/ext/libuv/src/ares/ares_nowarn.c +181 -0
  62. data/ext/libuv/src/ares/ares_nowarn.h +55 -0
  63. data/ext/libuv/src/ares/ares_options.c +248 -0
  64. data/ext/libuv/src/ares/ares_parse_a_reply.c +263 -0
  65. data/ext/libuv/src/ares/ares_parse_aaaa_reply.c +259 -0
  66. data/ext/libuv/src/ares/ares_parse_mx_reply.c +170 -0
  67. data/ext/libuv/src/ares/ares_parse_ns_reply.c +182 -0
  68. data/ext/libuv/src/ares/ares_parse_ptr_reply.c +217 -0
  69. data/ext/libuv/src/ares/ares_parse_srv_reply.c +179 -0
  70. data/ext/libuv/src/ares/ares_parse_txt_reply.c +201 -0
  71. data/ext/libuv/src/ares/ares_platform.c +11035 -0
  72. data/ext/libuv/src/ares/ares_platform.h +43 -0
  73. data/ext/libuv/src/ares/ares_private.h +355 -0
  74. data/ext/libuv/src/ares/ares_process.c +1295 -0
  75. data/ext/libuv/src/ares/ares_query.c +183 -0
  76. data/ext/libuv/src/ares/ares_rules.h +144 -0
  77. data/ext/libuv/src/ares/ares_search.c +321 -0
  78. data/ext/libuv/src/ares/ares_send.c +134 -0
  79. data/ext/libuv/src/ares/ares_setup.h +199 -0
  80. data/ext/libuv/src/ares/ares_strcasecmp.c +66 -0
  81. data/ext/libuv/src/ares/ares_strcasecmp.h +30 -0
  82. data/ext/libuv/src/ares/ares_strdup.c +42 -0
  83. data/ext/libuv/src/ares/ares_strdup.h +26 -0
  84. data/ext/libuv/src/ares/ares_strerror.c +56 -0
  85. data/ext/libuv/src/ares/ares_timeout.c +80 -0
  86. data/ext/libuv/src/ares/ares_version.c +11 -0
  87. data/ext/libuv/src/ares/ares_writev.c +79 -0
  88. data/ext/libuv/src/ares/ares_writev.h +36 -0
  89. data/ext/libuv/src/ares/bitncmp.c +59 -0
  90. data/ext/libuv/src/ares/bitncmp.h +26 -0
  91. data/ext/libuv/src/ares/config_cygwin/ares_config.h +512 -0
  92. data/ext/libuv/src/ares/config_darwin/ares_config.h +512 -0
  93. data/ext/libuv/src/ares/config_freebsd/ares_config.h +512 -0
  94. data/ext/libuv/src/ares/config_linux/ares_config.h +512 -0
  95. data/ext/libuv/src/ares/config_netbsd/ares_config.h +512 -0
  96. data/ext/libuv/src/ares/config_openbsd/ares_config.h +512 -0
  97. data/ext/libuv/src/ares/config_sunos/ares_config.h +512 -0
  98. data/ext/libuv/src/ares/config_win32/ares_config.h +369 -0
  99. data/ext/libuv/src/ares/get_ver.awk +35 -0
  100. data/ext/libuv/src/ares/inet_net_pton.c +451 -0
  101. data/ext/libuv/src/ares/inet_net_pton.h +31 -0
  102. data/ext/libuv/src/ares/inet_ntop.c +208 -0
  103. data/ext/libuv/src/ares/inet_ntop.h +26 -0
  104. data/ext/libuv/src/ares/nameser.h +203 -0
  105. data/ext/libuv/src/ares/setup_once.h +504 -0
  106. data/ext/libuv/src/ares/windows_port.c +22 -0
  107. data/ext/libuv/src/cares.c +225 -0
  108. data/ext/libuv/src/fs-poll.c +237 -0
  109. data/ext/libuv/src/unix/async.c +119 -0
  110. data/ext/libuv/src/unix/core.c +647 -0
  111. data/ext/libuv/src/unix/cygwin.c +84 -0
  112. data/ext/libuv/src/unix/darwin.c +341 -0
  113. data/ext/libuv/src/unix/dl.c +83 -0
  114. data/ext/libuv/src/unix/eio/Changes +63 -0
  115. data/ext/libuv/src/unix/eio/LICENSE +36 -0
  116. data/ext/libuv/src/unix/eio/Makefile.am +15 -0
  117. data/ext/libuv/src/unix/eio/aclocal.m4 +8957 -0
  118. data/ext/libuv/src/unix/eio/autogen.sh +3 -0
  119. data/ext/libuv/src/unix/eio/config.h.in +86 -0
  120. data/ext/libuv/src/unix/eio/config_cygwin.h +80 -0
  121. data/ext/libuv/src/unix/eio/config_darwin.h +141 -0
  122. data/ext/libuv/src/unix/eio/config_freebsd.h +81 -0
  123. data/ext/libuv/src/unix/eio/config_linux.h +94 -0
  124. data/ext/libuv/src/unix/eio/config_netbsd.h +81 -0
  125. data/ext/libuv/src/unix/eio/config_openbsd.h +137 -0
  126. data/ext/libuv/src/unix/eio/config_sunos.h +84 -0
  127. data/ext/libuv/src/unix/eio/configure.ac +22 -0
  128. data/ext/libuv/src/unix/eio/demo.c +194 -0
  129. data/ext/libuv/src/unix/eio/ecb.h +370 -0
  130. data/ext/libuv/src/unix/eio/eio.3 +3428 -0
  131. data/ext/libuv/src/unix/eio/eio.c +2593 -0
  132. data/ext/libuv/src/unix/eio/eio.pod +969 -0
  133. data/ext/libuv/src/unix/eio/libeio.m4 +195 -0
  134. data/ext/libuv/src/unix/eio/xthread.h +164 -0
  135. data/ext/libuv/src/unix/error.c +104 -0
  136. data/ext/libuv/src/unix/ev/Changes +388 -0
  137. data/ext/libuv/src/unix/ev/LICENSE +36 -0
  138. data/ext/libuv/src/unix/ev/Makefile.am +18 -0
  139. data/ext/libuv/src/unix/ev/Makefile.in +771 -0
  140. data/ext/libuv/src/unix/ev/README +58 -0
  141. data/ext/libuv/src/unix/ev/aclocal.m4 +8957 -0
  142. data/ext/libuv/src/unix/ev/autogen.sh +6 -0
  143. data/ext/libuv/src/unix/ev/config.guess +1526 -0
  144. data/ext/libuv/src/unix/ev/config.h.in +125 -0
  145. data/ext/libuv/src/unix/ev/config.sub +1658 -0
  146. data/ext/libuv/src/unix/ev/config_cygwin.h +123 -0
  147. data/ext/libuv/src/unix/ev/config_darwin.h +122 -0
  148. data/ext/libuv/src/unix/ev/config_freebsd.h +120 -0
  149. data/ext/libuv/src/unix/ev/config_linux.h +141 -0
  150. data/ext/libuv/src/unix/ev/config_netbsd.h +120 -0
  151. data/ext/libuv/src/unix/ev/config_openbsd.h +126 -0
  152. data/ext/libuv/src/unix/ev/config_sunos.h +122 -0
  153. data/ext/libuv/src/unix/ev/configure +13037 -0
  154. data/ext/libuv/src/unix/ev/configure.ac +18 -0
  155. data/ext/libuv/src/unix/ev/depcomp +630 -0
  156. data/ext/libuv/src/unix/ev/ev++.h +816 -0
  157. data/ext/libuv/src/unix/ev/ev.3 +5311 -0
  158. data/ext/libuv/src/unix/ev/ev.c +3925 -0
  159. data/ext/libuv/src/unix/ev/ev.pod +5243 -0
  160. data/ext/libuv/src/unix/ev/ev_epoll.c +266 -0
  161. data/ext/libuv/src/unix/ev/ev_kqueue.c +235 -0
  162. data/ext/libuv/src/unix/ev/ev_poll.c +148 -0
  163. data/ext/libuv/src/unix/ev/ev_port.c +179 -0
  164. data/ext/libuv/src/unix/ev/ev_select.c +310 -0
  165. data/ext/libuv/src/unix/ev/ev_vars.h +203 -0
  166. data/ext/libuv/src/unix/ev/ev_win32.c +153 -0
  167. data/ext/libuv/src/unix/ev/ev_wrap.h +196 -0
  168. data/ext/libuv/src/unix/ev/event.c +402 -0
  169. data/ext/libuv/src/unix/ev/event.h +170 -0
  170. data/ext/libuv/src/unix/ev/install-sh +294 -0
  171. data/ext/libuv/src/unix/ev/libev.m4 +39 -0
  172. data/ext/libuv/src/unix/ev/ltmain.sh +8413 -0
  173. data/ext/libuv/src/unix/ev/missing +336 -0
  174. data/ext/libuv/src/unix/ev/mkinstalldirs +111 -0
  175. data/ext/libuv/src/unix/freebsd.c +312 -0
  176. data/ext/libuv/src/unix/fs.c +699 -0
  177. data/ext/libuv/src/unix/internal.h +180 -0
  178. data/ext/libuv/src/unix/kqueue.c +122 -0
  179. data/ext/libuv/src/unix/linux/core.c +474 -0
  180. data/ext/libuv/src/unix/linux/inotify.c +209 -0
  181. data/ext/libuv/src/unix/linux/syscalls.c +268 -0
  182. data/ext/libuv/src/unix/linux/syscalls.h +93 -0
  183. data/ext/libuv/src/unix/loop-watcher.c +63 -0
  184. data/ext/libuv/src/unix/loop.c +81 -0
  185. data/ext/libuv/src/unix/netbsd.c +108 -0
  186. data/ext/libuv/src/unix/openbsd.c +295 -0
  187. data/ext/libuv/src/unix/pipe.c +261 -0
  188. data/ext/libuv/src/unix/poll.c +116 -0
  189. data/ext/libuv/src/unix/process.c +447 -0
  190. data/ext/libuv/src/unix/stream.c +1026 -0
  191. data/ext/libuv/src/unix/sunos.c +483 -0
  192. data/ext/libuv/src/unix/tcp.c +328 -0
  193. data/ext/libuv/src/unix/thread.c +211 -0
  194. data/ext/libuv/src/unix/timer.c +138 -0
  195. data/ext/libuv/src/unix/tty.c +146 -0
  196. data/ext/libuv/src/unix/udp.c +661 -0
  197. data/ext/libuv/src/unix/uv-eio.c +107 -0
  198. data/ext/libuv/src/unix/uv-eio.h +13 -0
  199. data/ext/libuv/src/uv-common.c +384 -0
  200. data/ext/libuv/src/uv-common.h +171 -0
  201. data/ext/libuv/src/win/async.c +130 -0
  202. data/ext/libuv/src/win/core.c +283 -0
  203. data/ext/libuv/src/win/dl.c +86 -0
  204. data/ext/libuv/src/win/error.c +150 -0
  205. data/ext/libuv/src/win/fs-event.c +512 -0
  206. data/ext/libuv/src/win/fs.c +1856 -0
  207. data/ext/libuv/src/win/getaddrinfo.c +365 -0
  208. data/ext/libuv/src/win/handle-inl.h +145 -0
  209. data/ext/libuv/src/win/handle.c +150 -0
  210. data/ext/libuv/src/win/internal.h +337 -0
  211. data/ext/libuv/src/win/loop-watcher.c +123 -0
  212. data/ext/libuv/src/win/pipe.c +1675 -0
  213. data/ext/libuv/src/win/poll.c +618 -0
  214. data/ext/libuv/src/win/process-stdio.c +479 -0
  215. data/ext/libuv/src/win/process.c +1013 -0
  216. data/ext/libuv/src/win/req-inl.h +225 -0
  217. data/ext/libuv/src/win/req.c +25 -0
  218. data/ext/libuv/src/win/stream-inl.h +69 -0
  219. data/ext/libuv/src/win/stream.c +167 -0
  220. data/ext/libuv/src/win/tcp.c +1400 -0
  221. data/ext/libuv/src/win/thread.c +372 -0
  222. data/ext/libuv/src/win/threadpool.c +74 -0
  223. data/ext/libuv/src/win/timer.c +226 -0
  224. data/ext/libuv/src/win/tty.c +1801 -0
  225. data/ext/libuv/src/win/udp.c +718 -0
  226. data/ext/libuv/src/win/util.c +864 -0
  227. data/ext/libuv/src/win/winapi.c +132 -0
  228. data/ext/libuv/src/win/winapi.h +4443 -0
  229. data/ext/libuv/src/win/winsock.c +557 -0
  230. data/ext/libuv/src/win/winsock.h +171 -0
  231. data/ext/libuv/test/benchmark-ares.c +117 -0
  232. data/ext/libuv/test/benchmark-fs-stat.c +164 -0
  233. data/ext/libuv/test/benchmark-getaddrinfo.c +94 -0
  234. data/ext/libuv/test/benchmark-list.h +114 -0
  235. data/ext/libuv/test/benchmark-loop-count.c +88 -0
  236. data/ext/libuv/test/benchmark-million-timers.c +65 -0
  237. data/ext/libuv/test/benchmark-ping-pongs.c +213 -0
  238. data/ext/libuv/test/benchmark-pound.c +324 -0
  239. data/ext/libuv/test/benchmark-pump.c +462 -0
  240. data/ext/libuv/test/benchmark-sizes.c +42 -0
  241. data/ext/libuv/test/benchmark-spawn.c +162 -0
  242. data/ext/libuv/test/benchmark-tcp-write-batch.c +140 -0
  243. data/ext/libuv/test/benchmark-thread.c +64 -0
  244. data/ext/libuv/test/benchmark-udp-packet-storm.c +247 -0
  245. data/ext/libuv/test/blackhole-server.c +118 -0
  246. data/ext/libuv/test/dns-server.c +321 -0
  247. data/ext/libuv/test/echo-server.c +378 -0
  248. data/ext/libuv/test/fixtures/empty_file +0 -0
  249. data/ext/libuv/test/fixtures/load_error.node +1 -0
  250. data/ext/libuv/test/run-benchmarks.c +64 -0
  251. data/ext/libuv/test/run-tests.c +138 -0
  252. data/ext/libuv/test/runner-unix.c +290 -0
  253. data/ext/libuv/test/runner-unix.h +36 -0
  254. data/ext/libuv/test/runner-win.c +285 -0
  255. data/ext/libuv/test/runner-win.h +42 -0
  256. data/ext/libuv/test/runner.c +326 -0
  257. data/ext/libuv/test/runner.h +159 -0
  258. data/ext/libuv/test/task.h +109 -0
  259. data/ext/libuv/test/test-async.c +118 -0
  260. data/ext/libuv/test/test-callback-order.c +76 -0
  261. data/ext/libuv/test/test-callback-stack.c +203 -0
  262. data/ext/libuv/test/test-connection-fail.c +148 -0
  263. data/ext/libuv/test/test-counters-init.c +215 -0
  264. data/ext/libuv/test/test-cwd-and-chdir.c +64 -0
  265. data/ext/libuv/test/test-delayed-accept.c +197 -0
  266. data/ext/libuv/test/test-dlerror.c +58 -0
  267. data/ext/libuv/test/test-error.c +59 -0
  268. data/ext/libuv/test/test-fail-always.c +29 -0
  269. data/ext/libuv/test/test-fs-event.c +442 -0
  270. data/ext/libuv/test/test-fs-poll.c +146 -0
  271. data/ext/libuv/test/test-fs.c +1843 -0
  272. data/ext/libuv/test/test-get-currentexe.c +63 -0
  273. data/ext/libuv/test/test-get-loadavg.c +36 -0
  274. data/ext/libuv/test/test-get-memory.c +38 -0
  275. data/ext/libuv/test/test-getaddrinfo.c +122 -0
  276. data/ext/libuv/test/test-gethostbyname.c +189 -0
  277. data/ext/libuv/test/test-getsockname.c +342 -0
  278. data/ext/libuv/test/test-hrtime.c +54 -0
  279. data/ext/libuv/test/test-idle.c +81 -0
  280. data/ext/libuv/test/test-ipc-send-recv.c +209 -0
  281. data/ext/libuv/test/test-ipc.c +620 -0
  282. data/ext/libuv/test/test-list.h +421 -0
  283. data/ext/libuv/test/test-loop-handles.c +336 -0
  284. data/ext/libuv/test/test-multiple-listen.c +102 -0
  285. data/ext/libuv/test/test-mutexes.c +63 -0
  286. data/ext/libuv/test/test-pass-always.c +28 -0
  287. data/ext/libuv/test/test-ping-pong.c +253 -0
  288. data/ext/libuv/test/test-pipe-bind-error.c +140 -0
  289. data/ext/libuv/test/test-pipe-connect-error.c +96 -0
  290. data/ext/libuv/test/test-platform-output.c +87 -0
  291. data/ext/libuv/test/test-poll-close.c +77 -0
  292. data/ext/libuv/test/test-poll.c +573 -0
  293. data/ext/libuv/test/test-process-title.c +42 -0
  294. data/ext/libuv/test/test-ref.c +338 -0
  295. data/ext/libuv/test/test-run-once.c +48 -0
  296. data/ext/libuv/test/test-semaphore.c +111 -0
  297. data/ext/libuv/test/test-shutdown-close.c +103 -0
  298. data/ext/libuv/test/test-shutdown-eof.c +183 -0
  299. data/ext/libuv/test/test-spawn.c +847 -0
  300. data/ext/libuv/test/test-stdio-over-pipes.c +246 -0
  301. data/ext/libuv/test/test-tcp-bind-error.c +191 -0
  302. data/ext/libuv/test/test-tcp-bind6-error.c +154 -0
  303. data/ext/libuv/test/test-tcp-close-while-connecting.c +80 -0
  304. data/ext/libuv/test/test-tcp-close.c +129 -0
  305. data/ext/libuv/test/test-tcp-connect-error-after-write.c +95 -0
  306. data/ext/libuv/test/test-tcp-connect-error.c +70 -0
  307. data/ext/libuv/test/test-tcp-connect-timeout.c +85 -0
  308. data/ext/libuv/test/test-tcp-connect6-error.c +68 -0
  309. data/ext/libuv/test/test-tcp-flags.c +51 -0
  310. data/ext/libuv/test/test-tcp-shutdown-after-write.c +131 -0
  311. data/ext/libuv/test/test-tcp-write-error.c +168 -0
  312. data/ext/libuv/test/test-tcp-write-to-half-open-connection.c +135 -0
  313. data/ext/libuv/test/test-tcp-writealot.c +170 -0
  314. data/ext/libuv/test/test-thread.c +183 -0
  315. data/ext/libuv/test/test-threadpool.c +57 -0
  316. data/ext/libuv/test/test-timer-again.c +141 -0
  317. data/ext/libuv/test/test-timer.c +152 -0
  318. data/ext/libuv/test/test-tty.c +110 -0
  319. data/ext/libuv/test/test-udp-dgram-too-big.c +86 -0
  320. data/ext/libuv/test/test-udp-ipv6.c +156 -0
  321. data/ext/libuv/test/test-udp-multicast-join.c +139 -0
  322. data/ext/libuv/test/test-udp-multicast-ttl.c +86 -0
  323. data/ext/libuv/test/test-udp-options.c +86 -0
  324. data/ext/libuv/test/test-udp-send-and-recv.c +208 -0
  325. data/ext/libuv/test/test-util.c +97 -0
  326. data/ext/libuv/test/test-walk-handles.c +77 -0
  327. data/ext/libuv/uv.gyp +452 -0
  328. data/ext/libuv/vcbuild.bat +105 -0
  329. data/ext/racer/extconf.rb +20 -0
  330. data/ext/racer/http_parser.c +2059 -0
  331. data/ext/racer/http_parser.h +318 -0
  332. data/ext/racer/mkmf.log +22 -0
  333. data/ext/racer/racer.c +551 -0
  334. data/lib/racer.rb +2 -0
  335. data/lib/racer/version.rb +3 -0
  336. metadata +382 -0
@@ -0,0 +1,1856 @@
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 <assert.h>
23
+ #include <malloc.h>
24
+ #include <direct.h>
25
+ #include <errno.h>
26
+ #include <fcntl.h>
27
+ #include <io.h>
28
+ #include <limits.h>
29
+ #include <sys/stat.h>
30
+ #include <sys/utime.h>
31
+ #include <stdio.h>
32
+
33
+ #include "uv.h"
34
+ #include "internal.h"
35
+ #include "req-inl.h"
36
+
37
+
38
+ #define UV_FS_ASYNC_QUEUED 0x0001
39
+ #define UV_FS_FREE_PATH 0x0002
40
+ #define UV_FS_FREE_NEW_PATH 0x0004
41
+ #define UV_FS_FREE_PTR 0x0008
42
+ #define UV_FS_CLEANEDUP 0x0010
43
+
44
+
45
+ #define UTF8_TO_UTF16(s, t) \
46
+ size = uv_utf8_to_utf16(s, NULL, 0) * sizeof(wchar_t); \
47
+ t = (wchar_t*)malloc(size); \
48
+ if (!t) { \
49
+ uv_fatal_error(ERROR_OUTOFMEMORY, "malloc"); \
50
+ } \
51
+ if (!uv_utf8_to_utf16(s, t, size / sizeof(wchar_t))) { \
52
+ uv__set_sys_error(loop, GetLastError()); \
53
+ return -1; \
54
+ }
55
+
56
+ #define QUEUE_FS_TP_JOB(loop, req) \
57
+ if (!QueueUserWorkItem(&uv_fs_thread_proc, \
58
+ req, \
59
+ WT_EXECUTEDEFAULT)) { \
60
+ uv__set_sys_error((loop), GetLastError()); \
61
+ return -1; \
62
+ } \
63
+ req->flags |= UV_FS_ASYNC_QUEUED; \
64
+ uv__req_register(loop, req);
65
+
66
+ #define SET_UV_LAST_ERROR_FROM_REQ(req) \
67
+ uv__set_error(req->loop, req->errorno, req->sys_errno_);
68
+
69
+ #define SET_REQ_RESULT(req, result_value) \
70
+ req->result = (result_value); \
71
+ if (req->result == -1) { \
72
+ req->sys_errno_ = _doserrno; \
73
+ req->errorno = uv_translate_sys_error(req->sys_errno_); \
74
+ }
75
+
76
+ #define SET_REQ_WIN32_ERROR(req, sys_errno) \
77
+ req->result = -1; \
78
+ req->sys_errno_ = (sys_errno); \
79
+ req->errorno = uv_translate_sys_error(req->sys_errno_);
80
+
81
+ #define SET_REQ_UV_ERROR(req, uv_errno, sys_errno) \
82
+ req->result = -1; \
83
+ req->sys_errno_ = (sys_errno); \
84
+ req->errorno = (uv_errno);
85
+
86
+ #define VERIFY_UV_FILE(file, req) \
87
+ if (file == -1) { \
88
+ req->result = -1; \
89
+ req->errorno = UV_EBADF; \
90
+ req->sys_errno_ = ERROR_SUCCESS; \
91
+ return; \
92
+ }
93
+
94
+ #define FILETIME_TO_TIME_T(filetime) \
95
+ ((*((uint64_t*) &(filetime)) - 116444736000000000ULL) / 10000000ULL);
96
+
97
+ #define IS_SLASH(c) ((c) == L'\\' || (c) == L'/')
98
+ #define IS_LETTER(c) (((c) >= L'a' && (c) <= L'z') || \
99
+ ((c) >= L'A' && (c) <= L'Z'))
100
+
101
+ const wchar_t JUNCTION_PREFIX[] = L"\\??\\";
102
+ const wchar_t JUNCTION_PREFIX_LEN = 4;
103
+
104
+ const wchar_t LONG_PATH_PREFIX[] = L"\\\\?\\";
105
+ const wchar_t LONG_PATH_PREFIX_LEN = 4;
106
+
107
+
108
+ void uv_fs_init() {
109
+ _fmode = _O_BINARY;
110
+ }
111
+
112
+
113
+ static void uv_fs_req_init_async(uv_loop_t* loop, uv_fs_t* req,
114
+ uv_fs_type fs_type, const char* path, const wchar_t* pathw, uv_fs_cb cb) {
115
+ uv_req_init(loop, (uv_req_t*) req);
116
+ req->type = UV_FS;
117
+ req->loop = loop;
118
+ req->flags = 0;
119
+ req->fs_type = fs_type;
120
+ req->cb = cb;
121
+ req->result = 0;
122
+ req->ptr = NULL;
123
+ req->path = path ? strdup(path) : NULL;
124
+ req->pathw = (wchar_t*)pathw;
125
+ req->errorno = 0;
126
+ req->sys_errno_ = 0;
127
+ memset(&req->overlapped, 0, sizeof(req->overlapped));
128
+ }
129
+
130
+
131
+ static void uv_fs_req_init_sync(uv_loop_t* loop, uv_fs_t* req,
132
+ uv_fs_type fs_type) {
133
+ uv_req_init(loop, (uv_req_t*) req);
134
+ req->type = UV_FS;
135
+ req->loop = loop;
136
+ req->flags = 0;
137
+ req->fs_type = fs_type;
138
+ req->result = 0;
139
+ req->ptr = NULL;
140
+ req->path = NULL;
141
+ req->pathw = NULL;
142
+ req->errorno = 0;
143
+ }
144
+
145
+
146
+ static int is_path_dir(const wchar_t* path) {
147
+ DWORD attr = GetFileAttributesW(path);
148
+
149
+ if (attr != INVALID_FILE_ATTRIBUTES) {
150
+ return attr & FILE_ATTRIBUTE_DIRECTORY ? 1 : 0;
151
+ } else {
152
+ return 0;
153
+ }
154
+ }
155
+
156
+
157
+ static int get_reparse_point(HANDLE handle, int* target_length) {
158
+ void* buffer = NULL;
159
+ REPARSE_DATA_BUFFER* reparse_data;
160
+ DWORD bytes_returned;
161
+ int rv = 0;
162
+
163
+ buffer = malloc(MAXIMUM_REPARSE_DATA_BUFFER_SIZE);
164
+ if (!buffer) {
165
+ uv_fatal_error(ERROR_OUTOFMEMORY, "malloc");
166
+ }
167
+
168
+ if (!DeviceIoControl(handle,
169
+ FSCTL_GET_REPARSE_POINT,
170
+ NULL,
171
+ 0,
172
+ buffer,
173
+ MAXIMUM_REPARSE_DATA_BUFFER_SIZE,
174
+ &bytes_returned,
175
+ NULL)) {
176
+ free(buffer);
177
+ return 0;
178
+ }
179
+
180
+ reparse_data = (REPARSE_DATA_BUFFER*)buffer;
181
+
182
+ if (reparse_data->ReparseTag == IO_REPARSE_TAG_SYMLINK) {
183
+ rv = 1;
184
+ if (target_length) {
185
+ *target_length = reparse_data->SymbolicLinkReparseBuffer.SubstituteNameLength /
186
+ sizeof(wchar_t);
187
+ }
188
+ } else if (reparse_data->ReparseTag == IO_REPARSE_TAG_MOUNT_POINT) {
189
+ rv = 1;
190
+ if (target_length) {
191
+ *target_length = reparse_data->MountPointReparseBuffer.SubstituteNameLength /
192
+ sizeof(wchar_t);
193
+ }
194
+ }
195
+
196
+ free(buffer);
197
+ return rv;
198
+ }
199
+
200
+
201
+ void fs__open(uv_fs_t* req, const wchar_t* path, int flags, int mode) {
202
+ DWORD access;
203
+ DWORD share;
204
+ DWORD disposition;
205
+ DWORD attributes;
206
+ HANDLE file;
207
+ int result, current_umask;
208
+
209
+ /* Obtain the active umask. umask() never fails and returns the previous */
210
+ /* umask. */
211
+ current_umask = umask(0);
212
+ umask(current_umask);
213
+
214
+ /* convert flags and mode to CreateFile parameters */
215
+ switch (flags & (_O_RDONLY | _O_WRONLY | _O_RDWR)) {
216
+ case _O_RDONLY:
217
+ access = FILE_GENERIC_READ;
218
+ break;
219
+ case _O_WRONLY:
220
+ access = FILE_GENERIC_WRITE;
221
+ break;
222
+ case _O_RDWR:
223
+ access = FILE_GENERIC_READ | FILE_GENERIC_WRITE;
224
+ break;
225
+ default:
226
+ result = -1;
227
+ goto end;
228
+ }
229
+
230
+ if (flags & _O_APPEND) {
231
+ access &= ~FILE_WRITE_DATA;
232
+ access |= FILE_APPEND_DATA;
233
+ }
234
+
235
+ /*
236
+ * Here is where we deviate significantly from what CRT's _open()
237
+ * does. We indiscriminately use all the sharing modes, to match
238
+ * UNIX semantics. In particular, this ensures that the file can
239
+ * be deleted even whilst it's open, fixing issue #1449.
240
+ */
241
+ share = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
242
+
243
+ switch (flags & (_O_CREAT | _O_EXCL | _O_TRUNC)) {
244
+ case 0:
245
+ case _O_EXCL:
246
+ disposition = OPEN_EXISTING;
247
+ break;
248
+ case _O_CREAT:
249
+ disposition = OPEN_ALWAYS;
250
+ break;
251
+ case _O_CREAT | _O_EXCL:
252
+ case _O_CREAT | _O_TRUNC | _O_EXCL:
253
+ disposition = CREATE_NEW;
254
+ break;
255
+ case _O_TRUNC:
256
+ case _O_TRUNC | _O_EXCL:
257
+ disposition = TRUNCATE_EXISTING;
258
+ break;
259
+ case _O_CREAT | _O_TRUNC:
260
+ disposition = CREATE_ALWAYS;
261
+ break;
262
+ default:
263
+ result = -1;
264
+ goto end;
265
+ }
266
+
267
+ attributes = FILE_ATTRIBUTE_NORMAL;
268
+ if (flags & _O_CREAT) {
269
+ if (!((mode & ~current_umask) & _S_IWRITE)) {
270
+ attributes |= FILE_ATTRIBUTE_READONLY;
271
+ }
272
+ }
273
+
274
+ if (flags & _O_TEMPORARY ) {
275
+ attributes |= FILE_FLAG_DELETE_ON_CLOSE | FILE_ATTRIBUTE_TEMPORARY;
276
+ access |= DELETE;
277
+ }
278
+
279
+ if (flags & _O_SHORT_LIVED) {
280
+ attributes |= FILE_ATTRIBUTE_TEMPORARY;
281
+ }
282
+
283
+ switch (flags & (_O_SEQUENTIAL | _O_RANDOM)) {
284
+ case 0:
285
+ break;
286
+ case _O_SEQUENTIAL:
287
+ attributes |= FILE_FLAG_SEQUENTIAL_SCAN;
288
+ break;
289
+ case _O_RANDOM:
290
+ attributes |= FILE_FLAG_RANDOM_ACCESS;
291
+ break;
292
+ default:
293
+ result = -1;
294
+ goto end;
295
+ }
296
+
297
+ /* Setting this flag makes it possible to open a directory. */
298
+ attributes |= FILE_FLAG_BACKUP_SEMANTICS;
299
+
300
+ file = CreateFileW(path,
301
+ access,
302
+ share,
303
+ NULL,
304
+ disposition,
305
+ attributes,
306
+ NULL);
307
+ if (file == INVALID_HANDLE_VALUE) {
308
+ DWORD error = GetLastError();
309
+ if (error == ERROR_FILE_EXISTS && (flags & _O_CREAT) &&
310
+ !(flags & _O_EXCL)) {
311
+ /* Special case: when ERROR_FILE_EXISTS happens and O_CREAT was */
312
+ /* specified, it means the path referred to a directory. */
313
+ SET_REQ_UV_ERROR(req, UV_EISDIR, error);
314
+ } else {
315
+ SET_REQ_WIN32_ERROR(req, GetLastError());
316
+ }
317
+ return;
318
+ }
319
+ result = _open_osfhandle((intptr_t)file, flags);
320
+ end:
321
+ SET_REQ_RESULT(req, result);
322
+ }
323
+
324
+ void fs__close(uv_fs_t* req, uv_file file) {
325
+ int result;
326
+
327
+ VERIFY_UV_FILE(file, req);
328
+
329
+ result = _close(file);
330
+ SET_REQ_RESULT(req, result);
331
+ }
332
+
333
+
334
+ void fs__read(uv_fs_t* req, uv_file file, void *buf, size_t length,
335
+ int64_t offset) {
336
+ HANDLE handle;
337
+ OVERLAPPED overlapped, *overlapped_ptr;
338
+ LARGE_INTEGER offset_;
339
+ DWORD bytes;
340
+ DWORD error;
341
+
342
+ VERIFY_UV_FILE(file, req);
343
+
344
+ handle = (HANDLE) _get_osfhandle(file);
345
+ if (handle == INVALID_HANDLE_VALUE) {
346
+ SET_REQ_RESULT(req, -1);
347
+ return;
348
+ }
349
+
350
+ if (length > INT_MAX) {
351
+ SET_REQ_WIN32_ERROR(req, ERROR_INSUFFICIENT_BUFFER);
352
+ return;
353
+ }
354
+
355
+ if (offset != -1) {
356
+ memset(&overlapped, 0, sizeof overlapped);
357
+
358
+ offset_.QuadPart = offset;
359
+ overlapped.Offset = offset_.LowPart;
360
+ overlapped.OffsetHigh = offset_.HighPart;
361
+
362
+ overlapped_ptr = &overlapped;
363
+ } else {
364
+ overlapped_ptr = NULL;
365
+ }
366
+
367
+ if (ReadFile(handle, buf, length, &bytes, overlapped_ptr)) {
368
+ SET_REQ_RESULT(req, bytes);
369
+ } else {
370
+ error = GetLastError();
371
+ if (error == ERROR_HANDLE_EOF) {
372
+ SET_REQ_RESULT(req, bytes);
373
+ } else {
374
+ SET_REQ_WIN32_ERROR(req, error);
375
+ }
376
+ }
377
+ }
378
+
379
+
380
+ void fs__write(uv_fs_t* req, uv_file file, void *buf, size_t length,
381
+ int64_t offset) {
382
+ HANDLE handle;
383
+ OVERLAPPED overlapped, *overlapped_ptr;
384
+ LARGE_INTEGER offset_;
385
+ DWORD bytes;
386
+
387
+ VERIFY_UV_FILE(file, req);
388
+
389
+ handle = (HANDLE) _get_osfhandle(file);
390
+ if (handle == INVALID_HANDLE_VALUE) {
391
+ SET_REQ_RESULT(req, -1);
392
+ return;
393
+ }
394
+
395
+ if (length > INT_MAX) {
396
+ SET_REQ_WIN32_ERROR(req, ERROR_INSUFFICIENT_BUFFER);
397
+ return;
398
+ }
399
+
400
+ if (offset != -1) {
401
+ memset(&overlapped, 0, sizeof overlapped);
402
+
403
+ offset_.QuadPart = offset;
404
+ overlapped.Offset = offset_.LowPart;
405
+ overlapped.OffsetHigh = offset_.HighPart;
406
+
407
+ overlapped_ptr = &overlapped;
408
+ } else {
409
+ overlapped_ptr = NULL;
410
+ }
411
+
412
+ if (WriteFile(handle, buf, length, &bytes, overlapped_ptr)) {
413
+ SET_REQ_RESULT(req, bytes);
414
+ } else {
415
+ SET_REQ_WIN32_ERROR(req, GetLastError());
416
+ }
417
+ }
418
+
419
+
420
+ void fs__rmdir(uv_fs_t* req, const wchar_t* path) {
421
+ int result = _wrmdir(path);
422
+ SET_REQ_RESULT(req, result);
423
+ }
424
+
425
+
426
+ void fs__unlink(uv_fs_t* req, const wchar_t* path) {
427
+ int result;
428
+ HANDLE handle;
429
+ BY_HANDLE_FILE_INFORMATION info;
430
+ int is_dir_symlink;
431
+
432
+ handle = CreateFileW(path,
433
+ 0,
434
+ 0,
435
+ NULL,
436
+ OPEN_EXISTING,
437
+ FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS,
438
+ NULL);
439
+
440
+ if (handle == INVALID_HANDLE_VALUE) {
441
+ SET_REQ_WIN32_ERROR(req, GetLastError());
442
+ return;
443
+ }
444
+
445
+ if (!GetFileInformationByHandle(handle, &info)) {
446
+ SET_REQ_WIN32_ERROR(req, GetLastError());
447
+ CloseHandle(handle);
448
+ return;
449
+ }
450
+
451
+ is_dir_symlink = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY &&
452
+ get_reparse_point(handle, NULL);
453
+
454
+ CloseHandle(handle);
455
+
456
+ if (is_dir_symlink) {
457
+ fs__rmdir(req, path);
458
+ } else {
459
+ result = _wunlink(path);
460
+ SET_REQ_RESULT(req, result);
461
+ }
462
+ }
463
+
464
+
465
+ void fs__mkdir(uv_fs_t* req, const wchar_t* path, int mode) {
466
+ int result = _wmkdir(path);
467
+ SET_REQ_RESULT(req, result);
468
+ }
469
+
470
+
471
+ void fs__readdir(uv_fs_t* req, const wchar_t* path, int flags) {
472
+ int result, size;
473
+ wchar_t* buf = NULL, *ptr, *name;
474
+ HANDLE dir;
475
+ WIN32_FIND_DATAW ent = {0};
476
+ size_t len = wcslen(path);
477
+ size_t buf_char_len = 4096;
478
+ wchar_t* path2;
479
+ const wchar_t* fmt = !len ? L"./*"
480
+ : (path[len - 1] == L'/' || path[len - 1] == L'\\') ? L"%s*"
481
+ : L"%s\\*";
482
+
483
+ /* Figure out whether path is a file or a directory. */
484
+ if (!(GetFileAttributesW(path) & FILE_ATTRIBUTE_DIRECTORY)) {
485
+ req->result = -1;
486
+ req->errorno = UV_ENOTDIR;
487
+ req->sys_errno_ = ERROR_SUCCESS;
488
+ return;
489
+ }
490
+
491
+ path2 = (wchar_t*)malloc(sizeof(wchar_t) * (len + 4));
492
+ if (!path2) {
493
+ uv_fatal_error(ERROR_OUTOFMEMORY, "malloc");
494
+ }
495
+
496
+ #ifdef _MSC_VER
497
+ swprintf(path2, len + 3, fmt, path);
498
+ #else
499
+ swprintf(path2, fmt, path);
500
+ #endif
501
+ dir = FindFirstFileW(path2, &ent);
502
+ free(path2);
503
+
504
+ if(dir == INVALID_HANDLE_VALUE) {
505
+ SET_REQ_WIN32_ERROR(req, GetLastError());
506
+ return;
507
+ }
508
+
509
+ result = 0;
510
+
511
+ do {
512
+ name = ent.cFileName;
513
+
514
+ if (name[0] != L'.' || (name[1] && (name[1] != L'.' || name[2]))) {
515
+ len = wcslen(name);
516
+
517
+ if (!buf) {
518
+ buf = (wchar_t*)malloc(buf_char_len * sizeof(wchar_t));
519
+ if (!buf) {
520
+ uv_fatal_error(ERROR_OUTOFMEMORY, "malloc");
521
+ }
522
+
523
+ ptr = buf;
524
+ }
525
+
526
+ while ((ptr - buf) + len + 1 > buf_char_len) {
527
+ buf_char_len *= 2;
528
+ path2 = buf;
529
+ buf = (wchar_t*)realloc(buf, buf_char_len * sizeof(wchar_t));
530
+ if (!buf) {
531
+ uv_fatal_error(ERROR_OUTOFMEMORY, "realloc");
532
+ }
533
+
534
+ ptr = buf + (ptr - path2);
535
+ }
536
+
537
+ wcscpy(ptr, name);
538
+ ptr += len + 1;
539
+ result++;
540
+ }
541
+ } while(FindNextFileW(dir, &ent));
542
+
543
+ FindClose(dir);
544
+
545
+ if (buf) {
546
+ /* Convert result to UTF8. */
547
+ size = uv_utf16_to_utf8(buf, buf_char_len, NULL, 0);
548
+ if (!size) {
549
+ SET_REQ_WIN32_ERROR(req, GetLastError());
550
+ return;
551
+ }
552
+
553
+ req->ptr = (char*)malloc(size + 1);
554
+ if (!req->ptr) {
555
+ uv_fatal_error(ERROR_OUTOFMEMORY, "malloc");
556
+ }
557
+
558
+ size = uv_utf16_to_utf8(buf, buf_char_len, (char*)req->ptr, size);
559
+ if (!size) {
560
+ free(buf);
561
+ free(req->ptr);
562
+ req->ptr = NULL;
563
+ SET_REQ_WIN32_ERROR(req, GetLastError());
564
+ return;
565
+ }
566
+ free(buf);
567
+
568
+ ((char*)req->ptr)[size] = '\0';
569
+ req->flags |= UV_FS_FREE_PTR;
570
+ } else {
571
+ req->ptr = NULL;
572
+ }
573
+
574
+ SET_REQ_RESULT(req, result);
575
+ }
576
+
577
+
578
+ static void fs__stat(uv_fs_t* req, const wchar_t* path, int link) {
579
+ HANDLE handle;
580
+ int target_length;
581
+ int symlink = 0;
582
+ BY_HANDLE_FILE_INFORMATION info;
583
+ DWORD flags;
584
+
585
+ req->ptr = NULL;
586
+ flags = FILE_FLAG_BACKUP_SEMANTICS;
587
+
588
+ if (link) {
589
+ flags |= FILE_FLAG_OPEN_REPARSE_POINT;
590
+ }
591
+
592
+ handle = CreateFileW(path,
593
+ FILE_READ_ATTRIBUTES,
594
+ FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
595
+ NULL,
596
+ OPEN_EXISTING,
597
+ flags,
598
+ NULL);
599
+ if (handle == INVALID_HANDLE_VALUE) {
600
+ SET_REQ_WIN32_ERROR(req, GetLastError());
601
+ return;
602
+ }
603
+
604
+ if (!GetFileInformationByHandle(handle, &info)) {
605
+ SET_REQ_WIN32_ERROR(req, GetLastError());
606
+ CloseHandle(handle);
607
+ return;
608
+ }
609
+
610
+ memset(&req->stat, 0, sizeof req->stat);
611
+
612
+ /* TODO: set st_dev and st_ino? */
613
+
614
+ if (link && get_reparse_point(handle, &target_length)) {
615
+ req->stat.st_mode = S_IFLNK;
616
+ /* Adjust for long path */
617
+ req->stat.st_size = target_length - JUNCTION_PREFIX_LEN;
618
+ } else {
619
+ if (info.dwFileAttributes & FILE_ATTRIBUTE_READONLY) {
620
+ req->stat.st_mode |= (_S_IREAD + (_S_IREAD >> 3) + (_S_IREAD >> 6));
621
+ } else {
622
+ req->stat.st_mode |= ((_S_IREAD|_S_IWRITE) + ((_S_IREAD|_S_IWRITE) >> 3) +
623
+ ((_S_IREAD|_S_IWRITE) >> 6));
624
+ }
625
+
626
+ if (info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
627
+ req->stat.st_mode |= _S_IFDIR;
628
+ } else {
629
+ req->stat.st_mode |= _S_IFREG;
630
+ }
631
+
632
+ req->stat.st_size = ((int64_t) info.nFileSizeHigh << 32) +
633
+ (int64_t) info.nFileSizeLow;
634
+ }
635
+
636
+ req->stat.st_mtime = FILETIME_TO_TIME_T(info.ftLastWriteTime);
637
+ req->stat.st_atime = FILETIME_TO_TIME_T(info.ftLastAccessTime);
638
+ req->stat.st_ctime = FILETIME_TO_TIME_T(info.ftCreationTime);
639
+
640
+ req->stat.st_nlink = (info.nNumberOfLinks <= SHRT_MAX) ?
641
+ (short) info.nNumberOfLinks : SHRT_MAX;
642
+
643
+ req->ptr = &req->stat;
644
+ req->result = 0;
645
+ CloseHandle(handle);
646
+ }
647
+
648
+
649
+ void fs__fstat(uv_fs_t* req, uv_file file) {
650
+ int result;
651
+
652
+ VERIFY_UV_FILE(file, req);
653
+
654
+ result = _fstati64(file, &req->stat);
655
+ if (result == -1) {
656
+ req->ptr = NULL;
657
+ } else {
658
+ req->ptr = &req->stat;
659
+ }
660
+
661
+ SET_REQ_RESULT(req, result);
662
+ }
663
+
664
+
665
+ void fs__rename(uv_fs_t* req, const wchar_t* path, const wchar_t* new_path) {
666
+ if (!MoveFileExW(path, new_path, MOVEFILE_REPLACE_EXISTING)) {
667
+ SET_REQ_WIN32_ERROR(req, GetLastError());
668
+ return;
669
+ }
670
+
671
+ SET_REQ_RESULT(req, 0);
672
+ }
673
+
674
+
675
+ void fs__fsync(uv_fs_t* req, uv_file file) {
676
+ int result;
677
+
678
+ VERIFY_UV_FILE(file, req);
679
+
680
+ result = FlushFileBuffers((HANDLE)_get_osfhandle(file)) ? 0 : -1;
681
+ if (result == -1) {
682
+ SET_REQ_WIN32_ERROR(req, GetLastError());
683
+ } else {
684
+ SET_REQ_RESULT(req, result);
685
+ }
686
+ }
687
+
688
+
689
+ void fs__ftruncate(uv_fs_t* req, uv_file file, int64_t offset) {
690
+ HANDLE handle;
691
+ NTSTATUS status;
692
+ IO_STATUS_BLOCK io_status;
693
+ FILE_END_OF_FILE_INFORMATION eof_info;
694
+
695
+ VERIFY_UV_FILE(file, req);
696
+
697
+ handle = (HANDLE)_get_osfhandle(file);
698
+
699
+ eof_info.EndOfFile.QuadPart = offset;
700
+
701
+ status = pNtSetInformationFile(handle,
702
+ &io_status,
703
+ &eof_info,
704
+ sizeof eof_info,
705
+ FileEndOfFileInformation);
706
+
707
+ if (NT_SUCCESS(status)) {
708
+ SET_REQ_RESULT(req, 0);
709
+ } else {
710
+ SET_REQ_WIN32_ERROR(req, pRtlNtStatusToDosError(status));
711
+ }
712
+ }
713
+
714
+
715
+ void fs__sendfile(uv_fs_t* req, uv_file out_file, uv_file in_file,
716
+ int64_t in_offset, size_t length) {
717
+ const size_t max_buf_size = 65536;
718
+ size_t buf_size = length < max_buf_size ? length : max_buf_size;
719
+ int n, result = 0;
720
+ int64_t result_offset = 0;
721
+ char* buf = (char*)malloc(buf_size);
722
+ if (!buf) {
723
+ uv_fatal_error(ERROR_OUTOFMEMORY, "malloc");
724
+ }
725
+
726
+ if (in_offset != -1) {
727
+ result_offset = _lseeki64(in_file, in_offset, SEEK_SET);
728
+ }
729
+
730
+ if (result_offset == -1) {
731
+ result = -1;
732
+ } else {
733
+ while (length > 0) {
734
+ n = _read(in_file, buf, length < buf_size ? length : buf_size);
735
+ if (n == 0) {
736
+ break;
737
+ } else if (n == -1) {
738
+ result = -1;
739
+ break;
740
+ }
741
+
742
+ length -= n;
743
+
744
+ n = _write(out_file, buf, n);
745
+ if (n == -1) {
746
+ result = -1;
747
+ break;
748
+ }
749
+
750
+ result += n;
751
+ }
752
+ }
753
+
754
+ SET_REQ_RESULT(req, result);
755
+ }
756
+
757
+
758
+ void fs__chmod(uv_fs_t* req, const wchar_t* path, int mode) {
759
+ int result = _wchmod(path, mode);
760
+ SET_REQ_RESULT(req, result);
761
+ }
762
+
763
+
764
+ void fs__fchmod(uv_fs_t* req, uv_file file, int mode) {
765
+ int result;
766
+ HANDLE handle;
767
+ NTSTATUS nt_status;
768
+ IO_STATUS_BLOCK io_status;
769
+ FILE_BASIC_INFORMATION file_info;
770
+
771
+ VERIFY_UV_FILE(file, req);
772
+
773
+ handle = (HANDLE)_get_osfhandle(file);
774
+
775
+ nt_status = pNtQueryInformationFile(handle,
776
+ &io_status,
777
+ &file_info,
778
+ sizeof file_info,
779
+ FileBasicInformation);
780
+
781
+ if (nt_status != STATUS_SUCCESS) {
782
+ result = -1;
783
+ goto done;
784
+ }
785
+
786
+ if (mode & _S_IWRITE) {
787
+ file_info.FileAttributes &= ~FILE_ATTRIBUTE_READONLY;
788
+ } else {
789
+ file_info.FileAttributes |= FILE_ATTRIBUTE_READONLY;
790
+ }
791
+
792
+ nt_status = pNtSetInformationFile(handle,
793
+ &io_status,
794
+ &file_info,
795
+ sizeof file_info,
796
+ FileBasicInformation);
797
+
798
+ if (nt_status != STATUS_SUCCESS) {
799
+ result = -1;
800
+ goto done;
801
+ }
802
+
803
+ result = 0;
804
+
805
+ done:
806
+ SET_REQ_RESULT(req, result);
807
+ }
808
+
809
+
810
+ void fs__utime(uv_fs_t* req, const wchar_t* path, double atime, double mtime) {
811
+ int result;
812
+ struct _utimbuf b = {(time_t)atime, (time_t)mtime};
813
+ result = _wutime(path, &b);
814
+ SET_REQ_RESULT(req, result);
815
+ }
816
+
817
+
818
+ void fs__futime(uv_fs_t* req, uv_file file, double atime, double mtime) {
819
+ int result;
820
+ struct _utimbuf b = {(time_t)atime, (time_t)mtime};
821
+
822
+ VERIFY_UV_FILE(file, req);
823
+
824
+ result = _futime(file, &b);
825
+ SET_REQ_RESULT(req, result);
826
+ }
827
+
828
+
829
+ void fs__link(uv_fs_t* req, const wchar_t* path, const wchar_t* new_path) {
830
+ int result = CreateHardLinkW(new_path, path, NULL) ? 0 : -1;
831
+ if (result == -1) {
832
+ SET_REQ_WIN32_ERROR(req, GetLastError());
833
+ } else {
834
+ SET_REQ_RESULT(req, result);
835
+ }
836
+ }
837
+
838
+
839
+ void fs__create_junction(uv_fs_t* req, const wchar_t* path, const wchar_t* new_path) {
840
+ HANDLE handle = INVALID_HANDLE_VALUE;
841
+ REPARSE_DATA_BUFFER *buffer = NULL;
842
+ int created = 0;
843
+ int target_len;
844
+ int is_absolute, is_long_path;
845
+ int needed_buf_size, used_buf_size, used_data_size, path_buf_len;
846
+ int start, len, i;
847
+ int add_slash;
848
+ DWORD bytes;
849
+ wchar_t* path_buf;
850
+
851
+ target_len = wcslen(path);
852
+ is_long_path = wcsncmp(path, LONG_PATH_PREFIX, LONG_PATH_PREFIX_LEN) == 0;
853
+
854
+ if (is_long_path) {
855
+ is_absolute = 1;
856
+ } else {
857
+ is_absolute = target_len >= 3 && IS_LETTER(path[0]) &&
858
+ path[1] == L':' && IS_SLASH(path[2]);
859
+ }
860
+
861
+ if (!is_absolute) {
862
+ /* Not supporting relative paths */
863
+ SET_REQ_UV_ERROR(req, UV_EINVAL, ERROR_NOT_SUPPORTED);
864
+ return;
865
+ }
866
+
867
+ // Do a pessimistic calculation of the required buffer size
868
+ needed_buf_size =
869
+ FIELD_OFFSET(REPARSE_DATA_BUFFER, MountPointReparseBuffer.PathBuffer) +
870
+ JUNCTION_PREFIX_LEN * sizeof(wchar_t) +
871
+ 2 * (target_len + 2) * sizeof(wchar_t);
872
+
873
+ // Allocate the buffer
874
+ buffer = (REPARSE_DATA_BUFFER*)malloc(needed_buf_size);
875
+ if (!buffer) {
876
+ uv_fatal_error(ERROR_OUTOFMEMORY, "malloc");
877
+ }
878
+
879
+ // Grab a pointer to the part of the buffer where filenames go
880
+ path_buf = (wchar_t*)&(buffer->MountPointReparseBuffer.PathBuffer);
881
+ path_buf_len = 0;
882
+
883
+ // Copy the substitute (internal) target path
884
+ start = path_buf_len;
885
+
886
+ wcsncpy((wchar_t*)&path_buf[path_buf_len], JUNCTION_PREFIX,
887
+ JUNCTION_PREFIX_LEN);
888
+ path_buf_len += JUNCTION_PREFIX_LEN;
889
+
890
+ add_slash = 0;
891
+ for (i = is_long_path ? LONG_PATH_PREFIX_LEN : 0; path[i] != L'\0'; i++) {
892
+ if (IS_SLASH(path[i])) {
893
+ add_slash = 1;
894
+ continue;
895
+ }
896
+
897
+ if (add_slash) {
898
+ path_buf[path_buf_len++] = L'\\';
899
+ add_slash = 0;
900
+ }
901
+
902
+ path_buf[path_buf_len++] = path[i];
903
+ }
904
+ path_buf[path_buf_len++] = L'\\';
905
+ len = path_buf_len - start;
906
+
907
+ // Set the info about the substitute name
908
+ buffer->MountPointReparseBuffer.SubstituteNameOffset = start * sizeof(wchar_t);
909
+ buffer->MountPointReparseBuffer.SubstituteNameLength = len * sizeof(wchar_t);
910
+
911
+ // Insert null terminator
912
+ path_buf[path_buf_len++] = L'\0';
913
+
914
+ // Copy the print name of the target path
915
+ start = path_buf_len;
916
+ add_slash = 0;
917
+ for (i = is_long_path ? LONG_PATH_PREFIX_LEN : 0; path[i] != L'\0'; i++) {
918
+ if (IS_SLASH(path[i])) {
919
+ add_slash = 1;
920
+ continue;
921
+ }
922
+
923
+ if (add_slash) {
924
+ path_buf[path_buf_len++] = L'\\';
925
+ add_slash = 0;
926
+ }
927
+
928
+ path_buf[path_buf_len++] = path[i];
929
+ }
930
+ len = path_buf_len - start;
931
+ if (len == 2) {
932
+ path_buf[path_buf_len++] = L'\\';
933
+ len++;
934
+ }
935
+
936
+ // Set the info about the print name
937
+ buffer->MountPointReparseBuffer.PrintNameOffset = start * sizeof(wchar_t);
938
+ buffer->MountPointReparseBuffer.PrintNameLength = len * sizeof(wchar_t);
939
+
940
+ // Insert another null terminator
941
+ path_buf[path_buf_len++] = L'\0';
942
+
943
+ // Calculate how much buffer space was actually used
944
+ used_buf_size = FIELD_OFFSET(REPARSE_DATA_BUFFER, MountPointReparseBuffer.PathBuffer) +
945
+ path_buf_len * sizeof(wchar_t);
946
+ used_data_size = used_buf_size -
947
+ FIELD_OFFSET(REPARSE_DATA_BUFFER, MountPointReparseBuffer);
948
+
949
+ // Put general info in the data buffer
950
+ buffer->ReparseTag = IO_REPARSE_TAG_MOUNT_POINT;
951
+ buffer->ReparseDataLength = used_data_size;
952
+ buffer->Reserved = 0;
953
+
954
+ // Create a new directory
955
+ if (!CreateDirectoryW(new_path, NULL)) {
956
+ SET_REQ_WIN32_ERROR(req, GetLastError());
957
+ goto error;
958
+ }
959
+ created = 1;
960
+
961
+ // Open the directory
962
+ handle = CreateFileW(new_path,
963
+ GENERIC_ALL,
964
+ 0,
965
+ NULL,
966
+ OPEN_EXISTING,
967
+ FILE_FLAG_BACKUP_SEMANTICS |
968
+ FILE_FLAG_OPEN_REPARSE_POINT,
969
+ NULL);
970
+ if (handle == INVALID_HANDLE_VALUE) {
971
+ SET_REQ_WIN32_ERROR(req, GetLastError());
972
+ goto error;
973
+ }
974
+
975
+ // Create the actual reparse point
976
+ if (!DeviceIoControl(handle,
977
+ FSCTL_SET_REPARSE_POINT,
978
+ buffer,
979
+ used_buf_size,
980
+ NULL,
981
+ 0,
982
+ &bytes,
983
+ NULL)) {
984
+ SET_REQ_WIN32_ERROR(req, GetLastError());
985
+ goto error;
986
+ }
987
+
988
+ // Clean up
989
+ CloseHandle(handle);
990
+ free(buffer);
991
+
992
+ SET_REQ_RESULT(req, 0);
993
+ return;
994
+
995
+ error:
996
+ free(buffer);
997
+
998
+ if (handle != INVALID_HANDLE_VALUE) {
999
+ CloseHandle(handle);
1000
+ }
1001
+
1002
+ if (created) {
1003
+ RemoveDirectoryW(new_path);
1004
+ }
1005
+ }
1006
+
1007
+
1008
+ void fs__symlink(uv_fs_t* req, const wchar_t* path, const wchar_t* new_path,
1009
+ int flags) {
1010
+ int result;
1011
+
1012
+ if (flags & UV_FS_SYMLINK_JUNCTION) {
1013
+ fs__create_junction(req, path, new_path);
1014
+ } else if (pCreateSymbolicLinkW) {
1015
+ result = pCreateSymbolicLinkW(new_path,
1016
+ path,
1017
+ flags & UV_FS_SYMLINK_DIR ? SYMBOLIC_LINK_FLAG_DIRECTORY : 0) ? 0 : -1;
1018
+ if (result == -1) {
1019
+ SET_REQ_WIN32_ERROR(req, GetLastError());
1020
+ } else {
1021
+ SET_REQ_RESULT(req, result);
1022
+ }
1023
+ } else {
1024
+ SET_REQ_UV_ERROR(req, UV_ENOSYS, ERROR_NOT_SUPPORTED);
1025
+ }
1026
+ }
1027
+
1028
+
1029
+ void fs__readlink(uv_fs_t* req, const wchar_t* path) {
1030
+ int result = -1;
1031
+ BOOL rv;
1032
+ HANDLE symlink;
1033
+ void* buffer = NULL;
1034
+ DWORD bytes_returned;
1035
+ REPARSE_DATA_BUFFER* reparse_data;
1036
+ int utf8size;
1037
+ wchar_t* substitute_name;
1038
+ int substitute_name_length;
1039
+
1040
+ symlink = CreateFileW(path,
1041
+ 0,
1042
+ 0,
1043
+ NULL,
1044
+ OPEN_EXISTING,
1045
+ FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS,
1046
+ NULL);
1047
+
1048
+ if (INVALID_HANDLE_VALUE == symlink) {
1049
+ result = -1;
1050
+ SET_REQ_WIN32_ERROR(req, GetLastError());
1051
+ goto done;
1052
+ }
1053
+
1054
+ buffer = malloc(MAXIMUM_REPARSE_DATA_BUFFER_SIZE);
1055
+ if (!buffer) {
1056
+ uv_fatal_error(ERROR_OUTOFMEMORY, "malloc");
1057
+ }
1058
+
1059
+ rv = DeviceIoControl(symlink,
1060
+ FSCTL_GET_REPARSE_POINT,
1061
+ NULL,
1062
+ 0,
1063
+ buffer,
1064
+ MAXIMUM_REPARSE_DATA_BUFFER_SIZE,
1065
+ &bytes_returned,
1066
+ NULL);
1067
+
1068
+ if (!rv) {
1069
+ result = -1;
1070
+ SET_REQ_WIN32_ERROR(req, GetLastError());
1071
+ goto done;
1072
+ }
1073
+
1074
+ reparse_data = (REPARSE_DATA_BUFFER*)buffer;
1075
+ if (reparse_data->ReparseTag == IO_REPARSE_TAG_SYMLINK) {
1076
+ substitute_name = reparse_data->SymbolicLinkReparseBuffer.PathBuffer +
1077
+ (reparse_data->SymbolicLinkReparseBuffer.SubstituteNameOffset /
1078
+ sizeof(wchar_t));
1079
+ substitute_name_length =
1080
+ reparse_data->SymbolicLinkReparseBuffer.SubstituteNameLength /
1081
+ sizeof(wchar_t);
1082
+ } else if (reparse_data->ReparseTag == IO_REPARSE_TAG_MOUNT_POINT) {
1083
+ substitute_name = reparse_data->MountPointReparseBuffer.PathBuffer +
1084
+ (reparse_data->MountPointReparseBuffer.SubstituteNameOffset /
1085
+ sizeof(wchar_t));
1086
+ substitute_name_length =
1087
+ reparse_data->MountPointReparseBuffer.SubstituteNameLength /
1088
+ sizeof(wchar_t);
1089
+ } else {
1090
+ result = -1;
1091
+ /* something is seriously wrong */
1092
+ SET_REQ_WIN32_ERROR(req, GetLastError());
1093
+ goto done;
1094
+ }
1095
+
1096
+ /* Strip off the leading \??\ from the substitute name buffer.*/
1097
+ if (wcsncmp(substitute_name, JUNCTION_PREFIX, JUNCTION_PREFIX_LEN) == 0) {
1098
+ substitute_name += JUNCTION_PREFIX_LEN;
1099
+ substitute_name_length -= JUNCTION_PREFIX_LEN;
1100
+ }
1101
+
1102
+ utf8size = uv_utf16_to_utf8(substitute_name,
1103
+ substitute_name_length,
1104
+ NULL,
1105
+ 0);
1106
+ if (!utf8size) {
1107
+ result = -1;
1108
+ SET_REQ_WIN32_ERROR(req, GetLastError());
1109
+ goto done;
1110
+ }
1111
+
1112
+ req->ptr = malloc(utf8size + 1);
1113
+ if (!req->ptr) {
1114
+ uv_fatal_error(ERROR_OUTOFMEMORY, "malloc");
1115
+ }
1116
+
1117
+ utf8size = uv_utf16_to_utf8(substitute_name,
1118
+ substitute_name_length,
1119
+ (char*)req->ptr,
1120
+ utf8size);
1121
+ if (!utf8size) {
1122
+ result = -1;
1123
+ SET_REQ_WIN32_ERROR(req, GetLastError());
1124
+ goto done;
1125
+ }
1126
+
1127
+ req->flags |= UV_FS_FREE_PTR;
1128
+ ((char*)req->ptr)[utf8size] = '\0';
1129
+ result = 0;
1130
+
1131
+ done:
1132
+ if (buffer) {
1133
+ free(buffer);
1134
+ }
1135
+
1136
+ if (symlink != INVALID_HANDLE_VALUE) {
1137
+ CloseHandle(symlink);
1138
+ }
1139
+
1140
+ SET_REQ_RESULT(req, result);
1141
+ }
1142
+
1143
+
1144
+ void fs__nop(uv_fs_t* req) {
1145
+ req->result = 0;
1146
+ }
1147
+
1148
+
1149
+ static DWORD WINAPI uv_fs_thread_proc(void* parameter) {
1150
+ uv_fs_t* req = (uv_fs_t*) parameter;
1151
+ uv_loop_t* loop = req->loop;
1152
+
1153
+ assert(req != NULL);
1154
+ assert(req->type == UV_FS);
1155
+
1156
+ switch (req->fs_type) {
1157
+ case UV_FS_OPEN:
1158
+ fs__open(req, req->pathw, req->file_flags, (int)req->mode);
1159
+ break;
1160
+ case UV_FS_CLOSE:
1161
+ fs__close(req, req->file);
1162
+ break;
1163
+ case UV_FS_READ:
1164
+ fs__read(req, req->file, req->buf, req->length, req->offset);
1165
+ break;
1166
+ case UV_FS_WRITE:
1167
+ fs__write(req, req->file, req->buf, req->length, req->offset);
1168
+ break;
1169
+ case UV_FS_UNLINK:
1170
+ fs__unlink(req, req->pathw);
1171
+ break;
1172
+ case UV_FS_MKDIR:
1173
+ fs__mkdir(req, req->pathw, req->mode);
1174
+ break;
1175
+ case UV_FS_RMDIR:
1176
+ fs__rmdir(req, req->pathw);
1177
+ break;
1178
+ case UV_FS_READDIR:
1179
+ fs__readdir(req, req->pathw, req->file_flags);
1180
+ break;
1181
+ case UV_FS_STAT:
1182
+ fs__stat(req, req->pathw, 0);
1183
+ break;
1184
+ case UV_FS_LSTAT:
1185
+ fs__stat(req, req->pathw, 1);
1186
+ break;
1187
+ case UV_FS_FSTAT:
1188
+ fs__fstat(req, req->file);
1189
+ break;
1190
+ case UV_FS_RENAME:
1191
+ fs__rename(req, req->pathw, req->new_pathw);
1192
+ break;
1193
+ case UV_FS_FSYNC:
1194
+ case UV_FS_FDATASYNC:
1195
+ fs__fsync(req, req->file);
1196
+ break;
1197
+ case UV_FS_FTRUNCATE:
1198
+ fs__ftruncate(req, req->file, req->offset);
1199
+ break;
1200
+ case UV_FS_SENDFILE:
1201
+ fs__sendfile(req, req->file_out, req->file, req->offset, req->length);
1202
+ break;
1203
+ case UV_FS_CHMOD:
1204
+ fs__chmod(req, req->pathw, req->mode);
1205
+ break;
1206
+ case UV_FS_FCHMOD:
1207
+ fs__fchmod(req, req->file, req->mode);
1208
+ break;
1209
+ case UV_FS_UTIME:
1210
+ fs__utime(req, req->pathw, req->atime, req->mtime);
1211
+ break;
1212
+ case UV_FS_FUTIME:
1213
+ fs__futime(req, req->file, req->atime, req->mtime);
1214
+ break;
1215
+ case UV_FS_LINK:
1216
+ fs__link(req, req->pathw, req->new_pathw);
1217
+ break;
1218
+ case UV_FS_SYMLINK:
1219
+ fs__symlink(req, req->pathw, req->new_pathw, req->file_flags);
1220
+ break;
1221
+ case UV_FS_READLINK:
1222
+ fs__readlink(req, req->pathw);
1223
+ break;
1224
+ case UV_FS_CHOWN:
1225
+ case UV_FS_FCHOWN:
1226
+ fs__nop(req);
1227
+ break;
1228
+ default:
1229
+ assert(!"bad uv_fs_type");
1230
+ }
1231
+
1232
+ POST_COMPLETION_FOR_REQ(loop, req);
1233
+
1234
+ return 0;
1235
+ }
1236
+
1237
+
1238
+ int uv_fs_open(uv_loop_t* loop, uv_fs_t* req, const char* path, int flags,
1239
+ int mode, uv_fs_cb cb) {
1240
+ wchar_t* pathw;
1241
+ int size;
1242
+
1243
+ /* Convert to UTF16. */
1244
+ UTF8_TO_UTF16(path, pathw);
1245
+
1246
+ if (cb) {
1247
+ uv_fs_req_init_async(loop, req, UV_FS_OPEN, path, pathw, cb);
1248
+ req->file_flags = flags;
1249
+ req->mode = mode;
1250
+ QUEUE_FS_TP_JOB(loop, req);
1251
+ } else {
1252
+ uv_fs_req_init_sync(loop, req, UV_FS_OPEN);
1253
+ fs__open(req, pathw, flags, mode);
1254
+ free(pathw);
1255
+ SET_UV_LAST_ERROR_FROM_REQ(req);
1256
+ return req->result;
1257
+ }
1258
+
1259
+ return 0;
1260
+ }
1261
+
1262
+
1263
+ int uv_fs_close(uv_loop_t* loop, uv_fs_t* req, uv_file file, uv_fs_cb cb) {
1264
+ if (cb) {
1265
+ uv_fs_req_init_async(loop, req, UV_FS_CLOSE, NULL, NULL, cb);
1266
+ req->file = file;
1267
+ QUEUE_FS_TP_JOB(loop, req);
1268
+ } else {
1269
+ uv_fs_req_init_sync(loop, req, UV_FS_CLOSE);
1270
+ fs__close(req, file);
1271
+ SET_UV_LAST_ERROR_FROM_REQ(req);
1272
+ return req->result;
1273
+ }
1274
+
1275
+ return 0;
1276
+ }
1277
+
1278
+
1279
+ int uv_fs_read(uv_loop_t* loop, uv_fs_t* req, uv_file file, void* buf,
1280
+ size_t length, int64_t offset, uv_fs_cb cb) {
1281
+ if (cb) {
1282
+ uv_fs_req_init_async(loop, req, UV_FS_READ, NULL, NULL, cb);
1283
+ req->file = file;
1284
+ req->buf = buf;
1285
+ req->length = length;
1286
+ req->offset = offset;
1287
+ QUEUE_FS_TP_JOB(loop, req);
1288
+ } else {
1289
+ uv_fs_req_init_sync(loop, req, UV_FS_READ);
1290
+ fs__read(req, file, buf, length, offset);
1291
+ SET_UV_LAST_ERROR_FROM_REQ(req);
1292
+ return req->result;
1293
+ }
1294
+
1295
+ return 0;
1296
+ }
1297
+
1298
+
1299
+ int uv_fs_write(uv_loop_t* loop, uv_fs_t* req, uv_file file, void* buf,
1300
+ size_t length, int64_t offset, uv_fs_cb cb) {
1301
+ if (cb) {
1302
+ uv_fs_req_init_async(loop, req, UV_FS_WRITE, NULL, NULL, cb);
1303
+ req->file = file;
1304
+ req->buf = buf;
1305
+ req->length = length;
1306
+ req->offset = offset;
1307
+ QUEUE_FS_TP_JOB(loop, req);
1308
+ } else {
1309
+ uv_fs_req_init_sync(loop, req, UV_FS_WRITE);
1310
+ fs__write(req, file, buf, length, offset);
1311
+ SET_UV_LAST_ERROR_FROM_REQ(req);
1312
+ return req->result;
1313
+ }
1314
+
1315
+ return 0;
1316
+ }
1317
+
1318
+
1319
+ int uv_fs_unlink(uv_loop_t* loop, uv_fs_t* req, const char* path,
1320
+ uv_fs_cb cb) {
1321
+ wchar_t* pathw;
1322
+ int size;
1323
+
1324
+ /* Convert to UTF16. */
1325
+ UTF8_TO_UTF16(path, pathw);
1326
+
1327
+ if (cb) {
1328
+ uv_fs_req_init_async(loop, req, UV_FS_UNLINK, path, pathw, cb);
1329
+ QUEUE_FS_TP_JOB(loop, req);
1330
+ } else {
1331
+ uv_fs_req_init_sync(loop, req, UV_FS_UNLINK);
1332
+ fs__unlink(req, pathw);
1333
+ free(pathw);
1334
+ SET_UV_LAST_ERROR_FROM_REQ(req);
1335
+ return req->result;
1336
+ }
1337
+
1338
+ return 0;
1339
+ }
1340
+
1341
+
1342
+ int uv_fs_mkdir(uv_loop_t* loop, uv_fs_t* req, const char* path, int mode,
1343
+ uv_fs_cb cb) {
1344
+ wchar_t* pathw;
1345
+ int size;
1346
+
1347
+ /* Convert to UTF16. */
1348
+ UTF8_TO_UTF16(path, pathw);
1349
+
1350
+ if (cb) {
1351
+ uv_fs_req_init_async(loop, req, UV_FS_MKDIR, path, pathw, cb);
1352
+ req->mode = mode;
1353
+ QUEUE_FS_TP_JOB(loop, req);
1354
+ } else {
1355
+ uv_fs_req_init_sync(loop, req, UV_FS_MKDIR);
1356
+ fs__mkdir(req, pathw, mode);
1357
+ free(pathw);
1358
+ SET_UV_LAST_ERROR_FROM_REQ(req);
1359
+ return req->result;
1360
+ }
1361
+
1362
+ return 0;
1363
+ }
1364
+
1365
+
1366
+ int uv_fs_rmdir(uv_loop_t* loop, uv_fs_t* req, const char* path, uv_fs_cb cb) {
1367
+ wchar_t* pathw;
1368
+ int size;
1369
+
1370
+ /* Convert to UTF16. */
1371
+ UTF8_TO_UTF16(path, pathw);
1372
+
1373
+ if (cb) {
1374
+ uv_fs_req_init_async(loop, req, UV_FS_RMDIR, path, pathw, cb);
1375
+ QUEUE_FS_TP_JOB(loop, req);
1376
+ } else {
1377
+ uv_fs_req_init_sync(loop, req, UV_FS_RMDIR);
1378
+ fs__rmdir(req, pathw);
1379
+ free(pathw);
1380
+ SET_UV_LAST_ERROR_FROM_REQ(req);
1381
+ return req->result;
1382
+ }
1383
+
1384
+ return 0;
1385
+ }
1386
+
1387
+
1388
+ int uv_fs_readdir(uv_loop_t* loop, uv_fs_t* req, const char* path, int flags,
1389
+ uv_fs_cb cb) {
1390
+ wchar_t* pathw;
1391
+ int size;
1392
+
1393
+ /* Convert to UTF16. */
1394
+ UTF8_TO_UTF16(path, pathw);
1395
+
1396
+ if (cb) {
1397
+ uv_fs_req_init_async(loop, req, UV_FS_READDIR, path, pathw, cb);
1398
+ req->file_flags = flags;
1399
+ QUEUE_FS_TP_JOB(loop, req);
1400
+ } else {
1401
+ uv_fs_req_init_sync(loop, req, UV_FS_READDIR);
1402
+ fs__readdir(req, pathw, flags);
1403
+ free(pathw);
1404
+ SET_UV_LAST_ERROR_FROM_REQ(req);
1405
+ return req->result;
1406
+ }
1407
+
1408
+ return 0;
1409
+ }
1410
+
1411
+
1412
+ int uv_fs_link(uv_loop_t* loop, uv_fs_t* req, const char* path,
1413
+ const char* new_path, uv_fs_cb cb) {
1414
+ wchar_t* pathw;
1415
+ wchar_t* new_pathw;
1416
+ int size;
1417
+
1418
+ /* Convert to UTF16. */
1419
+ UTF8_TO_UTF16(path, pathw);
1420
+ UTF8_TO_UTF16(new_path, new_pathw);
1421
+
1422
+ if (cb) {
1423
+ uv_fs_req_init_async(loop, req, UV_FS_LINK, path, pathw, cb);
1424
+ req->new_pathw = new_pathw;
1425
+ req->flags |= UV_FS_FREE_NEW_PATH;
1426
+ QUEUE_FS_TP_JOB(loop, req);
1427
+ } else {
1428
+ uv_fs_req_init_sync(loop, req, UV_FS_LINK);
1429
+ fs__link(req, pathw, new_pathw);
1430
+ free(pathw);
1431
+ free(new_pathw);
1432
+ SET_UV_LAST_ERROR_FROM_REQ(req);
1433
+ return req->result;
1434
+ }
1435
+
1436
+ return 0;
1437
+ }
1438
+
1439
+
1440
+ int uv_fs_symlink(uv_loop_t* loop, uv_fs_t* req, const char* path,
1441
+ const char* new_path, int flags, uv_fs_cb cb) {
1442
+ wchar_t* pathw;
1443
+ wchar_t* new_pathw;
1444
+ int size;
1445
+
1446
+ /* Convert to UTF16. */
1447
+ UTF8_TO_UTF16(path, pathw);
1448
+ UTF8_TO_UTF16(new_path, new_pathw);
1449
+
1450
+ if (cb) {
1451
+ uv_fs_req_init_async(loop, req, UV_FS_SYMLINK, path, pathw, cb);
1452
+ req->new_pathw = new_pathw;
1453
+ req->file_flags = flags;
1454
+ req->flags |= UV_FS_FREE_NEW_PATH;
1455
+ QUEUE_FS_TP_JOB(loop, req);
1456
+ } else {
1457
+ uv_fs_req_init_sync(loop, req, UV_FS_SYMLINK);
1458
+ fs__symlink(req, pathw, new_pathw, flags);
1459
+ free(pathw);
1460
+ free(new_pathw);
1461
+ SET_UV_LAST_ERROR_FROM_REQ(req);
1462
+ return req->result;
1463
+ }
1464
+
1465
+ return 0;
1466
+ }
1467
+
1468
+
1469
+ int uv_fs_readlink(uv_loop_t* loop, uv_fs_t* req, const char* path,
1470
+ uv_fs_cb cb) {
1471
+ wchar_t* pathw;
1472
+ int size;
1473
+
1474
+ /* Convert to UTF16. */
1475
+ UTF8_TO_UTF16(path, pathw);
1476
+
1477
+ if (cb) {
1478
+ uv_fs_req_init_async(loop, req, UV_FS_READLINK, path, pathw, cb);
1479
+ QUEUE_FS_TP_JOB(loop, req);
1480
+ } else {
1481
+ uv_fs_req_init_sync(loop, req, UV_FS_READLINK);
1482
+ fs__readlink(req, pathw);
1483
+ free(pathw);
1484
+ SET_UV_LAST_ERROR_FROM_REQ(req);
1485
+ return req->result;
1486
+ }
1487
+
1488
+ return 0;
1489
+ }
1490
+
1491
+
1492
+ int uv_fs_chown(uv_loop_t* loop, uv_fs_t* req, const char* path, int uid,
1493
+ int gid, uv_fs_cb cb) {
1494
+ wchar_t* pathw;
1495
+ int size;
1496
+
1497
+ /* Convert to UTF16. */
1498
+ UTF8_TO_UTF16(path, pathw);
1499
+
1500
+ if (cb) {
1501
+ uv_fs_req_init_async(loop, req, UV_FS_CHOWN, path, pathw, cb);
1502
+ QUEUE_FS_TP_JOB(loop, req);
1503
+ } else {
1504
+ uv_fs_req_init_sync(loop, req, UV_FS_CHOWN);
1505
+ fs__nop(req);
1506
+ free(pathw);
1507
+ SET_UV_LAST_ERROR_FROM_REQ(req);
1508
+ return req->result;
1509
+ }
1510
+
1511
+ return 0;
1512
+ }
1513
+
1514
+
1515
+ int uv_fs_fchown(uv_loop_t* loop, uv_fs_t* req, uv_file file, int uid,
1516
+ int gid, uv_fs_cb cb) {
1517
+ if (cb) {
1518
+ uv_fs_req_init_async(loop, req, UV_FS_FCHOWN, NULL, NULL, cb);
1519
+ QUEUE_FS_TP_JOB(loop, req);
1520
+ } else {
1521
+ uv_fs_req_init_sync(loop, req, UV_FS_FCHOWN);
1522
+ fs__nop(req);
1523
+ SET_UV_LAST_ERROR_FROM_REQ(req);
1524
+ return req->result;
1525
+ }
1526
+
1527
+ return 0;
1528
+ }
1529
+
1530
+
1531
+ int uv_fs_stat(uv_loop_t* loop, uv_fs_t* req, const char* path, uv_fs_cb cb) {
1532
+ int len = strlen(path);
1533
+ char* path2 = NULL;
1534
+ wchar_t* pathw;
1535
+ int size;
1536
+
1537
+ if (len > 1 && path[len - 2] != ':' &&
1538
+ (path[len - 1] == '\\' || path[len - 1] == '/')) {
1539
+ path2 = strdup(path);
1540
+ if (!path2) {
1541
+ uv_fatal_error(ERROR_OUTOFMEMORY, "malloc");
1542
+ }
1543
+
1544
+ path2[len - 1] = '\0';
1545
+ }
1546
+
1547
+ if (cb) {
1548
+ uv_fs_req_init_async(loop, req, UV_FS_STAT, NULL, NULL, cb);
1549
+ if (path2) {
1550
+ req->path = path2;
1551
+ UTF8_TO_UTF16(path2, req->pathw);
1552
+ } else {
1553
+ req->path = strdup(path);
1554
+ UTF8_TO_UTF16(path, req->pathw);
1555
+ }
1556
+
1557
+ QUEUE_FS_TP_JOB(loop, req);
1558
+ } else {
1559
+ uv_fs_req_init_sync(loop, req, UV_FS_STAT);
1560
+ UTF8_TO_UTF16(path2 ? path2 : path, pathw);
1561
+ fs__stat(req, pathw, 0);
1562
+ if (path2) {
1563
+ free(path2);
1564
+ }
1565
+ free(pathw);
1566
+ SET_UV_LAST_ERROR_FROM_REQ(req);
1567
+ return req->result;
1568
+ }
1569
+
1570
+ return 0;
1571
+ }
1572
+
1573
+
1574
+ /* TODO: add support for links. */
1575
+ int uv_fs_lstat(uv_loop_t* loop, uv_fs_t* req, const char* path, uv_fs_cb cb) {
1576
+ int len = strlen(path);
1577
+ char* path2 = NULL;
1578
+ wchar_t* pathw;
1579
+ int size;
1580
+
1581
+ if (len > 1 && path[len - 2] != ':' &&
1582
+ (path[len - 1] == '\\' || path[len - 1] == '/')) {
1583
+ path2 = strdup(path);
1584
+ if (!path2) {
1585
+ uv_fatal_error(ERROR_OUTOFMEMORY, "malloc");
1586
+ }
1587
+
1588
+ path2[len - 1] = '\0';
1589
+ }
1590
+
1591
+ if (cb) {
1592
+ uv_fs_req_init_async(loop, req, UV_FS_LSTAT, NULL, NULL, cb);
1593
+ if (path2) {
1594
+ req->path = path2;
1595
+ UTF8_TO_UTF16(path2, req->pathw);
1596
+ } else {
1597
+ req->path = strdup(path);
1598
+ UTF8_TO_UTF16(path, req->pathw);
1599
+ }
1600
+
1601
+ QUEUE_FS_TP_JOB(loop, req);
1602
+ } else {
1603
+ uv_fs_req_init_sync(loop, req, UV_FS_LSTAT);
1604
+ UTF8_TO_UTF16(path2 ? path2 : path, pathw);
1605
+ fs__stat(req, pathw, 1);
1606
+ if (path2) {
1607
+ free(path2);
1608
+ }
1609
+ free(pathw);
1610
+ SET_UV_LAST_ERROR_FROM_REQ(req);
1611
+ return req->result;
1612
+ }
1613
+
1614
+ return 0;
1615
+ }
1616
+
1617
+
1618
+ int uv_fs_fstat(uv_loop_t* loop, uv_fs_t* req, uv_file file, uv_fs_cb cb) {
1619
+ if (cb) {
1620
+ uv_fs_req_init_async(loop, req, UV_FS_FSTAT, NULL, NULL, cb);
1621
+ req->file = file;
1622
+ QUEUE_FS_TP_JOB(loop, req);
1623
+ } else {
1624
+ uv_fs_req_init_sync(loop, req, UV_FS_FSTAT);
1625
+ fs__fstat(req, file);
1626
+ SET_UV_LAST_ERROR_FROM_REQ(req);
1627
+ return req->result;
1628
+ }
1629
+
1630
+ return 0;
1631
+ }
1632
+
1633
+
1634
+ int uv_fs_rename(uv_loop_t* loop, uv_fs_t* req, const char* path,
1635
+ const char* new_path, uv_fs_cb cb) {
1636
+ wchar_t* pathw;
1637
+ wchar_t* new_pathw;
1638
+ int size;
1639
+
1640
+ /* Convert to UTF16. */
1641
+ UTF8_TO_UTF16(path, pathw);
1642
+ UTF8_TO_UTF16(new_path, new_pathw);
1643
+
1644
+ if (cb) {
1645
+ uv_fs_req_init_async(loop, req, UV_FS_RENAME, path, pathw, cb);
1646
+ req->new_pathw = new_pathw;
1647
+ req->flags |= UV_FS_FREE_NEW_PATH;
1648
+ QUEUE_FS_TP_JOB(loop, req);
1649
+ } else {
1650
+ uv_fs_req_init_sync(loop, req, UV_FS_RENAME);
1651
+ fs__rename(req, pathw, new_pathw);
1652
+ free(pathw);
1653
+ free(new_pathw);
1654
+ SET_UV_LAST_ERROR_FROM_REQ(req);
1655
+ return req->result;
1656
+ }
1657
+
1658
+ return 0;
1659
+ }
1660
+
1661
+
1662
+ int uv_fs_fdatasync(uv_loop_t* loop, uv_fs_t* req, uv_file file, uv_fs_cb cb) {
1663
+ if (cb) {
1664
+ uv_fs_req_init_async(loop, req, UV_FS_FDATASYNC, NULL, NULL, cb);
1665
+ req->file = file;
1666
+ QUEUE_FS_TP_JOB(loop, req);
1667
+ } else {
1668
+ uv_fs_req_init_sync(loop, req, UV_FS_FDATASYNC);
1669
+ fs__fsync(req, file);
1670
+ SET_UV_LAST_ERROR_FROM_REQ(req);
1671
+ return req->result;
1672
+ }
1673
+
1674
+ return 0;
1675
+ }
1676
+
1677
+
1678
+ int uv_fs_fsync(uv_loop_t* loop, uv_fs_t* req, uv_file file, uv_fs_cb cb) {
1679
+ if (cb) {
1680
+ uv_fs_req_init_async(loop, req, UV_FS_FSYNC, NULL, NULL, cb);
1681
+ req->file = file;
1682
+ QUEUE_FS_TP_JOB(loop, req);
1683
+ } else {
1684
+ uv_fs_req_init_sync(loop, req, UV_FS_FSYNC);
1685
+ fs__fsync(req, file);
1686
+ SET_UV_LAST_ERROR_FROM_REQ(req);
1687
+ return req->result;
1688
+ }
1689
+
1690
+ return 0;
1691
+ }
1692
+
1693
+
1694
+ int uv_fs_ftruncate(uv_loop_t* loop, uv_fs_t* req, uv_file file,
1695
+ int64_t offset, uv_fs_cb cb) {
1696
+ if (cb) {
1697
+ uv_fs_req_init_async(loop, req, UV_FS_FTRUNCATE, NULL, NULL, cb);
1698
+ req->file = file;
1699
+ req->offset = offset;
1700
+ QUEUE_FS_TP_JOB(loop, req);
1701
+ } else {
1702
+ uv_fs_req_init_sync(loop, req, UV_FS_FTRUNCATE);
1703
+ fs__ftruncate(req, file, offset);
1704
+ SET_UV_LAST_ERROR_FROM_REQ(req);
1705
+ return req->result;
1706
+ }
1707
+
1708
+ return 0;
1709
+ }
1710
+
1711
+
1712
+ int uv_fs_sendfile(uv_loop_t* loop, uv_fs_t* req, uv_file out_fd,
1713
+ uv_file in_fd, int64_t in_offset, size_t length, uv_fs_cb cb) {
1714
+ if (cb) {
1715
+ uv_fs_req_init_async(loop, req, UV_FS_SENDFILE, NULL, NULL, cb);
1716
+ req->file_out = out_fd;
1717
+ req->file = in_fd;
1718
+ req->offset = in_offset;
1719
+ req->length = length;
1720
+ QUEUE_FS_TP_JOB(loop, req);
1721
+ } else {
1722
+ uv_fs_req_init_sync(loop, req, UV_FS_SENDFILE);
1723
+ fs__sendfile(req, out_fd, in_fd, in_offset, length);
1724
+ SET_UV_LAST_ERROR_FROM_REQ(req);
1725
+ return req->result;
1726
+ }
1727
+
1728
+ return 0;
1729
+ }
1730
+
1731
+
1732
+ int uv_fs_chmod(uv_loop_t* loop, uv_fs_t* req, const char* path, int mode,
1733
+ uv_fs_cb cb) {
1734
+ wchar_t* pathw;
1735
+ int size;
1736
+
1737
+ /* Convert to UTF16. */
1738
+ UTF8_TO_UTF16(path, pathw);
1739
+
1740
+ if (cb) {
1741
+ uv_fs_req_init_async(loop, req, UV_FS_CHMOD, path, pathw, cb);
1742
+ req->mode = mode;
1743
+ QUEUE_FS_TP_JOB(loop, req);
1744
+ } else {
1745
+ uv_fs_req_init_sync(loop, req, UV_FS_CHMOD);
1746
+ fs__chmod(req, pathw, mode);
1747
+ free(pathw);
1748
+ SET_UV_LAST_ERROR_FROM_REQ(req);
1749
+ return req->result;
1750
+ }
1751
+
1752
+ return 0;
1753
+ }
1754
+
1755
+
1756
+ int uv_fs_fchmod(uv_loop_t* loop, uv_fs_t* req, uv_file file, int mode,
1757
+ uv_fs_cb cb) {
1758
+ if (cb) {
1759
+ uv_fs_req_init_async(loop, req, UV_FS_FCHMOD, NULL, NULL, cb);
1760
+ req->file = file;
1761
+ req->mode = mode;
1762
+ QUEUE_FS_TP_JOB(loop, req);
1763
+ } else {
1764
+ uv_fs_req_init_sync(loop, req, UV_FS_FCHMOD);
1765
+ fs__fchmod(req, file, mode);
1766
+ SET_UV_LAST_ERROR_FROM_REQ(req);
1767
+ return req->result;
1768
+ }
1769
+
1770
+ return 0;
1771
+ }
1772
+
1773
+
1774
+ int uv_fs_utime(uv_loop_t* loop, uv_fs_t* req, const char* path, double atime,
1775
+ double mtime, uv_fs_cb cb) {
1776
+ wchar_t* pathw;
1777
+ int size;
1778
+
1779
+ /* Convert to UTF16. */
1780
+ UTF8_TO_UTF16(path, pathw);
1781
+
1782
+ if (cb) {
1783
+ uv_fs_req_init_async(loop, req, UV_FS_UTIME, path, pathw, cb);
1784
+ req->atime = atime;
1785
+ req->mtime = mtime;
1786
+ QUEUE_FS_TP_JOB(loop, req);
1787
+ } else {
1788
+ uv_fs_req_init_sync(loop, req, UV_FS_UTIME);
1789
+ fs__utime(req, pathw, atime, mtime);
1790
+ free(pathw);
1791
+ SET_UV_LAST_ERROR_FROM_REQ(req);
1792
+ return req->result;
1793
+ }
1794
+
1795
+ return 0;
1796
+ }
1797
+
1798
+
1799
+ int uv_fs_futime(uv_loop_t* loop, uv_fs_t* req, uv_file file, double atime,
1800
+ double mtime, uv_fs_cb cb) {
1801
+ if (cb) {
1802
+ uv_fs_req_init_async(loop, req, UV_FS_FUTIME, NULL, NULL, cb);
1803
+ req->file = file;
1804
+ req->atime = atime;
1805
+ req->mtime = mtime;
1806
+ QUEUE_FS_TP_JOB(loop, req);
1807
+ } else {
1808
+ uv_fs_req_init_sync(loop, req, UV_FS_FUTIME);
1809
+ fs__futime(req, file, atime, mtime);
1810
+ SET_UV_LAST_ERROR_FROM_REQ(req);
1811
+ return req->result;
1812
+ }
1813
+
1814
+ return 0;
1815
+ }
1816
+
1817
+
1818
+ void uv_process_fs_req(uv_loop_t* loop, uv_fs_t* req) {
1819
+ assert(req->cb);
1820
+ uv__req_unregister(loop, req);
1821
+ SET_UV_LAST_ERROR_FROM_REQ(req);
1822
+ req->cb(req);
1823
+ }
1824
+
1825
+
1826
+ void uv_fs_req_cleanup(uv_fs_t* req) {
1827
+ uv_loop_t* loop = req->loop;
1828
+
1829
+ if (req->flags & UV_FS_CLEANEDUP) {
1830
+ return;
1831
+ }
1832
+
1833
+ if (req->flags & UV_FS_FREE_PATH && req->pathw) {
1834
+ free(req->pathw);
1835
+ req->pathw = NULL;
1836
+ }
1837
+
1838
+ if (req->flags & UV_FS_FREE_NEW_PATH && req->new_pathw) {
1839
+ free(req->new_pathw);
1840
+ req->new_pathw = NULL;
1841
+ }
1842
+
1843
+ if (req->flags & UV_FS_FREE_PTR && req->ptr) {
1844
+ free(req->ptr);
1845
+ }
1846
+
1847
+ req->ptr = NULL;
1848
+
1849
+ if (req->path) {
1850
+ free(req->path);
1851
+ req->path = NULL;
1852
+ }
1853
+
1854
+ req->flags |= UV_FS_CLEANEDUP;
1855
+ }
1856
+