Skip to content

Commit 03b0935

Browse files
committed
remove nix::off_t in favor of i64
1 parent e0b94c7 commit 03b0935

File tree

11 files changed

+47
-80
lines changed

11 files changed

+47
-80
lines changed

src/fcntl.rs

+5-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use crate::errno::Errno;
22
use libc::{self, c_char, c_int, c_uint, size_t, ssize_t};
3+
#[cfg(any(target_os = "freebsd"))]
4+
use libc::off_t;
35
use std::ffi::OsString;
46
#[cfg(not(target_os = "redox"))]
57
use std::os::raw;
@@ -11,17 +13,6 @@ use crate::{sys::stat::Mode, NixPath, Result};
1113
#[cfg(any(target_os = "android", target_os = "linux"))]
1214
use std::ptr; // For splice and copy_file_range
1315

14-
#[cfg(any(
15-
target_os = "linux",
16-
target_os = "android",
17-
target_os = "dragonfly",
18-
target_os = "emscripten",
19-
target_os = "fuchsia",
20-
target_os = "wasi",
21-
target_os = "freebsd"
22-
))]
23-
use crate::off_t;
24-
2516
#[cfg(any(
2617
target_os = "linux",
2718
target_os = "android",
@@ -757,7 +748,7 @@ feature! {
757748
/// file referred to by fd.
758749
#[cfg(target_os = "linux")]
759750
#[cfg(feature = "fs")]
760-
pub fn fallocate<Off: Into<off_t>>(
751+
pub fn fallocate<Off: Into<i64>>(
761752
fd: RawFd,
762753
mode: FallocateFlags,
763754
offset: Off,
@@ -922,7 +913,6 @@ pub fn fspacectl_all(
922913
mod posix_fadvise {
923914
use crate::errno::Errno;
924915
use crate::Result;
925-
use crate::off_t;
926916
use std::os::unix::io::RawFd;
927917

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

943933
feature! {
944934
#![feature = "fs"]
945-
pub fn posix_fadvise<Off: Into<off_t>>(
935+
pub fn posix_fadvise<Off: Into<i64>>(
946936
fd: RawFd,
947937
offset: Off,
948938
len: Off,
@@ -975,7 +965,7 @@ mod posix_fadvise {
975965
target_os = "wasi",
976966
target_os = "freebsd"
977967
))]
978-
pub fn posix_fallocate<Off: Into<off_t>>(
968+
pub fn posix_fallocate<Off: Into<i64>>(
979969
fd: RawFd,
980970
offset: Off,
981971
len: Off,

src/lib.rs

-20
Original file line numberDiff line numberDiff line change
@@ -55,33 +55,13 @@
5555
#![cfg_attr(docsrs, feature(doc_cfg))]
5656
#![deny(clippy::cast_ptr_alignment)]
5757

58-
use cfg_if::cfg_if;
59-
6058
// Re-exported external crates
6159
pub use libc;
6260

6361
// Private internal modules
6462
#[macro_use]
6563
mod macros;
6664

67-
// On some platforms, libc::off_t is not large enough to represent all file
68-
// offsets the platform supports, but the platform provides a different
69-
// type that allows larger offsets to be used. We define our own off_t type
70-
// that is large enough to represent all file offsets the platform supports.
71-
cfg_if! {
72-
if #[cfg(all(target_os = "linux", target_env = "gnu"))] {
73-
/// Used to represent offsets in files. May differ from libc::off_t
74-
/// on platforms where libc::off_t cannot represent the full range
75-
/// of file offsets.
76-
pub type off_t = libc::off64_t;
77-
} else {
78-
/// Used to represent offsets in files. May differ from libc::off_t
79-
/// on platforms where libc::off_t cannot represent the full range
80-
/// of file offsets.
81-
pub type off_t = libc::off_t;
82-
}
83-
}
84-
8565
// Public crates
8666
#[cfg(not(target_os = "redox"))]
8767
feature! {

src/sys/mman.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! Memory management declarations.
22
33
use crate::errno::Errno;
4-
use crate::off_t;
54
#[cfg(not(target_os = "android"))]
65
use crate::NixPath;
76
use crate::Result;
@@ -423,7 +422,7 @@ pub unsafe fn mmap<F: AsFd>(
423422
prot: ProtFlags,
424423
flags: MapFlags,
425424
f: Option<F>,
426-
offset: off_t,
425+
offset: i64,
427426
) -> Result<*mut c_void> {
428427
let ptr =
429428
addr.map_or(std::ptr::null_mut(), |a| usize::from(a) as *mut c_void);

src/sys/sendfile.rs

+13-14
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use std::ptr;
77
use libc;
88

99
use crate::errno::Errno;
10-
use crate::off_t;
1110
use crate::Result;
1211

1312
/// Copy up to `count` bytes to `out_fd` from `in_fd` starting at `offset`.
@@ -27,7 +26,7 @@ use crate::Result;
2726
pub fn sendfile<F1: AsFd, F2: AsFd>(
2827
out_fd: F1,
2928
in_fd: F2,
30-
offset: Option<&mut off_t>,
29+
offset: Option<&mut i64>,
3130
count: usize,
3231
) -> Result<usize> {
3332
let offset = offset
@@ -174,19 +173,19 @@ cfg_if! {
174173
pub fn sendfile<F1: AsFd, F2: AsFd>(
175174
in_fd: F1,
176175
out_sock: F2,
177-
offset: off_t,
176+
offset: i64,
178177
count: Option<usize>,
179178
headers: Option<&[&[u8]]>,
180179
trailers: Option<&[&[u8]]>,
181180
flags: SfFlags,
182181
readahead: u16
183-
) -> (Result<()>, off_t) {
182+
) -> (Result<()>, i64) {
184183
// Readahead goes in upper 16 bits
185184
// Flags goes in lower 16 bits
186185
// see `man 2 sendfile`
187186
let ra32 = u32::from(readahead);
188187
let flags: u32 = (ra32 << 16) | (flags.bits() as u32);
189-
let mut bytes_sent: off_t = 0;
188+
let mut bytes_sent: i64 = 0;
190189
let hdtr = headers.or(trailers).map(|_| SendfileHeaderTrailer::new(headers, trailers));
191190
let hdtr_ptr = hdtr.as_ref().map_or(ptr::null(), |s| &s.0 as *const libc::sf_hdtr);
192191
let return_code = unsafe {
@@ -195,7 +194,7 @@ cfg_if! {
195194
offset,
196195
count.unwrap_or(0),
197196
hdtr_ptr as *mut libc::sf_hdtr,
198-
&mut bytes_sent as *mut off_t,
197+
&mut bytes_sent as *mut i64,
199198
flags as c_int)
200199
};
201200
(Errno::result(return_code).and(Ok(())), bytes_sent)
@@ -224,12 +223,12 @@ cfg_if! {
224223
pub fn sendfile<F1: AsFd, F2: AsFd>(
225224
in_fd: F1,
226225
out_sock: F2,
227-
offset: off_t,
226+
offset: i64,
228227
count: Option<usize>,
229228
headers: Option<&[&[u8]]>,
230229
trailers: Option<&[&[u8]]>,
231-
) -> (Result<()>, off_t) {
232-
let mut bytes_sent: off_t = 0;
230+
) -> (Result<()>, i64) {
231+
let mut bytes_sent: i64 = 0;
233232
let hdtr = headers.or(trailers).map(|_| SendfileHeaderTrailer::new(headers, trailers));
234233
let hdtr_ptr = hdtr.as_ref().map_or(ptr::null(), |s| &s.0 as *const libc::sf_hdtr);
235234
let return_code = unsafe {
@@ -238,7 +237,7 @@ cfg_if! {
238237
offset,
239238
count.unwrap_or(0),
240239
hdtr_ptr as *mut libc::sf_hdtr,
241-
&mut bytes_sent as *mut off_t,
240+
&mut bytes_sent as *mut i64,
242241
0)
243242
};
244243
(Errno::result(return_code).and(Ok(())), bytes_sent)
@@ -270,19 +269,19 @@ cfg_if! {
270269
pub fn sendfile<F1: AsFd, F2: AsFd>(
271270
in_fd: F1,
272271
out_sock: F2,
273-
offset: off_t,
274-
count: Option<off_t>,
272+
offset: i64,
273+
count: Option<i64>,
275274
headers: Option<&[&[u8]]>,
276275
trailers: Option<&[&[u8]]>
277-
) -> (Result<()>, off_t) {
276+
) -> (Result<()>, i64_t) {
278277
let mut len = count.unwrap_or(0);
279278
let hdtr = headers.or(trailers).map(|_| SendfileHeaderTrailer::new(headers, trailers));
280279
let hdtr_ptr = hdtr.as_ref().map_or(ptr::null(), |s| &s.0 as *const libc::sf_hdtr);
281280
let return_code = unsafe {
282281
libc::sendfile(in_fd.as_fd().as_raw_fd(),
283282
out_sock.as_fd().as_raw_fd(),
284283
offset,
285-
&mut len as *mut off_t,
284+
&mut len as *mut i64,
286285
hdtr_ptr as *mut libc::sf_hdtr,
287286
0)
288287
};

src/sys/uio.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! Vectored I/O
22
33
use crate::errno::Errno;
4-
use crate::off_t;
54
use crate::Result;
65
use libc::{self, c_int, c_void, size_t};
76
use std::io::{IoSlice, IoSliceMut};
@@ -45,7 +44,7 @@ pub fn readv<Fd: AsFd>(fd: Fd, iov: &mut [IoSliceMut<'_>]) -> Result<usize> {
4544
/// See also: [`writev`](fn.writev.html) and [`pwrite`](fn.pwrite.html)
4645
#[cfg(not(any(target_os = "redox", target_os = "haiku")))]
4746
#[cfg_attr(docsrs, doc(cfg(all())))]
48-
pub fn pwritev<Fd: AsFd, Off: Into<off_t>>(
47+
pub fn pwritev<Fd: AsFd, Off: Into<i64>>(
4948
fd: Fd,
5049
iov: &[IoSlice<'_>],
5150
offset: Off,
@@ -78,7 +77,7 @@ pub fn pwritev<Fd: AsFd, Off: Into<off_t>>(
7877
/// See also: [`readv`](fn.readv.html) and [`pread`](fn.pread.html)
7978
#[cfg(not(any(target_os = "redox", target_os = "haiku")))]
8079
#[cfg_attr(docsrs, doc(cfg(all())))]
81-
pub fn preadv<Fd: AsFd, Off: Into<off_t>>(
80+
pub fn preadv<Fd: AsFd, Off: Into<i64>>(
8281
fd: Fd,
8382
iov: &mut [IoSliceMut<'_>],
8483
offset: Off,
@@ -106,7 +105,7 @@ pub fn preadv<Fd: AsFd, Off: Into<off_t>>(
106105
///
107106
/// See also [pwrite(2)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/pwrite.html)
108107
// TODO: move to unistd
109-
pub fn pwrite<Fd: AsFd, Off: Into<off_t>>(
108+
pub fn pwrite<Fd: AsFd, Off: Into<i64>>(
110109
fd: Fd,
111110
buf: &[u8],
112111
offset: Off,
@@ -127,7 +126,7 @@ pub fn pwrite<Fd: AsFd, Off: Into<off_t>>(
127126
///
128127
/// See also [pread(2)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/pread.html)
129128
// TODO: move to unistd
130-
pub fn pread<Fd: AsFd, Off: Into<off_t>>(
129+
pub fn pread<Fd: AsFd, Off: Into<i64>>(
131130
fd: Fd,
132131
buf: &mut [u8],
133132
offset: Off,

src/unistd.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use crate::errno::{self, Errno};
66
use crate::fcntl::{at_rawfd, AtFlags};
77
#[cfg(feature = "fs")]
88
use crate::fcntl::{fcntl, FcntlArg::F_SETFD, FdFlag, OFlag};
9-
use crate::off_t;
109
#[cfg(all(
1110
feature = "fs",
1211
any(
@@ -1168,14 +1167,14 @@ pub enum Whence {
11681167
/// Move the read/write file offset.
11691168
///
11701169
/// See also [lseek(2)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/lseek.html)
1171-
pub fn lseek<Off: Into<off_t>>(
1170+
pub fn lseek<Off: Into<i64>>(
11721171
fd: RawFd,
11731172
offset: Off,
11741173
whence: Whence,
1175-
) -> Result<off_t> {
1174+
) -> Result<i64> {
11761175
let res = unsafe { largefile_fn![lseek](fd, offset.into(), whence as i32) };
11771176

1178-
Errno::result(res).map(|r| r as off_t)
1177+
Errno::result(res).map(|r| r as i64)
11791178
}
11801179

11811180
#[cfg(any(target_os = "linux", target_os = "android"))]
@@ -1250,7 +1249,7 @@ pub fn pipe2(flags: OFlag) -> Result<(RawFd, RawFd)> {
12501249
/// See also
12511250
/// [truncate(2)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/truncate.html)
12521251
#[cfg(not(any(target_os = "redox", target_os = "fuchsia")))]
1253-
pub fn truncate<P: ?Sized + NixPath, Off: Into<off_t>>(
1252+
pub fn truncate<P: ?Sized + NixPath, Off: Into<i64>>(
12541253
path: &P,
12551254
len: Off,
12561255
) -> Result<()> {
@@ -1266,7 +1265,7 @@ pub fn truncate<P: ?Sized + NixPath, Off: Into<off_t>>(
12661265
///
12671266
/// See also
12681267
/// [ftruncate(2)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/ftruncate.html)
1269-
pub fn ftruncate<Fd: AsFd, Off: Into<off_t>>(fd: Fd, len: Off) -> Result<()> {
1268+
pub fn ftruncate<Fd: AsFd, Off: Into<i64>>(fd: Fd, len: Off) -> Result<()> {
12701269
Errno::result(unsafe {
12711270
largefile_fn![ftruncate](fd.as_fd().as_raw_fd(), len.into())
12721271
}).map(drop)

test/common/mod.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,15 @@ cfg_if! {
3333
}
3434

3535
/// Skip the test if we cannot handle offsets larger than 32 bits.
36+
///
37+
/// This is generally the case if libc::off_t::MAX is 1 << 32 or less.
38+
/// However, glibc on Linux provides variants of the standard I/O
39+
/// functions that do support 64-bit offsets (e.g. lseek64 instead of lseek).
3640
#[macro_export]
3741
macro_rules! require_largefile {
3842
($name:expr) => {
39-
if (nix::off_t::MAX >> 31) <= 1 {
43+
#[cfg(not(all(target_os = "linux", target_env = "gnu")))]
44+
if (libc::off_t::MAX >> 31) <= 1 {
4045
$crate::skip!(
4146
"{} requires file offsets \
4247
larger than 32 bits. Skipping test.",

test/sys/test_uio.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#[cfg(not(target_os = "redox"))]
22
use crate::require_largefile;
33
#[cfg(not(target_os = "redox"))]
4-
use nix::off_t;
54
use nix::sys::uio::*;
65
use nix::unistd::*;
76
use rand::distributions::Alphanumeric;
@@ -137,7 +136,7 @@ fn test_pwrite_largefile() {
137136

138137
let mut file = tempfile().unwrap();
139138
let buf = [255u8; 1];
140-
let pos: off_t = 0x1_0000_0002u64.try_into().unwrap();
139+
let pos = 0x1_0000_0002i64;
141140
assert_eq!(pwrite(&file, &buf, pos), Ok(1));
142141
assert_eq!(file.metadata().unwrap().len(), 0x1_0000_0003);
143142
file.seek(SeekFrom::End(-1)).unwrap();
@@ -175,7 +174,7 @@ fn test_pread_largefile() {
175174
let file = tempfile().unwrap();
176175
file.write_all_at(b"The text", 0x1_0000_0005).unwrap();
177176
let mut buf = [0u8; 4];
178-
let pos: off_t = 0x1_0000_0009u64.try_into().unwrap();
177+
let pos = 0x1_0000_0009i64;
179178
assert_eq!(pread(&file, &mut buf, pos), Ok(4));
180179
assert_eq!(&buf, b"text");
181180
}

test/test_fcntl.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ mod test_posix_fadvise {
565565
let mut tmp = tempfile::tempfile().unwrap();
566566
tmp.seek(std::io::SeekFrom::Start(0xfffffffc)).unwrap();
567567
tmp.write_all(b"forty-two").unwrap();
568-
let pos: nix::off_t = 0x1_0000_0004u64.try_into().unwrap();
568+
let pos = 0x1_0000_0004i64;
569569
assert!(posix_fadvise(
570570
tmp.as_raw_fd(),
571571
0,
@@ -589,7 +589,6 @@ mod test_posix_fallocate {
589589

590590
use nix::errno::Errno;
591591
use nix::fcntl::*;
592-
use nix::off_t;
593592
use nix::unistd::pipe;
594593
use std::{
595594
io::Read,
@@ -602,7 +601,7 @@ mod test_posix_fallocate {
602601
const LEN: usize = 100;
603602
let mut tmp = NamedTempFile::new().unwrap();
604603
let fd = tmp.as_raw_fd();
605-
let res = posix_fallocate(fd, 0, LEN as off_t);
604+
let res = posix_fallocate(fd, 0, LEN as i64);
606605
match res {
607606
Ok(_) => {
608607
let mut data = [1u8; LEN];

0 commit comments

Comments
 (0)