1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
//! Functions which operate on file descriptors.

#[cfg(not(target_os = "wasi"))]
use crate::fs::Mode;
#[cfg(not(any(target_os = "netbsd", target_os = "redox", target_os = "wasi")))]
// not implemented in libc for netbsd yet
use crate::fs::StatFs;
use crate::time::Timespec;
use io_lifetimes::{AsFd, BorrowedFd};
#[cfg(all(
    libc,
    not(any(
        target_os = "android",
        target_os = "linux",
        target_os = "emscripten",
        target_os = "l4re",
        target_os = "netbsd",
        target_os = "redox",
        target_os = "wasi"
    ))
))]
use libc::fstatfs as libc_fstatfs;
#[cfg(all(
    libc,
    not(any(
        target_os = "android",
        target_os = "linux",
        target_os = "emscripten",
        target_os = "l4re"
    ))
))]
use libc::lseek as libc_lseek;
#[cfg(all(
    libc,
    not(any(
        target_os = "ios",
        target_os = "freebsd",
        target_os = "macos",
        target_os = "netbsd",
        target_os = "openbsd",
        target_os = "redox",
        target_os = "wasi",
    ))
))]
use libc::off64_t as libc_off_t;
#[cfg(all(
    libc,
    any(
        target_os = "ios",
        target_os = "freebsd",
        target_os = "macos",
        target_os = "netbsd",
        target_os = "openbsd",
        target_os = "redox",
        target_os = "wasi",
    )
))]
use libc::off_t as libc_off_t;
#[cfg(all(
    libc,
    any(
        target_os = "android",
        target_os = "linux",
        target_os = "emscripten",
        target_os = "l4re"
    )
))]
use libc::{fstatfs64 as libc_fstatfs, lseek64 as libc_lseek};
#[cfg(all(
    libc,
    not(any(target_os = "netbsd", target_os = "redox", target_os = "wasi"))
))]
// not implemented in libc for netbsd yet
use std::mem::MaybeUninit;
use std::{
    convert::TryInto,
    io::{self, SeekFrom},
};
#[cfg(libc)]
use {
    crate::{negone_err, zero_ok},
    unsafe_io::os::posish::AsRawFd,
};

/// `lseek(fd, offset, whence)`
#[inline]
pub fn seek<'f, Fd: AsFd<'f>>(fd: Fd, pos: SeekFrom) -> io::Result<u64> {
    let fd = fd.as_fd();
    _seek(fd, pos)
}

#[cfg(libc)]
fn _seek(fd: BorrowedFd<'_>, pos: SeekFrom) -> io::Result<u64> {
    let (whence, offset): (libc::c_int, libc_off_t) = match pos {
        SeekFrom::Start(pos) => (
            libc::SEEK_SET,
            pos.try_into()
                .map_err(|_convert_err| io::Error::from_raw_os_error(libc::EOVERFLOW))?,
        ),
        SeekFrom::End(offset) => (libc::SEEK_END, offset),
        SeekFrom::Current(offset) => (libc::SEEK_CUR, offset),
    };
    let offset = unsafe { negone_err(libc_lseek(fd.as_raw_fd() as libc::c_int, offset, whence))? };
    Ok(offset.try_into().unwrap())
}

#[cfg(linux_raw)]
#[inline]
fn _seek(fd: BorrowedFd<'_>, pos: SeekFrom) -> io::Result<u64> {
    let (whence, offset) = match pos {
        SeekFrom::Start(pos) => (
            linux_raw_sys::general::SEEK_SET,
            pos.try_into().map_err(|_convert_err| {
                io::Error::from_raw_os_error(linux_raw_sys::errno::EOVERFLOW as i32)
            })?,
        ),
        SeekFrom::End(offset) => (linux_raw_sys::general::SEEK_END, offset),
        SeekFrom::Current(offset) => (linux_raw_sys::general::SEEK_CUR, offset),
    };
    crate::linux_raw::lseek(fd, offset, whence)
}

/// `lseek(fd, 0, SEEK_CUR)`
#[inline]
pub fn tell<'f, Fd: AsFd<'f>>(fd: Fd) -> io::Result<u64> {
    let fd = fd.as_fd();
    _tell(fd)
}

#[cfg(libc)]
fn _tell(fd: BorrowedFd<'_>) -> io::Result<u64> {
    let offset =
        unsafe { negone_err(libc_lseek(fd.as_raw_fd() as libc::c_int, 0, libc::SEEK_CUR))? };
    Ok(offset.try_into().unwrap())
}

