rbuv 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (233) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +5 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +5 -0
  5. data/Gemfile +0 -1
  6. data/README.md +6 -1
  7. data/Rakefile +42 -0
  8. data/deps/libuv/.gitignore +34 -0
  9. data/deps/libuv/.mailmap +16 -0
  10. data/deps/libuv/AUTHORS +81 -0
  11. data/deps/libuv/ChangeLog +45 -0
  12. data/deps/libuv/LICENSE +41 -0
  13. data/deps/libuv/Makefile +53 -0
  14. data/deps/libuv/README.md +118 -0
  15. data/deps/libuv/build.mk +164 -0
  16. data/deps/libuv/checksparse.sh +230 -0
  17. data/deps/libuv/common.gypi +197 -0
  18. data/deps/libuv/config-mingw.mk +48 -0
  19. data/deps/libuv/config-unix.mk +167 -0
  20. data/deps/libuv/gyp_uv +98 -0
  21. data/deps/libuv/include/uv-private/ngx-queue.h +129 -0
  22. data/deps/libuv/include/uv-private/stdint-msvc2008.h +247 -0
  23. data/deps/libuv/include/uv-private/tree.h +768 -0
  24. data/deps/libuv/include/uv-private/uv-bsd.h +34 -0
  25. data/deps/libuv/include/uv-private/uv-darwin.h +61 -0
  26. data/deps/libuv/include/uv-private/uv-linux.h +34 -0
  27. data/deps/libuv/include/uv-private/uv-sunos.h +44 -0
  28. data/deps/libuv/include/uv-private/uv-unix.h +332 -0
  29. data/deps/libuv/include/uv-private/uv-win.h +585 -0
  30. data/deps/libuv/include/uv.h +1987 -0
  31. data/deps/libuv/src/fs-poll.c +248 -0
  32. data/deps/libuv/src/inet.c +298 -0
  33. data/deps/libuv/src/unix/aix.c +393 -0
  34. data/deps/libuv/src/unix/async.c +281 -0
  35. data/deps/libuv/src/unix/core.c +714 -0
  36. data/deps/libuv/src/unix/cygwin.c +93 -0
  37. data/deps/libuv/src/unix/darwin-proctitle.m +78 -0
  38. data/deps/libuv/src/unix/darwin.c +431 -0
  39. data/deps/libuv/src/unix/dl.c +83 -0
  40. data/deps/libuv/src/unix/error.c +109 -0
  41. data/deps/libuv/src/unix/freebsd.c +343 -0
  42. data/deps/libuv/src/unix/fs.c +869 -0
  43. data/deps/libuv/src/unix/fsevents.c +299 -0
  44. data/deps/libuv/src/unix/getaddrinfo.c +159 -0
  45. data/deps/libuv/src/unix/internal.h +259 -0
  46. data/deps/libuv/src/unix/kqueue.c +347 -0
  47. data/deps/libuv/src/unix/linux-core.c +724 -0
  48. data/deps/libuv/src/unix/linux-inotify.c +236 -0
  49. data/deps/libuv/src/unix/linux-syscalls.c +388 -0
  50. data/deps/libuv/src/unix/linux-syscalls.h +150 -0
  51. data/deps/libuv/src/unix/loop-watcher.c +64 -0
  52. data/deps/libuv/src/unix/loop.c +114 -0
  53. data/deps/libuv/src/unix/netbsd.c +353 -0
  54. data/deps/libuv/src/unix/openbsd.c +304 -0
  55. data/deps/libuv/src/unix/pipe.c +261 -0
  56. data/deps/libuv/src/unix/poll.c +108 -0
  57. data/deps/libuv/src/unix/process.c +501 -0
  58. data/deps/libuv/src/unix/proctitle.c +103 -0
  59. data/deps/libuv/src/unix/signal.c +455 -0
  60. data/deps/libuv/src/unix/stream.c +1380 -0
  61. data/deps/libuv/src/unix/sunos.c +647 -0
  62. data/deps/libuv/src/unix/tcp.c +357 -0
  63. data/deps/libuv/src/unix/thread.c +431 -0
  64. data/deps/libuv/src/unix/threadpool.c +286 -0
  65. data/deps/libuv/src/unix/timer.c +153 -0
  66. data/deps/libuv/src/unix/tty.c +179 -0
  67. data/deps/libuv/src/unix/udp.c +715 -0
  68. data/deps/libuv/src/uv-common.c +431 -0
  69. data/deps/libuv/src/uv-common.h +204 -0
  70. data/deps/libuv/src/version.c +60 -0
  71. data/deps/libuv/src/win/async.c +99 -0
  72. data/deps/libuv/src/win/atomicops-inl.h +56 -0
  73. data/deps/libuv/src/win/core.c +310 -0
  74. data/deps/libuv/src/win/dl.c +86 -0
  75. data/deps/libuv/src/win/error.c +164 -0
  76. data/deps/libuv/src/win/fs-event.c +506 -0
  77. data/deps/libuv/src/win/fs.c +1951 -0
  78. data/deps/libuv/src/win/getaddrinfo.c +365 -0
  79. data/deps/libuv/src/win/handle-inl.h +164 -0
  80. data/deps/libuv/src/win/handle.c +153 -0
  81. data/deps/libuv/src/win/internal.h +346 -0
  82. data/deps/libuv/src/win/loop-watcher.c +124 -0
  83. data/deps/libuv/src/win/pipe.c +1656 -0
  84. data/deps/libuv/src/win/poll.c +615 -0
  85. data/deps/libuv/src/win/process-stdio.c +503 -0
  86. data/deps/libuv/src/win/process.c +1048 -0
  87. data/deps/libuv/src/win/req-inl.h +224 -0
  88. data/deps/libuv/src/win/req.c +25 -0
  89. data/deps/libuv/src/win/signal.c +354 -0
  90. data/deps/libuv/src/win/stream-inl.h +67 -0
  91. data/deps/libuv/src/win/stream.c +198 -0
  92. data/deps/libuv/src/win/tcp.c +1422 -0
  93. data/deps/libuv/src/win/thread.c +666 -0
  94. data/deps/libuv/src/win/threadpool.c +82 -0
  95. data/deps/libuv/src/win/timer.c +230 -0
  96. data/deps/libuv/src/win/tty.c +1857 -0
  97. data/deps/libuv/src/win/udp.c +744 -0
  98. data/deps/libuv/src/win/util.c +946 -0
  99. data/deps/libuv/src/win/winapi.c +152 -0
  100. data/deps/libuv/src/win/winapi.h +4476 -0
  101. data/deps/libuv/src/win/winsock.c +560 -0
  102. data/deps/libuv/src/win/winsock.h +171 -0
  103. data/deps/libuv/test/benchmark-async-pummel.c +119 -0
  104. data/deps/libuv/test/benchmark-async.c +139 -0
  105. data/deps/libuv/test/benchmark-fs-stat.c +136 -0
  106. data/deps/libuv/test/benchmark-getaddrinfo.c +91 -0
  107. data/deps/libuv/test/benchmark-list.h +163 -0
  108. data/deps/libuv/test/benchmark-loop-count.c +90 -0
  109. data/deps/libuv/test/benchmark-million-async.c +112 -0
  110. data/deps/libuv/test/benchmark-million-timers.c +77 -0
  111. data/deps/libuv/test/benchmark-multi-accept.c +432 -0
  112. data/deps/libuv/test/benchmark-ping-pongs.c +212 -0
  113. data/deps/libuv/test/benchmark-pound.c +325 -0
  114. data/deps/libuv/test/benchmark-pump.c +459 -0
  115. data/deps/libuv/test/benchmark-sizes.c +45 -0
  116. data/deps/libuv/test/benchmark-spawn.c +163 -0
  117. data/deps/libuv/test/benchmark-tcp-write-batch.c +141 -0
  118. data/deps/libuv/test/benchmark-thread.c +64 -0
  119. data/deps/libuv/test/benchmark-udp-pummel.c +238 -0
  120. data/deps/libuv/test/blackhole-server.c +118 -0
  121. data/deps/libuv/test/dns-server.c +329 -0
  122. data/deps/libuv/test/echo-server.c +384 -0
  123. data/deps/libuv/test/fixtures/empty_file +0 -0
  124. data/deps/libuv/test/fixtures/load_error.node +1 -0
  125. data/deps/libuv/test/run-benchmarks.c +64 -0
  126. data/deps/libuv/test/run-tests.c +159 -0
  127. data/deps/libuv/test/runner-unix.c +328 -0
  128. data/deps/libuv/test/runner-unix.h +36 -0
  129. data/deps/libuv/test/runner-win.c +318 -0
  130. data/deps/libuv/test/runner-win.h +43 -0
  131. data/deps/libuv/test/runner.c +394 -0
  132. data/deps/libuv/test/runner.h +165 -0
  133. data/deps/libuv/test/task.h +122 -0
  134. data/deps/libuv/test/test-active.c +83 -0
  135. data/deps/libuv/test/test-async.c +136 -0
  136. data/deps/libuv/test/test-barrier.c +98 -0
  137. data/deps/libuv/test/test-callback-order.c +77 -0
  138. data/deps/libuv/test/test-callback-stack.c +204 -0
  139. data/deps/libuv/test/test-condvar.c +173 -0
  140. data/deps/libuv/test/test-connection-fail.c +150 -0
  141. data/deps/libuv/test/test-cwd-and-chdir.c +64 -0
  142. data/deps/libuv/test/test-delayed-accept.c +189 -0
  143. data/deps/libuv/test/test-dlerror.c +58 -0
  144. data/deps/libuv/test/test-embed.c +136 -0
  145. data/deps/libuv/test/test-error.c +59 -0
  146. data/deps/libuv/test/test-fail-always.c +29 -0
  147. data/deps/libuv/test/test-fs-event.c +504 -0
  148. data/deps/libuv/test/test-fs-poll.c +148 -0
  149. data/deps/libuv/test/test-fs.c +1899 -0
  150. data/deps/libuv/test/test-get-currentexe.c +63 -0
  151. data/deps/libuv/test/test-get-loadavg.c +36 -0
  152. data/deps/libuv/test/test-get-memory.c +38 -0
  153. data/deps/libuv/test/test-getaddrinfo.c +120 -0
  154. data/deps/libuv/test/test-getsockname.c +344 -0
  155. data/deps/libuv/test/test-hrtime.c +54 -0
  156. data/deps/libuv/test/test-idle.c +82 -0
  157. data/deps/libuv/test/test-ipc-send-recv.c +218 -0
  158. data/deps/libuv/test/test-ipc.c +625 -0
  159. data/deps/libuv/test/test-list.h +492 -0
  160. data/deps/libuv/test/test-loop-handles.c +337 -0
  161. data/deps/libuv/test/test-loop-stop.c +73 -0
  162. data/deps/libuv/test/test-multiple-listen.c +103 -0
  163. data/deps/libuv/test/test-mutexes.c +63 -0
  164. data/deps/libuv/test/test-pass-always.c +28 -0
  165. data/deps/libuv/test/test-ping-pong.c +250 -0
  166. data/deps/libuv/test/test-pipe-bind-error.c +144 -0
  167. data/deps/libuv/test/test-pipe-connect-error.c +98 -0
  168. data/deps/libuv/test/test-platform-output.c +87 -0
  169. data/deps/libuv/test/test-poll-close.c +73 -0
  170. data/deps/libuv/test/test-poll.c +575 -0
  171. data/deps/libuv/test/test-process-title.c +49 -0
  172. data/deps/libuv/test/test-ref.c +415 -0
  173. data/deps/libuv/test/test-run-nowait.c +46 -0
  174. data/deps/libuv/test/test-run-once.c +49 -0
  175. data/deps/libuv/test/test-semaphore.c +111 -0
  176. data/deps/libuv/test/test-shutdown-close.c +105 -0
  177. data/deps/libuv/test/test-shutdown-eof.c +184 -0
  178. data/deps/libuv/test/test-signal-multiple-loops.c +270 -0
  179. data/deps/libuv/test/test-signal.c +152 -0
  180. data/deps/libuv/test/test-spawn.c +938 -0
  181. data/deps/libuv/test/test-stdio-over-pipes.c +250 -0
  182. data/deps/libuv/test/test-tcp-bind-error.c +198 -0
  183. data/deps/libuv/test/test-tcp-bind6-error.c +159 -0
  184. data/deps/libuv/test/test-tcp-close-while-connecting.c +81 -0
  185. data/deps/libuv/test/test-tcp-close.c +130 -0
  186. data/deps/libuv/test/test-tcp-connect-error-after-write.c +96 -0
  187. data/deps/libuv/test/test-tcp-connect-error.c +71 -0
  188. data/deps/libuv/test/test-tcp-connect-timeout.c +86 -0
  189. data/deps/libuv/test/test-tcp-connect6-error.c +69 -0
  190. data/deps/libuv/test/test-tcp-flags.c +52 -0
  191. data/deps/libuv/test/test-tcp-open.c +175 -0
  192. data/deps/libuv/test/test-tcp-read-stop.c +73 -0
  193. data/deps/libuv/test/test-tcp-shutdown-after-write.c +132 -0
  194. data/deps/libuv/test/test-tcp-unexpected-read.c +114 -0
  195. data/deps/libuv/test/test-tcp-write-to-half-open-connection.c +136 -0
  196. data/deps/libuv/test/test-tcp-writealot.c +171 -0
  197. data/deps/libuv/test/test-thread.c +183 -0
  198. data/deps/libuv/test/test-threadpool-cancel.c +311 -0
  199. data/deps/libuv/test/test-threadpool.c +77 -0
  200. data/deps/libuv/test/test-timer-again.c +142 -0
  201. data/deps/libuv/test/test-timer.c +266 -0
  202. data/deps/libuv/test/test-tty.c +111 -0
  203. data/deps/libuv/test/test-udp-dgram-too-big.c +87 -0
  204. data/deps/libuv/test/test-udp-ipv6.c +158 -0
  205. data/deps/libuv/test/test-udp-multicast-join.c +140 -0
  206. data/deps/libuv/test/test-udp-multicast-ttl.c +87 -0
  207. data/deps/libuv/test/test-udp-open.c +154 -0
  208. data/deps/libuv/test/test-udp-options.c +87 -0
  209. data/deps/libuv/test/test-udp-send-and-recv.c +210 -0
  210. data/deps/libuv/test/test-util.c +97 -0
  211. data/deps/libuv/test/test-walk-handles.c +78 -0
  212. data/deps/libuv/uv.gyp +431 -0
  213. data/deps/libuv/vcbuild.bat +128 -0
  214. data/ext/rbuv/debug.h +27 -0
  215. data/ext/rbuv/error.c +7 -0
  216. data/ext/rbuv/error.h +10 -0
  217. data/ext/rbuv/extconf.rb +35 -0
  218. data/ext/rbuv/handle.c +40 -0
  219. data/ext/rbuv/handle.h +14 -0
  220. data/ext/rbuv/libuv.mk +12 -0
  221. data/ext/rbuv/loop.c +50 -0
  222. data/ext/rbuv/loop.h +13 -0
  223. data/ext/rbuv/rbuv.c +15 -0
  224. data/ext/rbuv/rbuv.h +27 -0
  225. data/ext/rbuv/timer.c +133 -0
  226. data/ext/rbuv/timer.h +13 -0
  227. data/lib/rbuv/timer.rb +7 -0
  228. data/lib/rbuv/version.rb +1 -1
  229. data/lib/rbuv.rb +24 -2
  230. data/rbuv.gemspec +5 -1
  231. data/spec/spec_helper.rb +22 -0
  232. data/spec/timer_spec.rb +144 -0
  233. metadata +278 -9
