Skip to content

Commit

Permalink
Refactor win32 System::FileDescriptor#unbuffered_write
Browse files Browse the repository at this point in the history
  • Loading branch information
straight-shoota committed May 22, 2024
1 parent 420d691 commit 0378425
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions src/crystal/system/win32/file_descriptor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,7 @@ module Crystal::System::FileDescriptor
handle = windows_handle
until slice.empty?
if system_blocking?
if LibC.WriteFile(handle, slice, slice.size, out bytes_written, nil) == 0
case error = WinError.value
when .error_access_denied?
raise IO::Error.new "File not open for writing", target: self
when .error_broken_pipe?
return 0_u32
else
raise IO::Error.from_os_error("Error writing file", error, target: self)
end
end
blocking_write(handle, slice)
else
bytes_written = overlapped_operation(handle, "WriteFile", write_timeout, writing: true) do |overlapped|
ret = LibC.WriteFile(handle, slice, slice.size, out byte_count, overlapped)
Expand All @@ -73,6 +64,20 @@ module Crystal::System::FileDescriptor
end
end

private def blocking_write(handle, slice)
ret = LibC.WriteFile(handle, slice, slice.size, out bytes_written, nil)
if ret.zero?
case error = WinError.value
when .error_access_denied?
raise IO::Error.new "File not open for writing", target: self
when .error_broken_pipe?
return 0_u32
else
raise IO::Error.from_os_error("Error writing file", error, target: self)
end
end
end

private def system_blocking?
@system_blocking
end
Expand Down

0 comments on commit 0378425

Please sign in to comment.