libuv/test/test-fs-copyfile.c

235 lines
6.7 KiB
C
Raw Normal View History

/* Copyright libuv project contributors. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include "uv.h"
#include "task.h"
2018.02.22, Version 1.19.2 (Stable) Changes since version 1.19.1: * test: fix incorrect asserts (cjihrig) * test: fix a typo in test-fork.c (Felix Yan) * build: remove long-obsolete gyp workarounds (Ben Noordhuis) * build: split off tests into separate gyp file (Ben Noordhuis) * test: check uv_cond_timedwait more carefully (Jamie Davis) * include,src: introduce UV__ERR() macro (Mason X) * build: add url field to libuv.pc (Ben Noordhuis) * doc: mark IBM i as Tier 3 support (Jesse Gorzinski) * win,build: correct C2059 errors (Michael Fero) * zos: fix timeout for condition variable (jBarz) * win: CREATE_NO_WINDOW when stdio is not inherited (Nick Logan) * build: fix commmon.gypi comment (Ryuichi KAWAMATA) * doc: document uv_timer_start() on an active timer (Vladimír Čunát) * doc: add note about handle movability (Bartosz Sosnowski) * doc: fix syntax error in loop documentation (Bartosz Sosnowski) * osx,stream: retry sending handle on EMSGSIZE error (Santiago Gimeno) * unix: delay fs req register until after validation (cjihrig) * test: add tests for bad inputs (Joyee Cheung) * unix,win: ensure req->bufs is freed (cjihrig) * test: add additional fs memory management checks (cjihrig) -----BEGIN PGP SIGNATURE----- Comment: GPGTools - https://gpgtools.org iQIcBAABCgAGBQJajdXDAAoJEHQ0OQvb6bnF+gkQAIYnk1ujKETakcu/O8M/d+yu gZkLAnqkMYD5X5rTEFkaHrzrQMD85twxme+GgegSFweUDvhqVQWveeNG5zRJ8/ri HsysKioqBsMfzCtJw4W+wDLIL7ltja2vcZooTTyvrIArLd2zQ8sn0nHe/eTq2uKl wMPhup9qxl10THpevdab3AMr1jEJ3KSsFaNUaVLySf6DJJqY4pRV+ekDGQVrkNZU XzH67YN2XWP5dsavAVX0GsnQraX8kNWgYjcDK93D55hj93iyq+38ZYstEhNqIGF9 9StzptoUyqK5KD5tyDYC2WmgwBJd9eAodmjCdq+ZP0td/irxEhQoRAX7aoAjBGzY j3vTb0JXhvvEghivap86mkxPhAQ6rmbGZkG/yOi6eexL/es1GwhGmfzE3NgUexCx nyKVCWqVo4IjL53+jndaX3wE4AIKpo0geah7f82v7cIJJqYN8DtaNN9imwZO3r7p WFUx2ujOYGuFcz1xbFNyLzBH0Rda2JnsvtAstGvWXSoXzCNSEU5PZ6rpn3jcOVU5 mDjQ9z/x3kp25qp014+wxHD+mltZzcL8H8fJfQ9JkGWdYE+R3brzwib07GctdlKP 3/PGH6vbDyiiCEiwfwjhjZpZWyJcaEuGegEc7J80uEm+/yq1/4HrdaFbI20D2heB NIj8XaaPt8Vqsafp0ZWp =Aa+M -----END PGP SIGNATURE----- gpgsig -----BEGIN PGP SIGNATURE----- iQIzBAABCAAdFiEEYS8OrZQBYiN530QC8ow8jaM8A74FAlqRjUEACgkQ8ow8jaM8 A77UBw//b71/tAhf1ZOEwouhCjuoy17PNrgfmpQFw8NbaJc6YALe2oQ+1+gc/GPp 9kaPfjDLSlZwYJMVtiTmKaQoqYNj3Y6ZJatZYambZ89/3XCBjd4WbK2IMnpth+xU tLzvjd5RlNMNpyrdrlEXneZi5ql7/QeRLj85k2790LSSajODK8A4VoZZoRmfBL4S rnngc5BFI2MGNOyUcc4HBtAcusvt6KIZWxhiRHLa0vpNmrnUAcXqruxzp36+ct+3 efy+9cx6uzbnqg7dv8WHljre+2we4NaHcH4W2zCkNuf0JrO9reJMjojBb3+NsVIt HBHonkVLvRf8eTv8VFoKQPqeMStVj2FtVRmuKqcZ8l/fZNH/Fz2R2tO2ZaLCrSax TkAiuLi7uOY32oBNNyI6GGwcJAD52ROl+UCBEoRCOpYVx6PWjZnun7zP7bYq+oJt h1qL7avrGfp7vG+ggDxfeEOlWZKeXBN6/uh+KnTFryxMwZ9GFNu46s2EttS2lATg eKvPXTPBF9xKVCxckDph5sBhT420SBt2xa8neeMMdEKXPaDPtyj9kMau6HeU84cO bcnGytFhlhx7qUv1sEO0gHp/fYkvdUYUhzsY5lWeVdz3U3j/YhQO+ErzBzhIOU+E arC+ldYO9eWzdgRxodOcHX6YFAEnHFeyXs5KfOKbUNdqD0dLNJQ= =lWVS -----END PGP SIGNATURE----- Merge tag 'v1.19.2' into merge_1.19.2 PR-URL: https://github.com/libuv/libuv/pull/1753 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
2018-02-21 21:13:54 +00:00
#include <fcntl.h>
#if defined(__unix__) || defined(__POSIX__) || \
defined(__APPLE__) || defined(__sun) || \
defined(_AIX) || defined(__MVS__) || \
defined(__HAIKU__) || defined(__QNX__)
#include <unistd.h> /* unlink, etc. */
#else
# include <direct.h>
# include <io.h>
# define unlink _unlink
#endif
static const char fixture[] = "test/fixtures/load_error.node";
static const char dst[] = "test_file_dst";
static int result_check_count;
static void fail_cb(uv_fs_t* req) {
FATAL("fail_cb should not have been called");
}
static void handle_result(uv_fs_t* req) {
uv_fs_t stat_req;
uint64_t size;
uint64_t mode;
uint64_t uid;
uint64_t gid;
int r;
ASSERT_EQ(req->fs_type, UV_FS_COPYFILE);
ASSERT_OK(req->result);
/* Verify that the file size and mode are the same. */
r = uv_fs_stat(NULL, &stat_req, req->path, NULL);
ASSERT_OK(r);
size = stat_req.statbuf.st_size;
mode = stat_req.statbuf.st_mode;
uid = stat_req.statbuf.st_uid;
gid = stat_req.statbuf.st_gid;
uv_fs_req_cleanup(&stat_req);
r = uv_fs_stat(NULL, &stat_req, dst, NULL);
ASSERT_OK(r);
ASSERT_EQ(stat_req.statbuf.st_size, size);
ASSERT_EQ(stat_req.statbuf.st_mode, mode);
ASSERT_EQ(stat_req.statbuf.st_uid, uid);
ASSERT_EQ(stat_req.statbuf.st_gid, gid);
uv_fs_req_cleanup(&stat_req);
uv_fs_req_cleanup(req);
result_check_count++;
}
static void touch_file(const char* name, unsigned int size) {
2018.02.22, Version 1.19.2 (Stable) Changes since version 1.19.1: * test: fix incorrect asserts (cjihrig) * test: fix a typo in test-fork.c (Felix Yan) * build: remove long-obsolete gyp workarounds (Ben Noordhuis) * build: split off tests into separate gyp file (Ben Noordhuis) * test: check uv_cond_timedwait more carefully (Jamie Davis) * include,src: introduce UV__ERR() macro (Mason X) * build: add url field to libuv.pc (Ben Noordhuis) * doc: mark IBM i as Tier 3 support (Jesse Gorzinski) * win,build: correct C2059 errors (Michael Fero) * zos: fix timeout for condition variable (jBarz) * win: CREATE_NO_WINDOW when stdio is not inherited (Nick Logan) * build: fix commmon.gypi comment (Ryuichi KAWAMATA) * doc: document uv_timer_start() on an active timer (Vladimír Čunát) * doc: add note about handle movability (Bartosz Sosnowski) * doc: fix syntax error in loop documentation (Bartosz Sosnowski) * osx,stream: retry sending handle on EMSGSIZE error (Santiago Gimeno) * unix: delay fs req register until after validation (cjihrig) * test: add tests for bad inputs (Joyee Cheung) * unix,win: ensure req->bufs is freed (cjihrig) * test: add additional fs memory management checks (cjihrig) -----BEGIN PGP SIGNATURE----- Comment: GPGTools - https://gpgtools.org iQIcBAABCgAGBQJajdXDAAoJEHQ0OQvb6bnF+gkQAIYnk1ujKETakcu/O8M/d+yu gZkLAnqkMYD5X5rTEFkaHrzrQMD85twxme+GgegSFweUDvhqVQWveeNG5zRJ8/ri HsysKioqBsMfzCtJw4W+wDLIL7ltja2vcZooTTyvrIArLd2zQ8sn0nHe/eTq2uKl wMPhup9qxl10THpevdab3AMr1jEJ3KSsFaNUaVLySf6DJJqY4pRV+ekDGQVrkNZU XzH67YN2XWP5dsavAVX0GsnQraX8kNWgYjcDK93D55hj93iyq+38ZYstEhNqIGF9 9StzptoUyqK5KD5tyDYC2WmgwBJd9eAodmjCdq+ZP0td/irxEhQoRAX7aoAjBGzY j3vTb0JXhvvEghivap86mkxPhAQ6rmbGZkG/yOi6eexL/es1GwhGmfzE3NgUexCx nyKVCWqVo4IjL53+jndaX3wE4AIKpo0geah7f82v7cIJJqYN8DtaNN9imwZO3r7p WFUx2ujOYGuFcz1xbFNyLzBH0Rda2JnsvtAstGvWXSoXzCNSEU5PZ6rpn3jcOVU5 mDjQ9z/x3kp25qp014+wxHD+mltZzcL8H8fJfQ9JkGWdYE+R3brzwib07GctdlKP 3/PGH6vbDyiiCEiwfwjhjZpZWyJcaEuGegEc7J80uEm+/yq1/4HrdaFbI20D2heB NIj8XaaPt8Vqsafp0ZWp =Aa+M -----END PGP SIGNATURE----- gpgsig -----BEGIN PGP SIGNATURE----- iQIzBAABCAAdFiEEYS8OrZQBYiN530QC8ow8jaM8A74FAlqRjUEACgkQ8ow8jaM8 A77UBw//b71/tAhf1ZOEwouhCjuoy17PNrgfmpQFw8NbaJc6YALe2oQ+1+gc/GPp 9kaPfjDLSlZwYJMVtiTmKaQoqYNj3Y6ZJatZYambZ89/3XCBjd4WbK2IMnpth+xU tLzvjd5RlNMNpyrdrlEXneZi5ql7/QeRLj85k2790LSSajODK8A4VoZZoRmfBL4S rnngc5BFI2MGNOyUcc4HBtAcusvt6KIZWxhiRHLa0vpNmrnUAcXqruxzp36+ct+3 efy+9cx6uzbnqg7dv8WHljre+2we4NaHcH4W2zCkNuf0JrO9reJMjojBb3+NsVIt HBHonkVLvRf8eTv8VFoKQPqeMStVj2FtVRmuKqcZ8l/fZNH/Fz2R2tO2ZaLCrSax TkAiuLi7uOY32oBNNyI6GGwcJAD52ROl+UCBEoRCOpYVx6PWjZnun7zP7bYq+oJt h1qL7avrGfp7vG+ggDxfeEOlWZKeXBN6/uh+KnTFryxMwZ9GFNu46s2EttS2lATg eKvPXTPBF9xKVCxckDph5sBhT420SBt2xa8neeMMdEKXPaDPtyj9kMau6HeU84cO bcnGytFhlhx7qUv1sEO0gHp/fYkvdUYUhzsY5lWeVdz3U3j/YhQO+ErzBzhIOU+E arC+ldYO9eWzdgRxodOcHX6YFAEnHFeyXs5KfOKbUNdqD0dLNJQ= =lWVS -----END PGP SIGNATURE----- Merge tag 'v1.19.2' into merge_1.19.2 PR-URL: https://github.com/libuv/libuv/pull/1753 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
2018-02-21 21:13:54 +00:00
uv_os_fd_t file;
uv_fs_t req;
uv_buf_t buf;
int r;
unsigned int i;
r = uv_fs_open(NULL, &req, name,
UV_FS_O_WRONLY | UV_FS_O_CREAT | UV_FS_O_TRUNC,
S_IWUSR | S_IRUSR, NULL);
uv_fs_req_cleanup(&req);
2023.11.06, Version 1.47.0 (Stable) Changes since version 1.46.0: * test: fix license blurb (Ben Noordhuis) * linux: fix harmless warn_unused_result warning (Shuduo Sang) * darwin: fix build warnings (小明) * linux: don't use io_uring on pre-5.10.186 kernels (Ben Noordhuis) * fs: fix WTF-8 decoding issue (Jameson Nash) * test: enable disabled tcp_connect6_error_fault (Ben Noordhuis) * test: enable disabled fs_link (Ben Noordhuis) * test: enable disabled spawn_same_stdout_stderr (Ben Noordhuis) * linux: handle UNAME26 personality (Ben Noordhuis) * build: move cmake_minimum_required version to 3.9 (Keith Winstein) * unix: set ipv6 scope id for link-local addresses (Ben Noordhuis) * unix: match kqueue and epoll code (Trevor Norris) * win,spawn: allow `%PATH%` to be unset (Kyle Edwards) * doc: switch to Furo, a more modern Sphinx theme (Saúl Ibarra Corretgé) * darwin: make TCP_KEEPINTVL and TCP_KEEPCNT available (小明) * win,fs: avoid winapi macro redefinition (Brad King) * linux: add missing riscv syscall numbers (michalbiesek) * doc: fix broken "Shared library" Wikipedia link (Alois Klink) * unix: get mainline kernel version in Ubuntu (Santiago Gimeno) * unix: get mainline kernel version in Debian (Ben Noordhuis) * build: fix qemu install in CI-unix workflow (Santiago Gimeno) * unix: disable io_uring close on selected kernels (Santiago Gimeno) * test: skip tests when ipv6 is not available (Santiago Gimeno) * ibmi: implement ifaddrs, getifaddrs, freeifaddrs (Abdirahim Musse) * unix: reset signal counters after fork (SmorkalovG) * win,process: avoid assert after spawning Store app (Jameson Nash) * unix: remove pread/preadv conditionals (Ben Noordhuis) * unix: remove pwrite/pwritev conditionals (Ben Noordhuis) * darwin: remove workaround for data corruption bug (Ben Noordhuis) * src: default to stream=stderr in handle printer (Ben Noordhuis) * test: switch to new-style ASSERT_EQ macros (Pleuvens) * zos: correctly get cpu model in uv_cpu_info() (jolai) * test: fix get_passwd2 on IBM i (Abdirahim Musse) * unix: don't malloc on sync uv_fs_read (Ben Noordhuis) * freebsd: get fs event path with fcntl(F_KINFO) (David Carlier) * test: switch from ASSERT_* to ASSERT_PTR_* (Pleuvens) * darwin: workaround apple pthread_cond_wait bug (Julien Roncaglia) * doc: uv_close should be called after exit callback (Pleuvens) * test: 192.0.2.0/24 is the actual -TEST-NET-1 (prubel) * unix: add back preadv/pwritev fallback (Ben Noordhuis) * unix: rename variable for consistency (Ben Noordhuis) * unix: merge read/write code into single functions (Ben Noordhuis) * doc: filename arg to uv_fs_event_cb can be NULL (Ben Noordhuis) * build,win: we need to link against shell32.lib (Per Allansson) * unix: no preadv/pwritev workaround if not needed (Jeffrey H. Johnson) * build: add CI for Windows ARM64 (build only) (Per Allansson) * linux: disable io_uring on 32 bits arm systems (Ben Noordhuis) * build: run sanitizers on macos ci (Ben Noordhuis) * misc: export WTF8 conversion utilities (Jameson Nash) * build: fix libuv.a file name for cmake (Jameson Nash) * build: add windows ubsan and clang ci (Matheus Izvekov) * win: improve accuracy of ProductName between arch (Christian Heimlich) -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEEz7ucqaW+r9cOKzxaeaZ8VaNnnIsFAmVJLa4ACgkQeaZ8VaNn nIvdMw//XQzU1OasKcghWZZAFmrgy8D+40Q5drSgCFryAA+WA9wXruKOeqQ3vw7+ 9Xwai97fMbxGSFhR2IwjbwD8o8DZotDTMBgSUvuyAlzdw0xB2D+eAnvxaMmYg2km R5eNb2KgmzhC7HFuPbpjWbL1mEnBU8rTSzV6yeMs2LXfLquJoopmgAjB2MXS6/Fr +WkwJx7/B/3wEhhbX5sopd6PT8UcWdbSvB/S+PU12kI0KgJeJlq0jXd5LyDaQYI6 j4XYcpgEp8B/gyiXSgso+eg1CiZQUYVMXZ5+6YA5le/s5ieJwJ2cEvo/HJa5guZ/ PBb+ulucUaupBzQFaPYwnrEzE3YHNVppBcfryxqn48Ih0Z9pNs/6EdxqggYvAyWk VA9Yi0qDFSFgD0hZSkvi/m/UEH17FLTac1DSob4EKfwAJ04spmEtiBWaL+7wQ/ca Kc819NXBaFUb+BEkN711B1CvaT6aef9wZcis42MPHjHJ19BNgb4uAa3GbmrvB2dt EodLGlbOAkKLFC29qAGeKZKhnSWfZS/HvuyqMOgONuJTj0Jm/RdwqIsvILejtj32 7m9saTkRWav4L1yvksUYT24WIk1e324gBgm9411IggdDILgbLYrwWNZb56FgMDQb cqmg0Ap6zMZ9UOf4hfqTiKZPjqqfxgSp20zCjVTy+vsCSXJnFaQ= =7yzD -----END PGP SIGNATURE----- Merge tag 'v1.47.0' into merge_1.47.0
2023-11-15 15:50:31 +00:00
ASSERT_EQ(r, 0);
2018.02.22, Version 1.19.2 (Stable) Changes since version 1.19.1: * test: fix incorrect asserts (cjihrig) * test: fix a typo in test-fork.c (Felix Yan) * build: remove long-obsolete gyp workarounds (Ben Noordhuis) * build: split off tests into separate gyp file (Ben Noordhuis) * test: check uv_cond_timedwait more carefully (Jamie Davis) * include,src: introduce UV__ERR() macro (Mason X) * build: add url field to libuv.pc (Ben Noordhuis) * doc: mark IBM i as Tier 3 support (Jesse Gorzinski) * win,build: correct C2059 errors (Michael Fero) * zos: fix timeout for condition variable (jBarz) * win: CREATE_NO_WINDOW when stdio is not inherited (Nick Logan) * build: fix commmon.gypi comment (Ryuichi KAWAMATA) * doc: document uv_timer_start() on an active timer (Vladimír Čunát) * doc: add note about handle movability (Bartosz Sosnowski) * doc: fix syntax error in loop documentation (Bartosz Sosnowski) * osx,stream: retry sending handle on EMSGSIZE error (Santiago Gimeno) * unix: delay fs req register until after validation (cjihrig) * test: add tests for bad inputs (Joyee Cheung) * unix,win: ensure req->bufs is freed (cjihrig) * test: add additional fs memory management checks (cjihrig) -----BEGIN PGP SIGNATURE----- Comment: GPGTools - https://gpgtools.org iQIcBAABCgAGBQJajdXDAAoJEHQ0OQvb6bnF+gkQAIYnk1ujKETakcu/O8M/d+yu gZkLAnqkMYD5X5rTEFkaHrzrQMD85twxme+GgegSFweUDvhqVQWveeNG5zRJ8/ri HsysKioqBsMfzCtJw4W+wDLIL7ltja2vcZooTTyvrIArLd2zQ8sn0nHe/eTq2uKl wMPhup9qxl10THpevdab3AMr1jEJ3KSsFaNUaVLySf6DJJqY4pRV+ekDGQVrkNZU XzH67YN2XWP5dsavAVX0GsnQraX8kNWgYjcDK93D55hj93iyq+38ZYstEhNqIGF9 9StzptoUyqK5KD5tyDYC2WmgwBJd9eAodmjCdq+ZP0td/irxEhQoRAX7aoAjBGzY j3vTb0JXhvvEghivap86mkxPhAQ6rmbGZkG/yOi6eexL/es1GwhGmfzE3NgUexCx nyKVCWqVo4IjL53+jndaX3wE4AIKpo0geah7f82v7cIJJqYN8DtaNN9imwZO3r7p WFUx2ujOYGuFcz1xbFNyLzBH0Rda2JnsvtAstGvWXSoXzCNSEU5PZ6rpn3jcOVU5 mDjQ9z/x3kp25qp014+wxHD+mltZzcL8H8fJfQ9JkGWdYE+R3brzwib07GctdlKP 3/PGH6vbDyiiCEiwfwjhjZpZWyJcaEuGegEc7J80uEm+/yq1/4HrdaFbI20D2heB NIj8XaaPt8Vqsafp0ZWp =Aa+M -----END PGP SIGNATURE----- gpgsig -----BEGIN PGP SIGNATURE----- iQIzBAABCAAdFiEEYS8OrZQBYiN530QC8ow8jaM8A74FAlqRjUEACgkQ8ow8jaM8 A77UBw//b71/tAhf1ZOEwouhCjuoy17PNrgfmpQFw8NbaJc6YALe2oQ+1+gc/GPp 9kaPfjDLSlZwYJMVtiTmKaQoqYNj3Y6ZJatZYambZ89/3XCBjd4WbK2IMnpth+xU tLzvjd5RlNMNpyrdrlEXneZi5ql7/QeRLj85k2790LSSajODK8A4VoZZoRmfBL4S rnngc5BFI2MGNOyUcc4HBtAcusvt6KIZWxhiRHLa0vpNmrnUAcXqruxzp36+ct+3 efy+9cx6uzbnqg7dv8WHljre+2we4NaHcH4W2zCkNuf0JrO9reJMjojBb3+NsVIt HBHonkVLvRf8eTv8VFoKQPqeMStVj2FtVRmuKqcZ8l/fZNH/Fz2R2tO2ZaLCrSax TkAiuLi7uOY32oBNNyI6GGwcJAD52ROl+UCBEoRCOpYVx6PWjZnun7zP7bYq+oJt h1qL7avrGfp7vG+ggDxfeEOlWZKeXBN6/uh+KnTFryxMwZ9GFNu46s2EttS2lATg eKvPXTPBF9xKVCxckDph5sBhT420SBt2xa8neeMMdEKXPaDPtyj9kMau6HeU84cO bcnGytFhlhx7qUv1sEO0gHp/fYkvdUYUhzsY5lWeVdz3U3j/YhQO+ErzBzhIOU+E arC+ldYO9eWzdgRxodOcHX6YFAEnHFeyXs5KfOKbUNdqD0dLNJQ= =lWVS -----END PGP SIGNATURE----- Merge tag 'v1.19.2' into merge_1.19.2 PR-URL: https://github.com/libuv/libuv/pull/1753 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
2018-02-21 21:13:54 +00:00
file = (uv_os_fd_t) req.result;
buf = uv_buf_init("a", 1);
/* Inefficient but simple. */
for (i = 0; i < size; i++) {
r = uv_fs_write(NULL, &req, file, &buf, 1, i, NULL);
uv_fs_req_cleanup(&req);
ASSERT_GE(r, 0);
}
r = uv_fs_close(NULL, &req, file, NULL);
uv_fs_req_cleanup(&req);
ASSERT_OK(r);
}
TEST_IMPL(fs_copyfile) {
const char src[] = "test_file_src";
uv_loop_t* loop;
uv_fs_t req;
int r;
loop = uv_default_loop();
/* Fails with EINVAL if bad flags are passed. */
r = uv_fs_copyfile(NULL, &req, src, dst, -1, NULL);
ASSERT_EQ(r, UV_EINVAL);
uv_fs_req_cleanup(&req);
/* Fails with ENOENT if source does not exist. */
unlink(src);
unlink(dst);
r = uv_fs_copyfile(NULL, &req, src, dst, 0, NULL);
ASSERT_EQ(req.result, UV_ENOENT);
ASSERT_EQ(r, UV_ENOENT);
uv_fs_req_cleanup(&req);
/* The destination should not exist. */
r = uv_fs_stat(NULL, &req, dst, NULL);
ASSERT(r);
uv_fs_req_cleanup(&req);
/* Succeeds if src and dst files are identical. */
touch_file(src, 12);
r = uv_fs_copyfile(NULL, &req, src, src, 0, NULL);
ASSERT_OK(r);
uv_fs_req_cleanup(&req);
/* Verify that the src file did not get truncated. */
r = uv_fs_stat(NULL, &req, src, NULL);
ASSERT_OK(r);
ASSERT_EQ(12, req.statbuf.st_size);
uv_fs_req_cleanup(&req);
unlink(src);
/* Copies file synchronously. Creates new file. */
unlink(dst);
r = uv_fs_copyfile(NULL, &req, fixture, dst, 0, NULL);
ASSERT_OK(r);
handle_result(&req);
/* Copies a file of size zero. */
unlink(dst);
touch_file(src, 0);
r = uv_fs_copyfile(NULL, &req, src, dst, 0, NULL);
ASSERT_OK(r);
handle_result(&req);
/* Copies file synchronously. Overwrites existing file. */
r = uv_fs_copyfile(NULL, &req, fixture, dst, 0, NULL);
ASSERT_OK(r);
handle_result(&req);
/* Fails to overwrites existing file. */
ASSERT_OK(uv_fs_chmod(NULL, &req, dst, 0644, NULL));
uv_fs_req_cleanup(&req);
r = uv_fs_copyfile(NULL, &req, fixture, dst, UV_FS_COPYFILE_EXCL, NULL);
ASSERT_EQ(r, UV_EEXIST);
uv_fs_req_cleanup(&req);
/* Truncates when an existing destination is larger than the source file. */
ASSERT_OK(uv_fs_chmod(NULL, &req, dst, 0644, NULL));
uv_fs_req_cleanup(&req);
touch_file(src, 1);
r = uv_fs_copyfile(NULL, &req, src, dst, 0, NULL);
ASSERT_OK(r);
handle_result(&req);
/* Copies a larger file. */
unlink(dst);
touch_file(src, 4096 * 2);
r = uv_fs_copyfile(NULL, &req, src, dst, 0, NULL);
ASSERT_OK(r);
handle_result(&req);
unlink(src);
/* Copies file asynchronously */
unlink(dst);
r = uv_fs_copyfile(loop, &req, fixture, dst, 0, handle_result);
ASSERT_OK(r);
ASSERT_EQ(5, result_check_count);
uv_run(loop, UV_RUN_DEFAULT);
ASSERT_EQ(6, result_check_count);
/* Ensure file is user-writable (not copied from src). */
ASSERT_OK(uv_fs_chmod(NULL, &req, dst, 0644, NULL));
uv_fs_req_cleanup(&req);
/* If the flags are invalid, the loop should not be kept open */
unlink(dst);
r = uv_fs_copyfile(loop, &req, fixture, dst, -1, fail_cb);
ASSERT_EQ(r, UV_EINVAL);
uv_run(loop, UV_RUN_DEFAULT);
/* Copies file using UV_FS_COPYFILE_FICLONE. */
unlink(dst);
r = uv_fs_copyfile(NULL, &req, fixture, dst, UV_FS_COPYFILE_FICLONE, NULL);
ASSERT_OK(r);
handle_result(&req);
/* Copies file using UV_FS_COPYFILE_FICLONE_FORCE. */
unlink(dst);
r = uv_fs_copyfile(NULL, &req, fixture, dst, UV_FS_COPYFILE_FICLONE_FORCE,
NULL);
ASSERT_LE(r, 0);
if (r == 0)
handle_result(&req);
#ifndef _WIN32
/* Copying respects permissions/mode. */
unlink(dst);
touch_file(dst, 0);
chmod(dst, S_IRUSR|S_IRGRP|S_IROTH); /* Sets file mode to 444 (read-only). */
r = uv_fs_copyfile(NULL, &req, fixture, dst, 0, NULL);
/* On IBMi PASE, qsecofr users can overwrite read-only files */
# ifndef __PASE__
ASSERT_EQ(req.result, UV_EACCES);
ASSERT_EQ(r, UV_EACCES);
# endif
uv_fs_req_cleanup(&req);
#endif
unlink(dst); /* Cleanup */
MAKE_VALGRIND_HAPPY(loop);
return 0;
}