Skip to content

Commit

Permalink
remove nix::off_t in favor of i64
Browse files Browse the repository at this point in the history
  • Loading branch information
inglorion committed Jul 2, 2023
1 parent e0b94c7 commit 03b0935
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 80 deletions.
20 changes: 5 additions & 15 deletions src/fcntl.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::errno::Errno;
use libc::{self, c_char, c_int, c_uint, size_t, ssize_t};
#[cfg(any(target_os = "freebsd"))]
use libc::off_t;
use std::ffi::OsString;
#[cfg(not(target_os = "redox"))]
use std::os::raw;
Expand All @@ -11,17 +13,6 @@ use crate::{sys::stat::Mode, NixPath, Result};
#[cfg(any(target_os = "android", target_os = "linux"))]
use std::ptr; // For splice and copy_file_range

#[cfg(any(
target_os = "linux",
target_os = "android",
target_os = "dragonfly",
target_os = "emscripten",
target_os = "fuchsia",
target_os = "wasi",
target_os = "freebsd"
))]
use crate::off_t;

#[cfg(any(
target_os = "linux",
target_os = "android",
Expand Down Expand Up @@ -757,7 +748,7 @@ feature! {
/// file referred to by fd.
#[cfg(target_os = "linux")]
#[cfg(feature = "fs")]
pub fn fallocate<Off: Into<off_t>>(
pub fn fallocate<Off: Into<i64>>(
fd: RawFd,
mode: FallocateFlags,
offset: Off,
Expand Down Expand Up @@ -922,7 +913,6 @@ pub fn fspacectl_all(
mod posix_fadvise {
use crate::errno::Errno;
use crate::Result;
use crate::off_t;
use std::os::unix::io::RawFd;

#[cfg(feature = "fs")]
Expand All @@ -942,7 +932,7 @@ mod posix_fadvise {

feature! {
#![feature = "fs"]
pub fn posix_fadvise<Off: Into<off_t>>(
pub fn posix_fadvise<Off: Into<i64>>(
fd: RawFd,
offset: Off,
len: Off,
Expand Down Expand Up @@ -975,7 +965,7 @@ mod posix_fadvise {
target_os = "wasi",
target_os = "freebsd"
))]
pub fn posix_fallocate<Off: Into<off_t>>(
pub fn posix_fallocate<Off: Into<i64>>(
fd: RawFd,
offset: Off,
len: Off,
Expand Down
20 changes: 0 additions & 20 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,33 +55,13 @@
#![cfg_attr(docsrs, feature(doc_cfg))]
#![deny(clippy::cast_ptr_alignment)]

use cfg_if::cfg_if;

// Re-exported external crates
pub use libc;

// Private internal modules
#[macro_use]
mod macros;

// On some platforms, libc::off_t is not large enough to represent all file
// offsets the platform supports, but the platform provides a different
// type that allows larger offsets to be used. We define our own off_t type
// that is large enough to represent all file offsets the platform supports.
cfg_if! {
if #[cfg(all(target_os = "linux", target_env = "gnu"))] {
/// Used to represent offsets in files. May differ from libc::off_t
/// on platforms where libc::off_t cannot represent the full range
/// of file offsets.
pub type off_t = libc::off64_t;
} else {
/// Used to represent offsets in files. May differ from libc::off_t
/// on platforms where libc::off_t cannot represent the full range
/// of file offsets.
pub type off_t = libc::off_t;
}
}

// Public crates
#[cfg(not(target_os = "redox"))]
feature! {
Expand Down
3 changes: 1 addition & 2 deletions src/sys/mman.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Memory management declarations.

use crate::errno::Errno;
use crate::off_t;
#[cfg(not(target_os = "android"))]
use crate::NixPath;
use crate::Result;
Expand Down Expand Up @@ -423,7 +422,7 @@ pub unsafe fn mmap<F: AsFd>(
prot: ProtFlags,
flags: MapFlags,
f: Option<F>,
offset: off_t,
offset: i64,
) -> Result<*mut c_void> {
let ptr =
addr.map_or(std::ptr::null_mut(), |a| usize::from(a) as *mut c_void);
Expand Down
27 changes: 13 additions & 14 deletions src/sys/sendfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use std::ptr;
use libc;

use crate::errno::Errno;
use crate::off_t;
use crate::Result;

/// Copy up to `count` bytes to `out_fd` from `in_fd` starting at `offset`.
Expand All @@ -27,7 +26,7 @@ use crate::Result;
pub fn sendfile<F1: AsFd, F2: AsFd>(
out_fd: F1,
in_fd: F2,
offset: Option<&mut off_t>,
offset: Option<&mut i64>,
count: usize,
) -> Result<usize> {
let offset = offset
Expand Down Expand Up @@ -174,19 +173,19 @@ cfg_if! {
pub fn sendfile<F1: AsFd, F2: AsFd>(
in_fd: F1,
out_sock: F2,
offset: off_t,
offset: i64,
count: Option<usize>,
headers: Option<&[&[u8]]>,
trailers: Option<&[&[u8]]>,
flags: SfFlags,
readahead: u16
) -> (Result<()>, off_t) {
) -> (Result<()>, i64) {
// Readahead goes in upper 16 bits
// Flags goes in lower 16 bits
// see `man 2 sendfile`
let ra32 = u32::from(readahead);
let flags: u32 = (ra32 << 16) | (flags.bits() as u32);
let mut bytes_sent: off_t = 0;
let mut bytes_sent: i64 = 0;
let hdtr = headers.or(trailers).map(|_| SendfileHeaderTrailer::new(headers, trailers));
let hdtr_ptr = hdtr.as_ref().map_or(ptr::null(), |s| &s.0 as *const libc::sf_hdtr);
let return_code = unsafe {
Expand All @@ -195,7 +194,7 @@ cfg_if! {
offset,
count.unwrap_or(0),
hdtr_ptr as *mut libc::sf_hdtr,
&mut bytes_sent as *mut off_t,
&mut bytes_sent as *mut i64,
flags as c_int)
};
(Errno::result(return_code).and(Ok(())), bytes_sent)
Expand Down Expand Up @@ -224,12 +223,12 @@ cfg_if! {
pub fn sendfile<F1: AsFd, F2: AsFd>(
in_fd: F1,
out_sock: F2,
offset: off_t,
offset: i64,
count: Option<usize>,
headers: Option<&[&[u8]]>,
trailers: Option<&[&[u8]]>,
) -> (Result<()>, off_t) {
let mut bytes_sent: off_t = 0;
) -> (Result<()>, i64) {
let mut bytes_sent: i64 = 0;
let hdtr = headers.or(trailers).map(|_| SendfileHeaderTrailer::new(headers, trailers));
let hdtr_ptr = hdtr.as_ref().map_or(ptr::null(), |s| &s.0 as *const libc::sf_hdtr);
let return_code = unsafe {
Expand All @@ -238,7 +237,7 @@ cfg_if! {
offset,
count.unwrap_or(0),
hdtr_ptr as *mut libc::sf_hdtr,
&mut bytes_sent as *mut off_t,
&mut bytes_sent as *mut i64,
0)
};
(Errno::result(return_code).and(Ok(())), bytes_sent)
Expand Down Expand Up @@ -270,19 +269,19 @@ cfg_if! {
pub fn sendfile<F1: AsFd, F2: AsFd>(
in_fd: F1,
out_sock: F2,
offset: off_t,
count: Option<off_t>,
offset: i64,
count: Option<i64>,
headers: Option<&[&[u8]]>,
trailers: Option<&[&[u8]]>
) -> (Result<()>, off_t) {
) -> (Result<()>, i64_t) {
let mut len = count.unwrap_or(0);
let hdtr = headers.or(trailers).map(|_| SendfileHeaderTrailer::new(headers, trailers));
let hdtr_ptr = hdtr.as_ref().map_or(ptr::null(), |s| &s.0 as *const libc::sf_hdtr);
let return_code = unsafe {
libc::sendfile(in_fd.as_fd().as_raw_fd(),
out_sock.as_fd().as_raw_fd(),
offset,
&mut len as *mut off_t,
&mut len as *mut i64,
hdtr_ptr as *mut libc::sf_hdtr,
0)
};
Expand Down
9 changes: 4 additions & 5 deletions src/sys/uio.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Vectored I/O

use crate::errno::Errno;
use crate::off_t;
use crate::Result;
use libc::{self, c_int, c_void, size_t};
use std::io::{IoSlice, IoSliceMut};
Expand Down Expand Up @@ -45,7 +44,7 @@ pub fn readv<Fd: AsFd>(fd: Fd, iov: &mut [IoSliceMut<'_>]) -> Result<usize> {
/// See also: [`writev`](fn.writev.html) and [`pwrite`](fn.pwrite.html)
#[cfg(not(any(target_os = "redox", target_os = "haiku")))]
#[cfg_attr(docsrs, doc(cfg(all())))]
pub fn pwritev<Fd: AsFd, Off: Into<off_t>>(
pub fn pwritev<Fd: AsFd, Off: Into<i64>>(
fd: Fd,
iov: &[IoSlice<'_>],
offset: Off,
Expand Down Expand Up @@ -78,7 +77,7 @@ pub fn pwritev<Fd: AsFd, Off: Into<off_t>>(
/// See also: [`readv`](fn.readv.html) and [`pread`](fn.pread.html)
#[cfg(not(any(target_os = "redox", target_os = "haiku")))]
#[cfg_attr(docsrs, doc(cfg(all())))]
pub fn preadv<Fd: AsFd, Off: Into<off_t>>(
pub fn preadv<Fd: AsFd, Off: Into<i64>>(
fd: Fd,
iov: &mut [IoSliceMut<'_>],
offset: Off,
Expand Down Expand Up @@ -106,7 +105,7 @@ pub fn preadv<Fd: AsFd, Off: Into<off_t>>(
///
/// See also [pwrite(2)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/pwrite.html)
// TODO: move to unistd
pub fn pwrite<Fd: AsFd, Off: Into<off_t>>(
pub fn pwrite<Fd: AsFd, Off: Into<i64>>(
fd: Fd,
buf: &[u8],
offset: Off,
Expand All @@ -127,7 +126,7 @@ pub fn pwrite<Fd: AsFd, Off: Into<off_t>>(
///
/// See also [pread(2)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/pread.html)
// TODO: move to unistd
pub fn pread<Fd: AsFd, Off: Into<off_t>>(
pub fn pread<Fd: AsFd, Off: Into<i64>>(
fd: Fd,
buf: &mut [u8],
offset: Off,
Expand Down
11 changes: 5 additions & 6 deletions src/unistd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use crate::errno::{self, Errno};
use crate::fcntl::{at_rawfd, AtFlags};
#[cfg(feature = "fs")]
use crate::fcntl::{fcntl, FcntlArg::F_SETFD, FdFlag, OFlag};
use crate::off_t;
#[cfg(all(
feature = "fs",
any(
Expand Down Expand Up @@ -1168,14 +1167,14 @@ pub enum Whence {
/// Move the read/write file offset.
///
/// See also [lseek(2)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/lseek.html)
pub fn lseek<Off: Into<off_t>>(
pub fn lseek<Off: Into<i64>>(
fd: RawFd,
offset: Off,
whence: Whence,
) -> Result<off_t> {
) -> Result<i64> {
let res = unsafe { largefile_fn![lseek](fd, offset.into(), whence as i32) };

Errno::result(res).map(|r| r as off_t)
Errno::result(res).map(|r| r as i64)
}

#[cfg(any(target_os = "linux", target_os = "android"))]
Expand Down Expand Up @@ -1250,7 +1249,7 @@ pub fn pipe2(flags: OFlag) -> Result<(RawFd, RawFd)> {
/// See also
/// [truncate(2)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/truncate.html)
#[cfg(not(any(target_os = "redox", target_os = "fuchsia")))]
pub fn truncate<P: ?Sized + NixPath, Off: Into<off_t>>(
pub fn truncate<P: ?Sized + NixPath, Off: Into<i64>>(
path: &P,
len: Off,
) -> Result<()> {
Expand All @@ -1266,7 +1265,7 @@ pub fn truncate<P: ?Sized + NixPath, Off: Into<off_t>>(
///
/// See also
/// [ftruncate(2)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/ftruncate.html)
pub fn ftruncate<Fd: AsFd, Off: Into<off_t>>(fd: Fd, len: Off) -> Result<()> {
pub fn ftruncate<Fd: AsFd, Off: Into<i64>>(fd: Fd, len: Off) -> Result<()> {
Errno::result(unsafe {
largefile_fn![ftruncate](fd.as_fd().as_raw_fd(), len.into())
}).map(drop)
Expand Down
7 changes: 6 additions & 1 deletion test/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,15 @@ cfg_if! {
}

/// Skip the test if we cannot handle offsets larger than 32 bits.
///
/// This is generally the case if libc::off_t::MAX is 1 << 32 or less.
/// However, glibc on Linux provides variants of the standard I/O
/// functions that do support 64-bit offsets (e.g. lseek64 instead of lseek).
#[macro_export]
macro_rules! require_largefile {
($name:expr) => {
if (nix::off_t::MAX >> 31) <= 1 {
#[cfg(not(all(target_os = "linux", target_env = "gnu")))]
if (libc::off_t::MAX >> 31) <= 1 {
$crate::skip!(
"{} requires file offsets \
larger than 32 bits. Skipping test.",
Expand Down
5 changes: 2 additions & 3 deletions test/sys/test_uio.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#[cfg(not(target_os = "redox"))]
use crate::require_largefile;
#[cfg(not(target_os = "redox"))]
use nix::off_t;
use nix::sys::uio::*;
use nix::unistd::*;
use rand::distributions::Alphanumeric;
Expand Down Expand Up @@ -137,7 +136,7 @@ fn test_pwrite_largefile() {

let mut file = tempfile().unwrap();
let buf = [255u8; 1];
let pos: off_t = 0x1_0000_0002u64.try_into().unwrap();
let pos = 0x1_0000_0002i64;
assert_eq!(pwrite(&file, &buf, pos), Ok(1));
assert_eq!(file.metadata().unwrap().len(), 0x1_0000_0003);
file.seek(SeekFrom::End(-1)).unwrap();
Expand Down Expand Up @@ -175,7 +174,7 @@ fn test_pread_largefile() {
let file = tempfile().unwrap();
file.write_all_at(b"The text", 0x1_0000_0005).unwrap();
let mut buf = [0u8; 4];
let pos: off_t = 0x1_0000_0009u64.try_into().unwrap();
let pos = 0x1_0000_0009i64;
assert_eq!(pread(&file, &mut buf, pos), Ok(4));
assert_eq!(&buf, b"text");
}
Expand Down
5 changes: 2 additions & 3 deletions test/test_fcntl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ mod test_posix_fadvise {
let mut tmp = tempfile::tempfile().unwrap();
tmp.seek(std::io::SeekFrom::Start(0xfffffffc)).unwrap();
tmp.write_all(b"forty-two").unwrap();
let pos: nix::off_t = 0x1_0000_0004u64.try_into().unwrap();
let pos = 0x1_0000_0004i64;
assert!(posix_fadvise(
tmp.as_raw_fd(),
0,
Expand All @@ -589,7 +589,6 @@ mod test_posix_fallocate {

use nix::errno::Errno;
use nix::fcntl::*;
use nix::off_t;
use nix::unistd::pipe;
use std::{
io::Read,
Expand All @@ -602,7 +601,7 @@ mod test_posix_fallocate {
const LEN: usize = 100;
let mut tmp = NamedTempFile::new().unwrap();
let fd = tmp.as_raw_fd();
let res = posix_fallocate(fd, 0, LEN as off_t);
let res = posix_fallocate(fd, 0, LEN as i64);
match res {
Ok(_) => {
let mut data = [1u8; LEN];
Expand Down
Loading

0 comments on commit 03b0935

Please sign in to comment.