]>
git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - io/sendfile.c
1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2004-2005 Silicon Graphics, Inc.
9 #include <sys/sendfile.h>
13 static cmdinfo_t sendfile_cmd
;
20 " transfer a range of bytes from the given offset between files\n"
23 " 'send -f 2 512 20' - writes 20 bytes at 512 bytes into the open file\n"
25 " Copies data between one file descriptor and another. Because this copying\n"
26 " is done within the kernel, sendfile does not need to transfer data to and\n"
28 " -f -- specifies an input file from which to source data to write\n"
29 " -i -- specifies an input file name from which to source data to write.\n"
30 " An offset and length in the source file can be optionally specified.\n"
42 ssize_t bytes
, bytes_remaining
= count
;
47 bytes
= sendfile(file
->fd
, fd
, &off
, bytes_remaining
);
56 if (bytes
>= bytes_remaining
)
58 bytes_remaining
-= bytes
;
69 long long count
, total
;
70 size_t blocksize
, sectsize
;
71 struct timeval t1
, t2
;
77 init_cvtnum(&blocksize
, §size
);
78 while ((c
= getopt(argc
, argv
, "Cf:i:q")) != EOF
) {
88 if (fd
< 0 || fd
>= filecount
) {
89 printf(_("value %d is out of range (0-%d)\n"),
98 return command_usage(&sendfile_cmd
);
101 if (infile
&& fd
!= -1)
102 return command_usage(&sendfile_cmd
);
105 fd
= filetable
[fd
].fd
;
106 else if ((fd
= openfile(infile
, NULL
, IO_READONLY
, 0, NULL
)) < 0)
109 if (optind
== argc
- 2) {
110 offset
= cvtnum(blocksize
, sectsize
, argv
[optind
]);
112 printf(_("non-numeric offset argument -- %s\n"),
117 count
= cvtnum(blocksize
, sectsize
, argv
[optind
]);
119 printf(_("non-numeric length argument -- %s\n"),
126 if (fstat(fd
, &stat
) < 0) {
130 count
= stat
.st_size
;
133 gettimeofday(&t1
, NULL
);
134 c
= send_buffer(offset
, count
, fd
, &total
);
139 gettimeofday(&t2
, NULL
);
142 report_io_times("sent", &t2
, (long long)offset
, count
, total
, c
, Cflag
);
152 sendfile_cmd
.name
= "sendfile";
153 sendfile_cmd
.altname
= "send";
154 sendfile_cmd
.cfunc
= sendfile_f
;
155 sendfile_cmd
.argmin
= 2;
156 sendfile_cmd
.argmax
= -1;
157 sendfile_cmd
.flags
= CMD_NOMAP_OK
| CMD_FOREIGN_OK
;
159 _("-i infile | -f N [off len]");
160 sendfile_cmd
.oneline
=
161 _("Transfer data directly between file descriptors");
162 sendfile_cmd
.help
= sendfile_help
;
164 add_command(&sendfile_cmd
);