Skip to content

Commit

Permalink
Merge #1473 #1474 #1476
Browse files Browse the repository at this point in the history
1473: sys/stat: add a safe wrapper for mknodat(2) r=asomers a=lucab

This introduces a new `mknodat` helper.

Ref: https://pubs.opengroup.org/onlinepubs/9699919799/functions/mknod.html

1474: Mark most C-derived enums as non_exhaustive r=asomers a=asomers

Since libc may add new variants at any time, Nix's consumers should not
use exhaustive match patterns.

Fixes #1182

1476: Constify many functions r=asomers a=asomers

Constify most functions that can be constified.  The exceptions are
mostly accessors for structs that have no const constructor.

Co-authored-by: Luca BRUNO <luca.bruno@coreos.com>
Co-authored-by: Alan Somers <asomers@gmail.com>
  • Loading branch information
3 people authored Jul 29, 2021
4 parents 62b227c + 6e2158b + 358adaf + 86acc26 commit 3c45e08
Show file tree
Hide file tree
Showing 26 changed files with 150 additions and 43 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).
(#[1471](https://github.com/nix-rust/nix/pull/1471))
- Added `pthread_kill`.
(#[1472](https://github.com/nix-rust/nix/pull/1472))
- Added `mknodat`.
(#[1473](https://github.com/nix-rust/nix/pull/1473))

### Changed

Expand All @@ -30,6 +32,13 @@ This project adheres to [Semantic Versioning](https://semver.org/).
builds.
(#[1481](https://github.com/nix-rust/nix/pull/1481))

- Most enums that come from C, for example `Errno`, are now marked as
`#[non_exhaustive]`.
(#[1474](https://github.com/nix-rust/nix/pull/1474))

- Many more functions, mostly contructors, are now `const`.
(#[1476](https://github.com/nix-rust/nix/pull/1476))

### Fixed

- Added more errno definitions for better backwards compatibility with
Expand Down
14 changes: 11 additions & 3 deletions src/errno.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl Errno {
since = "0.22.0",
note = "It's a no-op now; just delete it."
)]
pub fn as_errno(self) -> Option<Self> {
pub const fn as_errno(self) -> Option<Self> {
Some(self)
}

Expand All @@ -81,7 +81,7 @@ impl Errno {
since = "0.22.0",
note = "Use Errno::EINVAL instead"
)]
pub fn invalid_argument() -> Error {
pub const fn invalid_argument() -> Error {
Errno::EINVAL
}

Expand Down Expand Up @@ -122,7 +122,7 @@ impl Errno {
)]
#[allow(non_snake_case)]
#[inline]
pub fn Sys(errno: Errno) -> Error {
pub const fn Sys(errno: Errno) -> Error {
errno
}
}
Expand Down Expand Up @@ -768,6 +768,7 @@ fn desc(errno: Errno) -> &'static str {
mod consts {
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[repr(i32)]
#[non_exhaustive]
pub enum Errno {
UnknownErrno = 0,
EPERM = libc::EPERM,
Expand Down Expand Up @@ -1073,6 +1074,7 @@ mod consts {
mod consts {
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[repr(i32)]
#[non_exhaustive]
pub enum Errno {
UnknownErrno = 0,
EPERM = libc::EPERM,
Expand Down Expand Up @@ -1324,6 +1326,7 @@ mod consts {
mod consts {
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[repr(i32)]
#[non_exhaustive]
pub enum Errno {
UnknownErrno = 0,
EPERM = libc::EPERM,
Expand Down Expand Up @@ -1562,6 +1565,7 @@ mod consts {
mod consts {
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[repr(i32)]
#[non_exhaustive]
pub enum Errno {
UnknownErrno = 0,
EPERM = libc::EPERM,
Expand Down Expand Up @@ -1796,6 +1800,7 @@ mod consts {
mod consts {
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[repr(i32)]
#[non_exhaustive]
pub enum Errno {
UnknownErrno = 0,
EPERM = libc::EPERM,
Expand Down Expand Up @@ -2019,6 +2024,7 @@ mod consts {
mod consts {
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[repr(i32)]
#[non_exhaustive]
pub enum Errno {
UnknownErrno = 0,
EPERM = libc::EPERM,
Expand Down Expand Up @@ -2244,6 +2250,7 @@ mod consts {
mod consts {
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[repr(i32)]
#[non_exhaustive]
pub enum Errno {
UnknownErrno = 0,
EPERM = libc::EPERM,
Expand Down Expand Up @@ -2441,6 +2448,7 @@ mod consts {
mod consts {
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[repr(i32)]
#[non_exhaustive]
pub enum Errno {
UnknownErrno = 0,
EPERM = libc::EPERM,
Expand Down
4 changes: 4 additions & 0 deletions src/fcntl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ libc_bitflags!(

#[cfg(not(target_os = "redox"))]
#[derive(Debug, Eq, Hash, PartialEq)]
#[non_exhaustive]
pub enum FcntlArg<'a> {
F_DUPFD(RawFd),
F_DUPFD_CLOEXEC(RawFd),
Expand Down Expand Up @@ -405,6 +406,7 @@ pub enum FcntlArg<'a> {

#[cfg(target_os = "redox")]
#[derive(Debug, Clone, Copy, Eq, Hash, PartialEq)]
#[non_exhaustive]
pub enum FcntlArg {
F_DUPFD(RawFd),
F_DUPFD_CLOEXEC(RawFd),
Expand Down Expand Up @@ -454,6 +456,7 @@ pub fn fcntl(fd: RawFd, arg: FcntlArg) -> Result<c_int> {
}

#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[non_exhaustive]
pub enum FlockArg {
LockShared,
LockExclusive,
Expand Down Expand Up @@ -649,6 +652,7 @@ mod posix_fadvise {

libc_enum! {
#[repr(i32)]
#[non_exhaustive]
pub enum PosixFadviseAdvice {
POSIX_FADV_NORMAL,
POSIX_FADV_SEQUENTIAL,
Expand Down
4 changes: 2 additions & 2 deletions src/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ mod os {
#[cfg(any(target_os = "illumos"))]
mod os {
/// Check if the OS supports atomic close-on-exec for sockets
pub fn socket_atomic_cloexec() -> bool {
pub const fn socket_atomic_cloexec() -> bool {
true
}
}
Expand All @@ -109,7 +109,7 @@ mod os {
target_os = "solaris"))]
mod os {
/// Check if the OS supports atomic close-on-exec for sockets
pub fn socket_atomic_cloexec() -> bool {
pub const fn socket_atomic_cloexec() -> bool {
false
}
}
2 changes: 1 addition & 1 deletion src/mount/bsd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ impl NmountError {
}

/// Returns the inner [`Error`]
pub fn error(&self) -> Error {
pub const fn error(&self) -> Error {
self.errno
}

Expand Down
2 changes: 1 addition & 1 deletion src/mqueue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl MqAttr {
}
}

pub fn flags(&self) -> mq_attr_member_t {
pub const fn flags(&self) -> mq_attr_member_t {
self.mq_attr.mq_flags
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/poll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub struct PollFd {
impl PollFd {
/// Creates a new `PollFd` specifying the events of interest
/// for a given file descriptor.
pub fn new(fd: RawFd, events: PollFlags) -> PollFd {
pub const fn new(fd: RawFd, events: PollFlags) -> PollFd {
PollFd {
pollfd: libc::pollfd {
fd,
Expand Down
2 changes: 1 addition & 1 deletion src/sched.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ mod sched_linux_like {
}

/// Return the maximum number of CPU in CpuSet
pub fn count() -> usize {
pub const fn count() -> usize {
8 * mem::size_of::<libc::cpu_set_t>()
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/sys/aio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ libc_enum! {
/// Mode for `AioCb::fsync`. Controls whether only data or both data and
/// metadata are synced.
#[repr(i32)]
#[non_exhaustive]
pub enum AioFsyncMode {
/// do it like `fsync`
O_SYNC,
Expand All @@ -57,6 +58,7 @@ libc_enum! {
/// given `aiocb` should be used for a read operation, a write operation, or
/// ignored. Has no effect for any other aio functions.
#[repr(i32)]
#[non_exhaustive]
pub enum LioOpcode {
LIO_NOP,
LIO_WRITE,
Expand Down
1 change: 1 addition & 0 deletions src/sys/epoll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ libc_bitflags!(

#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[repr(i32)]
#[non_exhaustive]
pub enum EpollOp {
EpollCtlAdd = libc::EPOLL_CTL_ADD,
EpollCtlDel = libc::EPOLL_CTL_DEL,
Expand Down
1 change: 1 addition & 0 deletions src/sys/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type type_of_event_filter = i16;
libc_enum! {
#[cfg_attr(target_os = "netbsd", repr(u32))]
#[cfg_attr(not(target_os = "netbsd"), repr(i16))]
#[non_exhaustive]
pub enum EventFilter {
EVFILT_AIO,
/// Returns whenever there is no remaining data in the write buffer
Expand Down
1 change: 1 addition & 0 deletions src/sys/mman.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ libc_enum!{
///
/// Used by [`madvise`](./fn.madvise.html).
#[repr(i32)]
#[non_exhaustive]
pub enum MmapAdvise {
/// No further special treatment. This is the default.
MADV_NORMAL,
Expand Down
1 change: 1 addition & 0 deletions src/sys/ptrace/bsd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ cfg_if! {
libc_enum! {
#[repr(i32)]
/// Ptrace Request enum defining the action to be taken.
#[non_exhaustive]
pub enum Request {
PT_TRACE_ME,
PT_READ_I,
Expand Down
2 changes: 2 additions & 0 deletions src/sys/ptrace/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ libc_enum!{
#[cfg_attr(not(any(target_env = "musl", target_os = "android")), repr(u32))]
#[cfg_attr(any(target_env = "musl", target_os = "android"), repr(i32))]
/// Ptrace Request enum defining the action to be taken.
#[non_exhaustive]
pub enum Request {
PTRACE_TRACEME,
PTRACE_PEEKTEXT,
Expand Down Expand Up @@ -123,6 +124,7 @@ libc_enum!{
/// Using the ptrace options the tracer can configure the tracee to stop
/// at certain events. This enum is used to define those events as defined
/// in `man ptrace`.
#[non_exhaustive]
pub enum Event {
/// Event that stops before a return from fork or clone.
PTRACE_EVENT_FORK,
Expand Down
2 changes: 2 additions & 0 deletions src/sys/quota.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ libc_enum!{
libc_enum!{
/// The scope of the quota.
#[repr(i32)]
#[non_exhaustive]
pub enum QuotaType {
/// Specify a user quota
USRQUOTA,
Expand All @@ -53,6 +54,7 @@ libc_enum!{
libc_enum!{
/// The type of quota format to use.
#[repr(i32)]
#[non_exhaustive]
pub enum QuotaFmt {
/// Use the original quota format.
QFMT_VFS_OLD,
Expand Down
1 change: 1 addition & 0 deletions src/sys/reboot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ libc_enum! {
/// See [`set_cad_enabled()`](fn.set_cad_enabled.html) for
/// enabling/disabling Ctrl-Alt-Delete.
#[repr(i32)]
#[non_exhaustive]
pub enum RebootMode {
RB_HALT_SYSTEM,
RB_KEXEC,
Expand Down
4 changes: 3 additions & 1 deletion src/sys/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ libc_enum!{
// We would prefer to use the libc::c_int alias in the repr attribute. Unfortunately
// this is not (yet) possible.
#[repr(i32)]
#[non_exhaustive]
pub enum Signal {
SIGHUP,
SIGINT,
Expand Down Expand Up @@ -356,7 +357,7 @@ impl Iterator for SignalIterator {
}

impl Signal {
pub fn iterator() -> SignalIterator {
pub const fn iterator() -> SignalIterator {
SignalIterator{next: 0}
}
}
Expand Down Expand Up @@ -396,6 +397,7 @@ libc_bitflags!{

libc_enum! {
#[repr(i32)]
#[non_exhaustive]
pub enum SigmaskHow {
SIG_BLOCK,
SIG_UNBLOCK,
Expand Down
Loading

0 comments on commit 3c45e08

Please sign in to comment.