]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - io/sendfile.c
xfsprogs: document environment variables
[thirdparty/xfsprogs-dev.git] / io / sendfile.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2004-2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
5 */
6
7 #include "command.h"
8 #include "input.h"
9 #include <sys/sendfile.h>
10 #include "init.h"
11 #include "io.h"
12
13 static cmdinfo_t sendfile_cmd;
14
15 static void
16 sendfile_help(void)
17 {
18 printf(_(
19 "\n"
20 " transfer a range of bytes from the given offset between files\n"
21 "\n"
22 " Example:\n"
23 " 'send -f 2 512 20' - writes 20 bytes at 512 bytes into the open file\n"
24 "\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"
27 " from user space.\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"
31 "\n"));
32 }
33
34 static int
35 send_buffer(
36 off64_t offset,
37 size_t count,
38 int fd,
39 long long *total)
40 {
41 off64_t off = offset;
42 ssize_t bytes, bytes_remaining = count;
43 int ops = 0;
44
45 *total = 0;
46 while (count > 0) {
47 bytes = sendfile(file->fd, fd, &off, bytes_remaining);
48 if (bytes == 0)
49 break;
50 if (bytes < 0) {
51 perror("sendfile");
52 return -1;
53 }
54 ops++;
55 *total += bytes;
56 if (bytes >= bytes_remaining)
57 break;
58 bytes_remaining -= bytes;
59 }
60 return ops;
61 }
62
63 static int
64 sendfile_f(
65 int argc,
66 char **argv)
67 {
68 off64_t offset = 0;
69 long long count, total;
70 size_t blocksize, sectsize;
71 struct timeval t1, t2;
72 char *infile = NULL;
73 int Cflag, qflag;
74 int c, fd = -1;
75
76 Cflag = qflag = 0;
77 init_cvtnum(&blocksize, &sectsize);
78 while ((c = getopt(argc, argv, "Cf:i:q")) != EOF) {
79 switch (c) {
80 case 'C':
81 Cflag = 1;
82 break;
83 case 'q':
84 qflag = 1;
85 break;
86 case 'f':
87 fd = atoi(argv[1]);
88 if (fd < 0 || fd >= filecount) {
89 printf(_("value %d is out of range (0-%d)\n"),
90 fd, filecount-1);
91 return 0;
92 }
93 break;
94 case 'i':
95 infile = optarg;
96 break;
97 default:
98 return command_usage(&sendfile_cmd);
99 }
100 }
101 if (infile && fd != -1)
102 return command_usage(&sendfile_cmd);
103
104 if (!infile)
105 fd = filetable[fd].fd;
106 else if ((fd = openfile(infile, NULL, IO_READONLY, 0, NULL)) < 0)
107 return 0;
108
109 if (optind == argc - 2) {
110 offset = cvtnum(blocksize, sectsize, argv[optind]);
111 if (offset < 0) {
112 printf(_("non-numeric offset argument -- %s\n"),
113 argv[optind]);
114 goto done;
115 }
116 optind++;
117 count = cvtnum(blocksize, sectsize, argv[optind]);
118 if (count < 0) {
119 printf(_("non-numeric length argument -- %s\n"),
120 argv[optind]);
121 goto done;
122 }
123 } else {
124 struct stat stat;
125
126 if (fstat(fd, &stat) < 0) {
127 perror("fstat");
128 goto done;
129 }
130 count = stat.st_size;
131 }
132
133 gettimeofday(&t1, NULL);
134 c = send_buffer(offset, count, fd, &total);
135 if (c < 0)
136 goto done;
137 if (qflag)
138 goto done;
139 gettimeofday(&t2, NULL);
140 t2 = tsub(t2, t1);
141
142 report_io_times("sent", &t2, (long long)offset, count, total, c, Cflag);
143 done:
144 if (infile)
145 close(fd);
146 return 0;
147 }
148
149 void
150 sendfile_init(void)
151 {
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;
158 sendfile_cmd.args =
159 _("-i infile | -f N [off len]");
160 sendfile_cmd.oneline =
161 _("Transfer data directly between file descriptors");
162 sendfile_cmd.help = sendfile_help;
163
164 add_command(&sendfile_cmd);
165 }