Xuanwo opened a new pull request, #8959:
URL: https://github.com/apache/opendal/pull/8959

   # Which issue does this PR close?
   
   None.
   
   # Rationale for this change
   
   `opendal-service-sftp` had eight bugs across directory listing, connection 
pooling, file closing, directory creation, server-side copy, capability 
reporting, and URI parsing:
   
   1. **Lister panics on non-UTF-8 filenames or missing permissions**: 
`SftpLister` called `.unwrap()` on `DirEntry::filename().to_str()` and 
`DirEntry::file_type()`, panicking on non-UTF-8 filenames or SFTP servers that 
omit `SSH_FILEXFER_ATTR_PERMISSIONS`.
   2. **Connection pool deadlock in `rename`**: `SftpBackend::rename` held a 
checked-out pool connection while calling `self.create_dir(...)`, which 
acquired a second connection from the same 64-slot pool and deadlocked under 
concurrency.
   3. **Unawaited `SSH_FXP_CLOSE` in `SftpWriter::close`**: `SftpWriter` 
wrapped `File` in `TokioCompatFile` and called `AsyncWriteExt::shutdown()`, 
which is a no-op that never sends `SSH_FXP_CLOSE`, leaving close errors to be 
dropped in a detached `Drop` task and flattening write errors through 
`std::io::Error`.
   4. **Connection pool leak and drop ordering on open handles**: 
`SftpLazyWriter` and `SftpLazyLister` dropped `SftpConnection` immediately 
after opening remote file/directory handles instead of retaining the pool slot 
for the handle lifetime, and `SftpReadStream` declared `_conn` before `file`, 
returning the connection to the pool before the file handle dropped.
   5. **Swallowed errors in `create_dir`**: `SftpBackend::create_dir` ignored 
every `SftpError(_, _)`, hiding `PermissionDenied`, `NoSuchFile`, and existing 
regular-file conflicts instead of verifying the existing path is a directory.
   6. **Broken port and dropped username in `SftpConfig::from_uri`**: 
`from_uri` stripped the `ssh://` scheme (causing 
`openssh::SessionBuilder::resolve` to treat `host:port` as a hostname) and 
ignored `uri.username()`.
   7. **Self-copy truncation and unawaited close in `copy`**: 
`SftpBackend::copy` opened and truncated the destination before checking 
`support_copy()` or whether `from` and `to` canonicalize to the same file, 
dropped `dst_file` without awaiting `close()`, and mapped 
`UnsupportedExtension` to `Unexpected`.
   8. **False `list_with_limit` capability**: `SftpBuilder` advertised 
`list_with_limit: true` even though `SftpBackend::list` ignores `OpList`.
   
   # What changes are included in this PR?
   
   - **`core/services/sftp/src/lister.rs`**: Replaced `.unwrap()` calls with 
UTF-8 validation (`ErrorKind::Unexpected`) and `Option`-safe `file_type()` 
handling (falling back to `EntryMode::Unknown` for entries and `EntryMode::DIR` 
for `.`), retained `_conn: SftpConnection` after the directory handle, and 
added unit tests.
   - **`core/services/sftp/src/core.rs`**: Extracted a shared 
`create_dir_all(&Sftp, base, path)` helper that reuses an existing SFTP 
connection, verifies existing entries via `metadata` (returning 
`ErrorKind::NotADirectory` for non-directories and propagating 
permission/not-found errors), and mapped 
`SftpClientError::UnsupportedExtension` to `ErrorKind::Unsupported`.
   - **`core/services/sftp/src/backend.rs`**:
     - Reused `create_dir_all` with the already-acquired connection in 
`create_dir`, `rename`, and `copy` (checking source existence before creating 
parent directories in `rename`).
     - Checked `client.support_copy()` and canonical source/destination 
equality (`ErrorKind::IsSameFile`) before opening/truncating the destination in 
`copy`, and awaited `dst_file.close().await` and `src_file.close().await`.
     - Set `list_with_limit` to `false` in `SftpBuilder` capabilities.
   - **`core/services/sftp/src/writer.rs` & 
`core/services/sftp/src/reader.rs`**:
     - Used `openssh_sftp_client::file::File` directly in `SftpWriter` with 
`write_all_zero_copy`, awaited `file.close().await` in `SftpWriter::close`, and 
mapped SFTP errors via `parse_sftp_error`.
     - Retained `SftpConnection` after the handle field in `SftpWriter`, 
`SftpLister`, and `SftpReadStream` so handles drop before connections return to 
the pool; reused the checked-out connection for parent directory creation in 
`SftpLazyWriter`.
   - **`core/services/sftp/src/config.rs`**: Preserved the `ssh://` prefix for 
`endpoint` in `SftpConfig::from_uri`, populated `user` from `uri.username()` 
when not explicitly overridden in options, and added unit tests.
   - **`core/services/sftp/tests/operations.rs`**: Added ignored real-OpenSSH 
fixture tests covering `create_dir` error propagation, canonical-alias 
self-copy protection, `from_uri` with port and username, pool-capacity 
retention by open writers/listers, and 64-way concurrent nested renames.
   
   # Are there any user-facing changes?
   
   - `Operator::from_uri("sftp://user@host:port/root";)` now preserves the port 
and uses the URI username when `user` is not set in options.
   - `SftpBackend` no longer advertises `list_with_limit: true`; 
`CompleteLayer` handles listing limits.
   - `create_dir`, `writer::close`, `copy`, and `list` now surface real SFTP 
errors (`NotADirectory`, `PermissionDenied`, `IsSameFile`, `Unsupported`, 
`Unexpected`) instead of panicking, truncating, or silently succeeding.
   
   # Breaking changes
   
   # AI Usage Statement
   
   - Harness: pi
   - Model: Gemini 4 Neon
   - Effort: high
   - Role: Investigated and verified all eight SFTP bugs against `origin/main`, 
implemented the fixes, added unit tests and OpenSSH SFTP fixture integration 
tests, and verified changes against a local OpenSSH SFTP fixture container as 
well as formatting, clippy, rustdoc, and unit/behavior test suites.
   


-- 
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]

Reply via email to