Skip to content

[Windows] [file_data_loader.cpp] Add compat_unistd.h #8913

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion extension/data_loader/file_data_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
#include <cstring>
#include <limits>

#include <executorch/runtime/platform/compat_unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include <executorch/runtime/core/error.h>
#include <executorch/runtime/core/result.h>
Expand Down Expand Up @@ -71,6 +71,9 @@ FileDataLoader::~FileDataLoader() {
std::free(const_cast<char*>(file_name_));
// fd_ can be -1 if this instance was moved from, but closing a negative fd is
// safe (though it will return an error).
if (fd_ == -1) {
return;
}
::close(fd_);
}

Expand Down
75 changes: 75 additions & 0 deletions runtime/platform/compat_unistd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/

/**
* @file
* unistd.h related macros for POSIX/Windows compatibility.
*/
#pragma once

#if defined(_WIN32) && !defined(_WIN64)
#error \
"You're trying to build ExecuTorch with a too old version of Windows. We need Windows 64-bit."
#endif

#if !defined(_WIN64)
#include <unistd.h>
#else
#include <io.h>
#define O_RDONLY _O_RDONLY
#define open _open
#define close _close
#define read _read
#define write _write
#define stat _stat64
#define fstat _fstat64
#define off_t _off_t
#define lseek _lseeki64

#include <executorch/runtime/platform/compiler.h> // For ssize_t.
#include <windows.h>
// To avoid conflicts with std::numeric_limits<int32_t>::max() in
// file_data_loader.cpp.
#undef max

inline ssize_t pread(int fd, void* buf, size_t nbytes, size_t offset) {
OVERLAPPED overlapped; /* The offset for ReadFile. */
memset(&overlapped, 0, sizeof(overlapped));
overlapped.Offset = offset;
overlapped.OffsetHigh = offset >> 32;

BOOL result; /* The result of ReadFile. */
DWORD bytes_read; /* The number of bytes read. */
HANDLE file = (HANDLE)_get_osfhandle(fd);

result = ReadFile(file, buf, nbytes, &bytes_read, &overlapped);
DWORD error = GetLastError();
if (!result) {
if (error == ERROR_IO_PENDING) {
result = GetOverlappedResult(file, &overlapped, &bytes_read, TRUE);
if (!result) {
error = GetLastError();
}
}
}
if (!result) {
// Translate error into errno.
switch (error) {
case ERROR_HANDLE_EOF:
errno = 0;
break;
default:
errno = EIO;
break;
}
return -1;
}
return bytes_read;
}

#endif // !defined(_WIN64)
1 change: 1 addition & 0 deletions runtime/platform/targets.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def define_common_targets():
"log.h",
"profiler.h",
"runtime.h",
"compat_unistd.h",
],
srcs = [
"abort.cpp",
Expand Down
Loading