Fix and simplify open() code

This commit is contained in:
Olivier 'reivilibre' 2022-01-26 19:20:23 +00:00
parent e594fad962
commit 6283f7cc6f
2 changed files with 39 additions and 45 deletions

View File

@ -43,7 +43,14 @@ pub async fn handle_command_stream(
send_bare_message(&mut tx, &file_access.lookup(dir_vnode, name).await?).await?;
}
DataCommand::OpenFile { vnode, mode } => {
send_bare_message(&mut tx, &file_access.open(vnode, mode).await?).await?;
send_bare_message(
&mut tx,
&file_access
.open(vnode, mode)
.await
.unwrap_or_else(error_to_response),
)
.await?;
}
DataCommand::ReleaseFile { file_handle } => {
send_bare_message(&mut tx, &file_access.release(file_handle).await?).await?;

View File

@ -498,54 +498,41 @@ impl FileAccess {
/// Symlink safety:
/// Safe — uses O_NOFOLLOW after dereferencing safe symlinks.
pub async fn open(&self, vnode: VnodeId, mode: OpenMode) -> anyhow::Result<DataResponse<u32>> {
match self
let path = self
.resolve_vnode_including_follow_symlinks_safely_best_effort(vnode)
.await
{
Ok(path) => {
let mut open_options = OpenOptions::new();
.await?;
let mut open_options = OpenOptions::new();
// IMPORTANT — Don't follow symlinks
open_options.custom_flags(O_NOFOLLOW);
// IMPORTANT — Don't follow symlinks
open_options.custom_flags(O_NOFOLLOW);
if mode.read {
open_options.read(true);
}
if mode.write {
open_options.write(true);
warn!("DANGER: file opened with write mode");
}
if mode.append {
open_options.append(true);
error!("UNSUPPORTED: file opened with append mode");
todo!();
}
// TODO truncate?
match open_options.open(&path).await {
Ok(file) => {
// TODO offset & append files: how does that work?
let handle = Arc::new(RwLock::new(FileHandle { file, offset: 0 }));
let file_handle: u32 = self
.client_state
.file_handles
.write()
.await
.insert(handle)
.try_into()
.unwrap();
Ok(DataResponse::Success(file_handle))
}
Err(error) => Ok(io_error_to_response(
error,
EFAULT,
&format!("open {:?}", path),
)),
}
}
Err(response) => Ok(response),
if mode.read {
open_options.read(true);
}
if mode.write {
open_options.write(true);
warn!("DANGER: file opened with write mode");
}
if mode.append {
open_options.append(true);
error!("UNSUPPORTED: file opened with append mode");
todo!();
}
// TODO truncate?
let file = open_options.open(&path).await?;
// TODO offset & append files: how does that work?
let handle = Arc::new(RwLock::new(FileHandle { file, offset: 0 }));
let file_handle: u32 = self
.client_state
.file_handles
.write()
.await
.insert(handle)
.try_into()
.unwrap();
Ok(DataResponse::Success(file_handle))
}
/// Symlink safety: