Skip to content

Commit

Permalink
common: Make file_offset dependency optional
Browse files Browse the repository at this point in the history
This feature is off by default.
`file_offset` only supports `unix` and `windows` build targets.

The additional code implements `FileExt` `std::fs::File`.
`FileExt` is currently only used in the `datafile` module.
  • Loading branch information
Patiga authored and heinrich5991 committed Feb 1, 2024
1 parent 1ba9066 commit b928667
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 8 deletions.
2 changes: 1 addition & 1 deletion common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ license = "MIT/Apache-2.0"

[dependencies]
arrayvec = "0.5.2"
file_offset = "0.1.0"
file_offset = { version = "0.1.0", optional = true }
serde = { version = "1.0.23", optional = true }

[dev-dependencies]
Expand Down
12 changes: 6 additions & 6 deletions common/src/io.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
use file_offset::FileExt as FileOffsetExt;
use std::error;
use std::fmt;
use std::fs::File;
use std::io;
use std::io::Read;

use num::Cast;

struct SeekOverflow(());

pub fn seek_overflow() -> io::Error {
Expand Down Expand Up @@ -48,16 +44,20 @@ pub trait FileExt {
fn read_offset_retry(&self, buffer: &mut [u8], offset: u64) -> io::Result<usize>;
}

impl FileExt for File {
#[cfg(feature = "file_offset")]
impl FileExt for std::fs::File {
fn read_offset_retry(&self, buffer: &mut [u8], offset: u64) -> io::Result<usize> {
use num::Cast;

// Make sure the additions in this function don't overflow
let _end_offset = offset
.checked_add(buffer.len().u64())
.ok_or_else(seek_overflow)?;

let mut read = 0;
while read != buffer.len() {
match self.read_offset(&mut buffer[read..], offset + read.u64()) {
match file_offset::FileExt::read_offset(self, &mut buffer[read..], offset + read.u64())
{
Ok(0) => break,
Ok(r) => read += r,
Err(ref e) if e.kind() == io::ErrorKind::Interrupted => {}
Expand Down
1 change: 1 addition & 0 deletions common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
extern crate quickcheck;

extern crate arrayvec;
#[cfg(feature = "file_offset")]
extern crate file_offset;
#[cfg(feature = "serde")]
extern crate serde;
Expand Down
2 changes: 1 addition & 1 deletion datafile/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ authors = ["heinrich5991 <[email protected]>"]
license = "MIT/Apache-2.0"

[dependencies]
common = { path = "../common/" }
common = { path = "../common/", features = ["file_offset"] }
hexdump = "0.1.1"
itertools = ">=0.3.0,<0.5.0"
log = "0.3.0"
Expand Down

0 comments on commit b928667

Please sign in to comment.