| ... | ... | @@ -8,6 +8,7 @@ const assert = std.debug.assert; |
| 8 | 8 | const windows = os.windows; |
| 9 | 9 | const Os = builtin.Os; |
| 10 | 10 | const maxInt = std.math.maxInt; |
| 11 | const is_windows = std.Target.current.os.tag == .windows; |
| 11 | 12 | |
| 12 | 13 | pub const File = struct { |
| 13 | 14 | /// The OS-specific file descriptor or file handle. |
| ... | ... | @@ -119,7 +120,9 @@ pub const File = struct { |
| 119 | 120 | /// Upon success, the stream is in an uninitialized state. To continue using it, |
| 120 | 121 | /// you must use the open() function. |
| 121 | 122 | pub fn close(self: File) void { |
| 122 | | if (self.capable_io_mode != self.intended_io_mode) { |
| 123 | if (is_windows) { |
| 124 | windows.CloseHandle(self.handle); |
| 125 | } else if (self.capable_io_mode != self.intended_io_mode) { |
| 123 | 126 | std.event.Loop.instance.?.close(self.handle); |
| 124 | 127 | } else { |
| 125 | 128 | os.close(self.handle); |
| ... | ... | @@ -302,7 +305,9 @@ pub const File = struct { |
| 302 | 305 | pub const PReadError = os.PReadError; |
| 303 | 306 | |
| 304 | 307 | pub fn read(self: File, buffer: []u8) ReadError!usize { |
| 305 | | if (self.capable_io_mode != self.intended_io_mode) { |
| 308 | if (is_windows) { |
| 309 | return windows.ReadFile(self.handle, buffer, null, self.intended_io_mode); |
| 310 | } else if (self.capable_io_mode != self.intended_io_mode) { |
| 306 | 311 | return std.event.Loop.instance.?.read(self.handle, buffer); |
| 307 | 312 | } else { |
| 308 | 313 | return os.read(self.handle, buffer); |
| ... | ... | @@ -322,7 +327,9 @@ pub const File = struct { |
| 322 | 327 | } |
| 323 | 328 | |
| 324 | 329 | pub fn pread(self: File, buffer: []u8, offset: u64) PReadError!usize { |
| 325 | | if (self.capable_io_mode != self.intended_io_mode) { |
| 330 | if (is_windows) { |
| 331 | return windows.ReadFile(self.handle, buffer, offset, self.intended_io_mode); |
| 332 | } else if (self.capable_io_mode != self.intended_io_mode) { |
| 326 | 333 | return std.event.Loop.instance.?.pread(self.handle, buffer, offset); |
| 327 | 334 | } else { |
| 328 | 335 | return os.pread(self.handle, buffer, offset); |
| ... | ... | @@ -342,7 +349,12 @@ pub const File = struct { |
| 342 | 349 | } |
| 343 | 350 | |
| 344 | 351 | pub fn readv(self: File, iovecs: []const os.iovec) ReadError!usize { |
| 345 | | if (self.capable_io_mode != self.intended_io_mode) { |
| 352 | if (is_windows) { |
| 353 | // TODO improve this to use ReadFileScatter |
| 354 | if (iovecs.len == 0) return @as(usize, 0); |
| 355 | const first = iovecs[0]; |
| 356 | return windows.ReadFile(self.handle, first.iov_base[0..first.iov_len], null, self.intended_io_mode); |
| 357 | } else if (self.capable_io_mode != self.intended_io_mode) { |
| 346 | 358 | return std.event.Loop.instance.?.readv(self.handle, iovecs); |
| 347 | 359 | } else { |
| 348 | 360 | return os.readv(self.handle, iovecs); |
| ... | ... | @@ -376,7 +388,12 @@ pub const File = struct { |
| 376 | 388 | } |
| 377 | 389 | |
| 378 | 390 | pub fn preadv(self: File, iovecs: []const os.iovec, offset: u64) PReadError!usize { |
| 379 | | if (self.capable_io_mode != self.intended_io_mode) { |
| 391 | if (is_windows) { |
| 392 | // TODO improve this to use ReadFileScatter |
| 393 | if (iovecs.len == 0) return @as(usize, 0); |
| 394 | const first = iovecs[0]; |
| 395 | return windows.ReadFile(self.handle, first.iov_base[0..first.iov_len], offset, self.intended_io_mode); |
| 396 | } else if (self.capable_io_mode != self.intended_io_mode) { |
| 380 | 397 | return std.event.Loop.instance.?.preadv(self.handle, iovecs, offset); |
| 381 | 398 | } else { |
| 382 | 399 | return os.preadv(self.handle, iovecs, offset); |
| ... | ... | @@ -413,7 +430,9 @@ pub const File = struct { |
| 413 | 430 | pub const PWriteError = os.PWriteError; |
| 414 | 431 | |
| 415 | 432 | pub fn write(self: File, bytes: []const u8) WriteError!usize { |
| 416 | | if (self.capable_io_mode != self.intended_io_mode) { |
| 433 | if (is_windows) { |
| 434 | return windows.WriteFile(self.handle, bytes, null, self.intended_io_mode); |
| 435 | } else if (self.capable_io_mode != self.intended_io_mode) { |
| 417 | 436 | return std.event.Loop.instance.?.write(self.handle, bytes); |
| 418 | 437 | } else { |
| 419 | 438 | return os.write(self.handle, bytes); |
| ... | ... | @@ -428,7 +447,9 @@ pub const File = struct { |
| 428 | 447 | } |
| 429 | 448 | |
| 430 | 449 | pub fn pwrite(self: File, bytes: []const u8, offset: u64) PWriteError!usize { |
| 431 | | if (self.capable_io_mode != self.intended_io_mode) { |
| 450 | if (is_windows) { |
| 451 | return windows.WriteFile(self.handle, bytes, offset, self.intended_io_mode); |
| 452 | } else if (self.capable_io_mode != self.intended_io_mode) { |
| 432 | 453 | return std.event.Loop.instance.?.pwrite(self.handle, bytes, offset); |
| 433 | 454 | } else { |
| 434 | 455 | return os.pwrite(self.handle, bytes, offset); |
| ... | ... | @@ -443,7 +464,12 @@ pub const File = struct { |
| 443 | 464 | } |
| 444 | 465 | |
| 445 | 466 | pub fn writev(self: File, iovecs: []const os.iovec_const) WriteError!usize { |
| 446 | | if (self.capable_io_mode != self.intended_io_mode) { |
| 467 | if (is_windows) { |
| 468 | // TODO improve this to use WriteFileScatter |
| 469 | if (iovecs.len == 0) return @as(usize, 0); |
| 470 | const first = iovecs[0]; |
| 471 | return windows.WriteFile(self.handle, first.iov_base[0..first.iov_len], null, self.intended_io_mode); |
| 472 | } else if (self.capable_io_mode != self.intended_io_mode) { |
| 447 | 473 | return std.event.Loop.instance.?.writev(self.handle, iovecs); |
| 448 | 474 | } else { |
| 449 | 475 | return os.writev(self.handle, iovecs); |
| ... | ... | @@ -469,7 +495,12 @@ pub const File = struct { |
| 469 | 495 | } |
| 470 | 496 | |
| 471 | 497 | pub fn pwritev(self: File, iovecs: []os.iovec_const, offset: usize) PWriteError!usize { |
| 472 | | if (self.capable_io_mode != self.intended_io_mode) { |
| 498 | if (is_windows) { |
| 499 | // TODO improve this to use WriteFileScatter |
| 500 | if (iovecs.len == 0) return @as(usize, 0); |
| 501 | const first = iovecs[0]; |
| 502 | return windows.WriteFile(self.handle, first.iov_base[0..first.iov_len], offset, self.intended_io_mode); |
| 503 | } else if (self.capable_io_mode != self.intended_io_mode) { |
| 473 | 504 | return std.event.Loop.instance.?.pwritev(self.handle, iovecs, offset); |
| 474 | 505 | } else { |
| 475 | 506 | return os.pwritev(self.handle, iovecs, offset); |