1use crate::pager::ErofsPacketReceiver;
6use crate::volume::ErofsVolume;
7use erofs::FileNode;
8use fidl_fuchsia_io as fio;
9use fuchsia_async as fasync;
10use std::sync::Arc;
11use vfs::ObjectRequestRef;
12use vfs::directory::entry::{DirectoryEntry, EntryInfo, GetEntryInfo, OpenRequest};
13use vfs::execution_scope::ExecutionScope;
14use vfs::file::connection::GetVmo;
15use vfs::file::{File, FileLike, FileOptions, SyncMode};
16use vfs::node::Node;
17
18pub struct ErofsFile {
20 volume: Arc<ErofsVolume>,
21 node: FileNode,
22 vmo: zx::Vmo,
23 registration: fasync::ReceiverRegistration<ErofsPacketReceiver>,
24}
25
26impl ErofsFile {
27 pub fn new(volume: Arc<ErofsVolume>, node: FileNode) -> Result<Arc<Self>, zx::Status> {
31 let file = Arc::new_cyclic(|weak| {
32 let (vmo, registration) = volume
33 .pager()
34 .create_vmo(weak.clone(), node.size())
35 .expect("Failed to create pager VMO");
36 Self { volume, node, vmo, registration }
37 });
38 Ok(file)
39 }
40
41 pub fn fs(&self) -> &erofs::ErofsFilesystem {
42 self.volume.fs()
43 }
44
45 pub fn node(&self) -> &FileNode {
46 &self.node
47 }
48
49 pub fn vmo(&self) -> &zx::Vmo {
50 &self.vmo
51 }
52
53 pub(crate) fn register_zero_children_wait(&self) -> Result<(), zx::Status> {
54 self.vmo.wait_async(
55 fasync::EHandle::local().port(),
56 self.registration.key(),
57 zx::Signals::VMO_ZERO_CHILDREN,
58 zx::WaitAsyncOpts::empty(),
59 )
60 }
61
62 pub fn watch_for_zero_children(&self) -> Result<bool, zx::Status> {
69 let mut file_holder = self.registration.receiver().file.lock().unwrap();
70 match &*file_holder {
71 crate::pager::FileHolder::Weak(weak) => {
72 let strong = weak.upgrade().ok_or(zx::Status::BAD_STATE)?;
73
74 self.register_zero_children_wait()?;
76
77 *file_holder = crate::pager::FileHolder::Strong(strong);
78 Ok(true)
79 }
80 crate::pager::FileHolder::Strong(_) => Ok(false),
81 }
82 }
83}
84
85impl DirectoryEntry for ErofsFile {
86 fn open_entry(self: Arc<Self>, request: OpenRequest<'_>) -> Result<(), zx::Status> {
87 request.open_file(self)
88 }
89}
90
91impl GetEntryInfo for ErofsFile {
92 fn entry_info(&self) -> EntryInfo {
93 EntryInfo::new(self.node.ino() as u64, fio::DirentType::File)
94 }
95}
96
97impl Node for ErofsFile {
98 async fn get_attributes(
99 &self,
100 requested_attributes: fio::NodeAttributesQuery,
101 ) -> Result<fio::NodeAttributes2, zx::Status> {
102 let content_size = self.node.size();
103 Ok(vfs::immutable_attributes!(
104 requested_attributes,
105 Immutable {
106 protocols: fio::NodeProtocolKinds::FILE,
107 abilities: fio::Operations::GET_ATTRIBUTES | fio::Operations::READ_BYTES,
108 content_size: content_size,
109 storage_size: content_size,
110 id: self.node.ino() as u64,
111 }
112 ))
113 }
114}
115
116impl GetVmo for ErofsFile {
117 const PAGER_ON_FIDL_EXECUTOR: bool = true;
118
119 fn get_vmo(&self) -> &zx::Vmo {
120 &self.vmo
121 }
122}
123
124impl File for ErofsFile {
125 fn readable(&self) -> bool {
126 true
127 }
128
129 fn writable(&self) -> bool {
130 false
131 }
132
133 fn executable(&self) -> bool {
134 false
135 }
136
137 async fn open_file(&self, _options: &FileOptions) -> Result<(), zx::Status> {
138 Ok(())
139 }
140
141 async fn truncate(&self, _length: u64) -> Result<(), zx::Status> {
142 Err(zx::Status::NOT_SUPPORTED)
143 }
144
145 async fn get_size(&self) -> Result<u64, zx::Status> {
146 Ok(self.node.size())
147 }
148
149 async fn update_attributes(
150 &self,
151 _attributes: fio::MutableNodeAttributes,
152 ) -> Result<(), zx::Status> {
153 Err(zx::Status::NOT_SUPPORTED)
154 }
155
156 async fn get_backing_memory(&self, flags: fio::VmoFlags) -> Result<zx::Vmo, zx::Status> {
157 let mut vmo_rights = vmo_flags_to_rights(flags)
158 | zx::Rights::BASIC
159 | zx::Rights::MAP
160 | zx::Rights::GET_PROPERTY;
161
162 let child_vmo = if flags.contains(fio::VmoFlags::PRIVATE_CLONE) {
163 vmo_rights |= zx::Rights::SET_PROPERTY;
164 let mut child_options = zx::VmoChildOptions::SNAPSHOT_AT_LEAST_ON_WRITE;
165 if flags.contains(fio::VmoFlags::WRITE) {
166 child_options |= zx::VmoChildOptions::RESIZABLE;
167 vmo_rights |= zx::Rights::RESIZE;
168 }
169 self.vmo.create_child(child_options, 0, self.node.size())?
170 } else {
171 self.vmo.create_child(zx::VmoChildOptions::REFERENCE, 0, 0)?
172 };
173
174 let child_vmo = child_vmo.replace_handle(vmo_rights)?;
175
176 let _ = self.watch_for_zero_children()?;
177
178 Ok(child_vmo)
179 }
180
181 async fn sync(&self, _mode: SyncMode) -> Result<(), zx::Status> {
182 Ok(())
183 }
184}
185
186impl FileLike for ErofsFile {
187 fn open(
188 self: Arc<Self>,
189 scope: ExecutionScope,
190 options: FileOptions,
191 object_request: ObjectRequestRef<'_>,
192 ) -> Result<(), zx::Status> {
193 let request = object_request.take();
194 let scope_clone = scope.clone();
195 scope.spawn(request.handle_async(async move |object_request_ref| {
196 vfs::file::StreamIoConnection::create(scope_clone, self, options, object_request_ref)
197 .await
198 }));
199 Ok(())
200 }
201}
202
203fn vmo_flags_to_rights(vmo_flags: fio::VmoFlags) -> zx::Rights {
205 let mut rights = zx::Rights::NONE;
206 if vmo_flags.contains(fio::VmoFlags::READ) {
207 rights |= zx::Rights::READ;
208 }
209 if vmo_flags.contains(fio::VmoFlags::WRITE) {
210 rights |= zx::Rights::WRITE;
211 }
212 if vmo_flags.contains(fio::VmoFlags::EXECUTE) {
213 rights |= zx::Rights::EXECUTE;
214 }
215 rights
216}