]> git.proxmox.com Git - ceph.git/blob - ceph/src/arrow/cpp/src/arrow/io/test_common.cc
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / cpp / src / arrow / io / test_common.cc
1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing,
12 // software distributed under the License is distributed on an
13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, either express or implied. See the License for the
15 // specific language governing permissions and limitations
16 // under the License.
17
18 #include "arrow/io/test_common.h"
19
20 #include <algorithm>
21 #include <cstdint>
22 #include <fstream> // IWYU pragma: keep
23
24 #ifdef _WIN32
25 #include <crtdbg.h>
26 #include <io.h>
27 #else
28 #include <fcntl.h>
29 #endif
30
31 #include "arrow/buffer.h"
32 #include "arrow/io/file.h"
33 #include "arrow/io/memory.h"
34 #include "arrow/memory_pool.h"
35 #include "arrow/testing/gtest_util.h"
36
37 namespace arrow {
38 namespace io {
39
40 void AssertFileContents(const std::string& path, const std::string& contents) {
41 ASSERT_OK_AND_ASSIGN(auto rf, ReadableFile::Open(path));
42 ASSERT_OK_AND_ASSIGN(int64_t size, rf->GetSize());
43 ASSERT_EQ(size, contents.size());
44
45 ASSERT_OK_AND_ASSIGN(auto actual_data, rf->Read(size));
46 ASSERT_TRUE(actual_data->Equals(Buffer(contents)));
47 }
48
49 bool FileExists(const std::string& path) { return std::ifstream(path.c_str()).good(); }
50
51 #if defined(_WIN32)
52 static void InvalidParamHandler(const wchar_t* expr, const wchar_t* func,
53 const wchar_t* source_file, unsigned int source_line,
54 uintptr_t reserved) {
55 wprintf(L"Invalid parameter in function '%s'. Source: '%s' line %d expression '%s'\n",
56 func, source_file, source_line, expr);
57 }
58 #endif
59
60 bool FileIsClosed(int fd) {
61 #if defined(_WIN32)
62 // Disables default behavior on wrong params which causes the application to crash
63 // https://msdn.microsoft.com/en-us/library/ksazx244.aspx
64 _set_invalid_parameter_handler(InvalidParamHandler);
65
66 // Disables possible assertion alert box on invalid input arguments
67 _CrtSetReportMode(_CRT_ASSERT, 0);
68
69 int new_fd = _dup(fd);
70 if (new_fd == -1) {
71 return errno == EBADF;
72 }
73 _close(new_fd);
74 return false;
75 #else
76 if (-1 != fcntl(fd, F_GETFD)) {
77 return false;
78 }
79 return errno == EBADF;
80 #endif
81 }
82
83 Status ZeroMemoryMap(MemoryMappedFile* file) {
84 constexpr int64_t kBufferSize = 512;
85 static constexpr uint8_t kZeroBytes[kBufferSize] = {0};
86
87 RETURN_NOT_OK(file->Seek(0));
88 int64_t position = 0;
89 ARROW_ASSIGN_OR_RAISE(int64_t file_size, file->GetSize());
90
91 int64_t chunksize;
92 while (position < file_size) {
93 chunksize = std::min(kBufferSize, file_size - position);
94 RETURN_NOT_OK(file->Write(kZeroBytes, chunksize));
95 position += chunksize;
96 }
97 return Status::OK();
98 }
99
100 void MemoryMapFixture::TearDown() {
101 for (auto path : tmp_files_) {
102 ARROW_UNUSED(std::remove(path.c_str()));
103 }
104 }
105
106 void MemoryMapFixture::CreateFile(const std::string& path, int64_t size) {
107 ASSERT_OK(MemoryMappedFile::Create(path, size));
108 tmp_files_.push_back(path);
109 }
110
111 Result<std::shared_ptr<MemoryMappedFile>> MemoryMapFixture::InitMemoryMap(
112 int64_t size, const std::string& path) {
113 ARROW_ASSIGN_OR_RAISE(auto mmap, MemoryMappedFile::Create(path, size));
114 tmp_files_.push_back(path);
115 return mmap;
116 }
117
118 void MemoryMapFixture::AppendFile(const std::string& path) { tmp_files_.push_back(path); }
119
120 } // namespace io
121 } // namespace arrow