mirror of
https://chromium.googlesource.com/crosvm/crosvm
synced 2025-02-05 18:20:34 +00:00
fdac5ede46
Found by running: `cargo rustc -- -D bare_trait_objects` Bare trait objects like `&Trait` and `Box<Trait>` are soft-deprecated in 2018 edition and will start warning at some point. As part of this, I replaced `Box<Trait + 'static>` with `Box<dyn Trait>` because the 'static bound is implied for boxed trait objects. TEST=cargo check --all-features TEST=cargo check --target aarch64-unknown-linux-gnu TEST=local kokoro Change-Id: I41c4f13530bece8a34a8ed1c1afd7035b8f86f19 Reviewed-on: https://chromium-review.googlesource.com/1513059 Commit-Ready: ChromeOS CL Exonerator Bot <chromiumos-cl-exonerator@appspot.gserviceaccount.com> Tested-by: David Tolnay <dtolnay@chromium.org> Tested-by: kokoro <noreply+kokoro@google.com> Reviewed-by: David Tolnay <dtolnay@chromium.org>
53 lines
1.5 KiB
Rust
53 lines
1.5 KiB
Rust
// Copyright 2018 The Chromium OS Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
|
|
use std::os::unix::io::AsRawFd;
|
|
|
|
use libc::{fcntl, EINVAL, F_GETFL, O_ACCMODE, O_RDONLY, O_RDWR, O_WRONLY};
|
|
|
|
use crate::{errno_result, Error, Result};
|
|
|
|
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
|
pub enum FileFlags {
|
|
Read,
|
|
Write,
|
|
ReadWrite,
|
|
}
|
|
|
|
impl FileFlags {
|
|
pub fn from_file(file: &dyn AsRawFd) -> Result<FileFlags> {
|
|
// Trivially safe because fcntl with the F_GETFL command is totally safe and we check for
|
|
// error.
|
|
let flags = unsafe { fcntl(file.as_raw_fd(), F_GETFL) };
|
|
if flags == -1 {
|
|
errno_result()
|
|
} else {
|
|
match flags & O_ACCMODE {
|
|
O_RDONLY => Ok(FileFlags::Read),
|
|
O_WRONLY => Ok(FileFlags::Write),
|
|
O_RDWR => Ok(FileFlags::ReadWrite),
|
|
_ => Err(Error::new(EINVAL)),
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use crate::{pipe, EventFd};
|
|
|
|
#[test]
|
|
fn pipe_pair() {
|
|
let (read_pipe, write_pipe) = pipe(true).unwrap();
|
|
assert_eq!(FileFlags::from_file(&read_pipe).unwrap(), FileFlags::Read);
|
|
assert_eq!(FileFlags::from_file(&write_pipe).unwrap(), FileFlags::Write);
|
|
}
|
|
|
|
#[test]
|
|
fn eventfd() {
|
|
let evt = EventFd::new().unwrap();
|
|
assert_eq!(FileFlags::from_file(&evt).unwrap(), FileFlags::ReadWrite);
|
|
}
|
|
}
|