read_range 0.2.1

A portable and efficient crate for reading a specific range of bytes from a file. Provides both sync/async APIs and uses concurrent-safe positional I/O.
Documentation
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
//! Provides the core implementation for reading specific byte ranges from files.
//!
//! On supported platforms (Unix-like, Windows), this module leverages efficient,
//! concurrent-safe positional I/O (Unix: `pread`, Windows: `FileExt::seek_read`).
//! For other targets, it uses a portable `seek` and `read` fallback.
//!
//! The public API includes synchronous and asynchronous functions, each with a
//! corresponding `_with_progress` variant for monitoring long-running reads.

use std::{io, path::Path};

use super::Progress;

/// Handles the result of a `spawn_blocking` call that returns an `io::Result`.
///
/// This helper correctly propagates panics from the background task, ensuring that
/// critical failures like out-of-memory are not hidden. It also gracefully handles
/// task cancellations by converting them into an `io::Error` of kind `Interrupted`,
/// which is the standard behavior for async operations that are cancelled.
#[cfg(all(any(unix, windows), feature = "async"))]
fn handle_blocking_io_task_result<T>(
    result: Result<io::Result<T>, tokio::task::JoinError>,
) -> io::Result<T> {
    match result {
        Ok(inner_result) => inner_result,
        Err(e) => {
            if e.is_panic() {
                // The blocking task panicked. Propagate the panic to the caller.
                std::panic::resume_unwind(e.into_panic());
            } else {
                // The task was cancelled. This is a normal event in async,
                // not a bug. We translate it into an error.
                Err(io::Error::new(
                    io::ErrorKind::Interrupted,
                    format!("blocking task was cancelled: {e}"),
                ))
            }
        }
    }
}

/// Reads a specific range of bytes from a file asynchronously.
///
/// This function provides a portable and efficient way to read a segment of a file.
/// On supported platforms (Unix, Windows), it uses the most efficient underlying
/// OS syscall (`pread` or `ReadFileScatter`) by running the blocking
/// `std::fs::File::read_at` call on Tokio's blocking thread pool. This avoids
/// mutating the file handle's cursor, making it safe for concurrent use.
///
/// For other platforms, it falls back to using Tokio's async-native seek-and-read
/// operations.
///
/// # Parameters
///
/// * `path`: The path to the file to read from.
/// * `offset`: The starting position (in bytes) from the beginning of the file.
/// * `len`: The number of bytes to read.
///
/// # Returns
///
/// A `Result` containing a `Vec<u8>` with the bytes read. The vector's length
/// will be equal to `len` unless the read operation reached the end of the file.
///
/// # Errors
///
/// This function will return an `io::Error` if:
/// - The file cannot be opened or the underlying read operation fails.
/// - The blocking I/O task is cancelled.
///
/// # Panics
///
/// This function will panic if the blocking task responsible for file I/O panics
/// (e.g., due to an out-of-memory error when allocating the read buffer).
#[cfg(feature = "async")]
pub async fn async_read_byte_range(
    path: impl AsRef<Path>,
    offset: u64,
    len: usize,
) -> io::Result<Vec<u8>> {
    if len == 0 {
        return Ok(Vec::new());
    }
    #[cfg(any(unix, windows))]
    {
        let path_buf = path.as_ref().to_path_buf();
        let result = tokio::task::spawn_blocking(move || {
            read_at_internal(path_buf, offset, len as u64, None::<&dyn Progress>)
        })
        .await;
        handle_blocking_io_task_result(result)
    }

    #[cfg(not(any(unix, windows)))]
    {
        seek_read_async_internal(path, offset, len as u64, None::<&dyn Progress>).await
    }
}

