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