1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT license.
3
4#pragma once
5
6#include <string>
7
8#ifdef _WIN32
9#define NOMINMAX
10#define _WINSOCKAPI_
11#include <Windows.h>
12#else
13#include <sys/types.h>
14#include <sys/stat.h>
15#include <fcntl.h>
16#include <unistd.h>
17#endif
18
19namespace FASTER {
20namespace benchmark {
21
22/// Basic wrapper around synchronous file read.
23class File {
24 public:
25 File(const std::string& filename) {
26#ifdef _WIN32
27 file_handle_ = ::CreateFileA(filename.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr,
28 OPEN_EXISTING, FILE_FLAG_NO_BUFFERING, nullptr);
29#else
30 fd_ = ::open(filename.c_str(), O_RDONLY | O_DIRECT, S_IRUSR);
31#endif
32 }
33
34 ~File() {
35#ifdef _WIN32
36 ::CloseHandle(file_handle_);
37#else
38 ::close(fd_);
39#endif
40 }
41
42 size_t Read(void* buf, size_t count, uint64_t offset) {
43#ifdef _WIN32
44 DWORD bytes_read { 0 };
45 ::ReadFile(file_handle_, buf, static_cast<DWORD>(count), &bytes_read, nullptr);
46 return bytes_read;
47#else
48 return ::pread(fd_, buf, count, offset);
49#endif
50 }
51
52 private:
53#ifdef _WIN32
54 HANDLE file_handle_;
55#else
56 int fd_;
57#endif
58};
59
60}
61} // namespace FASTER::benchmark
62