forked from pytorch/executorch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemp_file.h
More file actions
178 lines (152 loc) · 5.2 KB
/
temp_file.h
File metadata and controls
178 lines (152 loc) · 5.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
* 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.
*/
#pragma once
#include <array>
#include <fstream>
#include <memory>
#include <string>
#include <vector>
#include <gtest/gtest.h>
namespace executorch {
namespace extension {
namespace testing { // Test-only helpers belong in a "testing" sub-namespace.
/**
* Creates and manages a named temporary file in the file system. Deletes the
* file when this instance is destroyed.
*
* Only for use in gtest tests.
*/
class TempFile {
public:
/**
* Creates a temporary file whose contents are the same as the provided
* string.
*/
explicit TempFile(const std::string& contents)
: TempFile(contents.data(), contents.size()) {}
/**
* Creates a temporary file with the provided contents.
*/
TempFile(const void* data, size_t size) {
CreateFile(data, size, &path_);
}
/**
* Creates a sparse temporary file with a string at a specific offset.
* The file will have the specified total size, but only the data at the
* given offset will be written, creating a sparse file that doesn't
* allocate all the disk space.
*
* Example:
* // Create a 3GB file with "DATA_AT_3GB" at 3GB offset
* size_t offset = 3ULL * 1024 * 1024 * 1024;
* std::string data = "DATA_AT_3GB";
* TempFile tf(offset, data, offset + data.size());
*
* @param offset Byte offset where the string should be written
* @param data String to write at the specified offset
* @param file_size Total size of the sparse file (must be >= offset +
* data.size())
*/
TempFile(size_t offset, const std::string& data, size_t file_size) {
CreateSparseFile(offset, data, file_size, &path_);
}
~TempFile() {
if (!path_.empty()) {
std::remove(path_.c_str());
}
}
/**
* The absolute path to the temporary file.
*/
const std::string& path() const {
return path_;
}
private:
// ASSERT_x() macros can only be called from a function returning void, so
// this logic can't be directly in the ctor.
void CreateFile(const void* data, size_t size, std::string* out_path) {
// Find a unique temporary file name.
std::string path;
{
std::array<char, L_tmpnam> buf;
const char* ret = std::tmpnam(buf.data());
ASSERT_NE(ret, nullptr) << "Coult not generate temp file";
buf[L_tmpnam - 1] = '\0';
path = std::string(buf.data()) + "-executorch-testing";
}
// Write the contents to the file.
std::ofstream file(path, std::ios::out | std::ios::binary);
ASSERT_TRUE(file.is_open())
<< "open(" << path << ") failed: " << strerror(errno);
file.write((const char*)data, size);
ASSERT_TRUE(file.good())
<< "Failed to write " << size << " bytes: " << strerror(errno);
*out_path = path;
}
void CreateSparseFile(
size_t offset,
const std::string& data,
size_t file_size,
std::string* out_path) {
ASSERT_GE(file_size, offset + data.size())
<< "file_size must be >= offset + data.size()";
// Find a unique temporary file name.
std::string path;
{
std::array<char, L_tmpnam> buf;
const char* ret = std::tmpnam(buf.data());
ASSERT_NE(ret, nullptr) << "Could not generate temp file";
buf[L_tmpnam - 1] = '\0';
path = std::string(buf.data()) + "-executorch-testing";
}
// Open file in binary mode for writing.
std::ofstream file(path, std::ios::out | std::ios::binary);
ASSERT_TRUE(file.is_open())
<< "open(" << path << ") failed: " << strerror(errno);
// Seek to the offset.
file.seekp(offset, std::ios::beg);
ASSERT_TRUE(file.good()) << "Failed to seek to offset " << offset;
// Write the data.
file.write(data.data(), data.size());
ASSERT_TRUE(file.good())
<< "Failed to write " << data.size() << " bytes at offset " << offset;
// Ensure file is the specified size by seeking to the end and writing a
// byte, but only if the file needs to be extended beyond the data we just
// wrote.
if (file_size > offset + data.size()) {
file.seekp(file_size - 1, std::ios::beg);
ASSERT_TRUE(file.good())
<< "Failed to seek to file_size - 1: " << file_size - 1;
// Write a single byte to ensure file has the correct size.
file.write("\0", 1);
ASSERT_TRUE(file.good())
<< "Failed to write final byte at position " << file_size - 1;
}
file.close();
ASSERT_TRUE(file.good() || file.eof()) << "Error closing file: " << path;
*out_path = path;
}
// Not safely copyable/movable.
TempFile(const TempFile&) = delete;
TempFile& operator=(const TempFile&) = delete;
TempFile(TempFile&&) = delete;
TempFile& operator=(TempFile&&) = delete;
std::string path_;
};
} // namespace testing
} // namespace extension
} // namespace executorch
namespace torch {
namespace executor {
namespace testing {
// TODO(T197294990): Remove these deprecated aliases once all users have moved
// to the new `::executorch` namespaces.
using ::executorch::extension::testing::TempFile;
} // namespace testing
} // namespace executor
} // namespace torch