Skip to content

Commit edac716

Browse files
committed
LibCore: Support HTTP disk caching on Windows
This implements Core::System::transfer_file_through_pipe for Windows (which is actually a socket, not a pipe). This let's us enable the HTTP disk cache.
1 parent 079a2ba commit edac716

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

Libraries/LibCore/SystemWindows.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -404,12 +404,25 @@ ErrorOr<void> kill(pid_t pid, int signal)
404404

405405
ErrorOr<size_t> transfer_file_through_pipe(int source_fd, int target_fd, size_t source_offset, size_t source_length)
406406
{
407-
(void)source_fd;
408-
(void)target_fd;
409-
(void)source_offset;
410-
(void)source_length;
407+
// FIXME: We could use TransmitFile (https://learn.microsoft.com/en-us/windows/win32/api/mswsock/nf-mswsock-transmitfile)
408+
// here. But in order to transmit a subset of the file, we have to use overlapped IO.
411409

412-
return Error::from_string_literal("FIXME: Implement System::transfer_file_through_pipe on Windows (for HTTP disk cache)");
410+
static auto allocation_granularity = []() {
411+
SYSTEM_INFO system_info {};
412+
GetSystemInfo(&system_info);
413+
414+
return system_info.dwAllocationGranularity;
415+
}();
416+
417+
// MapViewOfFile requires the offset to be aligned to the system allocation granularity, so we must handle that here.
418+
auto aligned_source_offset = (source_offset / allocation_granularity) * allocation_granularity;
419+
auto offset_adjustment = source_offset - aligned_source_offset;
420+
auto mapped_source_length = source_length + offset_adjustment;
421+
422+
auto* mapped = TRY(mmap(nullptr, mapped_source_length, PROT_READ, MAP_SHARED, source_fd, aligned_source_offset));
423+
ScopeGuard guard { [&]() { (void)munmap(mapped, mapped_source_length); } };
424+
425+
return TRY(send(target_fd, { static_cast<u8*>(mapped) + offset_adjustment, source_length }, 0));
413426
}
414427

415428
}

0 commit comments

Comments
 (0)