38 #include <boost/filesystem.hpp>
40 namespace File_Namespace {
50 auto legacy_path = boost::filesystem::canonical(new_data_file_path);
52 return legacy_path.string();
55 std::pair<FILE*, std::string>
create(
const std::string& basePath,
57 const size_t pageSize,
58 const size_t numPages) {
60 if (numPages < 1 || pageSize < 1) {
61 LOG(
FATAL) <<
"Error trying to create file '" << path
62 <<
"', Number of pages and page size must be positive integers. numPages "
63 << numPages <<
" pageSize " << pageSize;
67 LOG(
FATAL) <<
"Error trying to create file '" << path
68 <<
"', the error was: " << std::strerror(errno);
70 fseek(f, static_cast<long>((pageSize * numPages) - 1), SEEK_SET);
72 fseek(f, 0, SEEK_SET);
73 if (
fileSize(f) != pageSize * numPages) {
74 LOG(
FATAL) <<
"Error trying to create file '" << path <<
"', file size "
75 <<
fileSize(f) <<
" does not equal pageSize * numPages "
76 << pageSize * numPages;
78 boost::filesystem::create_symlink(boost::filesystem::canonical(path).
filename(),
83 FILE*
create(
const std::string& full_path,
const size_t requested_file_size) {
86 LOG(
FATAL) <<
"Error trying to create file '" << full_path
87 <<
"', the error was: " << std::strerror(errno);
89 fseek(f, static_cast<long>(requested_file_size - 1), SEEK_SET);
91 fseek(f, 0, SEEK_SET);
92 if (
fileSize(f) != requested_file_size) {
93 LOG(
FATAL) <<
"Error trying to create file '" << full_path <<
"', file size "
94 <<
fileSize(f) <<
" does not equal requested_file_size "
95 << requested_file_size;
105 FILE*
open(
const std::string& path) {
108 LOG(
FATAL) <<
"Error trying to open file '" << path
109 <<
"', the errno was: " << std::strerror(errno);
121 const std::string file_path = base_path +
filename;
122 return remove(file_path.c_str()) == 0;
129 const std::string& file_path) {
131 CHECK_EQ(fseek(f, static_cast<long>(offset), SEEK_SET), 0);
132 size_t bytesRead = fread(buf,
sizeof(int8_t), size, f);
133 auto expected_bytes_read =
sizeof(int8_t) * size;
134 CHECK_EQ(bytesRead, expected_bytes_read)
135 <<
"Unexpected number of bytes read from file: " << file_path
136 <<
". Expected bytes read: " << expected_bytes_read
137 <<
", actual bytes read: " << bytesRead <<
", offset: " << offset
138 <<
", file stream error set: " << (std::ferror(f) ?
"true" :
"false")
139 <<
", EOF reached: " << (std::feof(f) ?
"true" :
"false");
143 size_t write(FILE*
f,
const size_t offset,
const size_t size,
const int8_t* buf) {
145 if (fseek(f, static_cast<long>(offset), SEEK_SET) != 0) {
147 <<
"Error trying to write to file (during positioning seek) the error was: "
148 << std::strerror(errno);
150 size_t bytesWritten = fwrite(buf,
sizeof(int8_t), size, f);
151 if (bytesWritten !=
sizeof(int8_t) * size) {
152 LOG(
FATAL) <<
"Error trying to write to file (during fwrite) the error was: "
153 << std::strerror(errno);
158 size_t append(FILE*
f,
const size_t size,
const int8_t* buf) {
163 const size_t pageSize,
164 const size_t pageNum,
166 const std::string& file_path) {
167 return read(f, pageNum * pageSize, pageSize, buf, file_path);
171 const size_t pageSize,
173 const size_t readSize,
174 const size_t pageNum,
176 const std::string& file_path) {
177 return read(f, pageNum * pageSize + offset, readSize, buf, file_path);
180 size_t writePage(FILE*
f,
const size_t pageSize,
const size_t pageNum, int8_t* buf) {
181 return write(f, pageNum * pageSize, pageSize, buf);
185 const size_t pageSize,
187 const size_t writeSize,
188 const size_t pageNum,
190 return write(f, pageNum * pageSize + offset, writeSize, buf);
199 fseek(f, 0, SEEK_END);
200 size_t size = (size_t)ftell(f);
201 fseek(f, 0, SEEK_SET);
212 boost::filesystem::path directoryPath(directoryName);
213 using namespace std::chrono;
214 milliseconds ms = duration_cast<milliseconds>(system_clock::now().time_since_epoch());
216 if (boost::filesystem::exists(directoryPath) &&
217 boost::filesystem::is_directory(directoryPath)) {
218 boost::filesystem::path newDirectoryPath(directoryName +
"_" +
220 boost::filesystem::rename(directoryPath, newDirectoryPath, ec);
230 while (ec.value() != boost::system::errc::success && tries) {
231 LOG(
ERROR) <<
"Failed to rename directory " << directoryPath <<
" error was " << ec
232 <<
" (" << tries <<
" attempts left)";
233 std::this_thread::sleep_for(std::chrono::milliseconds(100 / tries));
235 boost::filesystem::rename(directoryPath, newDirectoryPath, ec);
239 if (ec.value() == boost::system::errc::success) {
240 std::thread th([newDirectoryPath]() {
242 boost::filesystem::remove_all(newDirectoryPath, ec);
255 LOG(
FATAL) <<
"Failed to rename file " << directoryName <<
" to "
256 << directoryName +
"_" +
std::to_string(ms.count()) +
"_DELETE_ME Error: "
267 #include <boost/algorithm/string/predicate.hpp>
268 #include <boost/filesystem.hpp>
273 const unsigned int wait_interval_seconds,
274 const std::string base_path) {
275 const auto wait_duration = std::chrono::seconds(wait_interval_seconds);
276 const boost::filesystem::path path(base_path);
277 while (program_is_running) {
278 using vec = std::vector<boost::filesystem::path>;
284 copy(boost::filesystem::directory_iterator(path),
285 boost::filesystem::directory_iterator(),
287 for (vec::const_iterator it(v.begin()); it != v.end(); ++it) {
288 std::string object_name(it->string());
290 if (boost::algorithm::ends_with(object_name,
"DELETE_ME")) {
291 LOG(
INFO) <<
" removing object " << object_name;
292 boost::filesystem::remove_all(*it, ec);
293 if (ec.value() != boost::system::errc::success) {
294 LOG(
ERROR) <<
"Failed to remove object " << object_name <<
" error was " << ec;
299 std::this_thread::sleep_for(wait_duration);
size_t appendPage(FILE *f, const size_t pageSize, int8_t *buf)
Appends a page from buf to the file.
size_t append(FILE *f, const size_t size, const int8_t *buf)
Appends the specified number of bytes to the end of the file f from buf.
std::string get_legacy_data_file_path(const std::string &new_data_file_path)
size_t readPartialPage(FILE *f, const size_t pageSize, const size_t offset, const size_t readSize, const size_t pageNum, int8_t *buf, const std::string &file_path)
size_t writePartialPage(FILE *f, const size_t pageSize, const size_t offset, const size_t writeSize, const size_t pageNum, int8_t *buf)
size_t writePage(FILE *f, const size_t pageSize, const size_t pageNum, int8_t *buf)
Writes a page from buf to the file.
std::pair< FILE *, std::string > create(const std::string &basePath, const int fileId, const size_t pageSize, const size_t numPages)
size_t write(FILE *f, const size_t offset, const size_t size, const int8_t *buf)
Writes the specified number of bytes to the offset position in file f from buf.
bool removeFile(const std::string &base_path, const std::string &filename)
Deletes the file pointed to by the FILE pointer.
DEVICE auto copy(ARGS &&...args)
::FILE * fopen(const char *filename, const char *mode)
size_t read(FILE *f, const size_t offset, const size_t size, int8_t *buf, const std::string &file_path)
Reads the specified number of bytes from the offset position in file f into buf.
size_t fileSize(FILE *f)
Returns the size of the specified file.
torch::Tensor f(torch::Tensor x, torch::Tensor W_target, torch::Tensor b_target)
FILE * open(int file_id)
Opens the file with the given id; fatal crash on error.
std::string filename(char const *path)
void close(FILE *f)
Closes the file pointed to by the FILE pointer.
void file_delete(std::atomic< bool > &program_is_running, const unsigned int wait_interval_seconds, const std::string base_path)
std::string get_data_file_path(const std::string &base_path, int file_id, size_t page_size)
void renameForDelete(const std::string directoryName)
Renames a directory to DELETE_ME_<EPOCH>_<oldname>.
constexpr auto kLegacyDataFileExtension
A selection of helper methods for File I/O.
size_t readPage(FILE *f, const size_t pageSize, const size_t pageNum, int8_t *buf, const std::string &file_path)
Reads the specified page from the file f into buf.