Skip to content

Commit beb22da

Browse files
committed
made functions accept both nix::off_t and libc::off_t
1 parent 2d57f93 commit beb22da

File tree

6 files changed

+72
-32
lines changed

6 files changed

+72
-32
lines changed

src/fcntl.rs

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -757,13 +757,20 @@ feature! {
757757
/// file referred to by fd.
758758
#[cfg(target_os = "linux")]
759759
#[cfg(feature = "fs")]
760-
pub fn fallocate(
760+
pub fn fallocate<Off: Into<off_t>>(
761761
fd: RawFd,
762762
mode: FallocateFlags,
763-
offset: off_t,
764-
len: off_t,
763+
offset: Off,
764+
len: Off,
765765
) -> Result<()> {
766-
let res = unsafe { largefile_fn![fallocate](fd, mode.bits(), offset, len) };
766+
let res = unsafe {
767+
largefile_fn![fallocate](
768+
fd,
769+
mode.bits(),
770+
offset.into(),
771+
len.into(),
772+
)
773+
};
767774
Errno::result(res).map(drop)
768775
}
769776

@@ -935,14 +942,19 @@ mod posix_fadvise {
935942

936943
feature! {
937944
#![feature = "fs"]
938-
pub fn posix_fadvise(
945+
pub fn posix_fadvise<Off: Into<off_t>>(
939946
fd: RawFd,
940-
offset: off_t,
941-
len: off_t,
947+
offset: Off,
948+
len: Off,
942949
advice: PosixFadviseAdvice,
943950
) -> Result<()> {
944951
let res = unsafe {
945-
largefile_fn![posix_fadvise](fd, offset, len, advice as libc::c_int)
952+
largefile_fn![posix_fadvise](
953+
fd,
954+
offset.into(),
955+
len.into(),
956+
advice as libc::c_int,
957+
)
946958
};
947959

948960
if res == 0 {
@@ -963,12 +975,18 @@ mod posix_fadvise {
963975
target_os = "wasi",
964976
target_os = "freebsd"
965977
))]
966-
pub fn posix_fallocate(
978+
pub fn posix_fallocate<Off: Into<off_t>>(
967979
fd: RawFd,
968-
offset: off_t,
969-
len: off_t,
980+
offset: Off,
981+
len: Off,
970982
) -> Result<()> {
971-
let res = unsafe { largefile_fn![posix_fallocate](fd, offset, len) };
983+
let res = unsafe {
984+
largefile_fn![posix_fallocate](
985+
fd,
986+
offset.into(),
987+
len.into(),
988+
)
989+
};
972990
match Errno::result(res) {
973991
Err(err) => Err(err),
974992
Ok(0) => Ok(()),

src/sys/uio.rs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ pub fn readv<Fd: AsFd>(fd: Fd, iov: &mut [IoSliceMut<'_>]) -> Result<usize> {
4545
/// See also: [`writev`](fn.writev.html) and [`pwrite`](fn.pwrite.html)
4646
#[cfg(not(any(target_os = "redox", target_os = "haiku")))]
4747
#[cfg_attr(docsrs, doc(cfg(all())))]
48-
pub fn pwritev<Fd: AsFd>(fd: Fd, iov: &[IoSlice<'_>], offset: off_t) -> Result<usize> {
48+
pub fn pwritev<Fd: AsFd, Off: Into<off_t>>(
49+
fd: Fd,
50+
iov: &[IoSlice<'_>],
51+
offset: Off,
52+
) -> Result<usize> {
4953
#[cfg(target_env = "uclibc")]
5054
let offset = offset as libc::off64_t; // uclibc doesn't use off_t
5155

@@ -55,7 +59,7 @@ pub fn pwritev<Fd: AsFd>(fd: Fd, iov: &[IoSlice<'_>], offset: off_t) -> Result<u
5559
fd.as_fd().as_raw_fd(),
5660
iov.as_ptr() as *const libc::iovec,
5761
iov.len() as c_int,
58-
offset,
62+
offset.into(),
5963
)
6064
};
6165

@@ -71,10 +75,10 @@ pub fn pwritev<Fd: AsFd>(fd: Fd, iov: &[IoSlice<'_>], offset: off_t) -> Result<u
7175
/// See also: [`readv`](fn.readv.html) and [`pread`](fn.pread.html)
7276
#[cfg(not(any(target_os = "redox", target_os = "haiku")))]
7377
#[cfg_attr(docsrs, doc(cfg(all())))]
74-
pub fn preadv<Fd: AsFd>(
78+
pub fn preadv<Fd: AsFd, Off: Into<off_t>>(
7579
fd: Fd,
7680
iov: &mut [IoSliceMut<'_>],
77-
offset: off_t,
81+
offset: Off,
7882
) -> Result<usize> {
7983
#[cfg(target_env = "uclibc")]
8084
let offset = offset as libc::off64_t; // uclibc doesn't use off_t
@@ -85,7 +89,7 @@ pub fn preadv<Fd: AsFd>(
8589
fd.as_fd().as_raw_fd(),
8690
iov.as_ptr() as *const libc::iovec,
8791
iov.len() as c_int,
88-
offset,
92+
offset.into(),
8993
)
9094
};
9195

@@ -96,13 +100,17 @@ pub fn preadv<Fd: AsFd>(
96100
///
97101
/// See also [pwrite(2)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/pwrite.html)
98102
// TODO: move to unistd
99-
pub fn pwrite<Fd: AsFd>(fd: Fd, buf: &[u8], offset: off_t) -> Result<usize> {
103+
pub fn pwrite<Fd: AsFd, Off: Into<off_t>>(
104+
fd: Fd,
105+
buf: &[u8],
106+
offset: Off,
107+
) -> Result<usize> {
100108
let res = unsafe {
101109
largefile_fn![pwrite](
102110
fd.as_fd().as_raw_fd(),
103111
buf.as_ptr() as *const c_void,
104112
buf.len() as size_t,
105-
offset,
113+
offset.into(),
106114
)
107115
};
108116

@@ -113,13 +121,17 @@ pub fn pwrite<Fd: AsFd>(fd: Fd, buf: &[u8], offset: off_t) -> Result<usize> {
113121
///
114122
/// See also [pread(2)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/pread.html)
115123
// TODO: move to unistd
116-
pub fn pread<Fd: AsFd>(fd: Fd, buf: &mut [u8], offset: off_t) -> Result<usize> {
124+
pub fn pread<Fd: AsFd, Off: Into<off_t>>(
125+
fd: Fd,
126+
buf: &mut [u8],
127+
offset: Off,
128+
) -> Result<usize> {
117129
let res = unsafe {
118130
largefile_fn![pread](
119131
fd.as_fd().as_raw_fd(),
120132
buf.as_mut_ptr() as *mut c_void,
121133
buf.len() as size_t,
122-
offset,
134+
offset.into(),
123135
)
124136
};
125137

src/unistd.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1168,8 +1168,12 @@ pub enum Whence {
11681168
/// Move the read/write file offset.
11691169
///
11701170
/// See also [lseek(2)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/lseek.html)
1171-
pub fn lseek(fd: RawFd, offset: off_t, whence: Whence) -> Result<off_t> {
1172-
let res = unsafe { largefile_fn![lseek](fd, offset, whence as i32) };
1171+
pub fn lseek<Off: Into<off_t>>(
1172+
fd: RawFd,
1173+
offset: Off,
1174+
whence: Whence,
1175+
) -> Result<off_t> {
1176+
let res = unsafe { largefile_fn![lseek](fd, offset.into(), whence as i32) };
11731177

11741178
Errno::result(res).map(|r| r as off_t)
11751179
}
@@ -1246,10 +1250,13 @@ pub fn pipe2(flags: OFlag) -> Result<(RawFd, RawFd)> {
12461250
/// See also
12471251
/// [truncate(2)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/truncate.html)
12481252
#[cfg(not(any(target_os = "redox", target_os = "fuchsia")))]
1249-
pub fn truncate<P: ?Sized + NixPath>(path: &P, len: off_t) -> Result<()> {
1253+
pub fn truncate<P: ?Sized + NixPath, Off: Into<off_t>>(
1254+
path: &P,
1255+
len: Off,
1256+
) -> Result<()> {
12501257
let res = path
12511258
.with_nix_path(|cstr| unsafe {
1252-
largefile_fn![truncate](cstr.as_ptr(), len)
1259+
largefile_fn![truncate](cstr.as_ptr(), len.into())
12531260
})?;
12541261

12551262
Errno::result(res).map(drop)
@@ -1259,9 +1266,9 @@ pub fn truncate<P: ?Sized + NixPath>(path: &P, len: off_t) -> Result<()> {
12591266
///
12601267
/// See also
12611268
/// [ftruncate(2)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/ftruncate.html)
1262-
pub fn ftruncate<Fd: AsFd>(fd: Fd, len: off_t) -> Result<()> {
1269+
pub fn ftruncate<Fd: AsFd, Off: Into<off_t>>(fd: Fd, len: Off) -> Result<()> {
12631270
Errno::result(unsafe {
1264-
largefile_fn![ftruncate](fd.as_fd().as_raw_fd(), len)
1271+
largefile_fn![ftruncate](fd.as_fd().as_raw_fd(), len.into())
12651272
}).map(drop)
12661273
}
12671274

test/sys/test_uio.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#[cfg(not(target_os = "redox"))]
22
use crate::require_largefile;
3+
#[cfg(not(target_os = "redox"))]
4+
use nix::off_t;
35
use nix::sys::uio::*;
46
use nix::unistd::*;
57
use rand::distributions::Alphanumeric;
@@ -135,7 +137,7 @@ fn test_pwrite_largefile() {
135137

136138
let mut file = tempfile().unwrap();
137139
let buf = [255u8; 1];
138-
let pos = 0x1_0000_0002u64.try_into().unwrap();
140+
let pos: off_t = 0x1_0000_0002u64.try_into().unwrap();
139141
assert_eq!(pwrite(&file, &buf, pos), Ok(1));
140142
assert_eq!(file.metadata().unwrap().len(), 0x1_0000_0003);
141143
file.seek(SeekFrom::End(-1)).unwrap();
@@ -173,7 +175,7 @@ fn test_pread_largefile() {
173175
let file = tempfile().unwrap();
174176
file.write_all_at(b"The text", 0x1_0000_0005).unwrap();
175177
let mut buf = [0u8; 4];
176-
let pos = 0x1_0000_0009u64.try_into().unwrap();
178+
let pos: off_t = 0x1_0000_0009u64.try_into().unwrap();
177179
assert_eq!(pread(&file, &mut buf, pos), Ok(4));
178180
assert_eq!(&buf, b"text");
179181
}

test/test_fcntl.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,10 +565,11 @@ 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();
568569
assert!(posix_fadvise(
569570
tmp.as_raw_fd(),
570571
0,
571-
0x1_0000_0004u64.try_into().unwrap(),
572+
pos,
572573
PosixFadviseAdvice::POSIX_FADV_DONTNEED,
573574
)
574575
.is_ok());

test/test_unistd.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,7 @@ fn test_truncate_largefile() {
803803
let tempdir = tempdir().unwrap();
804804
let path = tempdir.path().join("file");
805805
File::create(&path).unwrap();
806-
let length = (0x1_0000_0008u64).try_into().unwrap();
806+
let length: off_t = (0x1_0000_0008u64).try_into().unwrap();
807807
truncate(&path, length).unwrap();
808808
let metadata = fs::metadata(&path).unwrap();
809809
assert_eq!(0x1_0000_0008, metadata.len());
@@ -830,7 +830,7 @@ fn test_ftruncate_largefile() {
830830
require_largefile!("test_ftruncate_largefile");
831831

832832
let tmp = tempfile().unwrap();
833-
let length = (0x1_0000_000cu64).try_into().unwrap();
833+
let length: off_t = (0x1_0000_000cu64).try_into().unwrap();
834834
ftruncate(&tmp, length).unwrap();
835835
let metadata = tmp.metadata().unwrap();
836836
assert_eq!(0x1_0000_000c, metadata.len());

0 commit comments

Comments
 (0)