1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
/*
* Copyright (c) 2016 Netapp, Inc. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <sys/syscall.h>
#include <sys/uio.h>
#include <xfs/xfs.h>
#include "command.h"
#include "input.h"
#include "init.h"
#include "io.h"
static cmdinfo_t copy_range_cmd;
static void
copy_range_help(void)
{
printf(_("\n\
Copies a range of bytes from a file into the open file, overwriting any data\n\
already there.\n\
\n\
Example:\n\
'copy_range -s 100 -d 200 -l 300 some_file' - copies 300 bytes from some_file\n\
at offset 100 into the open\n\
file at offset 200\n\
'copy_range some_file' - copies all bytes from some_file into the open file\n\
at position 0\n\
"));
}
static loff_t
copy_file_range(int fd, loff_t *src, loff_t *dst, size_t len)
{
loff_t ret;
do {
ret = syscall(__NR_copy_file_range, fd, src, file->fd, dst, len, 0);
if (ret == -1)
return errno;
len -= ret;
} while (len > 0);
return 0;
}
static off64_t
copy_src_filesize(int fd)
{
struct stat st;
if (fstat(fd, &st) < 0) {
perror("fstat");
return -1;
};
return st.st_size;
}
static int
copy_dst_truncate(void)
{
int ret = ftruncate(file->fd, 0);
if (ret < 0)
perror("ftruncate");
return ret;
}
static int
copy_range_f(int argc, char **argv)
{
loff_t src = 0;
loff_t dst = 0;
size_t len = 0;
char *sp;
int opt;
int ret;
int fd;
while ((opt = getopt(argc, argv, "s:d:l:")) != -1) {
switch (opt) {
case 's':
src = strtoull(optarg, &sp, 10);
if (!sp || sp == optarg) {
printf(_("invalid source offset -- %s\n"), sp);
return 0;
}
break;
case 'd':
dst = strtoull(optarg, &sp, 10);
if (!sp || sp == optarg) {
printf(_("invalid destination offset -- %s\n"), sp);
return 0;
}
break;
case 'l':
len = strtoull(optarg, &sp, 10);
if (!sp || sp == optarg) {
printf(_("invalid length -- %s\n"), sp);
return 0;
}
break;
}
}
if (optind != argc - 1)
return command_usage(©_range_cmd);
fd = openfile(argv[optind], NULL, IO_READONLY, 0);
if (fd < 0)
return 0;
if (src == 0 && dst == 0 && len == 0) {
len = copy_src_filesize(fd);
copy_dst_truncate();
}
ret = copy_file_range(fd, &src, &dst, len);
close(fd);
return ret;
}
void
copy_range_init(void)
{
copy_range_cmd.name = "copy_range";
copy_range_cmd.cfunc = copy_range_f;
copy_range_cmd.argmin = 1;
copy_range_cmd.argmax = 7;
copy_range_cmd.flags = CMD_NOMAP_OK | CMD_FOREIGN_OK;
copy_range_cmd.args = _("[-s src_off] [-d dst_off] [-l len] src_file");
copy_range_cmd.oneline = _("Copy a range of data between two files");
copy_range_cmd.help = copy_range_help;
add_command(©_range_cmd);
}
|