/// Reads a specific range of bytes from a file asynchronously with progress reporting.
///
/// This function provides a portable and efficient way to read a segment of a file
/// while reporting progress. To display progress, the file is read in chunks.
///
/// On supported platforms (Unix, Windows), it uses efficient `read_at` syscalls
/// on Tokio's blocking thread pool. For other platforms, it falls back to
/// Tokio's async-native seek-and-read operations.
///
/// # Parameters
///
/// * `path`: The path to the file to read from.
/// * `offset`: The starting position (in bytes) from the beginning of the file.
/// * `len`: The total number of bytes to read.
/// * `pb`: A progress tracking structure.
///
/// # Returns
///
/// A `Result` containing a `Vec<u8>` with the bytes read. The vector's length
/// will be equal to `len` unless the read operation reached the end of the file.
///
/// # Errors
///
/// This function will return an `io::Error` if:
/// - The file cannot be opened or the underlying read operation fails.
/// - `len` is too large to fit in memory (`> usize::MAX`).
/// - The sum of `offset` and `len` overflows a `u64`.
/// - The blocking I/O task is cancelled.
///
/// # Panics
///
/// This function will panic if the blocking task responsible for file I/O panics
/// (e.g., due to an out-of-memory error when allocating the read buffer).
#[cfg(feature = "async")]
pub async fn async_read_byte_range_with_progress(
    path: impl AsRef<Path>,
    offset: u64,
    len: u64,
    pb: impl Progress + Send + 'static,
) -> io::Result<Vec<u8>> {
    if len == 0 {
        return Ok(Vec::new());
    }
    #[cfg(any(unix, windows))]
    {
        let path_buf = path.as_ref().to_path_buf();
        let result =
            tokio::task::spawn_blocking(move || read_at_internal(path_buf, offset, len, Some(&pb)))
                .await;
        handle_blocking_io_task_result(result)
    }

    #[cfg(not(any(unix, windows)))]
    {
        seek_read_async_internal(path, offset, len, Some(&pb)).await
    }
}

/// Reads a specific range of bytes from a file synchronously.
///
/// # Errors
///
/// This function will return an `io::Error` if:
/// - The file cannot be opened or the underlying read operation fails.
/// - The sum of `offset` and `len` overflows a `u64`.
pub fn read_byte_range(path: impl AsRef<Path>, offset: u64, len: usize) -> io::Result<Vec<u8>> {
    #[cfg(any(unix, windows))]
    {
        read_at_internal(path, offset, len as u64, None::<&dyn Progress>)
    }

    #[cfg(not(any(unix, windows)))]
    {
        seek_read_blocking_internal(path, offset, len as u64, None::<&dyn Progress>)
    }
}

/// Reads a specific range of bytes from a file synchronously with progress reporting.
///
/// # Errors
///
/// This function will return an `io::Error` if:
/// - The file cannot be opened or the underlying read operation fails.
/// - `len` is too large to fit in memory (`> usize::MAX`).
/// - The sum of `offset` and `len` overflows a `u64`.
pub fn read_byte_range_with_progress(
    path: impl AsRef<Path>,
    offset: u64,
    len: u64,
    pb: &impl Progress,
) -> io::Result<Vec<u8>> {
    #[cfg(any(unix, windows))]
    {
        read_at_internal(path, offset, len, Some(pb))
    }

    #[cfg(not(any(unix, windows)))]
    {
        seek_read_blocking_internal(path, offset, len, Some(pb))
    }
}

//================================================================================
// Internal Implementation Details
//================================================================================

/// Checks if a u64 length can safely be converted to usize for buffer allocation.
#[inline]
fn validate_len_for_buffer(len: u64) -> io::Result<usize> {
    len.try_into().map_err(|_| {
        io::Error::new(
            io::ErrorKind::InvalidInput,
            "length does not fit in memory buffer (usize)",
        )
    })
}

