Skip to content

Commit a141847

Browse files
authored
Make the "hole" test check _PC_MIN_HOLE_SIZE on platforms that have it. (#786)
On Illumos, FreeBSD, and others, there's a `_PC_MIN_HOLE_SIZE` `pathconf` code for quering for hole support. Check for it before testing hole support.
1 parent 9e55075 commit a141847

File tree

1 file changed

+30
-9
lines changed

1 file changed

+30
-9
lines changed

tests/fs/seek.rs

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,44 @@ fn test_seek_holes() {
1717
let mut foo = std::fs::File::from(foo);
1818

1919
let stat = fstat(&foo).unwrap();
20-
let blksize = stat.st_blksize as u64;
20+
let hole_size = stat.st_blksize as u64;
21+
22+
#[cfg(any(solarish, freebsdlike, netbsdlike))]
23+
let hole_size = unsafe {
24+
use std::os::unix::io::AsRawFd;
25+
26+
let r = libc::fpathconf(foo.as_raw_fd(), libc::_PC_MIN_HOLE_SIZE);
27+
28+
if r < 0 {
29+
// Holes not supported.
30+
return;
31+
}
32+
33+
// Holes are supported.
34+
core::cmp::max(hole_size, r as u64)
35+
};
2136

2237
foo.write_all(b"prefix").unwrap();
23-
assert_eq!(seek(&foo, SeekFrom::Start(blksize * 2)), Ok(blksize * 2));
38+
assert_eq!(
39+
seek(&foo, SeekFrom::Start(hole_size * 2)),
40+
Ok(hole_size * 2)
41+
);
2442
foo.write_all(b"suffix").unwrap();
2543
assert_eq!(seek(&foo, SeekFrom::Start(0)), Ok(0));
2644
assert_eq!(seek(&foo, SeekFrom::Current(0)), Ok(0));
27-
assert_eq!(seek(&foo, SeekFrom::Hole(0)), Ok(blksize));
28-
assert_eq!(seek(&foo, SeekFrom::Hole(blksize as i64)), Ok(blksize));
45+
assert_eq!(seek(&foo, SeekFrom::Hole(0)), Ok(hole_size));
46+
assert_eq!(seek(&foo, SeekFrom::Hole(hole_size as i64)), Ok(hole_size));
2947
assert_eq!(
30-
seek(&foo, SeekFrom::Hole(blksize as i64 * 2)),
31-
Ok(blksize * 2 + 6)
48+
seek(&foo, SeekFrom::Hole(hole_size as i64 * 2)),
49+
Ok(hole_size * 2 + 6)
3250
);
3351
assert_eq!(seek(&foo, SeekFrom::Data(0)), Ok(0));
34-
assert_eq!(seek(&foo, SeekFrom::Data(blksize as i64)), Ok(blksize * 2));
3552
assert_eq!(
36-
seek(&foo, SeekFrom::Data(blksize as i64 * 2)),
37-
Ok(blksize * 2)
53+
seek(&foo, SeekFrom::Data(hole_size as i64)),
54+
Ok(hole_size * 2)
55+
);
56+
assert_eq!(
57+
seek(&foo, SeekFrom::Data(hole_size as i64 * 2)),
58+
Ok(hole_size * 2)
3859
);
3960
}

0 commit comments

Comments
 (0)