1b8a62b91Sopenharmony_ci#![allow(unsafe_code)]
2b8a62b91Sopenharmony_ci
3b8a62b91Sopenharmony_ciuse crate::fd::OwnedFd;
4b8a62b91Sopenharmony_ciuse crate::{backend, io};
5b8a62b91Sopenharmony_ci#[cfg(any(target_os = "android", target_os = "linux"))]
6b8a62b91Sopenharmony_ciuse backend::fd::AsFd;
7b8a62b91Sopenharmony_ci
8b8a62b91Sopenharmony_ci#[cfg(not(any(target_os = "ios", target_os = "macos")))]
9b8a62b91Sopenharmony_cipub use backend::io::types::PipeFlags;
10b8a62b91Sopenharmony_ci
11b8a62b91Sopenharmony_ci#[cfg(any(target_os = "android", target_os = "linux"))]
12b8a62b91Sopenharmony_cipub use backend::io::types::{IoSliceRaw, SpliceFlags};
13b8a62b91Sopenharmony_ci
14b8a62b91Sopenharmony_ci/// `PIPE_BUF`—The maximum length at which writes to a pipe are atomic.
15b8a62b91Sopenharmony_ci///
16b8a62b91Sopenharmony_ci/// # References
17b8a62b91Sopenharmony_ci///  - [Linux]
18b8a62b91Sopenharmony_ci///  - [POSIX]
19b8a62b91Sopenharmony_ci///
20b8a62b91Sopenharmony_ci/// [Linux]: https://man7.org/linux/man-pages/man7/pipe.7.html
21b8a62b91Sopenharmony_ci/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/write.html
22b8a62b91Sopenharmony_ci#[cfg(not(any(
23b8a62b91Sopenharmony_ci    windows,
24b8a62b91Sopenharmony_ci    target_os = "haiku",
25b8a62b91Sopenharmony_ci    target_os = "illumos",
26b8a62b91Sopenharmony_ci    target_os = "redox",
27b8a62b91Sopenharmony_ci    target_os = "solaris",
28b8a62b91Sopenharmony_ci    target_os = "wasi",
29b8a62b91Sopenharmony_ci)))]
30b8a62b91Sopenharmony_cipub const PIPE_BUF: usize = backend::io::types::PIPE_BUF;
31b8a62b91Sopenharmony_ci
32b8a62b91Sopenharmony_ci/// `pipe()`—Creates a pipe.
33b8a62b91Sopenharmony_ci///
34b8a62b91Sopenharmony_ci/// This function creates a pipe and returns two file descriptors, for the
35b8a62b91Sopenharmony_ci/// reading and writing ends of the pipe, respectively.
36b8a62b91Sopenharmony_ci///
37b8a62b91Sopenharmony_ci/// # References
38b8a62b91Sopenharmony_ci///  - [POSIX]
39b8a62b91Sopenharmony_ci///  - [Linux]
40b8a62b91Sopenharmony_ci///
41b8a62b91Sopenharmony_ci/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/pipe.html
42b8a62b91Sopenharmony_ci/// [Linux]: https://man7.org/linux/man-pages/man2/pipe.2.html
43b8a62b91Sopenharmony_ci#[inline]
44b8a62b91Sopenharmony_cipub fn pipe() -> io::Result<(OwnedFd, OwnedFd)> {
45b8a62b91Sopenharmony_ci    backend::io::syscalls::pipe()
46b8a62b91Sopenharmony_ci}
47b8a62b91Sopenharmony_ci
48b8a62b91Sopenharmony_ci/// `pipe2(flags)`—Creates a pipe, with flags.
49b8a62b91Sopenharmony_ci///
50b8a62b91Sopenharmony_ci/// This function creates a pipe and returns two file descriptors, for the
51b8a62b91Sopenharmony_ci/// reading and writing ends of the pipe, respectively.
52b8a62b91Sopenharmony_ci///
53b8a62b91Sopenharmony_ci/// # References
54b8a62b91Sopenharmony_ci///  - [Linux]
55b8a62b91Sopenharmony_ci///
56b8a62b91Sopenharmony_ci/// [Linux]: https://man7.org/linux/man-pages/man2/pipe2.2.html
57b8a62b91Sopenharmony_ci#[cfg(not(any(
58b8a62b91Sopenharmony_ci    target_os = "aix",
59b8a62b91Sopenharmony_ci    target_os = "haiku",
60b8a62b91Sopenharmony_ci    target_os = "ios",
61b8a62b91Sopenharmony_ci    target_os = "macos"
62b8a62b91Sopenharmony_ci)))]
63b8a62b91Sopenharmony_ci#[inline]
64b8a62b91Sopenharmony_ci#[doc(alias = "pipe2")]
65b8a62b91Sopenharmony_cipub fn pipe_with(flags: PipeFlags) -> io::Result<(OwnedFd, OwnedFd)> {
66b8a62b91Sopenharmony_ci    backend::io::syscalls::pipe_with(flags)
67b8a62b91Sopenharmony_ci}
68b8a62b91Sopenharmony_ci
69b8a62b91Sopenharmony_ci/// `splice(fd_in, off_in, fd_out, off_out, len, flags)`—Transfer data between a file and a pipe.
70b8a62b91Sopenharmony_ci///
71b8a62b91Sopenharmony_ci/// This function transfers up to `len` bytes of data from the file descriptor `fd_in`
72b8a62b91Sopenharmony_ci/// to the file descriptor `fd_out`, where one of the file descriptors
73b8a62b91Sopenharmony_ci/// must refer to a pipe.
74b8a62b91Sopenharmony_ci///
75b8a62b91Sopenharmony_ci/// `off_*` must be `None` if the corresponding fd refers to a pipe.
76b8a62b91Sopenharmony_ci/// Otherwise its value points to the starting offset to the file,
77b8a62b91Sopenharmony_ci/// from which the data is read/written.
78b8a62b91Sopenharmony_ci/// on success the number of bytes read/written is added to the offset.
79b8a62b91Sopenharmony_ci///
80b8a62b91Sopenharmony_ci/// passing `None` causes the read/write to start from the file offset,
81b8a62b91Sopenharmony_ci/// and the file offset is adjusted appropriately.
82b8a62b91Sopenharmony_ci///
83b8a62b91Sopenharmony_ci/// # References
84b8a62b91Sopenharmony_ci///  - [Linux]
85b8a62b91Sopenharmony_ci///
86b8a62b91Sopenharmony_ci/// [Linux]: https://man7.org/linux/man-pages/man2/splice.2.html
87b8a62b91Sopenharmony_ci#[cfg(any(target_os = "android", target_os = "linux"))]
88b8a62b91Sopenharmony_ci#[inline]
89b8a62b91Sopenharmony_cipub fn splice<FdIn: AsFd, FdOut: AsFd>(
90b8a62b91Sopenharmony_ci    fd_in: FdIn,
91b8a62b91Sopenharmony_ci    off_in: Option<&mut u64>,
92b8a62b91Sopenharmony_ci    fd_out: FdOut,
93b8a62b91Sopenharmony_ci    off_out: Option<&mut u64>,
94b8a62b91Sopenharmony_ci    len: usize,
95b8a62b91Sopenharmony_ci    flags: SpliceFlags,
96b8a62b91Sopenharmony_ci) -> io::Result<usize> {
97b8a62b91Sopenharmony_ci    backend::io::syscalls::splice(fd_in.as_fd(), off_in, fd_out.as_fd(), off_out, len, flags)
98b8a62b91Sopenharmony_ci}
99b8a62b91Sopenharmony_ci
100b8a62b91Sopenharmony_ci/// `vmsplice(fd, bufs, flags)`—Transfer data between memory and a pipe.
101b8a62b91Sopenharmony_ci///
102b8a62b91Sopenharmony_ci/// If `fd` is the write end of the pipe,
103b8a62b91Sopenharmony_ci/// the function maps the memory pointer at by `bufs` to the pipe.
104b8a62b91Sopenharmony_ci///
105b8a62b91Sopenharmony_ci/// If `fd` is the read end of the pipe,
106b8a62b91Sopenharmony_ci/// the function writes data from the pipe to said memory.
107b8a62b91Sopenharmony_ci///
108b8a62b91Sopenharmony_ci/// # Safety
109b8a62b91Sopenharmony_ci///
110b8a62b91Sopenharmony_ci/// If the memory must not be mutated (such as when `bufs` were originally immutable slices),
111b8a62b91Sopenharmony_ci/// it is up to the caller to ensure that the write end of the pipe is placed in `fd`.
112b8a62b91Sopenharmony_ci///
113b8a62b91Sopenharmony_ci/// Additionally if `SpliceFlags::GIFT` is set, the caller must also ensure
114b8a62b91Sopenharmony_ci/// that the contents of `bufs` in never modified following the call,
115b8a62b91Sopenharmony_ci/// and that all of the pointers in `bufs` are page aligned,
116b8a62b91Sopenharmony_ci/// and the lengths are multiples of a page size in bytes.
117b8a62b91Sopenharmony_ci///
118b8a62b91Sopenharmony_ci/// # References
119b8a62b91Sopenharmony_ci///  - [Linux]
120b8a62b91Sopenharmony_ci///
121b8a62b91Sopenharmony_ci/// [Linux]: https://man7.org/linux/man-pages/man2/vmsplice.2.html
122b8a62b91Sopenharmony_ci#[cfg(any(target_os = "android", target_os = "linux"))]
123b8a62b91Sopenharmony_ci#[inline]
124b8a62b91Sopenharmony_cipub unsafe fn vmsplice<PipeFd: AsFd>(
125b8a62b91Sopenharmony_ci    fd: PipeFd,
126b8a62b91Sopenharmony_ci    bufs: &[io::IoSliceRaw],
127b8a62b91Sopenharmony_ci    flags: SpliceFlags,
128b8a62b91Sopenharmony_ci) -> io::Result<usize> {
129b8a62b91Sopenharmony_ci    backend::io::syscalls::vmsplice(fd.as_fd(), bufs, flags)
130b8a62b91Sopenharmony_ci}
131