/// Internal implementation using positional reads for POSIX-compliant systems and Windows.
#[cfg(any(unix, windows))]
fn read_at_internal(
    path: impl AsRef<Path>,
    offset: u64,
    len: u64,
    pb: Option<&(impl Progress + ?Sized)>,
) -> io::Result<Vec<u8>> {
    /// Compatibility helper for `read_at` on unix and `seek_read` on windows.
    #[inline]
    fn read_at_compat(file: &std::fs::File, buf: &mut [u8], offset: u64) -> io::Result<usize> {
        #[cfg(unix)]
        {
            use std::os::unix::fs::FileExt as _;
            file.read_at(buf, offset)
        }
        #[cfg(windows)]
        {
            use std::os::windows::fs::FileExt as _;
            file.seek_read(buf, offset)
        }
    }

    if len == 0 {
        if let Some(pb) = pb {
            pb.finish();
        }
        return Ok(Vec::new());
    }

    let file = std::fs::File::open(path.as_ref())?;

    // Prevent silent data corruption from integer overflow when calculating read offset.
    if offset.checked_add(len).is_none() {
        return Err(io::Error::new(
            io::ErrorKind::InvalidInput,
            "read range offset + length overflows a u64",
        ));
    }

    let capacity = validate_len_for_buffer(len)?;
    let mut buffer = vec![0; capacity];

    let mut total_bytes_read = 0;
    let read_result = loop {
        if total_bytes_read >= capacity {
            break Ok(());
        }
        let current_slice = &mut buffer[total_bytes_read..];
        let current_offset = offset + total_bytes_read as u64;

        match read_at_compat(&file, current_slice, current_offset) {
            Ok(0) => break Ok(()), // End of file reached.
            Ok(bytes_read) => {
                total_bytes_read += bytes_read;
                if let Some(pb) = pb {
                    pb.inc(bytes_read as u64);
                }
            }
            Err(ref e) if e.kind() == io::ErrorKind::Interrupted => {}
            Err(e) => break Err(e),
        }
    };

    // Always finish the progress bar, even if an error occurred during read.
    if let Some(pb) = pb {
        pb.finish();
    }

    read_result?;

    buffer.truncate(total_bytes_read);
    Ok(buffer)
}

/// Macro to generate nearly identical sync and async `seek_read` functions.
#[cfg(not(any(unix, windows)))]
macro_rules! define_seek_read_internal {
    (
        $vis:vis,
        $name:ident,
        $doc:expr,
        $($async:ident)?,
        $( @$await:tt )?,
        $file:ty,
        $read_trait:path,
        $seek_trait:path
    ) => {
        #[doc = $doc]
        $vis $($async)? fn $name(
            path: impl AsRef<Path>,
            offset: u64,
            len: u64,
            pb: Option<&(impl Progress + ?Sized)>,
        ) -> io::Result<Vec<u8>> {
            use $read_trait;
            use $seek_trait;

            if len == 0 {
                if let Some(pb) = pb {
                    pb.finish();
                }
                return Ok(Vec::new());
            }

            let capacity = validate_len_for_buffer(len)?;

            let mut file = <$file>::open(path)$(.$await)? ?;

            // The seek_read fallback does not need an explicit overflow check because
            // `seek` itself will return an `InvalidInput` error on overflow.
            file.seek(io::SeekFrom::Start(offset))$(.$await)? ?;

            if let Some(pb) = pb {
                // Progress reporting path: read in chunks.
                let mut reader = file.take(len);
                let mut buffer = Vec::with_capacity(capacity);
                // 64 KiB is a common and reasonably performant chunk size for I/O.
                let mut read_buf = vec![0; READ_CHUNK.min(capacity)];

                let result = loop {
                    match reader.read(&mut read_buf)$(.$await)? {
                        Ok(0) => break Ok(buffer), // EOF
                        Ok(n) => {
                            buffer.extend_from_slice(&read_buf[..n]);
                            pb.inc(n as u64);
                        }
                        Err(ref e) if e.kind() == io::ErrorKind::Interrupted => {},
                        Err(e) => break Err(e),
                    }
                };
                // Always finish the progress bar, even if an error occurred.
                pb.finish();
                result
            } else {
                // No progress reporting: read all bytes up to `len` with EINTR retry.
                let mut reader = file.take(len);
                let mut buffer = Vec::with_capacity(capacity);
                loop {
                    match reader.read_to_end(&mut buffer)$(.$await)? {
                        Ok(_) => break Ok(buffer),
                        Err(ref e) if e.kind() == io::ErrorKind::Interrupted => {},
                        Err(e) => break Err(e),
                    }
                }
            }
        }
    };
}

#[cfg(not(any(unix, windows)))]
const READ_CHUNK: usize = 64 * 1024;

#[cfg(all(not(any(unix, windows)), feature = "async"))]
define_seek_read_internal!(
    , // private visibility
    seek_read_async_internal,
    "Internal async implementation using `seek` and `read` for other platforms.",
    async,
    @await,
    tokio::fs::File,
    tokio::io::AsyncReadExt,
    tokio::io::AsyncSeekExt
);

