| 1 | |
| 2 | |
| 3 |
|
| 4 | use crate::{
|
| 5 | debugfs::file_ops::FileOps,
|
| 6 | prelude::*,
|
| 7 | str::{
|
| 8 | CStr,
|
| 9 | CStrExt as _, |
| 10 | },
|
| 11 | sync::Arc,
|
| 12 | };
|
| 13 |
|
| 14 | use core::marker::PhantomData;
|
| 15 |
|
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | pub(crate) struct Entry<'a> {
|
| 22 | entry: *mut bindings::dentry,
|
| 23 | |
| 24 | _parent: Option<Arc<Entry<'static>>>,
|
| 25 | |
| 26 | _phantom: PhantomData<&'a ()>,
|
| 27 | }
|
| 28 |
|
| 29 | |
| 30 | |
| 31 | unsafe impl Send for Entry<'_> {}
|
| 32 |
|
| 33 | |
| 34 | unsafe impl Sync for Entry<'_> {}
|
| 35 |
|
| 36 | impl Entry<'static> {
|
| 37 | pub(crate) fn dynamic_dir(name: &CStr, parent: Option<Arc<Self>>) -> Self {
|
| 38 | let parent_ptr = match &parent {
|
| 39 | Some(entry) => entry.as_ptr(),
|
| 40 | None => core::ptr::null_mut(),
|
| 41 | };
|
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | let entry = unsafe { bindings::debugfs_create_dir(name.as_char_ptr(), parent_ptr) };
|
| 47 |
|
| 48 | Entry {
|
| 49 | entry,
|
| 50 | _parent: parent,
|
| 51 | _phantom: PhantomData,
|
| 52 | }
|
| 53 | }
|
| 54 |
|
| 55 | |
| 56 | |
| 57 | |
| 58 | pub(crate) unsafe fn dynamic_file<T>(
|
| 59 | name: &CStr,
|
| 60 | parent: Arc<Self>,
|
| 61 | data: &T,
|
| 62 | file_ops: &'static FileOps<T>,
|
| 63 | ) -> Self {
|
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | let entry = unsafe {
|
| 71 | bindings::debugfs_create_file_full(
|
| 72 | name.as_char_ptr(),
|
| 73 | file_ops.mode(),
|
| 74 | parent.as_ptr(),
|
| 75 | core::ptr::from_ref(data) as *mut c_void,
|
| 76 | core::ptr::null(),
|
| 77 | &**file_ops,
|
| 78 | )
|
| 79 | };
|
| 80 |
|
| 81 | Entry {
|
| 82 | entry,
|
| 83 | _parent: Some(parent),
|
| 84 | _phantom: PhantomData,
|
| 85 | }
|
| 86 | }
|
| 87 | }
|
| 88 |
|
| 89 | impl<'a> Entry<'a> {
|
| 90 | pub(crate) fn dir(name: &CStr, parent: Option<&'a Entry<'_>>) -> Self {
|
| 91 | let parent_ptr = match &parent {
|
| 92 | Some(entry) => entry.as_ptr(),
|
| 93 | None => core::ptr::null_mut(),
|
| 94 | };
|
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | let entry = unsafe { bindings::debugfs_create_dir(name.as_char_ptr(), parent_ptr) };
|
| 101 |
|
| 102 | Entry {
|
| 103 | entry,
|
| 104 | _parent: None,
|
| 105 | _phantom: PhantomData,
|
| 106 | }
|
| 107 | }
|
| 108 |
|
| 109 | pub(crate) fn file<T>(
|
| 110 | name: &CStr,
|
| 111 | parent: &'a Entry<'_>,
|
| 112 | data: &'a T,
|
| 113 | file_ops: &FileOps<T>,
|
| 114 | ) -> Self {
|
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | let entry = unsafe {
|
| 124 | bindings::debugfs_create_file_full(
|
| 125 | name.as_char_ptr(),
|
| 126 | file_ops.mode(),
|
| 127 | parent.as_ptr(),
|
| 128 | core::ptr::from_ref(data) as *mut c_void,
|
| 129 | core::ptr::null(),
|
| 130 | &**file_ops,
|
| 131 | )
|
| 132 | };
|
| 133 |
|
| 134 | Entry {
|
| 135 | entry,
|
| 136 | _parent: None,
|
| 137 | _phantom: PhantomData,
|
| 138 | }
|
| 139 | }
|
| 140 | }
|
| 141 |
|
| 142 | impl Entry<'_> {
|
| 143 | |
| 144 | pub(crate) fn empty() -> Self {
|
| 145 | Self {
|
| 146 | entry: core::ptr::null_mut(),
|
| 147 | _parent: None,
|
| 148 | _phantom: PhantomData,
|
| 149 | }
|
| 150 | }
|
| 151 |
|
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | pub(crate) fn as_ptr(&self) -> *mut bindings::dentry {
|
| 160 | self.entry
|
| 161 | }
|
| 162 | }
|
| 163 |
|
| 164 | impl Drop for Entry<'_> {
|
| 165 | fn drop(&mut self) {
|
| 166 | |
| 167 | |
| 168 | unsafe { bindings::debugfs_remove(self.as_ptr()) }
|
| 169 | }
|
| 170 | }
|