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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
use cfg_if::cfg_if;
use std::os::unix::io::RawFd;
use std::ptr;
use libc::{self, off_t};
use crate::Result;
use crate::errno::Errno;
#[cfg(any(target_os = "android", target_os = "linux"))]
pub fn sendfile(
out_fd: RawFd,
in_fd: RawFd,
offset: Option<&mut off_t>,
count: usize,
) -> Result<usize> {
let offset = offset
.map(|offset| offset as *mut _)
.unwrap_or(ptr::null_mut());
let ret = unsafe { libc::sendfile(out_fd, in_fd, offset, count) };
Errno::result(ret).map(|r| r as usize)
}
cfg_if! {
if #[cfg(any(target_os = "freebsd",
target_os = "ios",
target_os = "macos"))] {
use crate::sys::uio::IoVec;
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
struct SendfileHeaderTrailer<'a>(
libc::sf_hdtr,
Option<Vec<IoVec<&'a [u8]>>>,
Option<Vec<IoVec<&'a [u8]>>>,
);
impl<'a> SendfileHeaderTrailer<'a> {
fn new(
headers: Option<&'a [&'a [u8]]>,
trailers: Option<&'a [&'a [u8]]>
) -> SendfileHeaderTrailer<'a> {
let header_iovecs: Option<Vec<IoVec<&[u8]>>> =
headers.map(|s| s.iter().map(|b| IoVec::from_slice(b)).collect());
let trailer_iovecs: Option<Vec<IoVec<&[u8]>>> =
trailers.map(|s| s.iter().map(|b| IoVec::from_slice(b)).collect());
SendfileHeaderTrailer(
libc::sf_hdtr {
headers: {
header_iovecs
.as_ref()
.map_or(ptr::null(), |v| v.as_ptr()) as *mut libc::iovec
},
hdr_cnt: header_iovecs.as_ref().map(|v| v.len()).unwrap_or(0) as i32,
trailers: {
trailer_iovecs
.as_ref()
.map_or(ptr::null(), |v| v.as_ptr()) as *mut libc::iovec
},
trl_cnt: trailer_iovecs.as_ref().map(|v| v.len()).unwrap_or(0) as i32
},
header_iovecs,
trailer_iovecs,
)
}
}
}
}
cfg_if! {
if #[cfg(target_os = "freebsd")] {
use libc::c_int;
libc_bitflags!{
pub struct SfFlags: c_int {
SF_NODISKIO;
SF_SYNC;
SF_USER_READAHEAD;
SF_NOCACHE;
}
}
#[allow(clippy::too_many_arguments)]
pub fn sendfile(
in_fd: RawFd,
out_sock: RawFd,
offset: off_t,
count: Option<usize>,
headers: Option<&[&[u8]]>,
trailers: Option<&[&[u8]]>,
flags: SfFlags,
readahead: u16
) -> (Result<()>, off_t) {
let ra32 = u32::from(readahead);
let flags: u32 = (ra32 << 16) | (flags.bits() as u32);
let mut bytes_sent: off_t = 0;
let hdtr = headers.or(trailers).map(|_| SendfileHeaderTrailer::new(headers, trailers));
let hdtr_ptr = hdtr.as_ref().map_or(ptr::null(), |s| &s.0 as *const libc::sf_hdtr);
let return_code = unsafe {
libc::sendfile(in_fd,
out_sock,
offset,
count.unwrap_or(0),
hdtr_ptr as *mut libc::sf_hdtr,
&mut bytes_sent as *mut off_t,
flags as c_int)
};
(Errno::result(return_code).and(Ok(())), bytes_sent)
}
} else if #[cfg(any(target_os = "ios", target_os = "macos"))] {
pub fn sendfile(
in_fd: RawFd,
out_sock: RawFd,
offset: off_t,
count: Option<off_t>,
headers: Option<&[&[u8]]>,
trailers: Option<&[&[u8]]>
) -> (Result<()>, off_t) {
let mut len = count.unwrap_or(0);
let hdtr = headers.or(trailers).map(|_| SendfileHeaderTrailer::new(headers, trailers));
let hdtr_ptr = hdtr.as_ref().map_or(ptr::null(), |s| &s.0 as *const libc::sf_hdtr);
let return_code = unsafe {
libc::sendfile(in_fd,
out_sock,
offset,
&mut len as *mut off_t,
hdtr_ptr as *mut libc::sf_hdtr,
0)
};
(Errno::result(return_code).and(Ok(())), len)
}
}
}