You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@opendal.apache.org by su...@apache.org on 2026/04/16 14:38:50 UTC
(opendal) 02/05: fix(services/onedrive): align primitive semantics
This is an automated email from the ASF dual-hosted git repository.
suyanhanx pushed a commit to branch fix/onedrive-api-alignment
in repository https://gitbox.apache.org/repos/asf/opendal.git
commit 6384aaade783dfe16a20f8604fdda8d545a63b25
Author: suyanhanx <su...@gmail.com>
AuthorDate: Thu Apr 16 12:10:06 2026 +0800
fix(services/onedrive): align primitive semantics
---
core/services/onedrive/src/backend.rs | 15 ++++--
core/services/onedrive/src/core.rs | 98 +++++++++++++++++++++--------------
core/services/onedrive/src/deleter.rs | 9 +++-
core/services/onedrive/src/lister.rs | 19 +++----
core/services/onedrive/src/writer.rs | 7 +++
5 files changed, 90 insertions(+), 58 deletions(-)
diff --git a/core/services/onedrive/src/backend.rs b/core/services/onedrive/src/backend.rs
index e6ecb8150..6fbcffa40 100644
--- a/core/services/onedrive/src/backend.rs
+++ b/core/services/onedrive/src/backend.rs
@@ -50,11 +50,9 @@ impl Access for OnedriveBackend {
return Ok(RpCreateDir::default());
}
- let response = self.core.onedrive_create_dir(path).await?;
- match response.status() {
- StatusCode::CREATED | StatusCode::OK => Ok(RpCreateDir::default()),
- _ => Err(parse_error(response)),
- }
+ self.core.ensure_directory(path).await?;
+
+ Ok(RpCreateDir::default())
}
async fn stat(&self, path: &str, args: OpStat) -> Result<RpStat> {
@@ -96,6 +94,13 @@ impl Access for OnedriveBackend {
}
async fn copy(&self, from: &str, to: &str, _args: OpCopy) -> Result<RpCopy> {
+ if _args.if_not_exists() {
+ return Err(Error::new(
+ ErrorKind::Unsupported,
+ "copy with if_not_exists is not supported by OneDrive",
+ ));
+ }
+
let monitor_url = self.core.initialize_copy(from, to).await?;
self.core.wait_until_complete(monitor_url).await?;
Ok(RpCopy::default())
diff --git a/core/services/onedrive/src/core.rs b/core/services/onedrive/src/core.rs
index 2a7d4ac92..17df75a19 100644
--- a/core/services/onedrive/src/core.rs
+++ b/core/services/onedrive/src/core.rs
@@ -108,28 +108,55 @@ impl OneDriveCore {
///
/// * `path` - a relative folder path
pub(crate) async fn ensure_directory(&self, path: &str) -> Result<OneDriveItem> {
- let response = self.onedrive_get_stat_plain(path).await?;
- let item: OneDriveItem = match response.status() {
- StatusCode::OK => {
- let bytes = response.into_body();
- serde_json::from_reader(bytes.reader()).map_err(new_json_deserialize_error)?
- }
- StatusCode::NOT_FOUND => {
- // We must create directory for the destination
- let response = self.onedrive_create_dir(path).await?;
- match response.status() {
- StatusCode::CREATED | StatusCode::OK => {
- let bytes = response.into_body();
- serde_json::from_reader(bytes.reader())
- .map_err(new_json_deserialize_error)?
+ let mut pending: Vec<String> = Vec::new();
+ let mut current = path;
+
+ loop {
+ let response = self.onedrive_get_stat_plain(current).await?;
+ match response.status() {
+ StatusCode::OK => {
+ let bytes = response.into_body();
+ let item: OneDriveItem = serde_json::from_reader(bytes.reader())
+ .map_err(new_json_deserialize_error)?;
+
+ if !matches!(item.item_type, ItemType::Folder { .. }) {
+ return Err(Error::new(
+ ErrorKind::NotADirectory,
+ "path exists but is not a directory",
+ )
+ .with_context("path", current));
}
- _ => return Err(parse_error(response)),
+
+ let mut ensured = item;
+ for missing in pending.into_iter().rev() {
+ let response = self.onedrive_create_dir(&missing).await?;
+ ensured = match response.status() {
+ StatusCode::CREATED | StatusCode::OK => {
+ let bytes = response.into_body();
+ serde_json::from_reader(bytes.reader())
+ .map_err(new_json_deserialize_error)?
+ }
+ _ => return Err(parse_error(response)),
+ };
+ }
+
+ return Ok(ensured);
+ }
+ StatusCode::NOT_FOUND => {
+ pending.push(current.to_string());
+ let parent = get_parent(current);
+ if parent == current {
+ return Err(Error::new(
+ ErrorKind::Unexpected,
+ "failed to resolve parent while creating directory",
+ )
+ .with_context("path", path));
+ }
+ current = parent;
}
+ _ => return Err(parse_error(response)),
}
- _ => return Err(parse_error(response)),
- };
-
- Ok(item)
+ }
}
pub(crate) async fn sign<T>(&self, request: &mut Request<T>) -> Result<()> {
@@ -158,13 +185,15 @@ impl OneDriveCore {
///
/// See also [`onedrive_get_stat_plain()`].
pub(crate) async fn onedrive_stat(&self, path: &str, args: OpStat) -> Result<Metadata> {
- let mut url: String = self.onedrive_item_url(path, true);
if args.version().is_some() {
- url += "?$expand=versions(";
- url += VERSION_SELECT_PARAM;
- url += ")";
+ return Err(Error::new(
+ ErrorKind::Unsupported,
+ "stat with version is not supported by OneDrive",
+ ));
}
+ let url: String = self.onedrive_item_url(path, true);
+
let mut request = Request::get(&url);
if let Some(etag) = args.if_none_match() {
request = request.header(header::IF_NONE_MATCH, etag);
@@ -195,22 +224,6 @@ impl OneDriveCore {
.with_etag(decoded_response.e_tag)
.with_content_length(decoded_response.size.max(0) as u64);
- if let Some(version) = args.version() {
- for item_version in decoded_response.versions.as_deref().unwrap_or_default() {
- if item_version.id == version {
- meta.set_version(version);
- break; // early exit
- }
- }
-
- if meta.version().is_none() {
- return Err(Error::new(
- ErrorKind::NotFound,
- "cannot find this version of the item",
- ));
- }
- }
-
let last_modified = decoded_response.last_modified_date_time;
let date_utc_last_modified = last_modified.parse::<Timestamp>()?;
meta.set_last_modified(date_utc_last_modified);
@@ -277,6 +290,13 @@ impl OneDriveCore {
path: &str,
args: &OpRead,
) -> Result<Response<HttpBody>> {
+ if args.version().is_some() {
+ return Err(Error::new(
+ ErrorKind::Unsupported,
+ "read with version is not supported by OneDrive",
+ ));
+ }
+
// We can't "select" the OneDrive API response fields when reading because "select" shadows not found error
let url: String = format!("{}:/content", self.onedrive_item_url(path, true));
diff --git a/core/services/onedrive/src/deleter.rs b/core/services/onedrive/src/deleter.rs
index 30e477021..6671115f3 100644
--- a/core/services/onedrive/src/deleter.rs
+++ b/core/services/onedrive/src/deleter.rs
@@ -38,7 +38,14 @@ impl OneDriveDeleter {
}
impl oio::OneShotDelete for OneDriveDeleter {
- async fn delete_once(&self, path: String, _: OpDelete) -> Result<()> {
+ async fn delete_once(&self, path: String, args: OpDelete) -> Result<()> {
+ if args.version().is_some() {
+ return Err(Error::new(
+ ErrorKind::Unsupported,
+ "delete with version is not supported by OneDrive",
+ ));
+ }
+
let response = self.core.onedrive_delete(&path).await?;
match response.status() {
StatusCode::NO_CONTENT | StatusCode::NOT_FOUND => Ok(()),
diff --git a/core/services/onedrive/src/lister.rs b/core/services/onedrive/src/lister.rs
index 64e2a248b..001e0e0e3 100644
--- a/core/services/onedrive/src/lister.rs
+++ b/core/services/onedrive/src/lister.rs
@@ -135,25 +135,18 @@ impl oio::PageList for OneDriveLister {
// N+1 is horrendous but we can't do any better without OneDrive's API support.
// When OneDrive supports listing with versions API, remove this.
if list_with_versions && entry_mode == EntryMode::FILE {
- let versions = self.core.onedrive_list_versions(&path).await?;
- if versions.is_empty() {
- let entry = oio::Entry::new(&normalized_path, meta);
- ctx.entries.push_back(entry);
- continue;
- }
+ meta.set_is_current(true);
+ let current_entry = oio::Entry::new(&normalized_path, meta);
+ ctx.entries.push_back(current_entry);
- for (idx, version) in versions.into_iter().enumerate() {
+ let versions = self.core.onedrive_list_versions(&path).await?;
+ for version in versions {
let mut version_meta =
Metadata::new(entry_mode).with_content_length(version.size.max(0) as u64);
version_meta
.set_last_modified(version.last_modified_date_time.parse::<Timestamp>()?);
version_meta.set_version(&version.id);
- version_meta.set_is_current(idx == 0);
-
- // OneDrive only exposes the current item's etag from the children API.
- if idx == 0 {
- version_meta.set_etag(meta.etag().unwrap_or_default());
- }
+ version_meta.set_is_current(false);
let entry = oio::Entry::new(&normalized_path, version_meta);
ctx.entries.push_back(entry);
diff --git a/core/services/onedrive/src/writer.rs b/core/services/onedrive/src/writer.rs
index eefd16558..5e6c3fb7a 100644
--- a/core/services/onedrive/src/writer.rs
+++ b/core/services/onedrive/src/writer.rs
@@ -52,6 +52,13 @@ impl OneDriveWriter {
// use `OneShotWrite` instead of `MultipartWrite`.
impl oio::OneShotWrite for OneDriveWriter {
async fn write_once(&self, bs: Buffer) -> Result<Metadata> {
+ if self.op.if_none_match().is_some() || self.op.if_not_exists() {
+ return Err(Error::new(
+ ErrorKind::Unsupported,
+ "write with if_none_match/if_not_exists is not supported by OneDrive",
+ ));
+ }
+
let size = bs.len();
let meta = if size <= Self::MAX_SIMPLE_SIZE {