Skip to content

Commit

Permalink
📝 restructure unit fs directory (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
Xudong-Huang committed Mar 7, 2019
1 parent 76992c9 commit d0710f4
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 75 deletions.
17 changes: 15 additions & 2 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,27 @@ use rustc_version::{version_meta, Channel};
fn gen_linux_aio_bindings() {
use std::env;
use std::path::PathBuf;

let target = env::var("TARGET").expect("Cargo build scripts always have TARGET");

// The bindgen::Builder is the main entry point
// to bindgen, and lets you build up options for
// the resulting bindings.
let bindings = bindgen::Builder::default()
let mut bindings = bindgen::Builder::default()
.trust_clang_mangling(false)
.clang_arg("-target")
.clang_arg(target);

if let Ok(sysroot) = env::var("SYSROOT") {
bindings = bindings
.clang_arg("--sysroot")
.clang_arg(sysroot);
}

let bindings = bindings
// The input header we would like to generate
// bindings for.
.header("linux_aio_wrapper.h")
.header("src/io/sys/unix/fs/linux/aio_wrapper.h")
// Finish the builder and generate the bindings.
.generate()
// Unwrap the Result and panic on failure.
Expand Down
1 change: 1 addition & 0 deletions src/io/sys/unix/fs/bsd/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use std::ops::Deref;
use std::os::unix::io::{AsRawFd, RawFd};
use std::sync::atomic::Ordering;

use super::super::{co_io_result, from_nix_error};
use super::{AsFileIo, FileIo};
use coroutine_impl::{co_cancel_data, CoroutineImpl, EventSource};
use io::sys::{co_io_result, from_nix_error};
use nix::unistd::read;
use sync::delay_drop::DelayDrop;
use yield_now::yield_with;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use std::io;
use std::os::unix::io::{AsRawFd, RawFd};
use std::sync::atomic::Ordering;

use super::super::{co_io_result, from_nix_error};
use super::{AsFileIo, FileIo};
use coroutine_impl::{CoroutineImpl, EventSource};
use io::sys::{co_io_result, from_nix_error};
use nix::unistd::write;
use sync::delay_drop::DelayDrop;
use yield_now::yield_with;
Expand Down
72 changes: 72 additions & 0 deletions src/io/sys/unix/fs/linux/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
mod aio_bindings;
mod fs_read;
mod fs_write;

pub use self::fs_read::FileRead;
pub use self::fs_write::FileWrite;

use std::fs::File;
use std::io;
use std::os::unix::io::{AsRawFd, RawFd};

use self::aio_bindings::EFD_NONBLOCK;
use io::sys::IoData;

pub struct EventFd(RawFd);

impl AsRawFd for EventFd {
fn as_raw_fd(&self) -> RawFd {
self.0
}
}

pub struct FileIo {
pub fd: EventFd,
pub io: IoData,
}

impl Drop for FileIo {
// drop the EventFd first
fn drop(&mut self) {
use nix::unistd::close;
if self.fd.0 >= 0 {
close(self.fd.0).ok();
}
}
}

impl ::std::ops::Deref for FileIo {
type Target = IoData;
fn deref(&self) -> &IoData {
&self.io
}
}

pub trait AsFileIo {
fn as_file_io(&self) -> &FileIo;
}

impl FileIo {
pub fn new(file: Option<&File>) -> io::Result<FileIo> {
use libc::{eventfd, O_CLOEXEC};
if file.is_some() {
let flags = { O_CLOEXEC | EFD_NONBLOCK as i32 };

let fd = unsafe { eventfd(0, flags) };

if fd < 0 {
return Err(io::Error::last_os_error());
}
let ev_fd = EventFd(fd);
let io = crate::io::sys::add_file(&ev_fd)?;
Ok(FileIo { io, fd: ev_fd })
} else {
// don't close any fd, dummy one
let fd = EventFd(-1);
Ok(FileIo {
io: IoData::new(&fd),
fd,
})
}
}
}
83 changes: 16 additions & 67 deletions src/io/sys/unix/fs/mod.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
mod fs_read;
mod fs_write;

pub use self::fs_read::FileRead;
pub use self::fs_write::FileWrite;
#[cfg(any(target_os = "linux", target_os = "android"))]
#[path = "linux/mod.rs"]
mod detail;

#[cfg(any(
target_os = "bitrig",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "ios",
target_os = "macos",
target_os = "netbsd",
target_os = "openbsd"
))]
#[path = "bsd/mod.rs"]
mod detail;

use std::fs::{File, OpenOptions};
use std::io;
use std::os::unix::io::{AsRawFd, RawFd};
use std::path::Path;

use super::linux_aio_bindings::EFD_NONBLOCK;
use super::IoData;

pub fn open<P: AsRef<Path>>(path: P) -> io::Result<File> {
File::open(path)
}
Expand All @@ -24,61 +30,4 @@ pub fn open_with_options<P: AsRef<Path>>(options: &mut OpenOptions, path: P) ->
options.open(path.as_ref())
}

pub struct EventFd(RawFd);

impl AsRawFd for EventFd {
fn as_raw_fd(&self) -> RawFd {
self.0
}
}

pub struct FileIo {
pub fd: EventFd,
pub io: IoData,
}

impl Drop for FileIo {
// drop the EventFd first
fn drop(&mut self) {
use nix::unistd::close;
if self.fd.0 >= 0 {
close(self.fd.0).ok();
}
}
}

impl ::std::ops::Deref for FileIo {
type Target = IoData;
fn deref(&self) -> &IoData {
&self.io
}
}

pub trait AsFileIo {
fn as_file_io(&self) -> &FileIo;
}

impl FileIo {
pub fn new(file: Option<&File>) -> io::Result<FileIo> {
use libc::{eventfd, O_CLOEXEC};
if file.is_some() {
let flags = { O_CLOEXEC | EFD_NONBLOCK as i32 };

let fd = unsafe { eventfd(0, flags) };

if fd < 0 {
return Err(io::Error::last_os_error());
}
let ev_fd = EventFd(fd);
let io = super::add_file(&ev_fd)?;
Ok(FileIo { io, fd: ev_fd })
} else {
// don't close any fd, dummy one
let fd = EventFd(-1);
Ok(FileIo {
io: IoData::new(&fd),
fd,
})
}
}
}
pub use self::detail::{AsFileIo, FileIo, FileRead, FileWrite};
3 changes: 0 additions & 3 deletions src/io/sys/unix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
#[path = "epoll.rs"]
mod select;

#[cfg(any(target_os = "linux", target_os = "android"))]
mod linux_aio_bindings;

#[cfg(any(
target_os = "bitrig",
target_os = "dragonfly",
Expand Down
2 changes: 1 addition & 1 deletion src/io/sys/windows/fs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl ::std::ops::Deref for FileIo {
impl FileIo {
pub fn new(file: Option<&File>) -> io::Result<FileIo> {
if let Some(file) = file {
super::add_file(file).map(|io| FileIo(io))
super::add_file(file).map(FileIo)
} else {
Ok(FileIo(IoData::new(0)))
}
Expand Down

0 comments on commit d0710f4

Please sign in to comment.