#[cfg(not(any(unix, windows)))]
define_seek_read_internal!(
    pub, // public visibility
    seek_read_blocking_internal,
    "Internal blocking implementation using `seek` and `read` for other platforms.",
    , // no async
    , // no await
    std::fs::File,
    std::io::Read,
    std::io::Seek
);

#[cfg(test)]
mod tests {
    use std::{
        io::Write as _,
        sync::{
            Arc,
            atomic::{AtomicBool, AtomicU64, Ordering},
        },
    };

    use tempfile::NamedTempFile;

    use super::*;

    // A mock progress tracker for testing.
    struct MockProgress {
        total: Arc<AtomicU64>,
        finished: Arc<AtomicBool>,
    }

    impl Progress for MockProgress {
        fn inc(&self, delta: u64) {
            self.total.fetch_add(delta, Ordering::SeqCst);
        }
        fn finish(&self) {
            self.finished.store(true, Ordering::SeqCst);
        }
    }

    /// Helper to create a temporary file with specific content.
    /// Returns the temp file handle (to keep it alive), its path, and the content.
    fn setup_test_file(content: &[u8]) -> (NamedTempFile, std::path::PathBuf, Vec<u8>) {
        let mut temp_file = NamedTempFile::new().unwrap();
        temp_file.write_all(content).unwrap();
        let path = temp_file.path().to_path_buf();
        (temp_file, path, content.to_vec())
    }

    #[test]
    fn test_sync_read_middle() {
        let (_file, path, content) = setup_test_file(b"abcdefghijklmnopqrstuvwxyz");
        let result = read_byte_range(&path, 5, 10).unwrap();
        assert_eq!(result, &content[5..15]);
    }

    #[cfg(feature = "async")]
    #[tokio::test]
    async fn test_async_read_middle() {
        let (_file, path, content) = setup_test_file(b"abcdefghijklmnopqrstuvwxyz");
        let result = async_read_byte_range(&path, 5, 10).await.unwrap();
        assert_eq!(result, &content[5..15]);
    }

    #[test]
    fn test_read_at_start() {
        let (_file, path, content) = setup_test_file(b"abcdefghijklmnopqrstuvwxyz");
        let result = read_byte_range(&path, 0, 5).unwrap();
        assert_eq!(result, &content[0..5]);
    }

    #[test]
    fn test_read_at_end() {
        let (_file, path, content) = setup_test_file(b"abcdefghijklmnopqrstuvwxyz");
        let result = read_byte_range(&path, 21, 5).unwrap();
        assert_eq!(result, &content[21..26]);
    }

    #[test]
    fn test_read_full_file() {
        let (_file, path, content) = setup_test_file(b"abcdefghijklmnopqrstuvwxyz");
        let result = read_byte_range(&path, 0, content.len()).unwrap();
        assert_eq!(result, content);
    }

    #[cfg(feature = "async")]
    #[tokio::test]
    async fn test_async_read_full_file() {
        let (_file, path, content) = setup_test_file(b"abcdefghijklmnopqrstuvwxyz");
        let result = async_read_byte_range(&path, 0, content.len())
            .await
            .unwrap();
        assert_eq!(result, content);
    }

    #[test]
    fn test_read_past_eof() {
        let (_file, path, content) = setup_test_file(b"short file");
        // Try to read 20 bytes from a 10-byte file.
        let result = read_byte_range(&path, 0, 20).unwrap();
        // Should return all available bytes.
        assert_eq!(result, content);
    }

    #[cfg(feature = "async")]
    #[tokio::test]
    async fn test_async_read_past_eof() {
        let (_file, path, content) = setup_test_file(b"short file");
        let result = async_read_byte_range(&path, 5, 100).await.unwrap();
        assert_eq!(result, &content[5..]);
    }

    #[test]
    fn test_zero_length_read() {
        let (_file, path, _) = setup_test_file(b"some data");
        let result = read_byte_range(&path, 5, 0).unwrap();
        assert!(result.is_empty());
    }

