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 #if defined(__ASAN__)
100   RETURN_SKIP("Test does not currently work in ASAN");
101 #endif
102   const char src[] = "test_file_src";
103   uv_loop_t* loop;
104   uv_fs_t req;
105   int r;
106 
107   loop = uv_default_loop();
108 
109   /* Fails with EINVAL if bad flags are passed. */
110   r = uv_fs_copyfile(NULL, &req, src, dst, -1, NULL);
111   ASSERT(r == UV_EINVAL);
112   uv_fs_req_cleanup(&req);
113 
114   /* Fails with ENOENT if source does not exist. */
115   unlink(src);
116   unlink(dst);
117   r = uv_fs_copyfile(NULL, &req, src, dst, 0, NULL);
118   ASSERT(req.result == UV_ENOENT);
119   ASSERT(r == UV_ENOENT);
120   uv_fs_req_cleanup(&req);
121   /* The destination should not exist. */
122   r = uv_fs_stat(NULL, &req, dst, NULL);
123   ASSERT(r != 0);
124   uv_fs_req_cleanup(&req);
125 
126   /* Succeeds if src and dst files are identical. */
127   touch_file(src, 12);
128   r = uv_fs_copyfile(NULL, &req, src, src, 0, NULL);
129   ASSERT(r == 0);
130   uv_fs_req_cleanup(&req);
131   /* Verify that the src file did not get truncated. */
132   r = uv_fs_stat(NULL, &req, src, NULL);
133   ASSERT_EQ(r, 0);
134   ASSERT_EQ(req.statbuf.st_size, 12);
135   uv_fs_req_cleanup(&req);
136   unlink(src);
137 
138   /* Copies file synchronously. Creates new file. */
139   unlink(dst);
140   r = uv_fs_copyfile(NULL, &req, fixture, dst, 0, NULL);
141   ASSERT(r == 0);
142   handle_result(&req);
143 
144   /* Copies a file of size zero. */
145   unlink(dst);
146   touch_file(src, 0);
147   r = uv_fs_copyfile(NULL, &req, src, dst, 0, NULL);
148   ASSERT(r == 0);
149   handle_result(&req);
150 
151   /* Copies file synchronously. Overwrites existing file. */
152   r = uv_fs_copyfile(NULL, &req, fixture, dst, 0, NULL);
153   ASSERT(r == 0);
154   handle_result(&req);
155 
156   /* Fails to overwrites existing file. */
157   r = uv_fs_copyfile(NULL, &req, fixture, dst, UV_FS_COPYFILE_EXCL, NULL);
158   ASSERT(r == UV_EEXIST);
159   uv_fs_req_cleanup(&req);
160 
161   /* Truncates when an existing destination is larger than the source file. */
162   touch_file(src, 1);
163   r = uv_fs_copyfile(NULL, &req, src, dst, 0, NULL);
164   ASSERT(r == 0);
165   handle_result(&req);
166 
167   /* Copies a larger file. */
168   unlink(dst);
169   touch_file(src, 4096 * 2);
170   r = uv_fs_copyfile(NULL, &req, src, dst, 0, NULL);
171   ASSERT(r == 0);
172   handle_result(&req);
173   unlink(src);
174 
175   /* Copies file asynchronously */
176   unlink(dst);
177   r = uv_fs_copyfile(loop, &req, fixture, dst, 0, handle_result);
178   ASSERT(r == 0);
179   ASSERT(result_check_count == 5);
180   uv_run(loop, UV_RUN_DEFAULT);
181   ASSERT(result_check_count == 6);
182 
183   /* If the flags are invalid, the loop should not be kept open */
184   unlink(dst);
185   r = uv_fs_copyfile(loop, &req, fixture, dst, -1, fail_cb);
186   ASSERT(r == UV_EINVAL);
187   uv_run(loop, UV_RUN_DEFAULT);
188 
189   /* Copies file using UV_FS_COPYFILE_FICLONE. */
190   unlink(dst);
191   r = uv_fs_copyfile(NULL, &req, fixture, dst, UV_FS_COPYFILE_FICLONE, NULL);
192   ASSERT(r == 0);
193   handle_result(&req);
194 
195   /* Copies file using UV_FS_COPYFILE_FICLONE_FORCE. */
196   unlink(dst);
197   r = uv_fs_copyfile(NULL, &req, fixture, dst, UV_FS_COPYFILE_FICLONE_FORCE,
198                      NULL);
199   ASSERT(r <= 0);
200 
201   if (r == 0)
202     handle_result(&req);
203 
204 #ifndef _WIN32
205   /* Copying respects permissions/mode. */
206   unlink(dst);
207   touch_file(dst, 0);
208   chmod(dst, S_IRUSR|S_IRGRP|S_IROTH); /* Sets file mode to 444 (read-only). */
209   r = uv_fs_copyfile(NULL, &req, fixture, dst, 0, NULL);
210   /* On IBMi PASE, qsecofr users can overwrite read-only files */
211 # ifndef __PASE__
212   ASSERT(req.result == UV_EACCES);
213   ASSERT(r == UV_EACCES);
214 # endif
215   uv_fs_req_cleanup(&req);
216 #endif
217 
218   unlink(dst); /* Cleanup */
219   return 0;
220 }
221