|
8 | 8 | use crate::{bindings, cred::CredentialRef, error::Error, Result};
|
9 | 9 | use core::{mem::ManuallyDrop, ops::Deref};
|
10 | 10 |
|
| 11 | +/// Flags associated with a [`File`]. |
| 12 | +/// |
| 13 | +/// It is tagged with `non_exhaustive` to prevent users from instantiating it. |
| 14 | +#[non_exhaustive] |
| 15 | +pub struct FileFlags; |
| 16 | + |
| 17 | +impl FileFlags { |
| 18 | + /// File is opened in append mode. |
| 19 | + pub const O_APPEND: u32 = bindings::O_APPEND; |
| 20 | + |
| 21 | + /// Signal-driven I/O is enabled. |
| 22 | + pub const O_ASYNC: u32 = bindings::FASYNC; |
| 23 | + |
| 24 | + /// Close-on-exec flag is set. |
| 25 | + pub const O_CLOEXEC: u32 = bindings::O_CLOEXEC; |
| 26 | + |
| 27 | + /// File was created if it didn't already exist. |
| 28 | + pub const O_CREAT: u32 = bindings::O_CREAT; |
| 29 | + |
| 30 | + /// Direct I/O is enabled for this file. |
| 31 | + pub const O_DIRECT: u32 = bindings::O_DIRECT; |
| 32 | + |
| 33 | + /// File must be a directory. |
| 34 | + pub const O_DIRECTORY: u32 = bindings::O_DIRECTORY; |
| 35 | + |
| 36 | + /// Like `Self::O_SYNC` except metadata is not synced. |
| 37 | + pub const O_DSYNC: u32 = bindings::O_DSYNC; |
| 38 | + |
| 39 | + /// Ensure that this file is created with the `open(2)` call. |
| 40 | + pub const O_EXCL: u32 = bindings::O_EXCL; |
| 41 | + |
| 42 | + /// Large file size enabled (`off64_t` over `off_t`) |
| 43 | + pub const O_LARGEFILE: u32 = bindings::O_LARGEFILE; |
| 44 | + |
| 45 | + /// Do not update the file last access time. |
| 46 | + pub const O_NOATIME: u32 = bindings::O_NOATIME; |
| 47 | + |
| 48 | + /// File should not be used as process's controlling terminal. |
| 49 | + pub const O_NOCTTY: u32 = bindings::O_NOCTTY; |
| 50 | + |
| 51 | + /// If basename of path is a symbolic link, fail open. |
| 52 | + pub const O_NOFOLLOW: u32 = bindings::O_NOFOLLOW; |
| 53 | + |
| 54 | + /// File is using nonblocking I/O. |
| 55 | + pub const O_NONBLOCK: u32 = bindings::O_NONBLOCK; |
| 56 | + |
| 57 | + /// Also known as `O_NDELAY`. |
| 58 | + /// |
| 59 | + /// This is effectively the same flag as [`Self::O_NONBLOCK`] on all architectures |
| 60 | + /// except SPARC64. |
| 61 | + pub const O_NDELAY: u32 = bindings::O_NDELAY; |
| 62 | + |
| 63 | + /// Used to obtain a path file descriptor. |
| 64 | + pub const O_PATH: u32 = bindings::O_PATH; |
| 65 | + |
| 66 | + /// Write operations on this file will flush data and metadata. |
| 67 | + pub const O_SYNC: u32 = bindings::O_SYNC; |
| 68 | + |
| 69 | + /// This file is an unnamed temporary regular file. |
| 70 | + pub const O_TMPFILE: u32 = bindings::O_TMPFILE; |
| 71 | + |
| 72 | + /// File should be truncated to length 0. |
| 73 | + pub const O_TRUNC: u32 = bindings::O_TRUNC; |
| 74 | + |
| 75 | + /// Bitmask for access mode flags. |
| 76 | + /// |
| 77 | + /// # Examples |
| 78 | + /// |
| 79 | + /// ``` |
| 80 | + /// use kernel::file::FileFlags; |
| 81 | + /// # fn do_something() {} |
| 82 | + /// # let flags = 0; |
| 83 | + /// if (flags & FileFlags::O_ACCMODE) == FileFlags::O_RDONLY { |
| 84 | + /// do_something(); |
| 85 | + /// } |
| 86 | + /// ``` |
| 87 | + pub const O_ACCMODE: u32 = bindings::O_ACCMODE; |
| 88 | + |
| 89 | + /// File is read only. |
| 90 | + pub const O_RDONLY: u32 = bindings::O_RDONLY; |
| 91 | + |
| 92 | + /// File is write only. |
| 93 | + pub const O_WRONLY: u32 = bindings::O_WRONLY; |
| 94 | + |
| 95 | + /// File can be both read and written. |
| 96 | + pub const O_RDWR: u32 = bindings::O_RDWR; |
| 97 | +} |
| 98 | + |
11 | 99 | /// Wraps the kernel's `struct file`.
|
12 | 100 | ///
|
13 | 101 | /// # Invariants
|
@@ -56,6 +144,8 @@ impl File {
|
56 | 144 | }
|
57 | 145 |
|
58 | 146 | /// Returns the flags associated with the file.
|
| 147 | + /// |
| 148 | + /// The flags are a combination of the constants in [`FileFlags`]. |
59 | 149 | pub fn flags(&self) -> u32 {
|
60 | 150 | // SAFETY: `File::ptr` is guaranteed to be valid by the type invariants.
|
61 | 151 | unsafe { (*self.ptr).f_flags }
|
|
0 commit comments