Related bug: copy_file_range return "Invalid argument" when copy in the same file https://bugzilla.kernel.org/show_bug.cgi?id=202935 if argument of option -f is "-", use current file->fd as fd_in Usage: xfs_io -c 'copy_range -f -' some_file Signed-off-by: Jianhong Yin <yin-jianhong@xxxxxxx> --- io/copy_file_range.c | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/io/copy_file_range.c b/io/copy_file_range.c index b7b9fd88..2dde8a31 100644 --- a/io/copy_file_range.c +++ b/io/copy_file_range.c @@ -28,6 +28,7 @@ copy_range_help(void) at position 0\n\ 'copy_range -f 2' - copies all bytes from open file 2 into the current open file\n\ at position 0\n\ + 'copy_range -f -' - copies all bytes from current open file append the current open file\n\ ")); } @@ -114,11 +115,15 @@ copy_range_f(int argc, char **argv) } break; case 'f': - src_file_nr = atoi(argv[1]); - if (src_file_nr < 0 || src_file_nr >= filecount) { - printf(_("file value %d is out of range (0-%d)\n"), - src_file_nr, filecount - 1); - return 0; + if (strcmp(argv[1], "-")) + src_file_nr = (file - &filetable[0]) / sizeof(fileio_t); + else { + src_file_nr = atoi(argv[1]); + if (src_file_nr < 0 || src_file_nr >= filecount) { + printf(_("file value %d is out of range (0-%d)\n"), + src_file_nr, filecount - 1); + return 0; + } } /* Expect no src_path arg */ src_path_arg = 0; @@ -147,10 +152,14 @@ copy_range_f(int argc, char **argv) } len = sz; - ret = copy_dst_truncate(); - if (ret < 0) { - ret = 1; - goto out; + if (fd != file->fd) { + ret = copy_dst_truncate(); + if (ret < 0) { + ret = 1; + goto out; + } + } else { + dst = sz; } } -- 2.17.2