Skip to content

Commit f790ecb

Browse files
committed
Move File to its own module.
This is a pure refactor in preparation for adding more features to `File` so that file descriptors can be transferred between processes by Binder. Signed-off-by: Wedson Almeida Filho <[email protected]>
1 parent 912beef commit f790ecb

File tree

8 files changed

+53
-38
lines changed

8 files changed

+53
-38
lines changed

drivers/android/process.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ use core::{
88
};
99
use kernel::{
1010
bindings, c_types,
11-
file_operations::{File, FileOpener, FileOperations, IoctlCommand, IoctlHandler, PollTable},
11+
file::File,
12+
file_operations::{FileOpener, FileOperations, IoctlCommand, IoctlHandler, PollTable},
1213
io_buffer::{IoBufferReader, IoBufferWriter},
1314
linked_list::List,
1415
pages::Pages,

drivers/android/thread.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use alloc::sync::Arc;
44
use core::{alloc::AllocError, mem::size_of, pin::Pin};
55
use kernel::{
66
bindings,
7-
file_operations::{File, PollTable},
7+
file::File,
8+
file_operations::PollTable,
89
io_buffer::{IoBufferReader, IoBufferWriter},
910
linked_list::{GetLinks, Links, List},
1011
prelude::*,

rust/kernel/file.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
//! Files and file descriptors.
4+
//!
5+
//! C headers: [`include/linux/fs.h`](../../../../include/linux/fs.h) and
6+
//! [`include/linux/file.h`](../../../../include/linux/file.h)
7+
8+
use crate::bindings;
9+
10+
/// Wraps the kernel's `struct file`.
11+
///
12+
/// # Invariants
13+
///
14+
/// The pointer [`File::ptr`] is non-null and valid.
15+
pub struct File {
16+
pub(crate) ptr: *const bindings::file,
17+
}
18+
19+
impl File {
20+
/// Constructs a new [`struct file`] wrapper.
21+
///
22+
/// # Safety
23+
///
24+
/// The pointer `ptr` must be non-null and valid for the lifetime of the object.
25+
pub(crate) unsafe fn from_ptr(ptr: *const bindings::file) -> File {
26+
// INVARIANTS: the safety contract ensures the type invariant will hold.
27+
File { ptr }
28+
}
29+
30+
/// Returns the current seek/cursor/pointer position (`struct file::f_pos`).
31+
pub fn pos(&self) -> u64 {
32+
// SAFETY: `File::ptr` is guaranteed to be valid by the type invariants.
33+
unsafe { (*self.ptr).f_pos as u64 }
34+
}
35+
36+
/// Returns whether the file is in blocking mode.
37+
pub fn is_blocking(&self) -> bool {
38+
// SAFETY: `File::ptr` is guaranteed to be valid by the type invariants.
39+
unsafe { (*self.ptr).f_flags & bindings::O_NONBLOCK == 0 }
40+
}
41+
}

rust/kernel/file_operations.rs

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,45 +13,13 @@ use alloc::sync::Arc;
1313
use crate::{
1414
bindings, c_types,
1515
error::{Error, KernelResult},
16+
file::File,
1617
io_buffer::{IoBufferReader, IoBufferWriter},
1718
iov_iter::IovIter,
1819
sync::{CondVar, Ref, RefCounted},
1920
user_ptr::{UserSlicePtr, UserSlicePtrReader, UserSlicePtrWriter},
2021
};
2122

22-
/// Wraps the kernel's `struct file`.
23-
///
24-
/// # Invariants
25-
///
26-
/// The pointer [`File::ptr`] is non-null and valid.
27-
pub struct File {
28-
ptr: *const bindings::file,
29-
}
30-
31-
impl File {
32-
/// Constructs a new [`struct file`] wrapper.
33-
///
34-
/// # Safety
35-
///
36-
/// The pointer `ptr` must be non-null and valid for the lifetime of the object.
37-
unsafe fn from_ptr(ptr: *const bindings::file) -> File {
38-
// INVARIANTS: the safety contract ensures the type invariant will hold.
39-
File { ptr }
40-
}
41-
42-
/// Returns the current seek/cursor/pointer position (`struct file::f_pos`).
43-
pub fn pos(&self) -> u64 {
44-
// SAFETY: `File::ptr` is guaranteed to be valid by the type invariants.
45-
unsafe { (*self.ptr).f_pos as u64 }
46-
}
47-
48-
/// Returns whether the file is in blocking mode.
49-
pub fn is_blocking(&self) -> bool {
50-
// SAFETY: `File::ptr` is guaranteed to be valid by the type invariants.
51-
unsafe { (*self.ptr).f_flags & bindings::O_NONBLOCK == 0 }
52-
}
53-
}
54-
5523
/// Wraps the kernel's `struct poll_table_struct`.
5624
///
5725
/// # Invariants

rust/kernel/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ pub mod buffer;
4040
pub mod c_types;
4141
pub mod chrdev;
4242
mod error;
43+
pub mod file;
4344
pub mod file_operations;
4445
pub mod miscdev;
4546
pub mod pages;

samples/rust/rust_miscdev.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ use core::pin::Pin;
1010
use kernel::prelude::*;
1111
use kernel::{
1212
cstr,
13-
file_operations::{File, FileOpener, FileOperations},
13+
file::File,
14+
file_operations::{FileOpener, FileOperations},
1415
io_buffer::{IoBufferReader, IoBufferWriter},
1516
miscdev,
1617
sync::{CondVar, Mutex},

samples/rust/rust_random.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
#![feature(allocator_api, global_asm)]
1010

1111
use kernel::{
12-
file_operations::{File, FileOperations},
12+
file::File,
13+
file_operations::FileOperations,
1314
io_buffer::{IoBufferReader, IoBufferWriter},
1415
prelude::*,
1516
};

samples/rust/rust_semaphore.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ use core::{
2323
};
2424
use kernel::{
2525
condvar_init, cstr, declare_file_operations,
26-
file_operations::{File, FileOpener, FileOperations, IoctlCommand, IoctlHandler},
26+
file::File,
27+
file_operations::{FileOpener, FileOperations, IoctlCommand, IoctlHandler},
2728
io_buffer::{IoBufferReader, IoBufferWriter},
2829
miscdev::Registration,
2930
mutex_init,

0 commit comments

Comments
 (0)