]>
git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - io/copy_file_range.c
2 * Copyright (c) 2016 Netapp, Inc. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 #include <sys/syscall.h>
27 static cmdinfo_t copy_range_cmd
;
33 Copies a range of bytes from a file into the open file, overwriting any data\n\
37 'copy_range -s 100 -d 200 -l 300 some_file' - copies 300 bytes from some_file\n\
38 at offset 100 into the open\n\
40 'copy_range some_file' - copies all bytes from some_file into the open file\n\
46 copy_file_range(int fd
, loff_t
*src
, loff_t
*dst
, size_t len
)
51 ret
= syscall(__NR_copy_file_range
, fd
, src
, file
->fd
, dst
, len
, 0);
61 copy_src_filesize(int fd
)
65 if (fstat64(fd
, &st
) < 0) {
73 copy_dst_truncate(void)
75 int ret
= ftruncate64(file
->fd
, 0);
77 perror("ftruncate64");
82 copy_range_f(int argc
, char **argv
)
92 while ((opt
= getopt(argc
, argv
, "s:d:l:")) != -1) {
95 src
= strtoull(optarg
, &sp
, 10);
96 if (!sp
|| sp
== optarg
) {
97 printf(_("invalid source offset -- %s\n"), sp
);
102 dst
= strtoull(optarg
, &sp
, 10);
103 if (!sp
|| sp
== optarg
) {
104 printf(_("invalid destination offset -- %s\n"), sp
);
109 len
= strtoull(optarg
, &sp
, 10);
110 if (!sp
|| sp
== optarg
) {
111 printf(_("invalid length -- %s\n"), sp
);
118 if (optind
!= argc
- 1)
119 return command_usage(©_range_cmd
);
121 fd
= openfile(argv
[optind
], NULL
, IO_READONLY
, 0);
125 if (src
== 0 && dst
== 0 && len
== 0) {
126 len
= copy_src_filesize(fd
);
130 ret
= copy_file_range(fd
, &src
, &dst
, len
);
136 copy_range_init(void)
138 copy_range_cmd
.name
= "copy_range";
139 copy_range_cmd
.cfunc
= copy_range_f
;
140 copy_range_cmd
.argmin
= 1;
141 copy_range_cmd
.argmax
= 7;
142 copy_range_cmd
.flags
= CMD_NOMAP_OK
| CMD_FOREIGN_OK
;
143 copy_range_cmd
.args
= _("[-s src_off] [-d dst_off] [-l len] src_file");
144 copy_range_cmd
.oneline
= _("Copy a range of data between two files");
145 copy_range_cmd
.help
= copy_range_help
;
147 add_command(©_range_cmd
);