-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
fs: implement unix OpenOptionsExt for OpenOptions #2515
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
b07b2b9
fs: implement unix OpenOptionsExt for OpenOptions
c4c04f2
added missing #[cfg(unix)]
e9dbbc8
remove unecessary #[doc(hidden)]
chovine 07683b6
add OpenOptionsExt trait definition
chovine d119f2a
fix documentation errors
chovine 32ed5c1
fix wrong link in documentation
chovine 02651c4
fix ci
chovine 60cd05d
fix ci #2
chovine 5aa9413
fix ci 3
chovine 4c9a7e0
fix ci
chovine a9a17b5
Merge remote-tracking branch 'upstream/master' into HEAD
Darksonn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,79 @@ | ||
use crate::fs::open_options::OpenOptions; | ||
use std::os::unix::fs::OpenOptionsExt as StdOpenOptionsExt; | ||
|
||
/// Unix-specific extensions to [`fs::OpenOptions`]. | ||
/// | ||
/// This mirrors the definition of [`std::os::unix::fs::OpenOptionsExt`]. | ||
/// | ||
/// | ||
/// [`fs::OpenOptions`]: crate::fs::OpenOptions | ||
/// [`std::os::unix::fs::OpenOptionsExt`]: std::os::unix::fs::OpenOptionsExt | ||
pub trait OpenOptionsExt { | ||
/// Sets the mode bits that a new file will be created with. | ||
/// | ||
/// If a new file is created as part of an `OpenOptions::open` call then this | ||
/// specified `mode` will be used as the permission bits for the new file. | ||
/// If no `mode` is set, the default of `0o666` will be used. | ||
/// The operating system masks out bits with the system's `umask`, to produce | ||
/// the final permissions. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ```no_run | ||
/// use tokio::fs::OpenOptions; | ||
/// use tokio::fs::os::unix::OpenOptionsExt; | ||
/// use std::io; | ||
/// | ||
/// #[tokio::main] | ||
/// async fn main() -> io::Result<()> { | ||
/// let mut options = OpenOptions::new(); | ||
/// options.mode(0o644); // Give read/write for owner and read for others. | ||
/// let file = options.open("foo.txt").await?; | ||
/// | ||
/// Ok(()) | ||
/// } | ||
/// ``` | ||
fn mode(&mut self, mode: u32) -> &mut Self; | ||
|
||
/// Pass custom flags to the `flags` argument of `open`. | ||
/// | ||
/// The bits that define the access mode are masked out with `O_ACCMODE`, to | ||
/// ensure they do not interfere with the access mode set by Rusts options. | ||
/// | ||
/// Custom flags can only set flags, not remove flags set by Rusts options. | ||
/// This options overwrites any previously set custom flags. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ```no_run | ||
/// use libc; | ||
/// use tokio::fs::OpenOptions; | ||
/// use tokio::fs::os::unix::OpenOptionsExt; | ||
/// use std::io; | ||
/// | ||
/// #[tokio::main] | ||
/// async fn main() -> io::Result<()> { | ||
/// let mut options = OpenOptions::new(); | ||
/// options.write(true); | ||
/// if cfg!(unix) { | ||
/// options.custom_flags(libc::O_NOFOLLOW); | ||
/// } | ||
/// let file = options.open("foo.txt").await?; | ||
/// | ||
/// Ok(()) | ||
/// } | ||
/// ``` | ||
fn custom_flags(&mut self, flags: i32) -> &mut Self; | ||
} | ||
|
||
impl OpenOptionsExt for OpenOptions { | ||
fn mode(&mut self, mode: u32) -> &mut OpenOptions { | ||
self.as_inner_mut().mode(mode); | ||
self | ||
} | ||
|
||
fn custom_flags(&mut self, flags: i32) -> &mut OpenOptions { | ||
self.as_inner_mut().custom_flags(flags); | ||
self | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.