Repository: denoland/deno
File: ext/fs/std_fs.rs (Rust)

Code before:
  use winapi::um::handleapi::INVALID_HANDLE_VALUE;
  use winapi::um::winnt::FILE_SHARE_DELETE;
  use winapi::um::winnt::FILE_SHARE_READ;
  use winapi::um::winnt::FILE_SHARE_WRITE;

  unsafe fn get_dev(
    handle: winapi::shared::ntdef::HANDLE,
  ) -> std::io::Result<u64> {
    use winapi::shared::minwindef::FALSE;
    use winapi::um::fileapi::GetFileInformationByHandle;
    use winapi::um::fileapi::BY_HANDLE_FILE_INFORMATION;

    let info = {
      let mut info =
        std::mem::MaybeUninit::<BY_HANDLE_FILE_INFORMATION>::zeroed();
      if GetFileInformationByHandle(handle, info.as_mut_ptr()) == FALSE {
        return Err(std::io::Error::last_os_error());
      }

      info.assume_init()
    };

    Ok(info.dwVolumeSerialNumber as u64)
  }

  unsafe fn get_change_time(
    handle: winapi::shared::ntdef::HANDLE,
  ) -> std::io::Result<u64> {
    use winapi::shared::minwindef::FALSE;
    use winapi::um::fileapi::FILE_BASIC_INFO;
    use winapi::um::minwinbase::FileBasicInfo;
    use winapi::um::winbase::GetFileInformationByHandleEx;

    let file_info = {
      let mut file_info = std::mem::MaybeUninit::<FILE_BASIC_INFO>::zeroed();
      if GetFileInformationByHandleEx(
        handle,
        FileBasicInfo,
        &mut file_info as *mut _ as *mut _,
        std::mem::size_of::<FILE_BASIC_INFO>() as u32,
      ) == FALSE
      {
        return Err(std::io::Error::last_os_error());
      }

      file_info.assume_init()
    };

    let change_time = file_info.ChangeTime.QuadPart();
    let unix_time_msec = windows_time_to_unix_time_msec(change_time);


Review comment (suggestion):
I don't think that this is a good place for consts, but had no idea where to put them, so I will be happy for some suggestions

Code after:
  use winapi::um::handleapi::INVALID_HANDLE_VALUE;
  use winapi::um::winnt::FILE_SHARE_DELETE;
  use winapi::um::winnt::FILE_SHARE_READ;
  use winapi::um::winnt::FILE_SHARE_WRITE;

  unsafe fn get_dev(
    handle: winapi::shared::ntdef::HANDLE,
  ) -> std::io::Result<u64> {
    use winapi::shared::minwindef::FALSE;
    use winapi::um::fileapi::GetFileInformationByHandle;
    use winapi::um::fileapi::BY_HANDLE_FILE_INFORMATION;

    let info = {
      let mut info =
        std::mem::MaybeUninit::<BY_HANDLE_FILE_INFORMATION>::zeroed();
      if GetFileInformationByHandle(handle, info.as_mut_ptr()) == FALSE {
        return Err(std::io::Error::last_os_error());
      }

      info.assume_init()
    };

    Ok(info.dwVolumeSerialNumber as u64)
  }

  const WINDOWS_TICK: i64 = 10_000; // 100-nanosecond intervals in a millisecond
  const SEC_TO_UNIX_EPOCH: i64 = 11_644_473_600; // Seconds between Windows epoch and Unix epoch

  fn windows_time_to_unix_time_msec(windows_time: &i64) -> i64 {
    let milliseconds_since_windows_epoch = windows_time / WINDOWS_TICK;
    milliseconds_since_windows_epoch - SEC_TO_UNIX_EPOCH * 1000
  }

  use windows_sys::Wdk::Storage::FileSystem::FILE_ALL_INFORMATION;
  use windows_sys::Win32::Foundation::NTSTATUS;

  unsafe fn query_file_information(
    handle: winapi::shared::ntdef::HANDLE,
  ) -> Result<FILE_ALL_INFORMATION, NTSTATUS> {
    use windows_sys::Wdk::Storage::FileSystem::NtQueryInformationFile;
    use windows_sys::Win32::Foundation::RtlNtStatusToDosError;
    use windows_sys::Win32::Foundation::ERROR_MORE_DATA;
    use windows_sys::Win32::System::IO::IO_STATUS_BLOCK;

    let mut info = std::mem::MaybeUninit::<FILE_ALL_INFORMATION>::zeroed();
    let mut io_status_block =
      std::mem::MaybeUninit::<IO_STATUS_BLOCK>::zeroed();
    let status = NtQueryInformationFile(
      handle as _,
      io_status_block.as_mut_ptr(),
      info.as_mut_ptr() as *mut _,