From 28971fb424b1a7dba93aca7bc6ecc88a20be5b3e Mon Sep 17 00:00:00 2001 From: Olivier 'reivilibre Date: Wed, 26 Jan 2022 20:26:00 +0000 Subject: [PATCH] Clean up lookup() --- olivefsd/src/server/connections.rs | 9 ++++- olivefsd/src/server/file_access.rs | 57 ++++++++++++++---------------- 2 files changed, 34 insertions(+), 32 deletions(-) diff --git a/olivefsd/src/server/connections.rs b/olivefsd/src/server/connections.rs index b907cab..73e3c66 100644 --- a/olivefsd/src/server/connections.rs +++ b/olivefsd/src/server/connections.rs @@ -40,7 +40,14 @@ pub async fn handle_command_stream( send_bare_message(&mut tx, &file_access.releasedir(dir_handle).await?).await?; } DataCommand::Lookup { dir_vnode, name } => { - send_bare_message(&mut tx, &file_access.lookup(dir_vnode, name).await?).await?; + send_bare_message( + &mut tx, + &file_access + .lookup(dir_vnode, name) + .await + .unwrap_or_else(error_to_response), + ) + .await?; } DataCommand::OpenFile { vnode, mode } => { send_bare_message( diff --git a/olivefsd/src/server/file_access.rs b/olivefsd/src/server/file_access.rs index 6e5e9ff..2e77111 100644 --- a/olivefsd/src/server/file_access.rs +++ b/olivefsd/src/server/file_access.rs @@ -459,39 +459,34 @@ impl FileAccess { name: String, ) -> anyhow::Result> { // TODO switch to the easier ones and then handle I/O errors in the callsite - match self.resolve_vnode(dir_vnode).await { - Ok(dir_path) => { - let lookup_path = dir_path.join(name); - // TODO check the security of this: make sure you can't escape the root - match lookup_path.absolutize_virtually(&self.client_info.root) { - Ok(the_path) => { - // We'll allocate the Vnode after we know the target exists. Fill in 0 for now. - Ok( - match self.read_metadata(the_path.borrow(), VnodeId(0)).await { - Ok(mut metadata) => { - // Now allocate a Vnode and fill it in. - let vnode_id = - self.allocate_vnode(the_path.to_path_buf()).await?; - metadata.ino = vnode_id; + let dir_path = self + .resolve_vnode_including_follow_symlinks_safely_best_effort(dir_vnode) + .await?; + let lookup_path = dir_path.join(name); + // TODO check the security of this: make sure you can't escape the root + match lookup_path.absolutize_virtually(&self.client_info.root) { + Ok(the_path) => { + // We'll allocate the Vnode after we know the target exists. Fill in 0 for now. + Ok( + match self.read_metadata(the_path.borrow(), VnodeId(0)).await { + Ok(mut metadata) => { + // Now allocate a Vnode and fill it in. + let vnode_id = self.allocate_vnode(the_path.to_path_buf()).await?; + metadata.ino = vnode_id; - // Respond with the getattr-style response. - DataResponse::Success(metadata) - } - Err(error) => io_error_to_response( - error, - ENOENT, - &format!("lookup {:?}", the_path), - ), - }, - ) - } - Err(error) => Ok(DataResponse::Error { - code: ENOENT, - message: format!("Can't make absolute. {:?} ({:?})", error, lookup_path), - }), - } + // Respond with the getattr-style response. + DataResponse::Success(metadata) + } + Err(error) => { + io_error_to_response(error, ENOENT, &format!("lookup {:?}", the_path)) + } + }, + ) } - Err(response) => Ok(response), + Err(error) => Ok(DataResponse::Error { + code: ENOENT, + message: format!("Can't make absolute. {:?} ({:?})", error, lookup_path), + }), } }