kernel/object/capability/file/mod.rs
1//! File operations capability module
2//!
3//! This module provides system calls and traits for FileObject capability,
4//! which extends StreamOps with file-specific operations like seek and metadata.
5
6use crate::object::capability::stream::{StreamOps, StreamError};
7
8pub mod syscall;
9
10pub use syscall::{sys_file_seek, sys_file_truncate};
11
12/// Seek operations for file positioning
13#[derive(Debug, Clone, Copy)]
14pub enum SeekFrom {
15 /// Seek from the beginning of the file
16 Start(u64),
17 /// Seek relative to the current position
18 Current(i64),
19 /// Seek relative to the end of the file
20 End(i64),
21}
22
23/// Trait for file objects
24///
25/// This trait represents a file-like object that supports both stream operations
26/// and file-specific operations like seeking and metadata access.
27/// Directory reading is handled through normal read() operations.
28pub trait FileObject: StreamOps {
29 /// Seek to a position in the file stream
30 fn seek(&self, whence: SeekFrom) -> Result<u64, StreamError>;
31
32 /// Get metadata about the file
33 fn metadata(&self) -> Result<crate::fs::FileMetadata, StreamError>;
34
35 /// Truncate the file to the specified size
36 ///
37 /// This method changes the size of the file to the specified length.
38 /// If the new size is smaller than the current size, the file is truncated.
39 /// If the new size is larger, the file is extended with zero bytes.
40 ///
41 /// # Arguments
42 ///
43 /// * `size` - New size of the file in bytes
44 ///
45 /// # Returns
46 ///
47 /// * `Result<(), StreamError>` - Ok if the file was truncated successfully
48 ///
49 /// # Errors
50 ///
51 /// * `StreamError` - If the file is a directory or the operation is not supported
52 fn truncate(&self, size: u64) -> Result<(), StreamError> {
53 let _ = size;
54 Err(StreamError::NotSupported)
55 }
56}