-
Notifications
You must be signed in to change notification settings - Fork 792
fix(integrations/dav-server): improve WebDAV handling #7882
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ngg
wants to merge
1
commit into
apache:main
Choose a base branch
from
ngg:webdav-fixes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+119
−31
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,7 +23,8 @@ use dav_server::fs::{DavFile, FsStream}; | |
| use dav_server::fs::{DavFileSystem, ReadDirMeta}; | ||
| use futures::FutureExt; | ||
| use futures::StreamExt; | ||
| use opendal_core::Operator; | ||
| use opendal_core::raw::normalize_path; | ||
| use opendal_core::{ErrorKind, Operator}; | ||
| use std::path::Path; | ||
|
|
||
| use super::dir::OpendalStream; | ||
|
|
@@ -68,7 +69,24 @@ impl OpendalFs { | |
| } | ||
|
|
||
| fn fs_path(&self, path: &DavPath) -> Result<String, FsError> { | ||
| String::from_utf8(path.as_bytes().to_vec()).map_err(|_| FsError::GeneralFailure) | ||
| String::from_utf8(path.as_bytes().to_vec()) | ||
| .map(|path| normalize_path(path.as_str())) | ||
| .map_err(|_| FsError::GeneralFailure) | ||
| } | ||
|
|
||
| fn fs_dir_path(&self, path: &DavPath) -> Result<String, FsError> { | ||
| Ok(Self::into_dir_path(self.fs_path(path)?)) | ||
| } | ||
|
|
||
| fn into_dir_path(mut path: String) -> String { | ||
| if !Self::is_dir_path(&path) { | ||
| path.push('/'); | ||
| } | ||
| path | ||
| } | ||
|
|
||
| fn is_dir_path(path: &str) -> bool { | ||
| path.is_empty() || path.ends_with('/') | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -92,7 +110,7 @@ impl DavFileSystem for OpendalFs { | |
| _meta: ReadDirMeta, | ||
| ) -> FsFuture<'a, FsStream<Box<dyn DavDirEntry>>> { | ||
| async move { | ||
| let path = self.fs_path(path)?; | ||
| let path = self.fs_dir_path(path)?; | ||
| self.op | ||
| .lister(path.as_str()) | ||
| .await | ||
|
|
@@ -105,21 +123,26 @@ impl DavFileSystem for OpendalFs { | |
| fn metadata<'a>(&'a self, path: &'a DavPath) -> FsFuture<'a, Box<dyn DavMetaData>> { | ||
| async move { | ||
| let path = self.fs_path(path)?; | ||
| let opendal_metadata = self.op.stat(path.as_str()).await; | ||
| match opendal_metadata { | ||
| Ok(metadata) => { | ||
| let webdav_metadata = OpendalMetaData::new(metadata); | ||
| Ok(Box::new(webdav_metadata) as Box<dyn DavMetaData>) | ||
| let metadata = match self.op.stat(path.as_str()).await { | ||
| Ok(metadata) => metadata, | ||
| Err(e) | ||
| if !Self::is_dir_path(&path) | ||
| && matches!(e.kind(), ErrorKind::NotFound | ErrorKind::IsADirectory) => | ||
| { | ||
| let path = Self::into_dir_path(path); | ||
| self.op.stat(path.as_str()).await.map_err(convert_error)? | ||
| } | ||
| Err(e) => Err(convert_error(e)), | ||
| } | ||
| Err(e) => return Err(convert_error(e)), | ||
| }; | ||
|
|
||
| Ok(Box::new(OpendalMetaData::new(metadata)) as Box<dyn DavMetaData>) | ||
| } | ||
| .boxed() | ||
| } | ||
|
|
||
| fn create_dir<'a>(&'a self, path: &'a DavPath) -> FsFuture<'a, ()> { | ||
| async move { | ||
| let path = self.fs_path(path)?; | ||
| let path = self.fs_dir_path(path)?; | ||
|
|
||
| // check if the parent path is exist. | ||
| // During MKCOL processing, a server MUST make the Request-URI a member of its parent collection, unless the Request-URI is "/". If no such ancestor exists, the method MUST fail. | ||
|
|
@@ -161,7 +184,11 @@ impl DavFileSystem for OpendalFs { | |
| } | ||
|
|
||
| fn remove_dir<'a>(&'a self, path: &'a DavPath) -> FsFuture<'a, ()> { | ||
| self.remove_file(path) | ||
| async move { | ||
| let path = self.fs_dir_path(path)?; | ||
| self.op.delete(&path).await.map_err(convert_error) | ||
| } | ||
| .boxed() | ||
| } | ||
|
|
||
| fn remove_file<'a>(&'a self, path: &'a DavPath) -> FsFuture<'a, ()> { | ||
|
|
@@ -174,16 +201,54 @@ impl DavFileSystem for OpendalFs { | |
|
|
||
| fn rename<'a>(&'a self, from: &'a DavPath, to: &'a DavPath) -> FsFuture<'a, ()> { | ||
| async move { | ||
| let from_path = from | ||
| .as_rel_ospath() | ||
| .to_str() | ||
| .ok_or(FsError::GeneralFailure)?; | ||
| let to_path = to.as_rel_ospath().to_str().ok_or(FsError::GeneralFailure)?; | ||
| if from.is_collection() { | ||
| let _ = self.remove_file(to).await; | ||
| let from_path = self.fs_dir_path(from)?; | ||
| let to_path = self.fs_dir_path(to)?; | ||
|
|
||
| if self | ||
| .op | ||
| .exists(to_path.as_str()) | ||
| .await | ||
| .map_err(convert_error)? | ||
| { | ||
| return Err(FsError::Exists); | ||
| } | ||
|
|
||
| let mut lister = self | ||
| .op | ||
| .lister_with(from_path.as_str()) | ||
| .limit(2) | ||
| .await | ||
| .map_err(convert_error)?; | ||
| let mut found = false; | ||
| while let Some(entry) = lister.next().await { | ||
| let entry = entry.map_err(convert_error)?; | ||
| if entry.path() == from_path { | ||
| found = true; | ||
| } else { | ||
| // Only empty directory moves are supported. | ||
| return Err(FsError::NotImplemented); | ||
| } | ||
| } | ||
| if !found { | ||
| return Err(FsError::NotFound); | ||
| } | ||
|
|
||
| self.op | ||
| .create_dir(to_path.as_str()) | ||
| .await | ||
| .map_err(convert_error)?; | ||
| return self | ||
| .op | ||
| .delete(from_path.as_str()) | ||
| .await | ||
| .map_err(convert_error); | ||
| } | ||
|
|
||
| let from_path = self.fs_path(from)?; | ||
| let to_path = self.fs_path(to)?; | ||
| self.op | ||
| .rename(from_path, to_path) | ||
| .rename(from_path.as_str(), to_path.as_str()) | ||
| .await | ||
| .map_err(convert_error) | ||
| } | ||
|
|
@@ -192,13 +257,15 @@ impl DavFileSystem for OpendalFs { | |
|
|
||
| fn copy<'a>(&'a self, from: &'a DavPath, to: &'a DavPath) -> FsFuture<'a, ()> { | ||
| async move { | ||
| let from_path = from | ||
| .as_rel_ospath() | ||
| .to_str() | ||
| .ok_or(FsError::GeneralFailure)?; | ||
| let to_path = to.as_rel_ospath().to_str().ok_or(FsError::GeneralFailure)?; | ||
| if from.is_collection() || to.is_collection() { | ||
| // Directory copies are handled by dav-server. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does this mean? |
||
| return Err(FsError::NotImplemented); | ||
| } | ||
|
|
||
| let from_path = self.fs_path(from)?; | ||
| let to_path = self.fs_path(to)?; | ||
| self.op | ||
| .copy(from_path, to_path) | ||
| .copy(from_path.as_str(), to_path.as_str()) | ||
| .await | ||
| .map(|_| ()) | ||
| .map_err(convert_error) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,10 +17,14 @@ | |
|
|
||
| pub fn convert_error(opendal_error: opendal_core::Error) -> dav_server::fs::FsError { | ||
| match opendal_error.kind() { | ||
| opendal_core::ErrorKind::AlreadyExists | opendal_core::ErrorKind::IsSameFile => { | ||
| dav_server::fs::FsError::Exists | ||
| } | ||
| opendal_core::ErrorKind::Unsupported => dav_server::fs::FsError::NotImplemented, | ||
| opendal_core::ErrorKind::NotFound => dav_server::fs::FsError::NotFound, | ||
| opendal_core::ErrorKind::PermissionDenied | ||
| | opendal_core::ErrorKind::IsADirectory | ||
| | opendal_core::ErrorKind::NotADirectory => dav_server::fs::FsError::Forbidden, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will this be too broad? |
||
| opendal_core::ErrorKind::AlreadyExists | ||
| | opendal_core::ErrorKind::IsSameFile | ||
| | opendal_core::ErrorKind::ConditionNotMatch => dav_server::fs::FsError::Exists, | ||
| _ => dav_server::fs::FsError::GeneralFailure, | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can integration use OpenDAL layer? Correctness check in this example.