forked from torvalds/linux
-
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.
Merge pull request torvalds#239 from wedsonaf/file
Move `File` to its own module.
- Loading branch information
Showing
8 changed files
with
53 additions
and
38 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
|
||
//! Files and file descriptors. | ||
//! | ||
//! C headers: [`include/linux/fs.h`](../../../../include/linux/fs.h) and | ||
//! [`include/linux/file.h`](../../../../include/linux/file.h) | ||
use crate::bindings; | ||
|
||
/// Wraps the kernel's `struct file`. | ||
/// | ||
/// # Invariants | ||
/// | ||
/// The pointer [`File::ptr`] is non-null and valid. | ||
pub struct File { | ||
pub(crate) ptr: *const bindings::file, | ||
} | ||
|
||
impl File { | ||
/// Constructs a new [`struct file`] wrapper. | ||
/// | ||
/// # Safety | ||
/// | ||
/// The pointer `ptr` must be non-null and valid for the lifetime of the object. | ||
pub(crate) unsafe fn from_ptr(ptr: *const bindings::file) -> File { | ||
// INVARIANTS: the safety contract ensures the type invariant will hold. | ||
File { ptr } | ||
} | ||
|
||
/// Returns the current seek/cursor/pointer position (`struct file::f_pos`). | ||
pub fn pos(&self) -> u64 { | ||
// SAFETY: `File::ptr` is guaranteed to be valid by the type invariants. | ||
unsafe { (*self.ptr).f_pos as u64 } | ||
} | ||
|
||
/// Returns whether the file is in blocking mode. | ||
pub fn is_blocking(&self) -> bool { | ||
// SAFETY: `File::ptr` is guaranteed to be valid by the type invariants. | ||
unsafe { (*self.ptr).f_flags & bindings::O_NONBLOCK == 0 } | ||
} | ||
} |
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
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