    #[test]
    fn test_zero_length_read_with_progress() {
        let (_file, path, _) = setup_test_file(b"abc");
        let progress = MockProgress {
            total: Arc::new(AtomicU64::new(0)),
            finished: Arc::new(AtomicBool::new(false)),
        };
        let result = read_byte_range_with_progress(&path, 0, 0, &progress).unwrap();
        assert!(result.is_empty());
        assert!(progress.finished.load(Ordering::SeqCst));
        assert_eq!(progress.total.load(Ordering::SeqCst), 0);
    }

    #[cfg(feature = "async")]
    #[tokio::test]
    async fn test_async_zero_length_with_progress() {
        let (_file, path, _) = setup_test_file(b"abc");
        let progress = MockProgress {
            total: Arc::new(AtomicU64::new(0)),
            finished: Arc::new(AtomicBool::new(false)),
        };
        let result = async_read_byte_range_with_progress(path, 0, 0, progress)
            .await
            .unwrap();
        assert!(result.is_empty());
    }

    #[test]
    fn test_sync_with_progress() {
        let (_file, path, _) = setup_test_file(&[0u8; 1000]);
        let progress = MockProgress {
            total: Arc::new(AtomicU64::new(0)),
            finished: Arc::new(AtomicBool::new(false)),
        };

        let result = read_byte_range_with_progress(&path, 100, 500, &progress).unwrap();
        assert_eq!(result.len(), 500);
        assert_eq!(progress.total.load(Ordering::SeqCst), 500);
        assert!(progress.finished.load(Ordering::SeqCst));
    }

    #[cfg(feature = "async")]
    #[tokio::test]
    async fn test_async_with_progress() {
        let (_file, path, _) = setup_test_file(&[0u8; 1000]);
        let progress = MockProgress {
            total: Arc::new(AtomicU64::new(0)),
            finished: Arc::new(AtomicBool::new(false)),
        };

        let result = async_read_byte_range_with_progress(&path, 100, 500, progress)
            .await
            .unwrap();
        assert_eq!(result.len(), 500);

        // Retrieve the atomic values from the moved struct for assertion.
        let final_total = result.len() as u64; // In this mock, it's the same.
        assert_eq!(final_total, 500);
    }

    #[test]
    fn test_file_not_found() {
        let path = Path::new("a/file/that/does/not/exist.txt");
        let result = read_byte_range(path, 0, 10);
        assert!(result.is_err());
        assert_eq!(result.unwrap_err().kind(), io::ErrorKind::NotFound);
    }

    #[cfg(feature = "async")]
    #[tokio::test]
    async fn test_async_file_not_found() {
        let path = Path::new("a/file/that/does/not/exist.txt");
        let result = async_read_byte_range(path, 0, 10).await;
        assert!(result.is_err());
        assert_eq!(result.unwrap_err().kind(), io::ErrorKind::NotFound);
    }

    // This test is only effective on platforms with positional reads.
    #[cfg(any(unix, windows))]
    #[test]
    fn test_offset_overflow() {
        let (_file, path, _) = setup_test_file(b"data");
        let offset = u64::MAX - 5;
        let len = 10;
        let result = read_byte_range(&path, offset, len);
        assert!(result.is_err());
        assert_eq!(result.unwrap_err().kind(), io::ErrorKind::InvalidInput);
    }

    #[cfg(feature = "async")]
    #[tokio::test]
    async fn test_async_cancellation_is_not_panic() {
        // This test simulates a task being cancelled.
        // Note: we abort the *outer* Tokio task, so we only assert JoinError::is_cancelled().
        // The io::ErrorKind::Interrupted mapping happens inside the aborted task and isn't observable here.

        // Allocate on the heap to avoid a stack overflow in the test.
        let large_content = vec![0u8; 1024 * 1024];
        let (_file, path, _) = setup_test_file(&large_content); // Large file

        let task = tokio::spawn(async move { async_read_byte_range(path, 0, 1024 * 1024).await });

        // Immediately abort the task.
        task.abort();

        // Ensure that awaiting the aborted task results in an error, not a panic.
        let result = task.await;
        assert!(result.is_err());

        // The outer error is a `JoinError` because the task was aborted.
        // Our handler should turn an inner cancellation into `io::ErrorKind::Interrupted`.
        // If the task panicked, `result.unwrap_err().is_panic()` would be true.
        assert!(result.unwrap_err().is_cancelled());
    }
}