1 /* Copyright libuv project 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 #if defined(__unix__) || defined(__POSIX__) || \
26 defined(__APPLE__) || defined(__sun) || \
27 defined(_AIX) || defined(__MVS__) || \
28 defined(__HAIKU__) || defined(__QNX__)
29 #include <unistd.h> /* unlink, etc. */
30 #else
31 # include <direct.h>
32 # include <io.h>
33 # define unlink _unlink
34 #endif
35
36 static const char fixture[] = "test/fixtures/load_error.node";
37 static const char dst[] = "test_file_dst";
38 static int result_check_count;
39
40
fail_cb(uv_fs_t * req)41 static void fail_cb(uv_fs_t* req) {
42 FATAL("fail_cb should not have been called");
43 }
44
handle_result(uv_fs_t * req)45 static void handle_result(uv_fs_t* req) {
46 uv_fs_t stat_req;
47 uint64_t size;
48 uint64_t mode;
49 int r;
50
51 ASSERT(req->fs_type == UV_FS_COPYFILE);
52 ASSERT(req->result == 0);
53
54 /* Verify that the file size and mode are the same. */
55 r = uv_fs_stat(NULL, &stat_req, req->path, NULL);
56 ASSERT(r == 0);
57 size = stat_req.statbuf.st_size;
58 mode = stat_req.statbuf.st_mode;
59 uv_fs_req_cleanup(&stat_req);
60 r = uv_fs_stat(NULL, &stat_req, dst, NULL);
61 ASSERT(r == 0);
62 ASSERT(stat_req.statbuf.st_size == size);
63 ASSERT(stat_req.statbuf.st_mode == mode);
64 uv_fs_req_cleanup(&stat_req);
65 uv_fs_req_cleanup(req);
66 result_check_count++;
67 }
68
69
touch_file(const char * name,unsigned int size)70 static void touch_file(const char* name, unsigned int size) {
71 uv_file file;
72 uv_fs_t req;
73 uv_buf_t buf;
74 int r;
75 unsigned int i;
76
77 r = uv_fs_open(NULL, &req, name, O_WRONLY | O_CREAT | O_TRUNC,
78 S_IWUSR | S_IRUSR, NULL);
79 uv_fs_req_cleanup(&req);
80 ASSERT(r >= 0);
81 file = r;
82
83 buf = uv_buf_init("a", 1);
84
85 /* Inefficient but simple. */
86 for (i = 0; i < size; i++) {
87 r = uv_fs_write(NULL, &req, file, &buf, 1, i, NULL);
88 uv_fs_req_cleanup(&req);
89 ASSERT(r >= 0);
90 }
91
92 r = uv_fs_close(NULL, &req, file, NULL);
93 uv_fs_req_cleanup(&req);
94 ASSERT(r == 0);
95 }
96
97
TEST_IMPL(fs_copyfile)98 TEST_IMPL(fs_copyfile) {
99 const char src[] = "test_file_src";
100 uv_loop_t* loop;
101 uv_fs_t req;
102 int r;
103
104 loop = uv_default_loop();
105
106 /* Fails with EINVAL if bad flags are passed. */
107 r = uv_fs_copyfile(NULL, &req, src, dst, -1, NULL);
108 ASSERT(r == UV_EINVAL);
109 uv_fs_req_cleanup(&req);
110
111 /* Fails with ENOENT if source does not exist. */
112 unlink(src);
113 unlink(dst);
114 r = uv_fs_copyfile(NULL, &req, src, dst, 0, NULL);
115 ASSERT(req.result == UV_ENOENT);
116 ASSERT(r == UV_ENOENT);
117 uv_fs_req_cleanup(&req);
118 /* The destination should not exist. */
119 r = uv_fs_stat(NULL, &req, dst, NULL);
120 ASSERT(r != 0);
121 uv_fs_req_cleanup(&req);
122
123 /* Succeeds if src and dst files are identical. */
124 touch_file(src, 12);
125 r = uv_fs_copyfile(NULL, &req, src, src, 0, NULL);
126 ASSERT(r == 0);
127 uv_fs_req_cleanup(&req);
128 /* Verify that the src file did not get truncated. */
129 r = uv_fs_stat(NULL, &req, src, NULL);
130 ASSERT_EQ(r, 0);
131 ASSERT_EQ(req.statbuf.st_size, 12);
132 uv_fs_req_cleanup(&req);
133 unlink(src);
134
135 /* Copies file synchronously. Creates new file. */
136 unlink(dst);
137 r = uv_fs_copyfile(NULL, &req, fixture, dst, 0, NULL);
138 ASSERT(r == 0);
139 handle_result(&req);
140
141 /* Copies a file of size zero. */
142 unlink(dst);
143 touch_file(src, 0);
144 r = uv_fs_copyfile(NULL, &req, src, dst, 0, NULL);
145 ASSERT(r == 0);
146 handle_result(&req);
147
148 /* Copies file synchronously. Overwrites existing file. */
149 r = uv_fs_copyfile(NULL, &req, fixture, dst, 0, NULL);
150 ASSERT(r == 0);
151 handle_result(&req);
152
153 /* Fails to overwrites existing file. */
154 r = uv_fs_copyfile(NULL, &req, fixture, dst, UV_FS_COPYFILE_EXCL, NULL);
155 ASSERT(r == UV_EEXIST);
156 uv_fs_req_cleanup(&req);
157
158 /* Truncates when an existing destination is larger than the source file. */
159 touch_file(src, 1);
160 r = uv_fs_copyfile(NULL, &req, src, dst, 0, NULL);
161 ASSERT(r == 0);
162 handle_result(&req);
163
164 /* Copies a larger file. */
165 unlink(dst);
166 touch_file(src, 4096 * 2);
167 r = uv_fs_copyfile(NULL, &req, src, dst, 0, NULL);
168 ASSERT(r == 0);
169 handle_result(&req);
170 unlink(src);
171
172 /* Copies file asynchronously */
173 unlink(dst);
174 r = uv_fs_copyfile(loop, &req, fixture, dst, 0, handle_result);
175 ASSERT(r == 0);
176 ASSERT(result_check_count == 5);
177 uv_run(loop, UV_RUN_DEFAULT);
178 ASSERT(result_check_count == 6);
179
180 /* If the flags are invalid, the loop should not be kept open */
181 unlink(dst);
182 r = uv_fs_copyfile(loop, &req, fixture, dst, -1, fail_cb);
183 ASSERT(r == UV_EINVAL);
184 uv_run(loop, UV_RUN_DEFAULT);
185
186 /* Copies file using UV_FS_COPYFILE_FICLONE. */
187 unlink(dst);
188 r = uv_fs_copyfile(NULL, &req, fixture, dst, UV_FS_COPYFILE_FICLONE, NULL);
189 ASSERT(r == 0);
190 handle_result(&req);
191
192 /* Copies file using UV_FS_COPYFILE_FICLONE_FORCE. */
193 unlink(dst);
194 r = uv_fs_copyfile(NULL, &req, fixture, dst, UV_FS_COPYFILE_FICLONE_FORCE,
195 NULL);
196 ASSERT(r <= 0);
197
198 if (r == 0)
199 handle_result(&req);
200
201 unlink(dst); /* Cleanup */
202 return 0;
203 }
204