]> git.proxmox.com Git - proxmox-backup.git/blob - pbs-tools/src/io.rs
move remaining client tools to pbs-tools/datastore
[proxmox-backup.git] / pbs-tools / src / io.rs
1 //! I/O utilities.
2
3 use anyhow::Error;
4
5 use proxmox::tools::fd::Fd;
6
7 /// The `BufferedRead` trait provides a single function
8 /// `buffered_read`. It returns a reference to an internal buffer. The
9 /// purpose of this traid is to avoid unnecessary data copies.
10 pub trait BufferedRead {
11 /// This functions tries to fill the internal buffers, then
12 /// returns a reference to the available data. It returns an empty
13 /// buffer if `offset` points to the end of the file.
14 fn buffered_read(&mut self, offset: u64) -> Result<&[u8], Error>;
15 }
16
17 /// safe wrapper for `nix::unistd::pipe2` defaulting to `O_CLOEXEC` and guarding the file
18 /// descriptors.
19 pub fn pipe() -> Result<(Fd, Fd), Error> {
20 let (pin, pout) = nix::unistd::pipe2(nix::fcntl::OFlag::O_CLOEXEC)?;
21 Ok((Fd(pin), Fd(pout)))
22 }