test-fs-copyfile.c
Go to the documentation of this file.
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 
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 
41 static void fail_cb(uv_fs_t* req) {
42  FATAL("fail_cb should not have been called");
43 }
44 
45 static void handle_result(uv_fs_t* 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);
60  r = uv_fs_stat(NULL, &stat_req, dst, NULL);
61  ASSERT(r == 0);
67 }
68 
69 
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);
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);
89  ASSERT(r >= 0);
90  }
91 
92  r = uv_fs_close(NULL, &req, file, NULL);
94  ASSERT(r == 0);
95 }
96 
97 
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);
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);
118  /* The destination should not exist. */
119  r = uv_fs_stat(NULL, &req, dst, NULL);
120  ASSERT(r != 0);
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);
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);
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);
170  ASSERT(r == 0);
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);
180 
181  /* Copies file using UV_FS_COPYFILE_FICLONE. */
182  unlink(dst);
184  ASSERT(r == 0);
185  handle_result(&req);
186 
187  /* Copies file using UV_FS_COPYFILE_FICLONE_FORCE. */
188  unlink(dst);
190  NULL);
191  ASSERT(r <= 0);
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  /* On IBMi PASE, qsecofr users can overwrite read-only files */
203 # ifndef __PASE__
204  ASSERT(req.result == UV_EACCES);
205  ASSERT(r == UV_EACCES);
206 # endif
208 #endif
209 
210  unlink(dst); /* Cleanup */
211  return 0;
212 }
UV_FS_COPYFILE_EXCL
#define UV_FS_COPYFILE_EXCL
Definition: uv.h:1345
async_greeter_server_with_graceful_shutdown.loop
loop
Definition: async_greeter_server_with_graceful_shutdown.py:59
uv_fs_s::statbuf
uv_stat_t statbuf
Definition: uv.h:1302
dst
static const char dst[]
Definition: test-fs-copyfile.c:37
uv_fs_open
UV_EXTERN int uv_fs_open(uv_loop_t *loop, uv_fs_t *req, const char *path, int flags, int mode, uv_fs_cb cb)
Definition: unix/fs.c:1812
task.h
uv_fs_copyfile
UV_EXTERN int uv_fs_copyfile(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *new_path, int flags, uv_fs_cb cb)
Definition: unix/fs.c:2057
file
const grpc_generator::File * file
Definition: python_private_generator.h:38
uv_fs_stat
UV_EXTERN int uv_fs_stat(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb)
Definition: unix/fs.c:1954
buf
voidpf void * buf
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
uv_stat_t::st_size
uint64_t st_size
Definition: uv.h:354
ASSERT
#define ASSERT(expr)
Definition: task.h:102
file
Definition: bloaty/third_party/zlib/examples/gzappend.c:170
mode
const char int mode
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:135
setup.name
name
Definition: setup.py:542
uv_run
UV_EXTERN int uv_run(uv_loop_t *, uv_run_mode mode)
Definition: unix/core.c:361
uv_fs_s
Definition: uv.h:1294
uv_default_loop
UV_EXTERN uv_loop_t * uv_default_loop(void)
Definition: uv-common.c:733
TEST_IMPL
TEST_IMPL(fs_copyfile)
Definition: test-fs-copyfile.c:98
fixture
static const char fixture[]
Definition: test-fs-copyfile.c:36
req
static uv_connect_t req
Definition: test-connection-fail.c:30
UV_RUN_DEFAULT
@ UV_RUN_DEFAULT
Definition: uv.h:254
UV_FS_COPYFILE_FICLONE
#define UV_FS_COPYFILE_FICLONE
Definition: uv.h:1351
uv_stat_t::st_mode
uint64_t st_mode
Definition: uv.h:348
uint64_t
unsigned __int64 uint64_t
Definition: stdint-msvc2008.h:90
stat_req
static uv_fs_t stat_req
Definition: test-fs.c:117
uv_file
int uv_file
Definition: unix.h:126
uv_fs_close
UV_EXTERN int uv_fs_close(uv_loop_t *loop, uv_fs_t *req, uv_file file, uv_fs_cb cb)
Definition: unix/fs.c:1651
fail_cb
static void fail_cb(uv_fs_t *req)
Definition: test-fs-copyfile.c:41
UV_FS_COPYFILE_FICLONE_FORCE
#define UV_FS_COPYFILE_FICLONE_FORCE
Definition: uv.h:1357
uv.h
FATAL
#define FATAL(msg)
Definition: task.h:88
uv_buf_t
Definition: unix.h:121
UV_FS_COPYFILE
@ UV_FS_COPYFILE
Definition: uv.h:1276
uv_fs_write
UV_EXTERN int uv_fs_write(uv_loop_t *loop, uv_fs_t *req, uv_file file, const uv_buf_t bufs[], unsigned int nbufs, int64_t offset, uv_fs_cb cb)
Definition: unix/fs.c:1995
fix_build_deps.r
r
Definition: fix_build_deps.py:491
uv_buf_init
UV_EXTERN uv_buf_t uv_buf_init(char *base, unsigned int len)
Definition: uv-common.c:157
handle_result
static void handle_result(uv_fs_t *req)
Definition: test-fs-copyfile.c:45
unlink
#define unlink
Definition: test-fs-copyfile.c:33
uv_fs_req_cleanup
UV_EXTERN void uv_fs_req_cleanup(uv_fs_t *req)
Definition: unix/fs.c:2024
result_check_count
static int result_check_count
Definition: test-fs-copyfile.c:38
touch_file
static void touch_file(const char *name, unsigned int size)
Definition: test-fs-copyfile.c:70
uv_loop_s
Definition: uv.h:1767
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:26