]>
git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - io/copy_file_range.c
1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2016 Netapp, Inc. All rights reserved.
6 #include <sys/syscall.h>
14 static cmdinfo_t copy_range_cmd
;
20 Copies a range of bytes from a file into the open file, overwriting any data\n\
24 'copy_range -s 100 -d 200 -l 300 some_file' - copies 300 bytes from some_file\n\
25 at offset 100 into the open\n\
27 'copy_range some_file' - copies all bytes from some_file into the open file\n\
33 * Issue a raw copy_file_range syscall; for our test program we don't want the
34 * glibc buffered copy fallback.
37 copy_file_range_cmd(int fd
, long long *src
, long long *dst
, size_t len
)
42 ret
= syscall(__NR_copy_file_range
, fd
, src
, file
->fd
, dst
,
56 copy_src_filesize(int fd
)
60 if (fstat(fd
, &st
) < 0) {
68 copy_dst_truncate(void)
70 int ret
= ftruncate(file
->fd
, 0);
77 copy_range_f(int argc
, char **argv
)
85 size_t fsblocksize
, fssectsize
;
87 init_cvtnum(&fsblocksize
, &fssectsize
);
89 while ((opt
= getopt(argc
, argv
, "s:d:l:")) != -1) {
92 src
= cvtnum(fsblocksize
, fssectsize
, optarg
);
94 printf(_("invalid source offset -- %s\n"), optarg
);
99 dst
= cvtnum(fsblocksize
, fssectsize
, optarg
);
101 printf(_("invalid destination offset -- %s\n"), optarg
);
106 len
= cvtnum(fsblocksize
, fssectsize
, optarg
);
108 printf(_("invalid length -- %s\n"), optarg
);
115 if (optind
!= argc
- 1)
116 return command_usage(©_range_cmd
);
118 fd
= openfile(argv
[optind
], NULL
, IO_READONLY
, 0, NULL
);
122 if (src
== 0 && dst
== 0 && len
== 0) {
125 sz
= copy_src_filesize(fd
);
126 if (sz
< 0 || (unsigned long long)sz
> SIZE_MAX
) {
132 ret
= copy_dst_truncate();
139 ret
= copy_file_range_cmd(fd
, &src
, &dst
, len
);
146 copy_range_init(void)
148 copy_range_cmd
.name
= "copy_range";
149 copy_range_cmd
.cfunc
= copy_range_f
;
150 copy_range_cmd
.argmin
= 1;
151 copy_range_cmd
.argmax
= 7;
152 copy_range_cmd
.flags
= CMD_NOMAP_OK
| CMD_FOREIGN_OK
;
153 copy_range_cmd
.args
= _("[-s src_off] [-d dst_off] [-l len] src_file");
154 copy_range_cmd
.oneline
= _("Copy a range of data between two files");
155 copy_range_cmd
.help
= copy_range_help
;
157 add_command(©_range_cmd
);