MCPcopy Create free account
hub / github.com/bendudson/EuraliOS / handle_file_readonly

Function handle_file_readonly

euralios_std/src/server.rs:264–306  ·  view source on GitHub ↗

Serve messages received from a communication channel Only allow reading from the file

(file: Arc<RwLock<dyn FileLike + Sync + Send>>,
                        comm_handle: CommHandle)

Source from the content-addressed store, hash-verified

262/// Serve messages received from a communication channel
263/// Only allow reading from the file
264fn handle_file_readonly(file: Arc<RwLock<dyn FileLike + Sync + Send>>,
265 comm_handle: CommHandle) {
266 dispatch_loop(
267 &comm_handle,
268 |msg| {
269 match msg {
270 syscalls::Message::Short(
271 message::READ, start, length) => {
272
273 let f = file.read();
274 let len = cmp::min(f.len(), length as usize);
275
276 if let Err((err, _msg)) = if len == 0 {
277 // No data
278 syscalls::send(&comm_handle,
279 syscalls::Message::Short(
280 message::ERROR,
281 syscalls::SYSCALL_ERROR_NO_DATA.as_u64(), 0))
282 } else {
283 // Allocate memory
284 let (mut mem_handle, _) = malloc(len as u64, 0).unwrap();
285 // Read data
286 match f.read(start as usize, mem_handle.as_mut_slice(len)) {
287 Ok(nbytes) => syscalls::send(&comm_handle,
288 syscalls::Message::Long(
289 message::DATA,
290 (nbytes as u64).into(),
291 mem_handle.into())),
292 Err(sys_err) => syscalls::send(&comm_handle,
293 syscalls::Message::Short(
294 message::ERROR, sys_err.as_u64(), 0))
295 }
296 } {
297 // Failed to send reply
298 println!("[std:handle_file_ro] Reply failed: {}", err);
299 }
300 }
301 msg => {
302 println!("[std:handle_file_ro] unexpected {:?}", msg);
303 }
304 }
305 });
306}
307
308/// Handle messages for a given directory
309///

Callers 1

openFunction · 0.85

Calls 7

dispatch_loopFunction · 0.85
sendFunction · 0.85
mallocFunction · 0.85
as_mut_sliceMethod · 0.80
readMethod · 0.45
lenMethod · 0.45
as_u64Method · 0.45

Tested by

no test coverage detected