-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add set_blocking function to IO (#109)
* Add set_blocking function to IO * Fix issues * Cargo fmt
- Loading branch information
1 parent
4c85678
commit 889ddc5
Showing
9 changed files
with
165 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,13 @@ | ||
mod read_chars; | ||
mod read_lines; | ||
#[cfg(target_family = "unix")] | ||
mod set_blocking; | ||
#[cfg(target_family = "unix")] | ||
mod unix_functions; | ||
|
||
pub use read_chars::*; | ||
pub use read_lines::read_lines; | ||
#[cfg(target_family = "unix")] | ||
pub use set_blocking::set_blocking; | ||
#[cfg(target_family = "unix")] | ||
pub use unix_functions::{chmod, chown, stat}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
use nix::libc::{fcntl, F_GETFL, F_SETFL, O_NONBLOCK}; | ||
use std::io; | ||
use std::os::unix::io::AsRawFd; | ||
|
||
// TODO: This is using libc directly, but nix has a wrapper for this. We should use that instead. | ||
pub fn set_blocking<T: AsRawFd>(fd: &T, blocking: bool) -> io::Result<()> { | ||
let raw_fd = fd.as_raw_fd(); | ||
let flags = unsafe { fcntl(raw_fd, F_GETFL, 0) }; | ||
if flags < 0 { | ||
return Err(io::Error::last_os_error()); | ||
} | ||
|
||
let flags = if blocking { | ||
flags & !O_NONBLOCK | ||
} else { | ||
flags | O_NONBLOCK | ||
}; | ||
let res = unsafe { fcntl(raw_fd, F_SETFL, flags) }; | ||
if res != 0 { | ||
return Err(io::Error::last_os_error()); | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters