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__)
29#include <unistd.h> /* unlink, etc. */
30#else
31# include <direct.h>
32# include <io.h>
33# define unlink _unlink
34#endif
35
36static const char fixture[] = "test/fixtures/load_error.node";
37static const char dst[] = "test_file_dst";
38static int result_check_count;
39
40
41static void fail_cb(uv_fs_t* req) {
42 FATAL("fail_cb should not have been called");
43}
44
45static 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
70static 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
98TEST_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 unlink(src);
129
130 /* Copies file synchronously. Creates new file. */
131 unlink(dst);
132 r = uv_fs_copyfile(NULL, &req, fixture, dst, 0, NULL);
133 ASSERT(r == 0);
134 handle_result(&req);
135
136 /* Copies a file of size zero. */
137 unlink(dst);
138 touch_file(src, 0);
139 r = uv_fs_copyfile(NULL, &req, src, dst, 0, NULL);
140 ASSERT(r == 0);
141 handle_result(&req);
142
143 /* Copies file synchronously. Overwrites existing file. */
144 r = uv_fs_copyfile(NULL, &req, fixture, dst, 0, NULL);
145 ASSERT(r == 0);
146 handle_result(&req);
147
148 /* Fails to overwrites existing file. */
149 r = uv_fs_copyfile(NULL, &req, fixture, dst, UV_FS_COPYFILE_EXCL, NULL);
150 ASSERT(r == UV_EEXIST);
151 uv_fs_req_cleanup(&req);
152
153 /* Truncates when an existing destination is larger than the source file. */
154 touch_file(src, 1);
155 r = uv_fs_copyfile(NULL, &req, src, dst, 0, NULL);
156 ASSERT(r == 0);
157 handle_result(&req);
158
159 /* Copies a larger file. */
160 unlink(dst);
161 touch_file(src, 4096 * 2);
162 r = uv_fs_copyfile(NULL, &req, src, dst, 0, NULL);
163 ASSERT(r == 0);
164 handle_result(&req);
165 unlink(src);
166
167 /* Copies file asynchronously */
168 unlink(dst);
169 r = uv_fs_copyfile(loop, &req, fixture, dst, 0, handle_result);
170 ASSERT(r == 0);
171 ASSERT(result_check_count == 5);
172 uv_run(loop, UV_RUN_DEFAULT);
173 ASSERT(result_check_count == 6);
174
175 /* If the flags are invalid, the loop should not be kept open */
176 unlink(dst);
177 r = uv_fs_copyfile(loop, &req, fixture, dst, -1, fail_cb);
178 ASSERT(r == UV_EINVAL);
179 uv_run(loop, UV_RUN_DEFAULT);
180
181 /* Copies file using UV_FS_COPYFILE_FICLONE. */
182 unlink(dst);
183 r = uv_fs_copyfile(NULL, &req, fixture, dst, UV_FS_COPYFILE_FICLONE, NULL);
184 ASSERT(r == 0);
185 handle_result(&req);
186
187 /* Copies file using UV_FS_COPYFILE_FICLONE_FORCE. */
188 unlink(dst);
189 r = uv_fs_copyfile(NULL, &req, fixture, dst, UV_FS_COPYFILE_FICLONE_FORCE,
190 NULL);
191 ASSERT(r == 0 || r == UV_ENOSYS || r == UV_ENOTSUP);
192
193 if (r == 0)
194 handle_result(&req);
195
196#ifndef _WIN32
197 /* Copying respects permissions/mode. */
198 unlink(dst);
199 touch_file(dst, 0);
200 chmod(dst, S_IRUSR|S_IRGRP|S_IROTH); /* Sets file mode to 444 (read-only). */
201 r = uv_fs_copyfile(NULL, &req, fixture, dst, 0, NULL);
202 ASSERT(req.result == UV_EACCES);
203 ASSERT(r == UV_EACCES);
204 uv_fs_req_cleanup(&req);
205#endif
206
207 unlink(dst); /* Cleanup */
208 return 0;
209}
210