Repository: denoland/deno
File: ext/io/lib.rs (Rust)

Code before:
        fs3::FileExt::lock_shared(file)?;
      }
      Ok(())
    })
  }
  async fn lock_async(self: Rc<Self>, exclusive: bool) -> FsResult<()> {
    self
      .with_inner_blocking_task(move |file| {
        if exclusive {
          file.lock_exclusive()?;
        } else {
          fs3::FileExt::lock_shared(file)?;
        }
        Ok(())
      })
      .await
  }

  fn try_lock_sync(self: Rc<Self>, exclusive: bool) -> FsResult<bool> {
    fn is_lock_contention_error(err: &std::io::Error) -> bool {
      err.kind() == std::io::ErrorKind::WouldBlock
        || err.raw_os_error() == Some(33) // ERROR_LOCK_VIOLATION on Windows
    }
    self.with_sync(|file| {
      let result = if exclusive {
        fs3::FileExt::try_lock_exclusive(file)
      } else {
        fs3::FileExt::try_lock_shared(file)
      };
      match result {
        Ok(()) => Ok(true),
        Err(err) if is_lock_contention_error(&err) => Ok(false),
        Err(err) => Err(err.into()),
      }
    })
  }
  async fn try_lock_async(self: Rc<Self>, exclusive: bool) -> FsResult<bool> {
    fn is_lock_contention_error(err: &std::io::Error) -> bool {
      err.kind() == std::io::ErrorKind::WouldBlock
        || err.raw_os_error() == Some(33) // ERROR_LOCK_VIOLATION on Windows
    }
    self
      .with_inner_blocking_task(move |file| {
        let result = if exclusive {
          fs3::FileExt::try_lock_exclusive(file)
        } else {
          fs3::FileExt::try_lock_shared(file)
        };
        match result {
          Ok(()) => Ok(true),
          Err(err) if is_lock_contention_error(&err) => Ok(false),

Review comment (suggestion):
We should get rid of this fs3 dependency. The Rust standard library has this functionality now as of Rust 1.89: https://doc.rust-lang.org/beta/std/fs/struct.File.html#method.try_lock

That way we don't need the special handling for Windows I think.

Code after:
        fs3::FileExt::lock_shared(file)?;
      }
      Ok(())
    })
  }
  async fn lock_async(self: Rc<Self>, exclusive: bool) -> FsResult<()> {
    self
      .with_inner_blocking_task(move |file| {
        if exclusive {
          file.lock_exclusive()?;
        } else {
          fs3::FileExt::lock_shared(file)?;
        }
        Ok(())
      })
      .await
  }

  fn try_lock_sync(self: Rc<Self>, exclusive: bool) -> FsResult<bool> {
    use std::fs::TryLockError;
    self.with_sync(|file| {
      let result = if exclusive {
        file.try_lock()
      } else {
        file.try_lock_shared()
      };
      match result {
        Ok(()) => Ok(true),
        Err(TryLockError::WouldBlock) => Ok(false),
        Err(TryLockError::Error(err)) => Err(err.into()),
      }
    })
  }
  async fn try_lock_async(self: Rc<Self>, exclusive: bool) -> FsResult<bool> {
    use std::fs::TryLockError;
    self
      .with_inner_blocking_task(move |file| {
        let result = if exclusive {
          file.try_lock()
        } else {
          file.try_lock_shared()
        };
        match result {
          Ok(()) => Ok(true),
          Err(TryLockError::WouldBlock) => Ok(false),
          Err(TryLockError::Error(err)) => Err(err.into()),
        }
      })
      .await
  }