#[cfg(linux_raw)]
#[inline]
fn _tell(fd: BorrowedFd<'_>) -> io::Result<u64> {
    crate::linux_raw::lseek(fd, 0, linux_raw_sys::general::SEEK_CUR).map(|x| x as u64)
}

/// `fchmod(fd)`.
///
/// Note that this implementation does not support `O_PATH` file descriptors,
/// even on platforms where the host libc emulates it.
#[cfg(not(target_os = "wasi"))]
#[inline]
pub fn fchmod<'f, Fd: AsFd<'f>>(fd: Fd, mode: Mode) -> io::Result<()> {
    let fd = fd.as_fd();
    _fchmod(fd, mode)
}

#[cfg(all(
    libc,
    not(any(target_os = "android", target_os = "linux", target_os = "wasi"))
))]
fn _fchmod(fd: BorrowedFd<'_>, mode: Mode) -> io::Result<()> {
    unsafe { zero_ok(libc::fchmod(fd.as_raw_fd() as libc::c_int, mode.bits())) }
}

#[cfg(all(libc, any(target_os = "android", target_os = "linux")))]
fn _fchmod(fd: BorrowedFd<'_>, mode: Mode) -> io::Result<()> {
    // Use `libc::syscall` rather than `libc::fchmod` because some libc
    // implementations, such as musl, add extra logic to `fchmod` to emulate
    // support for `O_PATH`, which uses `/proc` outside our control and
    // interferes with our own use of `O_PATH`.
    unsafe {
        zero_ok(libc::syscall(
            libc::SYS_fchmod,
            fd.as_raw_fd() as libc::c_int,
            mode.bits(),
        ))
    }
}

#[cfg(linux_raw)]
#[inline]
fn _fchmod(fd: BorrowedFd<'_>, mode: Mode) -> io::Result<()> {
    crate::linux_raw::fchmod(fd, mode.bits() as u16)
}

/// `fstatfs(fd)`
#[cfg(not(any(target_os = "netbsd", target_os = "redox", target_os = "wasi")))] // not implemented in libc for netbsd yet
#[inline]
pub fn fstatfs<'f, Fd: AsFd<'f>>(fd: Fd) -> io::Result<StatFs> {
    let fd = fd.as_fd();
    _fstatfs(fd)
}

#[cfg(all(
    libc,
    not(any(target_os = "netbsd", target_os = "redox", target_os = "wasi"))
))] // not implemented in libc for netbsd yet
fn _fstatfs(fd: BorrowedFd<'_>) -> io::Result<StatFs> {
    let mut statfs = MaybeUninit::<StatFs>::uninit();
    unsafe {
        zero_ok(libc_fstatfs(
            fd.as_raw_fd() as libc::c_int,
            statfs.as_mut_ptr(),
        ))?;
        Ok(statfs.assume_init())
    }
}

#[cfg(linux_raw)]
fn _fstatfs(fd: BorrowedFd<'_>) -> io::Result<StatFs> {
    crate::linux_raw::fstatfs(fd)
}

/// `futimens(fd, times)`
#[inline]
pub fn futimens<'f, Fd: AsFd<'f>>(fd: Fd, times: &[Timespec; 2]) -> io::Result<()> {
    let fd = fd.as_fd();
    _futimens(fd, times)
}

#[cfg(libc)]
fn _futimens(fd: BorrowedFd<'_>, times: &[Timespec; 2]) -> io::Result<()> {
    unsafe {
        zero_ok(libc::futimens(
            fd.as_raw_fd() as libc::c_int,
            times.as_ptr(),
        ))
    }
}

#[cfg(linux_raw)]
#[inline]
fn _futimens(fd: BorrowedFd<'_>, times: &[Timespec; 2]) -> io::Result<()> {
    crate::linux_raw::utimensat(fd, None, times, 0)
}

/// `posix_fallocate(fd, offset, len)`
#[cfg(not(any(target_os = "netbsd", target_os = "redox", target_os = "openbsd")))] // not implemented in libc for netbsd yet
#[inline]
pub fn posix_fallocate<'f, Fd: AsFd<'f>>(fd: Fd, offset: u64, len: u64) -> io::Result<()> {
    let fd = fd.as_fd();
    _posix_fallocate(fd, offset, len)
}

