tur-rs 0.9.2

A relentless, high-concurrency download manager built for speed and efficiency. Tur uses dynamic work-stealing and aligned storage to saturate your bandwidth while maintaining a minuscule memory footprint. Inspired by the legends, built for the modern Rust ecosystem.
Documentation
use super::*;

use anyhow::anyhow;

pub const DIRECT_IO_ALIGNMENT: usize = 4096;

pub fn prepare_download_file(file: &std::fs::File, total_size: u64) -> Result<()> {
    let _ = (file, total_size);
    Ok(())
}

pub async fn open_download_file_for_write(
    path: &Path,
    _config: &StorageConfig,
) -> Result<DownloadFile> {
    use std::os::unix::fs::OpenOptionsExt;

    let path = path.to_path_buf();
    let file = tokio::task::spawn_blocking(move || {
        let file = std::fs::OpenOptions::new()
            .write(true)
            .custom_flags(0)
            .open(&path)
            .map_err(|e| anyhow!("failed to open file for macos pwrite: {e}"))?;

        rustix::fs::fcntl_nocache(&file, true)?;
        Ok::<_, anyhow::Error>(file)
    })
    .await
    .map_err(|e| anyhow!("spawn_blocking failed: {e}"))??;

    Ok(DownloadFile {
        inner: DownloadFileInner::MacosPwrite(file),
        backend: StorageBackendKind::MacosPwrite,
    })
}

pub async fn write_all_at_tokio(file: &mut File, offset: u64, data: &[u8]) -> Result<()> {
    file.seek(SeekFrom::Start(offset)).await?;
    file.write_all(data).await?;
    Ok(())
}

pub async fn write_all_at_pwrite(file: &mut std::fs::File, offset: u64, data: &[u8]) -> Result<()> {
    use std::os::unix::fs::FileExt;

    let data = data.to_vec();
    let cloned = file.try_clone()?;

    tokio::task::spawn_blocking(move || {
        cloned.write_all_at(&data, offset)?;
        Ok::<_, anyhow::Error>(())
    })
    .await
    .map_err(|e| anyhow::anyhow!("spawn_blocking failed: {e}"))??;

    Ok(())
}