Clean up lookup()

This commit is contained in:
Olivier 'reivilibre' 2022-01-26 20:26:00 +00:00
parent 6283f7cc6f
commit 28971fb424
2 changed files with 34 additions and 32 deletions

View File

@ -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(

View File

@ -459,39 +459,34 @@ impl FileAccess {
name: String,
) -> anyhow::Result<DataResponse<FileMetadata>> {
// 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),
}),
}
}