summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicolás Caracci <ncaracci@users.noreply.github.com>2017-08-10 12:33:56 -0400
committerNicolás Caracci <ncaracci@users.noreply.github.com>2017-08-14 22:13:08 -0300
commit3d7d7ea5445ea0e2626822bd397dd3dad45ac8e6 (patch)
treebb2309e364c9d888fc273800c7f1b9a104f8db20
parent087aece15568b3e7cb6a351301e2afc83c746ca7 (diff)
downloadnix-3d7d7ea5445ea0e2626822bd397dd3dad45ac8e6.zip
Added documentation to Whence.
Changed SeekData and SeekHole hardcoded values to libc ones and added availability only to linux freebsd and dragonflybsd. Removed impl Whence and assigned the libc values directly in the enum definition.
-rw-r--r--src/unistd.rs50
1 files changed, 30 insertions, 20 deletions
diff --git a/src/unistd.rs b/src/unistd.rs
index 47057e34..86cce832 100644
--- a/src/unistd.rs
+++ b/src/unistd.rs
@@ -737,36 +737,46 @@ pub fn write(fd: RawFd, buf: &[u8]) -> Result<usize> {
Errno::result(res).map(|r| r as usize)
}
+/// Directive that tells [`lseek`] and [`lseek64`] what the offset is relative to.
+/// [`lseek`]: ./fn.lseek.html
+/// [`lseek64`]: ./fn.lseek64.html
+#[repr(i32)]
pub enum Whence {
- SeekSet,
- SeekCur,
- SeekEnd,
- SeekData,
- SeekHole
-}
-
-impl Whence {
- fn to_libc_type(&self) -> c_int {
- match self {
- &Whence::SeekSet => libc::SEEK_SET,
- &Whence::SeekCur => libc::SEEK_CUR,
- &Whence::SeekEnd => libc::SEEK_END,
- &Whence::SeekData => 3,
- &Whence::SeekHole => 4
- }
- }
-
+ /// Specify an offset relative to the start of the file.
+ SeekSet = libc::SEEK_SET,
+ /// Specify an offset relative to the current file location.
+ SeekCur = libc::SEEK_CUR,
+ /// Specify an offset relative to the end of the file.
+ SeekEnd = libc::SEEK_END,
+ /// Specify an offset relative to the next location in the file greater than or
+ /// equal to offset that contains some data. If offset points to
+ /// some data, then the file offset is set to offset.
+ #[cfg(any(target_os = "dragonfly", target_os = "freebsd",
+ all(target_os = "linux", not(any(target_env = "musl",
+ target_arch = "mips",
+ target_arch = "mips64")))))]
+ SeekData = libc::SEEK_DATA,
+ /// Specify an offset relative to the next hole in the file greater than
+ /// or equal to offset. If offset points into the middle of a hole, then
+ /// the file offset should be set to offset. If there is no hole past offset,
+ /// then the file offset should be adjusted to the end of the file (i.e., there
+ /// is an implicit hole at the end of any file).
+ #[cfg(any(target_os = "dragonfly", target_os = "freebsd",
+ all(target_os = "linux", not(any(target_env = "musl",
+ target_arch = "mips",
+ target_arch = "mips64")))))]
+ SeekHole = libc::SEEK_HOLE
}
pub fn lseek(fd: RawFd, offset: libc::off_t, whence: Whence) -> Result<libc::off_t> {
- let res = unsafe { libc::lseek(fd, offset, whence.to_libc_type()) };
+ let res = unsafe { libc::lseek(fd, offset, whence as i32) };
Errno::result(res).map(|r| r as libc::off_t)
}
#[cfg(any(target_os = "linux", target_os = "android"))]
pub fn lseek64(fd: RawFd, offset: libc::off64_t, whence: Whence) -> Result<libc::off64_t> {
- let res = unsafe { libc::lseek64(fd, offset, whence.to_libc_type()) };
+ let res = unsafe { libc::lseek64(fd, offset, whence as i32) };
Errno::result(res).map(|r| r as libc::off64_t)
}