Skip to main content

splice_transfer

Function splice_transfer 

Source
pub fn splice_transfer(
    fd_in: RawFd,
    off_in: Option<i64>,
    fd_out: RawFd,
    off_out: Option<i64>,
    len: usize,
) -> Result<usize>
Expand description

Transfer up to len bytes from fd_in to fd_out using splice(2).

A temporary kernel pipe is created and used as an intermediate buffer: each round performs splice(fd_in -> pipe) followed by a fully draining splice(pipe -> fd_out). This keeps the entire byte path inside the kernel — no user-space buffer is ever touched.

§Arguments

  • fd_in - Source file descriptor (e.g. a TCP socket). Must be seekable or a stream; if off_in is None, the fd’s current offset is used and advanced by the kernel.
  • off_in - Starting offset in the source. None uses / advances the fd’s own file offset. When Some, the kernel updates the offset in place.
  • fd_out - Destination file descriptor (must be a regular file or a pipe — splice cannot target a socket with this helper).
  • off_out- Starting offset in the destination, semantics mirror off_in.
  • len - Maximum number of bytes to transfer.

§Returns

The number of bytes actually transferred. 0 indicates EOF on the source before any byte was read in the current call.

§Errors

Returns io::Error on any underlying syscall failure. Transient EINTR interruptions are retried automatically and never surface to the caller. On non-Linux platforms this always returns Unsupported.

§Examples

use std::os::fd::AsRawFd;
use aria2_core::util::zero_copy::splice_transfer;

let src = std::fs::File::open("in.bin")?;
let dst = std::fs::OpenOptions::new().write(true).create(true)
    .truncate(true).open("out.bin")?;
let n = splice_transfer(src.as_raw_fd(), None, dst.as_raw_fd(), None, 1 << 20)?;
println!("transferred {n} bytes");