Repository: sxyazi/yazi
File: yazi-vfs/src/provider/sftp/sftp.rs (Rust)

Code before:
		Ok(written)
	}

	fn copy_with_progress<P, A>(&self, to: P, attrs: A) -> io::Result<Receiver<io::Result<u64>>>
	where
		P: AsPath,
		A: Into<yazi_fs::provider::Attrs>,
	{
		let to = UrlBuf::Sftp {
			loc:    LocBuf::<typed_path::UnixPathBuf>::saturated(
				to.as_path().to_unix_owned()?,
				SchemeKind::Sftp,
			),
			domain: self.name.intern(),
		};
		let from = self.url.to_owned();

		Ok(crate::provider::copy_with_progress_impl(from, to, attrs.into()))
	}

	async fn create_dir(&self) -> io::Result<()> {
		Ok(self.op().await?.mkdir(self.path, Attrs::default()).await?)
	}

	async fn hard_link<P>(&self, to: P) -> io::Result<()>
	where
		P: AsPath,
	{
		let to = to.as_path().as_unix()?;

		Ok(self.op().await?.hardlink(self.path, to).await?)
	}

	async fn metadata(&self) -> io::Result<yazi_fs::cha::Cha> {
		let attrs = self.op().await?.stat(self.path).await?;
		Ok(Cha::try_from((self.path.file_name().unwrap_or_default(), &attrs))?.0)
	}

	async fn new<'b>(url: Url<'b>) -> io::Result<Self::Me<'b>> {
		match url {
			Url::Regular(_) | Url::Search { .. } | Url::Archive { .. } => {
				Err(io::Error::new(io::ErrorKind::InvalidInput, format!("Not a SFTP URL: {url:?}")))
			}
			Url::Sftp { loc, domain } => {
				let (name, config) = Vfs::service::<&ServiceSftp>(domain).await?;
				Ok(Self::Me { url, path: loc.as_inner(), name, config })
			}
		}
	}

	async fn read_dir(self) -> io::Result<Self::ReadDir> {

Review comment (bug):
The condition `status.is_failure()` only checks for `StatusCode::Failure` (value 4). However, SFTP servers may also return `StatusCode::FileAlreadyExists` (value 11) for directory creation or file creation operations when the file already exists. This status code should also be checked to avoid making an unnecessary `lstat()` call. Consider checking for both `status.is_failure()` and checking if the status code is `FileAlreadyExists`, or use a more comprehensive check that covers all error cases where a subsequent lstat might be needed.

Code after:
		Ok(written)
	}

	fn copy_with_progress<P, A>(&self, to: P, attrs: A) -> io::Result<Receiver<io::Result<u64>>>
	where
		P: AsPath,
		A: Into<yazi_fs::provider::Attrs>,
	{
		let to = UrlBuf::Sftp {
			loc:    LocBuf::<typed_path::UnixPathBuf>::saturated(
				to.as_path().to_unix_owned()?,
				SchemeKind::Sftp,
			),
			domain: self.name.intern(),
		};
		let from = self.url.to_owned();

		Ok(crate::provider::copy_with_progress_impl(from, to, attrs.into()))
	}

	async fn create_dir(&self) -> io::Result<()> {
		let op = self.op().await?;
		let result = op.mkdir(self.path, Attrs::default()).await;

		if let Err(yazi_sftp::Error::Status(status)) = &result
			&& status.is_failure()
			&& op.lstat(self.path).await.is_ok()
		{
			return Err(io::Error::from(io::ErrorKind::AlreadyExists));
		}

		Ok(result?)
	}

	async fn hard_link<P>(&self, to: P) -> io::Result<()>
	where
		P: AsPath,
	{
		let to = to.as_path().as_unix()?;

		Ok(self.op().await?.hardlink(self.path, to).await?)
	}

	async fn metadata(&self) -> io::Result<yazi_fs::cha::Cha> {
		let attrs = self.op().await?.stat(self.path).await?;
		Ok(Cha::try_from((self.path.file_name().unwrap_or_default(), &attrs))?.0)
	}

	async fn new<'b>(url: Url<'b>) -> io::Result<Self::Me<'b>> {
		match url {
			Url::Regular(_) | Url::Search { .. } | Url::Archive { .. } => {