JMLX42 commented on code in PR #7005: URL: https://github.com/apache/opendal/pull/7005#discussion_r2642893946
########## core/services/wasi-fs/src/writer.rs: ########## @@ -0,0 +1,93 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use std::sync::Arc; + +use opendal_core::raw::*; +use opendal_core::*; +use wasi::filesystem::types::{Descriptor, DescriptorFlags, OpenFlags}; +use wasi::io::streams::OutputStream; + +use super::core::WasiFsCore; +use super::error::{parse_stream_error, parse_wasi_error}; + +pub struct WasiFsWriter { + file: Descriptor, + stream: OutputStream, + offset: u64, +} + +impl WasiFsWriter { + pub fn new(core: Arc<WasiFsCore>, path: &str, args: OpWrite) -> Result<Self> { + let mut open_flags = OpenFlags::CREATE; + + if !args.append() { + open_flags |= OpenFlags::TRUNCATE; + } + + let file = core.open_file(path, open_flags, DescriptorFlags::WRITE)?; + + let offset = if args.append() { + let stat = file.stat().map_err(parse_wasi_error)?; + stat.size + } else { + 0 + }; + + let stream = file.write_via_stream(offset).map_err(parse_wasi_error)?; + + Ok(Self { + file, + stream, + offset, + }) + } +} + +/// # Safety +/// +/// WasiFsWriter only accesses WASI resources which are single-threaded in WASM. +unsafe impl Sync for WasiFsWriter {} + +impl oio::Write for WasiFsWriter { + async fn write(&mut self, bs: Buffer) -> Result<()> { + let data: Vec<u8> = bs.to_vec(); + + self.stream + .blocking_write_and_flush(&data) Review Comment: Yes, WASI Preview 2 only provides blocking APIs - this is a platform limitation. ### Comparison | | Standard OpenDAL | WASIp2 (current) | WASIp3 (future) | |--|------------------|------------------|-----------------| | **Async Runtime** | tokio | None (blocking only) | Component Model native (`future<T>`, `stream<T>`) | | **Operator** | `Operator` | Needs `WasiOperator` (blocking) | `WasiOperator` (async) | | **Compiles for wasm32-wasip2** | ❌ | ✅ | N/A | | **Compiles for wasm32-wasip3** | ❌ | ✅ | ✅ | ### Current State (WASIp2) To make wasi-fs usable at the Operator level today, OpenDAL would need a `WasiOperator` that doesn't depend on tokio. This operator would use blocking calls internally since that's all WASIp2 offers. ### Future Enhancement (WASIp3) WASI 0.3 (available in Wasmtime v39+) introduces native async support via `future<T>` and `stream<T>` in the Component Model. A future `WasiOperator` could leverage WASIp3's async primitives instead of blocking calls, aligning with OpenDAL's async-first architecture without requiring tokio. --- *Disclosure: This response was drafted with assistance from Claude Code (AI).* -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