@@ -0,0 +1,1899 @@
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 "task.h"
24
+
25
+ #include <errno.h>
26
+ #include <string.h> /* memset */
27
+ #include <fcntl.h>
28
+ #include <sys/stat.h>
29
+
30
+ /* FIXME we shouldn't need to branch in this file */
31
+ #if defined(__unix__) || defined(__POSIX__) || \
32
+ defined(__APPLE__) || defined(_AIX)
33
+ #include <unistd.h> /* unlink, rmdir, etc. */
34
+ #else
35
+ # include <direct.h>
36
+ # include <io.h>
37
+ # define unlink _unlink
38
+ # define rmdir _rmdir
39
+ # define stat _stati64
40
+ # define open _open
41
+ # define write _write
42
+ # define lseek _lseek
43
+ # define close _close
44
+ #endif
45
+
46
+ #define TOO_LONG_NAME_LENGTH 65536
47
+ #define PATHMAX 1024
48
+
49
+ typedef struct {
50
+ const char* path;
51
+ double atime;
52
+ double mtime;
53
+ } utime_check_t;
54
+
55
+
56
+ static int dummy_cb_count;
57
+ static int close_cb_count;
58
+ static int create_cb_count;
59
+ static int open_cb_count;
60
+ static int read_cb_count;
61
+ static int write_cb_count;
62
+ static int unlink_cb_count;
63
+ static int mkdir_cb_count;
64
+ static int rmdir_cb_count;
65
+ static int readdir_cb_count;
66
+ static int stat_cb_count;
67
+ static int rename_cb_count;
68
+ static int fsync_cb_count;
69
+ static int fdatasync_cb_count;
70
+ static int ftruncate_cb_count;
71
+ static int sendfile_cb_count;
72
+ static int fstat_cb_count;
73
+ static int chmod_cb_count;
74
+ static int fchmod_cb_count;
75
+ static int chown_cb_count;
76
+ static int fchown_cb_count;
77
+ static int link_cb_count;
78
+ static int symlink_cb_count;
79
+ static int readlink_cb_count;
80
+ static int utime_cb_count;
81
+ static int futime_cb_count;
82
+
83
+ static uv_loop_t* loop;
84
+
85
+ static uv_fs_t open_req1;
86
+ static uv_fs_t open_req2;
87
+ static uv_fs_t read_req;
88
+ static uv_fs_t write_req;
89
+ static uv_fs_t unlink_req;
90
+ static uv_fs_t close_req;
91
+ static uv_fs_t mkdir_req;
92
+ static uv_fs_t rmdir_req;
93
+ static uv_fs_t readdir_req;
94
+ static uv_fs_t stat_req;
95
+ static uv_fs_t rename_req;
96
+ static uv_fs_t fsync_req;
97
+ static uv_fs_t fdatasync_req;
98
+ static uv_fs_t ftruncate_req;
99
+ static uv_fs_t sendfile_req;
100
+ static uv_fs_t utime_req;
101
+ static uv_fs_t futime_req;
102
+
103
+ static char buf[32];
104
+ static char test_buf[] = "test-buffer\n";
105
+
106
+
107
+ static void check_permission(const char* filename, int mode) {
108
+ int r;
109
+ uv_fs_t req;
110
+ uv_statbuf_t* s;
111
+
112
+ r = uv_fs_stat(uv_default_loop(), &req, filename, NULL);
113
+ ASSERT(r == 0);
114
+ ASSERT(req.result == 0);
115
+
116
+ s = &req.statbuf;
117
+ #ifdef _WIN32
118
+ /*
119
+ * On Windows, chmod can only modify S_IWUSR (_S_IWRITE) bit,
120
+ * so only testing for the specified flags.
121
+ */
122
+ ASSERT((s->st_mode & 0777) & mode);
123
+ #else
124
+ ASSERT((s->st_mode & 0777) == mode);
125
+ #endif
126
+
127
+ uv_fs_req_cleanup(&req);
128
+ }
129
+
130
+
131
+ static void dummy_cb(uv_fs_t* req) {
132
+ (void) req;
133
+ dummy_cb_count++;
134
+ }
135
+
136
+
137
+ static void link_cb(uv_fs_t* req) {
138
+ ASSERT(req->fs_type == UV_FS_LINK);
139
+ ASSERT(req->result == 0);
140
+ link_cb_count++;
141
+ uv_fs_req_cleanup(req);
142
+ }
143
+
144
+
145
+ static void symlink_cb(uv_fs_t* req) {
146
+ ASSERT(req->fs_type == UV_FS_SYMLINK);
147
+ ASSERT(req->result == 0);
148
+ symlink_cb_count++;
149
+ uv_fs_req_cleanup(req);
150
+ }
151
+
152
+ static void readlink_cb(uv_fs_t* req) {
153
+ ASSERT(req->fs_type == UV_FS_READLINK);
154
+ ASSERT(req->result == 0);
155
+ ASSERT(strcmp(req->ptr, "test_file_symlink2") == 0);
156
+ readlink_cb_count++;
157
+ uv_fs_req_cleanup(req);
158
+ }
159
+
160
+ static void fchmod_cb(uv_fs_t* req) {
161
+ ASSERT(req->fs_type == UV_FS_FCHMOD);
162
+ ASSERT(req->result == 0);
163
+ fchmod_cb_count++;
164
+ uv_fs_req_cleanup(req);
165
+ check_permission("test_file", *(int*)req->data);
166
+ }
167
+
168
+
169
+ static void chmod_cb(uv_fs_t* req) {
170
+ ASSERT(req->fs_type == UV_FS_CHMOD);
171
+ ASSERT(req->result == 0);
172
+ chmod_cb_count++;
173
+ uv_fs_req_cleanup(req);
174
+ check_permission("test_file", *(int*)req->data);
175
+ }
176
+
177
+
178
+ static void fchown_cb(uv_fs_t* req) {
179
+ ASSERT(req->fs_type == UV_FS_FCHOWN);
180
+ ASSERT(req->result == 0);
181
+ fchown_cb_count++;
182
+ uv_fs_req_cleanup(req);
183
+ }
184
+
185
+
186
+ static void chown_cb(uv_fs_t* req) {
187
+ ASSERT(req->fs_type == UV_FS_CHOWN);
188
+ ASSERT(req->result == 0);
189
+ chown_cb_count++;
190
+ uv_fs_req_cleanup(req);
191
+ }
192
+
193
+ static void chown_root_cb(uv_fs_t* req) {
194
+ ASSERT(req->fs_type == UV_FS_CHOWN);
195
+ #ifdef _WIN32
196
+ /* On windows, chown is a no-op and always succeeds. */
197
+ ASSERT(req->result == 0);
198
+ #else
199
+ /* On unix, chown'ing the root directory is not allowed. */
200
+ ASSERT(req->result == -1);
201
+ ASSERT(req->errorno == UV_EPERM);
202
+ #endif
203
+ chown_cb_count++;
204
+ uv_fs_req_cleanup(req);
205
+ }
206
+
207
+ static void unlink_cb(uv_fs_t* req) {
208
+ ASSERT(req == &unlink_req);
209
+ ASSERT(req->fs_type == UV_FS_UNLINK);
210
+ ASSERT(req->result != -1);
211
+ unlink_cb_count++;
212
+ uv_fs_req_cleanup(req);
213
+ }
214
+
215
+ static void fstat_cb(uv_fs_t* req) {
216
+ struct stat* s = req->ptr;
217
+ ASSERT(req->fs_type == UV_FS_FSTAT);
218
+ ASSERT(req->result == 0);
219
+ ASSERT(s->st_size == sizeof(test_buf));
220
+ uv_fs_req_cleanup(req);
221
+ fstat_cb_count++;
222
+ }
223
+
224
+
225
+ static void close_cb(uv_fs_t* req) {
226
+ int r;
227
+ ASSERT(req == &close_req);
228
+ ASSERT(req->fs_type == UV_FS_CLOSE);
229
+ ASSERT(req->result != -1);
230
+ close_cb_count++;
231
+ uv_fs_req_cleanup(req);
232
+ if (close_cb_count == 3) {
233
+ r = uv_fs_unlink(loop, &unlink_req, "test_file2", unlink_cb);
234
+ ASSERT(r == 0);
235
+ }
236
+ }
237
+
238
+
239
+ static void ftruncate_cb(uv_fs_t* req) {
240
+ int r;
241
+ ASSERT(req == &ftruncate_req);
242
+ ASSERT(req->fs_type == UV_FS_FTRUNCATE);
243
+ ASSERT(req->result != -1);
244
+ ftruncate_cb_count++;
245
+ uv_fs_req_cleanup(req);
246
+ r = uv_fs_close(loop, &close_req, open_req1.result, close_cb);
247
+ ASSERT(r == 0);
248
+ }
249
+
250
+
251
+ static void read_cb(uv_fs_t* req) {
252
+ int r;
253
+ ASSERT(req == &read_req);
254
+ ASSERT(req->fs_type == UV_FS_READ);
255
+ ASSERT(req->result != -1);
256
+ read_cb_count++;
257
+ uv_fs_req_cleanup(req);
258
+ if (read_cb_count == 1) {
259
+ ASSERT(strcmp(buf, test_buf) == 0);
260
+ r = uv_fs_ftruncate(loop, &ftruncate_req, open_req1.result, 7,
261
+ ftruncate_cb);
262
+ } else {
263
+ ASSERT(strcmp(buf, "test-bu") == 0);
264
+ r = uv_fs_close(loop, &close_req, open_req1.result, close_cb);
265
+ }
266
+ ASSERT(r == 0);
267
+ }
268
+
269
+
270
+ static void open_cb(uv_fs_t* req) {
271
+ int r;
272
+ ASSERT(req == &open_req1);
273
+ ASSERT(req->fs_type == UV_FS_OPEN);
274
+ if (req->result < 0) {
275
+ /* TODO get error with uv_last_error() */
276
+ fprintf(stderr, "async open error: %d\n", req->errorno);
277
+ ASSERT(0);
278
+ }
279
+ open_cb_count++;
280
+ ASSERT(req->path);
281
+ ASSERT(memcmp(req->path, "test_file2\0", 11) == 0);
282
+ uv_fs_req_cleanup(req);
283
+ memset(buf, 0, sizeof(buf));
284
+ r = uv_fs_read(loop, &read_req, open_req1.result, buf, sizeof(buf), -1,
285
+ read_cb);
286
+ ASSERT(r == 0);
287
+ }
288
+
289
+
290
+ static void open_cb_simple(uv_fs_t* req) {
291
+ ASSERT(req->fs_type == UV_FS_OPEN);
292
+ if (req->result < 0) {
293
+ /* TODO get error with uv_last_error() */
294
+ fprintf(stderr, "async open error: %d\n", req->errorno);
295
+ ASSERT(0);
296
+ }
297
+ open_cb_count++;
298
+ ASSERT(req->path);
299
+ uv_fs_req_cleanup(req);
300
+ }
301
+
302
+
303
+ static void fsync_cb(uv_fs_t* req) {
304
+ int r;
305
+ ASSERT(req == &fsync_req);
306
+ ASSERT(req->fs_type == UV_FS_FSYNC);
307
+ ASSERT(req->result != -1);
308
+ fsync_cb_count++;
309
+ uv_fs_req_cleanup(req);
310
+ r = uv_fs_close(loop, &close_req, open_req1.result, close_cb);
311
+ ASSERT(r == 0);
312
+ }
313
+
314
+
315
+ static void fdatasync_cb(uv_fs_t* req) {
316
+ int r;
317
+ ASSERT(req == &fdatasync_req);
318
+ ASSERT(req->fs_type == UV_FS_FDATASYNC);
319
+ ASSERT(req->result != -1);
320
+ fdatasync_cb_count++;
321
+ uv_fs_req_cleanup(req);
322
+ r = uv_fs_fsync(loop, &fsync_req, open_req1.result, fsync_cb);
323
+ ASSERT(r == 0);
324
+ }
325
+
326
+
327
+ static void write_cb(uv_fs_t* req) {
328
+ int r;
329
+ ASSERT(req == &write_req);
330
+ ASSERT(req->fs_type == UV_FS_WRITE);
331
+ ASSERT(req->result != -1);
332
+ write_cb_count++;
333
+ uv_fs_req_cleanup(req);
334
+ r = uv_fs_fdatasync(loop, &fdatasync_req, open_req1.result, fdatasync_cb);
335
+ ASSERT(r == 0);
336
+ }
337
+
338
+
339
+ static void create_cb(uv_fs_t* req) {
340
+ int r;
341
+ ASSERT(req == &open_req1);
342
+ ASSERT(req->fs_type == UV_FS_OPEN);
343
+ ASSERT(req->result != -1);
344
+ create_cb_count++;
345
+ uv_fs_req_cleanup(req);
346
+ r = uv_fs_write(loop, &write_req, req->result, test_buf, sizeof(test_buf),
347
+ -1, write_cb);
348
+ ASSERT(r == 0);
349
+ }
350
+
351
+
352
+ static void rename_cb(uv_fs_t* req) {
353
+ ASSERT(req == &rename_req);
354
+ ASSERT(req->fs_type == UV_FS_RENAME);
355
+ ASSERT(req->result != -1);
356
+ rename_cb_count++;
357
+ uv_fs_req_cleanup(req);
358
+ }
359
+
360
+
361
+ static void mkdir_cb(uv_fs_t* req) {
362
+ ASSERT(req == &mkdir_req);
363
+ ASSERT(req->fs_type == UV_FS_MKDIR);
364
+ ASSERT(req->result != -1);
365
+ mkdir_cb_count++;
366
+ ASSERT(req->path);
367
+ ASSERT(memcmp(req->path, "test_dir\0", 9) == 0);
368
+ uv_fs_req_cleanup(req);
369
+ }
370
+
371
+
372
+ static void rmdir_cb(uv_fs_t* req) {
373
+ ASSERT(req == &rmdir_req);
374
+ ASSERT(req->fs_type == UV_FS_RMDIR);
375
+ ASSERT(req->result != -1);
376
+ rmdir_cb_count++;
377
+ ASSERT(req->path);
378
+ ASSERT(memcmp(req->path, "test_dir\0", 9) == 0);
379
+ uv_fs_req_cleanup(req);
380
+ }
381
+
382
+
383
+ static void readdir_cb(uv_fs_t* req) {
384
+ ASSERT(req == &readdir_req);
385
+ ASSERT(req->fs_type == UV_FS_READDIR);
386
+ ASSERT(req->result == 2);
387
+ ASSERT(req->ptr);
388
+ ASSERT(memcmp(req->ptr, "file1\0file2\0", 12) == 0
389
+ || memcmp(req->ptr, "file2\0file1\0", 12) == 0);
390
+ readdir_cb_count++;
391
+ ASSERT(req->path);
392
+ ASSERT(memcmp(req->path, "test_dir\0", 9) == 0);
393
+ uv_fs_req_cleanup(req);
394
+ ASSERT(!req->ptr);
395
+ }
396
+
397
+
398
+ static void empty_readdir_cb(uv_fs_t* req) {
399
+ ASSERT(req == &readdir_req);
400
+ ASSERT(req->fs_type == UV_FS_READDIR);
401
+ ASSERT(req->result == 0);
402
+ ASSERT(req->ptr == NULL);
403
+ uv_fs_req_cleanup(req);
404
+ readdir_cb_count++;
405
+ }
406
+
407
+
408
+ static void file_readdir_cb(uv_fs_t* req) {
409
+ ASSERT(req == &readdir_req);
410
+ ASSERT(req->fs_type == UV_FS_READDIR);
411
+ ASSERT(req->result == -1);
412
+ ASSERT(req->ptr == NULL);
413
+ ASSERT(uv_last_error(req->loop).code == UV_ENOTDIR);
414
+ uv_fs_req_cleanup(req);
415
+ readdir_cb_count++;
416
+ }
417
+
418
+
419
+ static void stat_cb(uv_fs_t* req) {
420
+ ASSERT(req == &stat_req);
421
+ ASSERT(req->fs_type == UV_FS_STAT || req->fs_type == UV_FS_LSTAT);
422
+ ASSERT(req->result != -1);
423
+ ASSERT(req->ptr);
424
+ stat_cb_count++;
425
+ uv_fs_req_cleanup(req);
426
+ ASSERT(!req->ptr);
427
+ }
428
+
429
+
430
+ static void sendfile_cb(uv_fs_t* req) {
431
+ ASSERT(req == &sendfile_req);
432
+ ASSERT(req->fs_type == UV_FS_SENDFILE);
433
+ ASSERT(req->result == 65546);
434
+ sendfile_cb_count++;
435
+ uv_fs_req_cleanup(req);
436
+ }
437
+
438
+
439
+ static void open_noent_cb(uv_fs_t* req) {
440
+ ASSERT(req->fs_type == UV_FS_OPEN);
441
+ ASSERT(req->errorno == UV_ENOENT);
442
+ ASSERT(req->result == -1);
443
+ open_cb_count++;
444
+ uv_fs_req_cleanup(req);
445
+ }
446
+
447
+ static void open_nametoolong_cb(uv_fs_t* req) {
448
+ ASSERT(req->fs_type == UV_FS_OPEN);
449
+ ASSERT(req->errorno == UV_ENAMETOOLONG);
450
+ ASSERT(req->result == -1);
451
+ open_cb_count++;
452
+ uv_fs_req_cleanup(req);
453
+ }
454
+
455
+ static void open_loop_cb(uv_fs_t* req) {
456
+ ASSERT(req->fs_type == UV_FS_OPEN);
457
+ ASSERT(req->errorno == UV_ELOOP);
458
+ ASSERT(req->result == -1);
459
+ open_cb_count++;
460
+ uv_fs_req_cleanup(req);
461
+ }
462
+
463
+
464
+ TEST_IMPL(fs_file_noent) {
465
+ uv_fs_t req;
466
+ int r;
467
+
468
+ loop = uv_default_loop();
469
+
470
+ r = uv_fs_open(loop, &req, "does_not_exist", O_RDONLY, 0, NULL);
471
+ ASSERT(r == -1);
472
+ ASSERT(req.result == -1);
473
+ ASSERT(uv_last_error(loop).code == UV_ENOENT);
474
+ uv_fs_req_cleanup(&req);
475
+
476
+ r = uv_fs_open(loop, &req, "does_not_exist", O_RDONLY, 0, open_noent_cb);
477
+ ASSERT(r == 0);
478
+
479
+ ASSERT(open_cb_count == 0);
480
+ uv_run(loop, UV_RUN_DEFAULT);
481
+ ASSERT(open_cb_count == 1);
482
+
483
+ /* TODO add EACCES test */
484
+
485
+ MAKE_VALGRIND_HAPPY();
486
+ return 0;
487
+ }
488
+
489
+ TEST_IMPL(fs_file_nametoolong) {
490
+ uv_fs_t req;
491
+ int r;
492
+ char name[TOO_LONG_NAME_LENGTH + 1];
493
+
494
+ loop = uv_default_loop();
495
+
496
+ memset(name, 'a', TOO_LONG_NAME_LENGTH);
497
+ name[TOO_LONG_NAME_LENGTH] = 0;
498
+
499
+ r = uv_fs_open(loop, &req, name, O_RDONLY, 0, NULL);
500
+ ASSERT(r == -1);
501
+ ASSERT(req.result == -1);
502
+ ASSERT(uv_last_error(loop).code == UV_ENAMETOOLONG);
503
+ uv_fs_req_cleanup(&req);
504
+
505
+ r = uv_fs_open(loop, &req, name, O_RDONLY, 0, open_nametoolong_cb);
506
+ ASSERT(r == 0);
507
+
508
+ ASSERT(open_cb_count == 0);
509
+ uv_run(loop, UV_RUN_DEFAULT);
510
+ ASSERT(open_cb_count == 1);
511
+
512
+ MAKE_VALGRIND_HAPPY();
513
+ return 0;
514
+ }
515
+
516
+ TEST_IMPL(fs_file_loop) {
517
+ uv_fs_t req;
518
+ int r;
519
+
520
+ loop = uv_default_loop();
521
+
522
+ unlink("test_symlink");
523
+ uv_fs_symlink(loop, &req, "test_symlink", "test_symlink", 0, NULL);
524
+ uv_fs_req_cleanup(&req);
525
+
526
+ r = uv_fs_open(loop, &req, "test_symlink", O_RDONLY, 0, NULL);
527
+ ASSERT(r == -1);
528
+ ASSERT(req.result == -1);
529
+ ASSERT(uv_last_error(loop).code == UV_ELOOP);
530
+ uv_fs_req_cleanup(&req);
531
+
532
+ r = uv_fs_open(loop, &req, "test_symlink", O_RDONLY, 0, open_loop_cb);
533
+ ASSERT(r == 0);
534
+
535
+ ASSERT(open_cb_count == 0);
536
+ uv_run(loop, UV_RUN_DEFAULT);
537
+ ASSERT(open_cb_count == 1);
538
+
539
+ unlink("test_symlink");
540
+
541
+ MAKE_VALGRIND_HAPPY();
542
+ return 0;
543
+ }
544
+
545
+ static void check_utime(const char* path, double atime, double mtime) {
546
+ uv_statbuf_t* s;
547
+ uv_fs_t req;
548
+ int r;
549
+
550
+ r = uv_fs_stat(loop, &req, path, NULL);
551
+ ASSERT(r == 0);
552
+
553
+ ASSERT(req.result == 0);
554
+ s = &req.statbuf;
555
+
556
+ #if defined(_WIN32) || defined(_AIX)
557
+ ASSERT(s->st_atime == atime);
558
+ ASSERT(s->st_mtime == mtime);
559
+ #elif !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
560
+ ASSERT(s->st_atimespec.tv_sec == atime);
561
+ ASSERT(s->st_mtimespec.tv_sec == mtime);
562
+ #else
563
+ ASSERT(s->st_atim.tv_sec == atime);
564
+ ASSERT(s->st_mtim.tv_sec == mtime);
565
+ #endif
566
+
567
+ uv_fs_req_cleanup(&req);
568
+ }
569
+
570
+
571
+ static void utime_cb(uv_fs_t* req) {
572
+ utime_check_t* c;
573
+
574
+ ASSERT(req == &utime_req);
575
+ ASSERT(req->result == 0);
576
+ ASSERT(req->fs_type == UV_FS_UTIME);
577
+
578
+ c = req->data;
579
+ check_utime(c->path, c->atime, c->mtime);
580
+
581
+ uv_fs_req_cleanup(req);
582
+ utime_cb_count++;
583
+ }
584
+
585
+
586
+ static void futime_cb(uv_fs_t* req) {
587
+ utime_check_t* c;
588
+
589
+ ASSERT(req == &futime_req);
590
+ ASSERT(req->result == 0);
591
+ ASSERT(req->fs_type == UV_FS_FUTIME);
592
+
593
+ c = req->data;
594
+ check_utime(c->path, c->atime, c->mtime);
595
+
596
+ uv_fs_req_cleanup(req);
597
+ futime_cb_count++;
598
+ }
599
+
600
+
601
+ TEST_IMPL(fs_file_async) {
602
+ int r;
603
+
604
+ /* Setup. */
605
+ unlink("test_file");
606
+ unlink("test_file2");
607
+
608
+ loop = uv_default_loop();
609
+
610
+ r = uv_fs_open(loop, &open_req1, "test_file", O_WRONLY | O_CREAT,
611
+ S_IREAD | S_IWRITE, create_cb);
612
+ ASSERT(r == 0);
613
+ uv_run(loop, UV_RUN_DEFAULT);
614
+
615
+ ASSERT(create_cb_count == 1);
616
+ ASSERT(write_cb_count == 1);
617
+ ASSERT(fsync_cb_count == 1);
618
+ ASSERT(fdatasync_cb_count == 1);
619
+ ASSERT(close_cb_count == 1);
620
+
621
+ r = uv_fs_rename(loop, &rename_req, "test_file", "test_file2", rename_cb);
622
+ ASSERT(r == 0);
623
+
624
+ uv_run(loop, UV_RUN_DEFAULT);
625
+ ASSERT(create_cb_count == 1);
626
+ ASSERT(write_cb_count == 1);
627
+ ASSERT(close_cb_count == 1);
628
+ ASSERT(rename_cb_count == 1);
629
+
630
+ r = uv_fs_open(loop, &open_req1, "test_file2", O_RDWR, 0, open_cb);
631
+ ASSERT(r == 0);
632
+
633
+ uv_run(loop, UV_RUN_DEFAULT);
634
+ ASSERT(open_cb_count == 1);
635
+ ASSERT(read_cb_count == 1);
636
+ ASSERT(close_cb_count == 2);
637
+ ASSERT(rename_cb_count == 1);
638
+ ASSERT(create_cb_count == 1);
639
+ ASSERT(write_cb_count == 1);
640
+ ASSERT(ftruncate_cb_count == 1);
641
+
642
+ r = uv_fs_open(loop, &open_req1, "test_file2", O_RDONLY, 0, open_cb);
643
+ ASSERT(r == 0);
644
+
645
+ uv_run(loop, UV_RUN_DEFAULT);
646
+ ASSERT(open_cb_count == 2);
647
+ ASSERT(read_cb_count == 2);
648
+ ASSERT(close_cb_count == 3);
649
+ ASSERT(rename_cb_count == 1);
650
+ ASSERT(unlink_cb_count == 1);
651
+ ASSERT(create_cb_count == 1);
652
+ ASSERT(write_cb_count == 1);
653
+ ASSERT(ftruncate_cb_count == 1);
654
+
655
+ /* Cleanup. */
656
+ unlink("test_file");
657
+ unlink("test_file2");
658
+
659
+ MAKE_VALGRIND_HAPPY();
660
+ return 0;
661
+ }
662
+
663
+
664
+ TEST_IMPL(fs_file_sync) {
665
+ int r;
666
+
667
+ /* Setup. */
668
+ unlink("test_file");
669
+ unlink("test_file2");
670
+
671
+ loop = uv_default_loop();
672
+
673
+ r = uv_fs_open(loop, &open_req1, "test_file", O_WRONLY | O_CREAT,
674
+ S_IWRITE | S_IREAD, NULL);
675
+ ASSERT(r != -1);
676
+ ASSERT(open_req1.result != -1);
677
+ uv_fs_req_cleanup(&open_req1);
678
+
679
+ r = uv_fs_write(loop, &write_req, open_req1.result, test_buf,
680
+ sizeof(test_buf), -1, NULL);
681
+ ASSERT(r != -1);
682
+ ASSERT(write_req.result != -1);
683
+ uv_fs_req_cleanup(&write_req);
684
+
685
+ r = uv_fs_close(loop, &close_req, open_req1.result, NULL);
686
+ ASSERT(r != -1);
687
+ ASSERT(close_req.result != -1);
688
+ uv_fs_req_cleanup(&close_req);
689
+
690
+ r = uv_fs_open(loop, &open_req1, "test_file", O_RDWR, 0, NULL);
691
+ ASSERT(r != -1);
692
+ ASSERT(open_req1.result != -1);
693
+ uv_fs_req_cleanup(&open_req1);
694
+
695
+ r = uv_fs_read(loop, &read_req, open_req1.result, buf, sizeof(buf), -1,
696
+ NULL);
697
+ ASSERT(r != -1);
698
+ ASSERT(read_req.result != -1);
699
+ ASSERT(strcmp(buf, test_buf) == 0);
700
+ uv_fs_req_cleanup(&read_req);
701
+
702
+ r = uv_fs_ftruncate(loop, &ftruncate_req, open_req1.result, 7, NULL);
703
+ ASSERT(r != -1);
704
+ ASSERT(ftruncate_req.result != -1);
705
+ uv_fs_req_cleanup(&ftruncate_req);
706
+
707
+ r = uv_fs_close(loop, &close_req, open_req1.result, NULL);
708
+ ASSERT(r != -1);
709
+ ASSERT(close_req.result != -1);
710
+ uv_fs_req_cleanup(&close_req);
711
+
712
+ r = uv_fs_rename(loop, &rename_req, "test_file", "test_file2", NULL);
713
+ ASSERT(r != -1);
714
+ ASSERT(rename_req.result != -1);
715
+ uv_fs_req_cleanup(&rename_req);
716
+
717
+ r = uv_fs_open(loop, &open_req1, "test_file2", O_RDONLY, 0, NULL);
718
+ ASSERT(r != -1);
719
+ ASSERT(open_req1.result != -1);
720
+ uv_fs_req_cleanup(&open_req1);
721
+
722
+ memset(buf, 0, sizeof(buf));
723
+ r = uv_fs_read(loop, &read_req, open_req1.result, buf, sizeof(buf), -1,
724
+ NULL);
725
+ ASSERT(r != -1);
726
+ ASSERT(read_req.result != -1);
727
+ ASSERT(strcmp(buf, "test-bu") == 0);
728
+ uv_fs_req_cleanup(&read_req);
729
+
730
+ r = uv_fs_close(loop, &close_req, open_req1.result, NULL);
731
+ ASSERT(r != -1);
732
+ ASSERT(close_req.result != -1);
733
+ uv_fs_req_cleanup(&close_req);
734
+
735
+ r = uv_fs_unlink(loop, &unlink_req, "test_file2", NULL);
736
+ ASSERT(r != -1);
737
+ ASSERT(unlink_req.result != -1);
738
+ uv_fs_req_cleanup(&unlink_req);
739
+
740
+ /* Cleanup */
741
+ unlink("test_file");
742
+ unlink("test_file2");
743
+
744
+ MAKE_VALGRIND_HAPPY();
745
+ return 0;
746
+ }
747
+
748
+
749
+ TEST_IMPL(fs_async_dir) {
750
+ int r;
751
+
752
+ /* Setup */
753
+ unlink("test_dir/file1");
754
+ unlink("test_dir/file2");
755
+ rmdir("test_dir");
756
+
757
+ loop = uv_default_loop();
758
+
759
+ r = uv_fs_mkdir(loop, &mkdir_req, "test_dir", 0755, mkdir_cb);
760
+ ASSERT(r == 0);
761
+
762
+ uv_run(loop, UV_RUN_DEFAULT);
763
+ ASSERT(mkdir_cb_count == 1);
764
+
765
+ /* Create 2 files synchronously. */
766
+ r = uv_fs_open(loop, &open_req1, "test_dir/file1", O_WRONLY | O_CREAT,
767
+ S_IWRITE | S_IREAD, NULL);
768
+ ASSERT(r != -1);
769
+ uv_fs_req_cleanup(&open_req1);
770
+ r = uv_fs_close(loop, &close_req, open_req1.result, NULL);
771
+ ASSERT(r == 0);
772
+ uv_fs_req_cleanup(&close_req);
773
+
774
+ r = uv_fs_open(loop, &open_req1, "test_dir/file2", O_WRONLY | O_CREAT,
775
+ S_IWRITE | S_IREAD, NULL);
776
+ ASSERT(r != -1);
777
+ uv_fs_req_cleanup(&open_req1);
778
+ r = uv_fs_close(loop, &close_req, open_req1.result, NULL);
779
+ ASSERT(r == 0);
780
+ uv_fs_req_cleanup(&close_req);
781
+
782
+ r = uv_fs_readdir(loop, &readdir_req, "test_dir", 0, readdir_cb);
783
+ ASSERT(r == 0);
784
+
785
+ uv_run(loop, UV_RUN_DEFAULT);
786
+ ASSERT(readdir_cb_count == 1);
787
+
788
+ /* sync uv_fs_readdir */
789
+ r = uv_fs_readdir(loop, &readdir_req, "test_dir", 0, NULL);
790
+ ASSERT(r == 2);
791
+ ASSERT(readdir_req.result == 2);
792
+ ASSERT(readdir_req.ptr);
793
+ ASSERT(memcmp(readdir_req.ptr, "file1\0file2\0", 12) == 0
794
+ || memcmp(readdir_req.ptr, "file2\0file1\0", 12) == 0);
795
+ uv_fs_req_cleanup(&readdir_req);
796
+ ASSERT(!readdir_req.ptr);
797
+
798
+ r = uv_fs_stat(loop, &stat_req, "test_dir", stat_cb);
799
+ ASSERT(r == 0);
800
+ uv_run(loop, UV_RUN_DEFAULT);
801
+
802
+ r = uv_fs_stat(loop, &stat_req, "test_dir/", stat_cb);
803
+ ASSERT(r == 0);
804
+ uv_run(loop, UV_RUN_DEFAULT);
805
+
806
+ r = uv_fs_lstat(loop, &stat_req, "test_dir", stat_cb);
807
+ ASSERT(r == 0);
808
+ uv_run(loop, UV_RUN_DEFAULT);
809
+
810
+ r = uv_fs_lstat(loop, &stat_req, "test_dir/", stat_cb);
811
+ ASSERT(r == 0);
812
+ uv_run(loop, UV_RUN_DEFAULT);
813
+
814
+ ASSERT(stat_cb_count == 4);
815
+
816
+ r = uv_fs_unlink(loop, &unlink_req, "test_dir/file1", unlink_cb);
817
+ ASSERT(r == 0);
818
+ uv_run(loop, UV_RUN_DEFAULT);
819
+ ASSERT(unlink_cb_count == 1);
820
+
821
+ r = uv_fs_unlink(loop, &unlink_req, "test_dir/file2", unlink_cb);
822
+ ASSERT(r == 0);
823
+ uv_run(loop, UV_RUN_DEFAULT);
824
+ ASSERT(unlink_cb_count == 2);
825
+
826
+ r = uv_fs_rmdir(loop, &rmdir_req, "test_dir", rmdir_cb);
827
+ ASSERT(r == 0);
828
+ uv_run(loop, UV_RUN_DEFAULT);
829
+ ASSERT(rmdir_cb_count == 1);
830
+
831
+ /* Cleanup */
832
+ unlink("test_dir/file1");
833
+ unlink("test_dir/file2");
834
+ rmdir("test_dir");
835
+
836
+ MAKE_VALGRIND_HAPPY();
837
+ return 0;
838
+ }
839
+
840
+
841
+ TEST_IMPL(fs_async_sendfile) {
842
+ int f, r;
843
+ struct stat s1, s2;
844
+
845
+ loop = uv_default_loop();
846
+
847
+ /* Setup. */
848
+ unlink("test_file");
849
+ unlink("test_file2");
850
+
851
+ f = open("test_file", O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
852
+ ASSERT(f != -1);
853
+
854
+ r = write(f, "begin\n", 6);
855
+ ASSERT(r == 6);
856
+
857
+ r = lseek(f, 65536, SEEK_CUR);
858
+ ASSERT(r == 65542);
859
+
860
+ r = write(f, "end\n", 4);
861
+ ASSERT(r != -1);
862
+
863
+ r = close(f);
864
+ ASSERT(r == 0);
865
+
866
+ /* Test starts here. */
867
+ r = uv_fs_open(loop, &open_req1, "test_file", O_RDWR, 0, NULL);
868
+ ASSERT(r != -1);
869
+ ASSERT(open_req1.result != -1);
870
+ uv_fs_req_cleanup(&open_req1);
871
+
872
+ r = uv_fs_open(loop, &open_req2, "test_file2", O_WRONLY | O_CREAT,
873
+ S_IWRITE | S_IREAD, NULL);
874
+ ASSERT(r != -1);
875
+ ASSERT(open_req2.result != -1);
876
+ uv_fs_req_cleanup(&open_req2);
877
+
878
+ r = uv_fs_sendfile(loop, &sendfile_req, open_req2.result, open_req1.result,
879
+ 0, 131072, sendfile_cb);
880
+ ASSERT(r == 0);
881
+ uv_run(loop, UV_RUN_DEFAULT);
882
+
883
+ ASSERT(sendfile_cb_count == 1);
884
+
885
+ r = uv_fs_close(loop, &close_req, open_req1.result, NULL);
886
+ ASSERT(r == 0);
887
+ uv_fs_req_cleanup(&close_req);
888
+ r = uv_fs_close(loop, &close_req, open_req2.result, NULL);
889
+ ASSERT(r == 0);
890
+ uv_fs_req_cleanup(&close_req);
891
+
892
+ stat("test_file", &s1);
893
+ stat("test_file2", &s2);
894
+ ASSERT(65546 == s2.st_size && s1.st_size == s2.st_size);
895
+
896
+ /* Cleanup. */
897
+ unlink("test_file");
898
+ unlink("test_file2");
899
+
900
+ MAKE_VALGRIND_HAPPY();
901
+ return 0;
902
+ }
903
+
904
+
905
+ TEST_IMPL(fs_fstat) {
906
+ int r;
907
+ uv_fs_t req;
908
+ uv_file file;
909
+ struct stat* s;
910
+
911
+ /* Setup. */
912
+ unlink("test_file");
913
+
914
+ loop = uv_default_loop();
915
+
916
+ r = uv_fs_open(loop, &req, "test_file", O_RDWR | O_CREAT,
917
+ S_IWRITE | S_IREAD, NULL);
918
+ ASSERT(r != -1);
919
+ ASSERT(req.result != -1);
920
+ file = req.result;
921
+ uv_fs_req_cleanup(&req);
922
+
923
+ r = uv_fs_write(loop, &req, file, test_buf, sizeof(test_buf), -1, NULL);
924
+ ASSERT(r == sizeof(test_buf));
925
+ ASSERT(req.result == sizeof(test_buf));
926
+ uv_fs_req_cleanup(&req);
927
+
928
+ r = uv_fs_fstat(loop, &req, file, NULL);
929
+ ASSERT(r == 0);
930
+ ASSERT(req.result == 0);
931
+ s = req.ptr;
932
+ ASSERT(s->st_size == sizeof(test_buf));
933
+ uv_fs_req_cleanup(&req);
934
+
935
+ /* Now do the uv_fs_fstat call asynchronously */
936
+ r = uv_fs_fstat(loop, &req, file, fstat_cb);
937
+ ASSERT(r == 0);
938
+ uv_run(loop, UV_RUN_DEFAULT);
939
+ ASSERT(fstat_cb_count == 1);
940
+
941
+
942
+ r = uv_fs_close(loop, &req, file, NULL);
943
+ ASSERT(r == 0);
944
+ ASSERT(req.result == 0);
945
+ uv_fs_req_cleanup(&req);
946
+
947
+ /*
948
+ * Run the loop just to check we don't have make any extraneous uv_ref()
949
+ * calls. This should drop out immediately.
950
+ */
951
+ uv_run(loop, UV_RUN_DEFAULT);
952
+
953
+ /* Cleanup. */
954
+ unlink("test_file");
955
+
956
+ MAKE_VALGRIND_HAPPY();
957
+ return 0;
958
+ }
959
+
960
+
961
+ TEST_IMPL(fs_chmod) {
962
+ int r;
963
+ uv_fs_t req;
964
+ uv_file file;
965
+
966
+ /* Setup. */
967
+ unlink("test_file");
968
+
969
+ loop = uv_default_loop();
970
+
971
+ r = uv_fs_open(loop, &req, "test_file", O_RDWR | O_CREAT,
972
+ S_IWRITE | S_IREAD, NULL);
973
+ ASSERT(r != -1);
974
+ ASSERT(req.result != -1);
975
+ file = req.result;
976
+ uv_fs_req_cleanup(&req);
977
+
978
+ r = uv_fs_write(loop, &req, file, test_buf, sizeof(test_buf), -1, NULL);
979
+ ASSERT(r == sizeof(test_buf));
980
+ ASSERT(req.result == sizeof(test_buf));
981
+ uv_fs_req_cleanup(&req);
982
+
983
+ #ifndef _WIN32
984
+ /* Make the file write-only */
985
+ r = uv_fs_chmod(loop, &req, "test_file", 0200, NULL);
986
+ ASSERT(r == 0);
987
+ ASSERT(req.result == 0);
988
+ uv_fs_req_cleanup(&req);
989
+
990
+ check_permission("test_file", 0200);
991
+ #endif
992
+
993
+ /* Make the file read-only */
994
+ r = uv_fs_chmod(loop, &req, "test_file", 0400, NULL);
995
+ ASSERT(r == 0);
996
+ ASSERT(req.result == 0);
997
+ uv_fs_req_cleanup(&req);
998
+
999
+ check_permission("test_file", 0400);
1000
+
1001
+ /* Make the file read+write with sync uv_fs_fchmod */
1002
+ r = uv_fs_fchmod(loop, &req, file, 0600, NULL);
1003
+ ASSERT(r == 0);
1004
+ ASSERT(req.result == 0);
1005
+ uv_fs_req_cleanup(&req);
1006
+
1007
+ check_permission("test_file", 0600);
1008
+
1009
+ #ifndef _WIN32
1010
+ /* async chmod */
1011
+ {
1012
+ static int mode = 0200;
1013
+ req.data = &mode;
1014
+ }
1015
+ r = uv_fs_chmod(loop, &req, "test_file", 0200, chmod_cb);
1016
+ ASSERT(r == 0);
1017
+ uv_run(loop, UV_RUN_DEFAULT);
1018
+ ASSERT(chmod_cb_count == 1);
1019
+ chmod_cb_count = 0; /* reset for the next test */
1020
+ #endif
1021
+
1022
+ /* async chmod */
1023
+ {
1024
+ static int mode = 0400;
1025
+ req.data = &mode;
1026
+ }
1027
+ r = uv_fs_chmod(loop, &req, "test_file", 0400, chmod_cb);
1028
+ ASSERT(r == 0);
1029
+ uv_run(loop, UV_RUN_DEFAULT);
1030
+ ASSERT(chmod_cb_count == 1);
1031
+
1032
+ /* async fchmod */
1033
+ {
1034
+ static int mode = 0600;
1035
+ req.data = &mode;
1036
+ }
1037
+ r = uv_fs_fchmod(loop, &req, file, 0600, fchmod_cb);
1038
+ ASSERT(r == 0);
1039
+ uv_run(loop, UV_RUN_DEFAULT);
1040
+ ASSERT(fchmod_cb_count == 1);
1041
+
1042
+ close(file);
1043
+
1044
+ /*
1045
+ * Run the loop just to check we don't have make any extraneous uv_ref()
1046
+ * calls. This should drop out immediately.
1047
+ */
1048
+ uv_run(loop, UV_RUN_DEFAULT);
1049
+
1050
+ /* Cleanup. */
1051
+ unlink("test_file");
1052
+
1053
+ MAKE_VALGRIND_HAPPY();
1054
+ return 0;
1055
+ }
1056
+
1057
+
1058
+ TEST_IMPL(fs_chown) {
1059
+ int r;
1060
+ uv_fs_t req;
1061
+ uv_file file;
1062
+
1063
+ /* Setup. */
1064
+ unlink("test_file");
1065
+
1066
+ loop = uv_default_loop();
1067
+
1068
+ r = uv_fs_open(loop, &req, "test_file", O_RDWR | O_CREAT,
1069
+ S_IWRITE | S_IREAD, NULL);
1070
+ ASSERT(r != -1);
1071
+ ASSERT(req.result != -1);
1072
+ file = req.result;
1073
+ uv_fs_req_cleanup(&req);
1074
+
1075
+ /* sync chown */
1076
+ r = uv_fs_chown(loop, &req, "test_file", -1, -1, NULL);
1077
+ ASSERT(r == 0);
1078
+ ASSERT(req.result == 0);
1079
+ uv_fs_req_cleanup(&req);
1080
+
1081
+ /* sync fchown */
1082
+ r = uv_fs_fchown(loop, &req, file, -1, -1, NULL);
1083
+ ASSERT(r == 0);
1084
+ ASSERT(req.result == 0);
1085
+ uv_fs_req_cleanup(&req);
1086
+
1087
+ /* async chown */
1088
+ r = uv_fs_chown(loop, &req, "test_file", -1, -1, chown_cb);
1089
+ ASSERT(r == 0);
1090
+ uv_run(loop, UV_RUN_DEFAULT);
1091
+ ASSERT(chown_cb_count == 1);
1092
+
1093
+ /* chown to root (fail) */
1094
+ chown_cb_count = 0;
1095
+ r = uv_fs_chown(loop, &req, "test_file", 0, 0, chown_root_cb);
1096
+ uv_run(loop, UV_RUN_DEFAULT);
1097
+ ASSERT(chown_cb_count == 1);
1098
+
1099
+ /* async fchown */
1100
+ r = uv_fs_fchown(loop, &req, file, -1, -1, fchown_cb);
1101
+ ASSERT(r == 0);
1102
+ uv_run(loop, UV_RUN_DEFAULT);
1103
+ ASSERT(fchown_cb_count == 1);
1104
+
1105
+ close(file);
1106
+
1107
+ /*
1108
+ * Run the loop just to check we don't have make any extraneous uv_ref()
1109
+ * calls. This should drop out immediately.
1110
+ */
1111
+ uv_run(loop, UV_RUN_DEFAULT);
1112
+
1113
+ /* Cleanup. */
1114
+ unlink("test_file");
1115
+
1116
+ MAKE_VALGRIND_HAPPY();
1117
+ return 0;
1118
+ }
1119
+
1120
+
1121
+ TEST_IMPL(fs_link) {
1122
+ int r;
1123
+ uv_fs_t req;
1124
+ uv_file file;
1125
+ uv_file link;
1126
+
1127
+ /* Setup. */
1128
+ unlink("test_file");
1129
+ unlink("test_file_link");
1130
+ unlink("test_file_link2");
1131
+
1132
+ loop = uv_default_loop();
1133
+
1134
+ r = uv_fs_open(loop, &req, "test_file", O_RDWR | O_CREAT,
1135
+ S_IWRITE | S_IREAD, NULL);
1136
+ ASSERT(r != -1);
1137
+ ASSERT(req.result != -1);
1138
+ file = req.result;
1139
+ uv_fs_req_cleanup(&req);
1140
+
1141
+ r = uv_fs_write(loop, &req, file, test_buf, sizeof(test_buf), -1, NULL);
1142
+ ASSERT(r == sizeof(test_buf));
1143
+ ASSERT(req.result == sizeof(test_buf));
1144
+ uv_fs_req_cleanup(&req);
1145
+
1146
+ close(file);
1147
+
1148
+ /* sync link */
1149
+ r = uv_fs_link(loop, &req, "test_file", "test_file_link", NULL);
1150
+ ASSERT(r == 0);
1151
+ ASSERT(req.result == 0);
1152
+ uv_fs_req_cleanup(&req);
1153
+
1154
+ r = uv_fs_open(loop, &req, "test_file_link", O_RDWR, 0, NULL);
1155
+ ASSERT(r != -1);
1156
+ ASSERT(req.result != -1);
1157
+ link = req.result;
1158
+ uv_fs_req_cleanup(&req);
1159
+
1160
+ memset(buf, 0, sizeof(buf));
1161
+ r = uv_fs_read(loop, &req, link, buf, sizeof(buf), 0, NULL);
1162
+ ASSERT(r != -1);
1163
+ ASSERT(req.result != -1);
1164
+ ASSERT(strcmp(buf, test_buf) == 0);
1165
+
1166
+ close(link);
1167
+
1168
+ /* async link */
1169
+ r = uv_fs_link(loop, &req, "test_file", "test_file_link2", link_cb);
1170
+ ASSERT(r == 0);
1171
+ uv_run(loop, UV_RUN_DEFAULT);
1172
+ ASSERT(link_cb_count == 1);
1173
+
1174
+ r = uv_fs_open(loop, &req, "test_file_link2", O_RDWR, 0, NULL);
1175
+ ASSERT(r != -1);
1176
+ ASSERT(req.result != -1);
1177
+ link = req.result;
1178
+ uv_fs_req_cleanup(&req);
1179
+
1180
+ memset(buf, 0, sizeof(buf));
1181
+ r = uv_fs_read(loop, &req, link, buf, sizeof(buf), 0, NULL);
1182
+ ASSERT(r != -1);
1183
+ ASSERT(req.result != -1);
1184
+ ASSERT(strcmp(buf, test_buf) == 0);
1185
+
1186
+ close(link);
1187
+
1188
+ /*
1189
+ * Run the loop just to check we don't have make any extraneous uv_ref()
1190
+ * calls. This should drop out immediately.
1191
+ */
1192
+ uv_run(loop, UV_RUN_DEFAULT);
1193
+
1194
+ /* Cleanup. */
1195
+ unlink("test_file");
1196
+ unlink("test_file_link");
1197
+ unlink("test_file_link2");
1198
+
1199
+ MAKE_VALGRIND_HAPPY();
1200
+ return 0;
1201
+ }
1202
+
1203
+
1204
+ TEST_IMPL(fs_readlink) {
1205
+ uv_fs_t req;
1206
+
1207
+ loop = uv_default_loop();
1208
+ ASSERT(0 == uv_fs_readlink(loop, &req, "no_such_file", dummy_cb));
1209
+ ASSERT(0 == uv_run(loop, UV_RUN_DEFAULT));
1210
+ ASSERT(dummy_cb_count == 1);
1211
+ ASSERT(req.ptr == NULL);
1212
+ ASSERT(req.result == -1);
1213
+ ASSERT(req.errorno == UV_ENOENT);
1214
+ uv_fs_req_cleanup(&req);
1215
+
1216
+ ASSERT(-1 == uv_fs_readlink(loop, &req, "no_such_file", NULL));
1217
+ ASSERT(req.ptr == NULL);
1218
+ ASSERT(req.result == -1);
1219
+ ASSERT(req.errorno == UV_ENOENT);
1220
+ uv_fs_req_cleanup(&req);
1221
+
1222
+ MAKE_VALGRIND_HAPPY();
1223
+ return 0;
1224
+ }
1225
+
1226
+
1227
+ TEST_IMPL(fs_symlink) {
1228
+ int r;
1229
+ uv_fs_t req;
1230
+ uv_file file;
1231
+ uv_file link;
1232
+
1233
+ /* Setup. */
1234
+ unlink("test_file");
1235
+ unlink("test_file_symlink");
1236
+ unlink("test_file_symlink2");
1237
+ unlink("test_file_symlink_symlink");
1238
+ unlink("test_file_symlink2_symlink");
1239
+
1240
+ loop = uv_default_loop();
1241
+
1242
+ r = uv_fs_open(loop, &req, "test_file", O_RDWR | O_CREAT,
1243
+ S_IWRITE | S_IREAD, NULL);
1244
+ ASSERT(r != -1);
1245
+ ASSERT(req.result != -1);
1246
+ file = req.result;
1247
+ uv_fs_req_cleanup(&req);
1248
+
1249
+ r = uv_fs_write(loop, &req, file, test_buf, sizeof(test_buf), -1, NULL);
1250
+ ASSERT(r == sizeof(test_buf));
1251
+ ASSERT(req.result == sizeof(test_buf));
1252
+ uv_fs_req_cleanup(&req);
1253
+
1254
+ close(file);
1255
+
1256
+ /* sync symlink */
1257
+ r = uv_fs_symlink(loop, &req, "test_file", "test_file_symlink", 0, NULL);
1258
+ #ifdef _WIN32
1259
+ if (r == -1) {
1260
+ if (uv_last_error(loop).code == UV_ENOTSUP) {
1261
+ /*
1262
+ * Windows doesn't support symlinks on older versions.
1263
+ * We just pass the test and bail out early if we get ENOTSUP.
1264
+ */
1265
+ return 0;
1266
+ } else if (uv_last_error(loop).code == UV_EPERM) {
1267
+ /*
1268
+ * Creating a symlink is only allowed when running elevated.
1269
+ * We pass the test and bail out early if we get UV_EPERM.
1270
+ */
1271
+ return 0;
1272
+ }
1273
+ }
1274
+ #endif
1275
+ ASSERT(r == 0);
1276
+ ASSERT(req.result == 0);
1277
+ uv_fs_req_cleanup(&req);
1278
+
1279
+ r = uv_fs_open(loop, &req, "test_file_symlink", O_RDWR, 0, NULL);
1280
+ ASSERT(r != -1);
1281
+ ASSERT(req.result != -1);
1282
+ link = req.result;
1283
+ uv_fs_req_cleanup(&req);
1284
+
1285
+ memset(buf, 0, sizeof(buf));
1286
+ r = uv_fs_read(loop, &req, link, buf, sizeof(buf), 0, NULL);
1287
+ ASSERT(r != -1);
1288
+ ASSERT(req.result != -1);
1289
+ ASSERT(strcmp(buf, test_buf) == 0);
1290
+
1291
+ close(link);
1292
+
1293
+ r = uv_fs_symlink(loop, &req, "test_file_symlink", "test_file_symlink_symlink", 0, NULL);
1294
+ ASSERT(r != -1);
1295
+ uv_fs_req_cleanup(&req);
1296
+
1297
+ r = uv_fs_readlink(loop, &req, "test_file_symlink_symlink", NULL);
1298
+ ASSERT(r != -1);
1299
+ ASSERT(strcmp(req.ptr, "test_file_symlink") == 0);
1300
+ uv_fs_req_cleanup(&req);
1301
+
1302
+ /* async link */
1303
+ r = uv_fs_symlink(loop, &req, "test_file", "test_file_symlink2", 0, symlink_cb);
1304
+ ASSERT(r == 0);
1305
+ uv_run(loop, UV_RUN_DEFAULT);
1306
+ ASSERT(symlink_cb_count == 1);
1307
+
1308
+ r = uv_fs_open(loop, &req, "test_file_symlink2", O_RDWR, 0, NULL);
1309
+ ASSERT(r != -1);
1310
+ ASSERT(req.result != -1);
1311
+ link = req.result;
1312
+ uv_fs_req_cleanup(&req);
1313
+
1314
+ memset(buf, 0, sizeof(buf));
1315
+ r = uv_fs_read(loop, &req, link, buf, sizeof(buf), 0, NULL);
1316
+ ASSERT(r != -1);
1317
+ ASSERT(req.result != -1);
1318
+ ASSERT(strcmp(buf, test_buf) == 0);
1319
+
1320
+ close(link);
1321
+
1322
+ r = uv_fs_symlink(loop, &req, "test_file_symlink2", "test_file_symlink2_symlink", 0, NULL);
1323
+ ASSERT(r != -1);
1324
+ uv_fs_req_cleanup(&req);
1325
+
1326
+ r = uv_fs_readlink(loop, &req, "test_file_symlink2_symlink", readlink_cb);
1327
+ ASSERT(r != -1);
1328
+ uv_run(loop, UV_RUN_DEFAULT);
1329
+ ASSERT(readlink_cb_count == 1);
1330
+
1331
+ /*
1332
+ * Run the loop just to check we don't have make any extraneous uv_ref()
1333
+ * calls. This should drop out immediately.
1334
+ */
1335
+ uv_run(loop, UV_RUN_DEFAULT);
1336
+
1337
+ /* Cleanup. */
1338
+ unlink("test_file");
1339
+ unlink("test_file_symlink");
1340
+ unlink("test_file_symlink_symlink");
1341
+ unlink("test_file_symlink2");
1342
+ unlink("test_file_symlink2_symlink");
1343
+
1344
+ MAKE_VALGRIND_HAPPY();
1345
+ return 0;
1346
+ }
1347
+
1348
+
1349
+ TEST_IMPL(fs_symlink_dir) {
1350
+ uv_fs_t req;
1351
+ int r;
1352
+ char* test_dir;
1353
+
1354
+ /* set-up */
1355
+ unlink("test_dir/file1");
1356
+ unlink("test_dir/file2");
1357
+ rmdir("test_dir");
1358
+ rmdir("test_dir_symlink");
1359
+
1360
+ loop = uv_default_loop();
1361
+
1362
+ uv_fs_mkdir(loop, &req, "test_dir", 0777, NULL);
1363
+ uv_fs_req_cleanup(&req);
1364
+
1365
+ #ifdef _WIN32
1366
+ {
1367
+ static char src_path_buf[PATHMAX];
1368
+ strcpy(src_path_buf, "\\\\?\\");
1369
+ uv_cwd(src_path_buf + 4, sizeof(src_path_buf));
1370
+ strcat(src_path_buf, "\\test_dir\\");
1371
+ test_dir = src_path_buf;
1372
+ }
1373
+ #else
1374
+ test_dir = "test_dir";
1375
+ #endif
1376
+
1377
+ r = uv_fs_symlink(loop, &req, test_dir, "test_dir_symlink",
1378
+ UV_FS_SYMLINK_JUNCTION, NULL);
1379
+ ASSERT(r == 0);
1380
+ ASSERT(req.result == 0);
1381
+ uv_fs_req_cleanup(&req);
1382
+
1383
+ r = uv_fs_stat(loop, &req, "test_dir_symlink", NULL);
1384
+ ASSERT(r == 0);
1385
+ ASSERT(((struct stat*)req.ptr)->st_mode & S_IFDIR);
1386
+ uv_fs_req_cleanup(&req);
1387
+
1388
+ r = uv_fs_lstat(loop, &req, "test_dir_symlink", NULL);
1389
+ ASSERT(r == 0);
1390
+ ASSERT(((struct stat*)req.ptr)->st_mode & S_IFLNK);
1391
+ #ifdef _WIN32
1392
+ ASSERT(((struct stat*)req.ptr)->st_size == strlen(test_dir + 4));
1393
+ #else
1394
+ ASSERT(((struct stat*)req.ptr)->st_size == strlen(test_dir));
1395
+ #endif
1396
+ uv_fs_req_cleanup(&req);
1397
+
1398
+ r = uv_fs_readlink(loop, &req, "test_dir_symlink", NULL);
1399
+ ASSERT(r == 0);
1400
+ #ifdef _WIN32
1401
+ ASSERT(strcmp(req.ptr, test_dir + 4) == 0);
1402
+ #else
1403
+ ASSERT(strcmp(req.ptr, test_dir) == 0);
1404
+ #endif
1405
+ uv_fs_req_cleanup(&req);
1406
+
1407
+ r = uv_fs_open(loop, &open_req1, "test_dir/file1", O_WRONLY | O_CREAT,
1408
+ S_IWRITE | S_IREAD, NULL);
1409
+ ASSERT(r != -1);
1410
+ uv_fs_req_cleanup(&open_req1);
1411
+ r = uv_fs_close(loop, &close_req, open_req1.result, NULL);
1412
+ ASSERT(r == 0);
1413
+ uv_fs_req_cleanup(&close_req);
1414
+
1415
+ r = uv_fs_open(loop, &open_req1, "test_dir/file2", O_WRONLY | O_CREAT,
1416
+ S_IWRITE | S_IREAD, NULL);
1417
+ ASSERT(r != -1);
1418
+ uv_fs_req_cleanup(&open_req1);
1419
+ r = uv_fs_close(loop, &close_req, open_req1.result, NULL);
1420
+ ASSERT(r == 0);
1421
+ uv_fs_req_cleanup(&close_req);
1422
+
1423
+ r = uv_fs_readdir(loop, &readdir_req, "test_dir_symlink", 0, NULL);
1424
+ ASSERT(r == 2);
1425
+ ASSERT(readdir_req.result == 2);
1426
+ ASSERT(readdir_req.ptr);
1427
+ ASSERT(memcmp(readdir_req.ptr, "file1\0file2\0", 12) == 0
1428
+ || memcmp(readdir_req.ptr, "file2\0file1\0", 12) == 0);
1429
+ uv_fs_req_cleanup(&readdir_req);
1430
+ ASSERT(!readdir_req.ptr);
1431
+
1432
+ /* unlink will remove the directory symlink */
1433
+ r = uv_fs_unlink(loop, &req, "test_dir_symlink", NULL);
1434
+ ASSERT(r == 0);
1435
+ uv_fs_req_cleanup(&req);
1436
+
1437
+ r = uv_fs_readdir(loop, &readdir_req, "test_dir_symlink", 0, NULL);
1438
+ ASSERT(r == -1);
1439
+ uv_fs_req_cleanup(&readdir_req);
1440
+
1441
+ r = uv_fs_readdir(loop, &readdir_req, "test_dir", 0, NULL);
1442
+ ASSERT(r == 2);
1443
+ ASSERT(readdir_req.result == 2);
1444
+ ASSERT(readdir_req.ptr);
1445
+ ASSERT(memcmp(readdir_req.ptr, "file1\0file2\0", 12) == 0
1446
+ || memcmp(readdir_req.ptr, "file2\0file1\0", 12) == 0);
1447
+ uv_fs_req_cleanup(&readdir_req);
1448
+ ASSERT(!readdir_req.ptr);
1449
+
1450
+ /* clean-up */
1451
+ unlink("test_dir/file1");
1452
+ unlink("test_dir/file2");
1453
+ rmdir("test_dir");
1454
+ rmdir("test_dir_symlink");
1455
+
1456
+ MAKE_VALGRIND_HAPPY();
1457
+ return 0;
1458
+ }
1459
+
1460
+
1461
+ TEST_IMPL(fs_utime) {
1462
+ utime_check_t checkme;
1463
+ const char* path = "test_file";
1464
+ double atime;
1465
+ double mtime;
1466
+ uv_fs_t req;
1467
+ int r;
1468
+
1469
+ /* Setup. */
1470
+ loop = uv_default_loop();
1471
+ unlink(path);
1472
+ r = uv_fs_open(loop, &req, path, O_RDWR | O_CREAT,
1473
+ S_IWRITE | S_IREAD, NULL);
1474
+ ASSERT(r != -1);
1475
+ ASSERT(req.result != -1);
1476
+ uv_fs_req_cleanup(&req);
1477
+ close(r);
1478
+
1479
+ atime = mtime = 400497753; /* 1982-09-10 11:22:33 */
1480
+
1481
+ r = uv_fs_utime(loop, &req, path, atime, mtime, NULL);
1482
+ ASSERT(r == 0);
1483
+ ASSERT(req.result == 0);
1484
+ uv_fs_req_cleanup(&req);
1485
+
1486
+ r = uv_fs_stat(loop, &req, path, NULL);
1487
+ ASSERT(r == 0);
1488
+ ASSERT(req.result == 0);
1489
+ check_utime(path, atime, mtime);
1490
+ uv_fs_req_cleanup(&req);
1491
+
1492
+ atime = mtime = 1291404900; /* 2010-12-03 20:35:00 - mees <3 */
1493
+ checkme.path = path;
1494
+ checkme.atime = atime;
1495
+ checkme.mtime = mtime;
1496
+
1497
+ /* async utime */
1498
+ utime_req.data = &checkme;
1499
+ r = uv_fs_utime(loop, &utime_req, path, atime, mtime, utime_cb);
1500
+ ASSERT(r == 0);
1501
+ uv_run(loop, UV_RUN_DEFAULT);
1502
+ ASSERT(utime_cb_count == 1);
1503
+
1504
+ /* Cleanup. */
1505
+ unlink(path);
1506
+
1507
+ MAKE_VALGRIND_HAPPY();
1508
+ return 0;
1509
+ }
1510
+
1511
+
1512
+ #ifdef _WIN32
1513
+ TEST_IMPL(fs_stat_root) {
1514
+ int r;
1515
+ uv_loop_t* loop = uv_default_loop();
1516
+
1517
+ r = uv_fs_stat(loop, &stat_req, "\\", NULL);
1518
+ ASSERT(r == 0);
1519
+
1520
+ r = uv_fs_stat(loop, &stat_req, "..\\..\\..\\..\\..\\..\\..", NULL);
1521
+ ASSERT(r == 0);
1522
+
1523
+ r = uv_fs_stat(loop, &stat_req, "..", NULL);
1524
+ ASSERT(r == 0);
1525
+
1526
+ r = uv_fs_stat(loop, &stat_req, "..\\", NULL);
1527
+ ASSERT(r == 0);
1528
+
1529
+ /* stats the current directory on c: */
1530
+ r = uv_fs_stat(loop, &stat_req, "c:", NULL);
1531
+ ASSERT(r == 0);
1532
+
1533
+ r = uv_fs_stat(loop, &stat_req, "c:\\", NULL);
1534
+ ASSERT(r == 0);
1535
+
1536
+ r = uv_fs_stat(loop, &stat_req, "\\\\?\\C:\\", NULL);
1537
+ ASSERT(r == 0);
1538
+
1539
+ MAKE_VALGRIND_HAPPY();
1540
+ return 0;
1541
+ }
1542
+ #endif
1543
+
1544
+
1545
+ TEST_IMPL(fs_futime) {
1546
+ utime_check_t checkme;
1547
+ const char* path = "test_file";
1548
+ double atime;
1549
+ double mtime;
1550
+ uv_file file;
1551
+ uv_fs_t req;
1552
+ int r;
1553
+
1554
+ /* Setup. */
1555
+ loop = uv_default_loop();
1556
+ unlink(path);
1557
+ r = uv_fs_open(loop, &req, path, O_RDWR | O_CREAT,
1558
+ S_IWRITE | S_IREAD, NULL);
1559
+ ASSERT(r != -1);
1560
+ ASSERT(req.result != -1);
1561
+ uv_fs_req_cleanup(&req);
1562
+ close(r);
1563
+
1564
+ atime = mtime = 400497753; /* 1982-09-10 11:22:33 */
1565
+
1566
+ r = uv_fs_open(loop, &req, path, O_RDWR, 0, NULL);
1567
+ ASSERT(r != -1);
1568
+ ASSERT(req.result != -1);
1569
+ file = req.result; /* FIXME probably not how it's supposed to be used */
1570
+ uv_fs_req_cleanup(&req);
1571
+
1572
+ r = uv_fs_futime(loop, &req, file, atime, mtime, NULL);
1573
+ ASSERT(r == 0);
1574
+ ASSERT(req.result == 0);
1575
+ uv_fs_req_cleanup(&req);
1576
+
1577
+ r = uv_fs_stat(loop, &req, path, NULL);
1578
+ ASSERT(r == 0);
1579
+ ASSERT(req.result == 0);
1580
+ check_utime(path, atime, mtime);
1581
+ uv_fs_req_cleanup(&req);
1582
+
1583
+ atime = mtime = 1291404900; /* 2010-12-03 20:35:00 - mees <3 */
1584
+
1585
+ checkme.atime = atime;
1586
+ checkme.mtime = mtime;
1587
+ checkme.path = path;
1588
+
1589
+ /* async futime */
1590
+ futime_req.data = &checkme;
1591
+ r = uv_fs_futime(loop, &futime_req, file, atime, mtime, futime_cb);
1592
+ ASSERT(r == 0);
1593
+ uv_run(loop, UV_RUN_DEFAULT);
1594
+ ASSERT(futime_cb_count == 1);
1595
+
1596
+ /* Cleanup. */
1597
+ unlink(path);
1598
+
1599
+ MAKE_VALGRIND_HAPPY();
1600
+ return 0;
1601
+ }
1602
+
1603
+
1604
+ TEST_IMPL(fs_stat_missing_path) {
1605
+ uv_fs_t req;
1606
+ int r;
1607
+
1608
+ loop = uv_default_loop();
1609
+
1610
+ r = uv_fs_stat(loop, &req, "non_existent_file", NULL);
1611
+ ASSERT(r == -1);
1612
+ ASSERT(req.result == -1);
1613
+ ASSERT(uv_last_error(loop).code == UV_ENOENT);
1614
+ uv_fs_req_cleanup(&req);
1615
+
1616
+ MAKE_VALGRIND_HAPPY();
1617
+ return 0;
1618
+ }
1619
+
1620
+
1621
+ TEST_IMPL(fs_readdir_empty_dir) {
1622
+ const char* path;
1623
+ uv_fs_t req;
1624
+ int r;
1625
+
1626
+ path = "./empty_dir/";
1627
+ loop = uv_default_loop();
1628
+
1629
+ uv_fs_mkdir(loop, &req, path, 0777, NULL);
1630
+ uv_fs_req_cleanup(&req);
1631
+
1632
+ r = uv_fs_readdir(loop, &req, path, 0, NULL);
1633
+ ASSERT(r == 0);
1634
+ ASSERT(req.result == 0);
1635
+ ASSERT(req.ptr == NULL);
1636
+ uv_fs_req_cleanup(&req);
1637
+
1638
+ r = uv_fs_readdir(loop, &readdir_req, path, 0, empty_readdir_cb);
1639
+ ASSERT(r == 0);
1640
+
1641
+ ASSERT(readdir_cb_count == 0);
1642
+ uv_run(loop, UV_RUN_DEFAULT);
1643
+ ASSERT(readdir_cb_count == 1);
1644
+
1645
+ uv_fs_rmdir(loop, &req, path, NULL);
1646
+ uv_fs_req_cleanup(&req);
1647
+
1648
+ MAKE_VALGRIND_HAPPY();
1649
+ return 0;
1650
+ }
1651
+
1652
+
1653
+ TEST_IMPL(fs_readdir_file) {
1654
+ const char* path;
1655
+ int r;
1656
+
1657
+ path = "test/fixtures/empty_file";
1658
+ loop = uv_default_loop();
1659
+
1660
+ r = uv_fs_readdir(loop, &readdir_req, path, 0, NULL);
1661
+ ASSERT(r == -1);
1662
+ ASSERT(uv_last_error(loop).code == UV_ENOTDIR);
1663
+ uv_fs_req_cleanup(&readdir_req);
1664
+
1665
+ r = uv_fs_readdir(loop, &readdir_req, path, 0, file_readdir_cb);
1666
+ ASSERT(r == 0);
1667
+
1668
+ ASSERT(readdir_cb_count == 0);
1669
+ uv_run(loop, UV_RUN_DEFAULT);
1670
+ ASSERT(readdir_cb_count == 1);
1671
+
1672
+ MAKE_VALGRIND_HAPPY();
1673
+ return 0;
1674
+ }
1675
+
1676
+
1677
+ TEST_IMPL(fs_open_dir) {
1678
+ const char* path;
1679
+ uv_fs_t req;
1680
+ int r, file;
1681
+
1682
+ path = ".";
1683
+ loop = uv_default_loop();
1684
+
1685
+ r = uv_fs_open(loop, &req, path, O_RDONLY, 0, NULL);
1686
+ ASSERT(r != -1);
1687
+ ASSERT(req.result != -1);
1688
+ ASSERT(req.ptr == NULL);
1689
+ file = r;
1690
+ uv_fs_req_cleanup(&req);
1691
+
1692
+ r = uv_fs_close(loop, &req, file, NULL);
1693
+ ASSERT(r == 0);
1694
+
1695
+ r = uv_fs_open(loop, &req, path, O_RDONLY, 0, open_cb_simple);
1696
+ ASSERT(r == 0);
1697
+
1698
+ ASSERT(open_cb_count == 0);
1699
+ uv_run(loop, UV_RUN_DEFAULT);
1700
+ ASSERT(open_cb_count == 1);
1701
+
1702
+ MAKE_VALGRIND_HAPPY();
1703
+ return 0;
1704
+ }
1705
+
1706
+
1707
+ TEST_IMPL(fs_file_open_append) {
1708
+ int r;
1709
+
1710
+ /* Setup. */
1711
+ unlink("test_file");
1712
+
1713
+ loop = uv_default_loop();
1714
+
1715
+ r = uv_fs_open(loop, &open_req1, "test_file", O_WRONLY | O_CREAT,
1716
+ S_IWRITE | S_IREAD, NULL);
1717
+ ASSERT(r != -1);
1718
+ ASSERT(open_req1.result != -1);
1719
+ uv_fs_req_cleanup(&open_req1);
1720
+
1721
+ r = uv_fs_write(loop, &write_req, open_req1.result, test_buf,
1722
+ sizeof(test_buf), -1, NULL);
1723
+ ASSERT(r != -1);
1724
+ ASSERT(write_req.result != -1);
1725
+ uv_fs_req_cleanup(&write_req);
1726
+
1727
+ r = uv_fs_close(loop, &close_req, open_req1.result, NULL);
1728
+ ASSERT(r != -1);
1729
+ ASSERT(close_req.result != -1);
1730
+ uv_fs_req_cleanup(&close_req);
1731
+
1732
+ r = uv_fs_open(loop, &open_req1, "test_file", O_RDWR | O_APPEND, 0, NULL);
1733
+ ASSERT(r != -1);
1734
+ ASSERT(open_req1.result != -1);
1735
+ uv_fs_req_cleanup(&open_req1);
1736
+
1737
+ r = uv_fs_write(loop, &write_req, open_req1.result, test_buf,
1738
+ sizeof(test_buf), -1, NULL);
1739
+ ASSERT(r != -1);
1740
+ ASSERT(write_req.result != -1);
1741
+ uv_fs_req_cleanup(&write_req);
1742
+
1743
+ r = uv_fs_close(loop, &close_req, open_req1.result, NULL);
1744
+ ASSERT(r != -1);
1745
+ ASSERT(close_req.result != -1);
1746
+ uv_fs_req_cleanup(&close_req);
1747
+
1748
+ r = uv_fs_open(loop, &open_req1, "test_file", O_RDONLY, S_IREAD, NULL);
1749
+ ASSERT(r != -1);
1750
+ ASSERT(open_req1.result != -1);
1751
+ uv_fs_req_cleanup(&open_req1);
1752
+
1753
+ r = uv_fs_read(loop, &read_req, open_req1.result, buf, sizeof(buf), -1,
1754
+ NULL);
1755
+ printf("read = %d\n", r);
1756
+ ASSERT(r == 26);
1757
+ ASSERT(read_req.result == 26);
1758
+ ASSERT(memcmp(buf,
1759
+ "test-buffer\n\0test-buffer\n\0",
1760
+ sizeof("test-buffer\n\0test-buffer\n\0") - 1) == 0);
1761
+ uv_fs_req_cleanup(&read_req);
1762
+
1763
+ r = uv_fs_close(loop, &close_req, open_req1.result, NULL);
1764
+ ASSERT(r != -1);
1765
+ ASSERT(close_req.result != -1);
1766
+ uv_fs_req_cleanup(&close_req);
1767
+
1768
+ /* Cleanup */
1769
+ unlink("test_file");
1770
+
1771
+ MAKE_VALGRIND_HAPPY();
1772
+ return 0;
1773
+ }
1774
+
1775
+
1776
+ TEST_IMPL(fs_rename_to_existing_file) {
1777
+ int r;
1778
+
1779
+ /* Setup. */
1780
+ unlink("test_file");
1781
+ unlink("test_file2");
1782
+
1783
+ loop = uv_default_loop();
1784
+
1785
+ r = uv_fs_open(loop, &open_req1, "test_file", O_WRONLY | O_CREAT,
1786
+ S_IWRITE | S_IREAD, NULL);
1787
+ ASSERT(r != -1);
1788
+ ASSERT(open_req1.result != -1);
1789
+ uv_fs_req_cleanup(&open_req1);
1790
+
1791
+ r = uv_fs_write(loop, &write_req, open_req1.result, test_buf,
1792
+ sizeof(test_buf), -1, NULL);
1793
+ ASSERT(r != -1);
1794
+ ASSERT(write_req.result != -1);
1795
+ uv_fs_req_cleanup(&write_req);
1796
+
1797
+ r = uv_fs_close(loop, &close_req, open_req1.result, NULL);
1798
+ ASSERT(r != -1);
1799
+ ASSERT(close_req.result != -1);
1800
+ uv_fs_req_cleanup(&close_req);
1801
+
1802
+ r = uv_fs_open(loop, &open_req1, "test_file2", O_WRONLY | O_CREAT,
1803
+ S_IWRITE | S_IREAD, NULL);
1804
+ ASSERT(r != -1);
1805
+ ASSERT(open_req1.result != -1);
1806
+ uv_fs_req_cleanup(&open_req1);
1807
+
1808
+ r = uv_fs_close(loop, &close_req, open_req1.result, NULL);
1809
+ ASSERT(r != -1);
1810
+ ASSERT(close_req.result != -1);
1811
+ uv_fs_req_cleanup(&close_req);
1812
+
1813
+ r = uv_fs_rename(loop, &rename_req, "test_file", "test_file2", NULL);
1814
+ ASSERT(r != -1);
1815
+ ASSERT(rename_req.result != -1);
1816
+ uv_fs_req_cleanup(&rename_req);
1817
+
1818
+ r = uv_fs_open(loop, &open_req1, "test_file2", O_RDONLY, 0, NULL);
1819
+ ASSERT(r != -1);
1820
+ ASSERT(open_req1.result != -1);
1821
+ uv_fs_req_cleanup(&open_req1);
1822
+
1823
+ memset(buf, 0, sizeof(buf));
1824
+ r = uv_fs_read(loop, &read_req, open_req1.result, buf, sizeof(buf), -1,
1825
+ NULL);
1826
+ ASSERT(r != -1);
1827
+ ASSERT(read_req.result != -1);
1828
+ ASSERT(strcmp(buf, test_buf) == 0);
1829
+ uv_fs_req_cleanup(&read_req);
1830
+
1831
+ r = uv_fs_close(loop, &close_req, open_req1.result, NULL);
1832
+ ASSERT(r != -1);
1833
+ ASSERT(close_req.result != -1);
1834
+ uv_fs_req_cleanup(&close_req);
1835
+
1836
+ /* Cleanup */
1837
+ unlink("test_file");
1838
+ unlink("test_file2");
1839
+
1840
+ MAKE_VALGRIND_HAPPY();
1841
+ return 0;
1842
+ }
1843
+
1844
+
1845
+ TEST_IMPL(fs_read_file_eof) {
1846
+ int r;
1847
+
1848
+ /* Setup. */
1849
+ unlink("test_file");
1850
+
1851
+ loop = uv_default_loop();
1852
+
1853
+ r = uv_fs_open(loop, &open_req1, "test_file", O_WRONLY | O_CREAT,
1854
+ S_IWRITE | S_IREAD, NULL);
1855
+ ASSERT(r != -1);
1856
+ ASSERT(open_req1.result != -1);
1857
+ uv_fs_req_cleanup(&open_req1);
1858
+
1859
+ r = uv_fs_write(loop, &write_req, open_req1.result, test_buf,
1860
+ sizeof(test_buf), -1, NULL);
1861
+ ASSERT(r != -1);
1862
+ ASSERT(write_req.result != -1);
1863
+ uv_fs_req_cleanup(&write_req);
1864
+
1865
+ r = uv_fs_close(loop, &close_req, open_req1.result, NULL);
1866
+ ASSERT(r != -1);
1867
+ ASSERT(close_req.result != -1);
1868
+ uv_fs_req_cleanup(&close_req);
1869
+
1870
+ r = uv_fs_open(loop, &open_req1, "test_file", O_RDONLY, 0, NULL);
1871
+ ASSERT(r != -1);
1872
+ ASSERT(open_req1.result != -1);
1873
+ uv_fs_req_cleanup(&open_req1);
1874
+
1875
+ memset(buf, 0, sizeof(buf));
1876
+ r = uv_fs_read(loop, &read_req, open_req1.result, buf, sizeof(buf), -1,
1877
+ NULL);
1878
+ ASSERT(r != -1);
1879
+ ASSERT(read_req.result != -1);
1880
+ ASSERT(strcmp(buf, test_buf) == 0);
1881
+ uv_fs_req_cleanup(&read_req);
1882
+
1883
+ r = uv_fs_read(loop, &read_req, open_req1.result, buf, sizeof(buf),
1884
+ read_req.result, NULL);
1885
+ ASSERT(r == 0);
1886
+ ASSERT(read_req.result == 0);
1887
+ uv_fs_req_cleanup(&read_req);
1888
+
1889
+ r = uv_fs_close(loop, &close_req, open_req1.result, NULL);
1890
+ ASSERT(r != -1);
1891
+ ASSERT(close_req.result != -1);
1892
+ uv_fs_req_cleanup(&close_req);
1893
+
1894
+ /* Cleanup */
1895
+ unlink("test_file");
1896
+
1897
+ MAKE_VALGRIND_HAPPY();
1898
+ return 0;
1899
+ }