#[cfg(all(
    libc,
    not(any(
        target_os = "ios",
        target_os = "macos",
        target_os = "netbsd",
        target_os = "openbsd",
        target_os = "redox"
    ))
))]
fn _posix_fallocate(fd: BorrowedFd<'_>, offset: u64, len: u64) -> io::Result<()> {
    let offset = offset
        .try_into()
        .map_err(|_overflow_err| io::Error::from_raw_os_error(libc::EOVERFLOW))?;
    let len = len
        .try_into()
        .map_err(|_overflow_err| io::Error::from_raw_os_error(libc::EOVERFLOW))?;
    let err = unsafe { libc::posix_fallocate(fd.as_raw_fd() as libc::c_int, offset, len) };

    // `posix_fallocate` returns its error status rather than using `errno`.
    if err == 0 {
        Ok(())
    } else {
        Err(io::Error::from_raw_os_error(err))
    }
}

#[cfg(any(target_os = "ios", target_os = "macos",))]
fn _posix_fallocate(fd: BorrowedFd<'_>, offset: u64, len: u64) -> io::Result<()> {
    let offset: i64 = offset
        .try_into()
        .map_err(|_overflow_err| io::Error::from_raw_os_error(libc::EOVERFLOW))?;
    let len = len
        .try_into()
        .map_err(|_overflow_err| io::Error::from_raw_os_error(libc::EOVERFLOW))?;
    let new_len = offset.checked_add(len).ok_or_else(|| {
        io::Error::new(io::ErrorKind::Other, "overflow while allocating file space")
    })?;
    let mut store = libc::fstore_t {
        fst_flags: libc::F_ALLOCATECONTIG,
        fst_posmode: libc::F_PEOFPOSMODE,
        fst_offset: 0,
        fst_length: new_len,
        fst_bytesalloc: 0,
    };
    unsafe {
        let ret = libc::fcntl(fd.as_raw_fd() as libc::c_int, libc::F_PREALLOCATE, &store);
        if ret == -1 {
            store.fst_flags = libc::F_ALLOCATEALL;
            negone_err(libc::fcntl(fd.as_raw_fd(), libc::F_PREALLOCATE, &store))?;
        }
        zero_ok(libc::ftruncate(fd.as_raw_fd(), new_len))
    }
}

#[cfg(linux_raw)]
#[inline]
fn _posix_fallocate(fd: BorrowedFd<'_>, offset: u64, len: u64) -> io::Result<()> {
    let offset = offset.try_into().map_err(|_overflow_err| {
        io::Error::from_raw_os_error(linux_raw_sys::errno::EOVERFLOW as i32)
    })?;
    let len = len.try_into().map_err(|_overflow_err| {
        io::Error::from_raw_os_error(linux_raw_sys::errno::EOVERFLOW as i32)
    })?;
    crate::linux_raw::fallocate(fd, 0, offset, len)
}

/// `fcntl(fd, F_GETFL) & O_ACCMODE`. Returns a pair of booleans indicating
/// whether the file descriptor is readable and/or writeable, respectively.
/// This is only reliable on files; for example, it doesn't reflect whether
/// sockets have been shut down; for general I/O handle support, use
/// [`crate::io::is_read_write`].
#[inline]
pub fn is_file_read_write<'f, Fd: AsFd<'f>>(fd: Fd) -> io::Result<(bool, bool)> {
    let fd = fd.as_fd();
    _is_file_read_write(fd)
}

pub(crate) fn _is_file_read_write(fd: BorrowedFd<'_>) -> io::Result<(bool, bool)> {
    let mode = crate::fs::fcntl::_fcntl_getfl(fd)?;

    // Check for `O_PATH`.
    #[cfg(any(
        target_os = "android",
        target_os = "fuchsia",
        target_os = "linux",
        target_os = "emscripten"
    ))]
    if mode.contains(crate::fs::OFlags::PATH) {
        return Ok((false, false));
    }

    // Use `RWMODE` rather than `ACCMODE` as `ACCMODE` may include `O_PATH`.
    // We handled `O_PATH` above.
    match mode & crate::fs::OFlags::RWMODE {
        crate::fs::OFlags::RDONLY => Ok((true, false)),
        crate::fs::OFlags::RDWR => Ok((true, true)),
        crate::fs::OFlags::WRONLY => Ok((false, true)),
        _ => unreachable!(),
    }
}