Skip to content

Move File to its own module. #239

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 1 commit into from
Apr 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion drivers/android/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ use core::{
};
use kernel::{
bindings, c_types,
file_operations::{File, FileOpener, FileOperations, IoctlCommand, IoctlHandler, PollTable},
file::File,
file_operations::{FileOpener, FileOperations, IoctlCommand, IoctlHandler, PollTable},
io_buffer::{IoBufferReader, IoBufferWriter},
linked_list::List,
pages::Pages,
Expand Down
3 changes: 2 additions & 1 deletion drivers/android/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use alloc::sync::Arc;
use core::{alloc::AllocError, mem::size_of, pin::Pin};
use kernel::{
bindings,
file_operations::{File, PollTable},
file::File,
file_operations::PollTable,
io_buffer::{IoBufferReader, IoBufferWriter},
linked_list::{GetLinks, Links, List},
prelude::*,
Expand Down
41 changes: 41 additions & 0 deletions rust/kernel/file.rs
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 }
}
}
34 changes: 1 addition & 33 deletions rust/kernel/file_operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,45 +13,13 @@ use alloc::sync::Arc;
use crate::{
bindings, c_types,
error::{Error, KernelResult},
file::File,
io_buffer::{IoBufferReader, IoBufferWriter},
iov_iter::IovIter,
sync::{CondVar, Ref, RefCounted},
user_ptr::{UserSlicePtr, UserSlicePtrReader, UserSlicePtrWriter},
};

/// Wraps the kernel's `struct file`.
///
/// # Invariants
///
/// The pointer [`File::ptr`] is non-null and valid.
pub struct File {
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.
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 }
}
}

/// Wraps the kernel's `struct poll_table_struct`.
///
/// # Invariants
Expand Down
1 change: 1 addition & 0 deletions rust/kernel/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub mod buffer;
pub mod c_types;
pub mod chrdev;
mod error;
pub mod file;
pub mod file_operations;
pub mod miscdev;
pub mod pages;
Expand Down
3 changes: 2 additions & 1 deletion samples/rust/rust_miscdev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ use core::pin::Pin;
use kernel::prelude::*;
use kernel::{
cstr,
file_operations::{File, FileOpener, FileOperations},
file::File,
file_operations::{FileOpener, FileOperations},
io_buffer::{IoBufferReader, IoBufferWriter},
miscdev,
sync::{CondVar, Mutex},
Expand Down
3 changes: 2 additions & 1 deletion samples/rust/rust_random.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
#![feature(allocator_api, global_asm)]

use kernel::{
file_operations::{File, FileOperations},
file::File,
file_operations::FileOperations,
io_buffer::{IoBufferReader, IoBufferWriter},
prelude::*,
};
Expand Down
3 changes: 2 additions & 1 deletion samples/rust/rust_semaphore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ use core::{
};
use kernel::{
condvar_init, cstr, declare_file_operations,
file_operations::{File, FileOpener, FileOperations, IoctlCommand, IoctlHandler},
file::File,
file_operations::{FileOpener, FileOperations, IoctlCommand, IoctlHandler},
io_buffer::{IoBufferReader, IoBufferWriter},
miscdev::Registration,
mutex_init,
Expand Down