41 const std::filesystem::path& directory,
42 std::filesystem::path& result)
45 std::error_code error;
46 const auto status = std::filesystem::symlink_status(location, error);
47 if (error == std::errc::no_such_file_or_directory ||
48 (!error && status.type() == std::filesystem::file_type::not_found))
53 if (error || !std::filesystem::is_regular_file(status))
55 log::engineError(
"Cannot read application database location: invalid file.");
56 return errors::FAILED_OPEN_STORE;
58 const auto size = std::filesystem::file_size(location, error);
59 if (error || size == 0 || size > 32768)
61 log::engineError(
"Cannot read application database location: invalid file size.");
62 return errors::FAILED_OPEN_STORE;
64 std::ifstream input(location, std::ios::binary);
68 return errors::FAILED_OPEN_STORE;
70 std::string value(
static_cast<std::size_t
>(size),
'\0');
71 input.read(value.data(),
static_cast<std::streamsize
>(value.size()));
72 if (input.gcount() !=
static_cast<std::streamsize
>(value.size()) || input.peek() != std::char_traits<char>::eof() ||
73 input.bad() || value.find(
'\0') != std::string::npos ||
74 !std::filesystem::path(value).is_absolute())
76 log::engineError(
"Cannot read application database location: invalid path.");
77 return errors::FAILED_OPEN_STORE;
79 result = std::filesystem::path(value);
92 const std::filesystem::path& directory,
93 const std::string& filename,
94 const std::string& value,
95 bool replaceExisting =
true)
97 if (filename.empty() || std::filesystem::path(filename).filename() != filename ||
98 filename ==
"." || filename ==
".." || value.size() > 1024 * 1024 ||
99 value.find(
'\0') != std::string::npos)
101 log::engineError(
"Cannot save application file: invalid name or contents.");
102 return errors::FAILED_SAVE_STATE;
104 static std::atomic<unsigned long> sequence{0};
106 const auto process = GetCurrentProcessId();
110 std::filesystem::path temporary;
112 HANDLE file = INVALID_HANDLE_VALUE;
113 for (
unsigned attempt = 0; attempt < 64; ++attempt)
115 temporary = directory / (std::string(
".fray-file.") + std::to_string(
process) +
"." +
116 std::to_string(sequence.fetch_add(1)) +
".tmp");
117 file = CreateFileW(temporary.c_str(), GENERIC_WRITE, 0,
nullptr, CREATE_NEW,
118 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OPEN_REPARSE_POINT,
nullptr);
119 if (file != INVALID_HANDLE_VALUE || GetLastError() != ERROR_FILE_EXISTS)
122 if (file == INVALID_HANDLE_VALUE)
125 return errors::FAILED_SAVE_STATE;
128 const bool wrote = WriteFile(file, value.data(),
static_cast<DWORD
>(value.size()), &written,
nullptr) &&
129 written == value.size() && FlushFileBuffers(file);
130 const bool closed = CloseHandle(file);
131 bool replaced =
false;
134 replaced = MoveFileExW(temporary.c_str(), (directory / filename).c_str(),
135 (replaceExisting ? MOVEFILE_REPLACE_EXISTING : 0) | MOVEFILE_WRITE_THROUGH);
136 if (!replaced && !replaceExisting &&
137 (GetLastError() == ERROR_FILE_EXISTS || GetLastError() == ERROR_ALREADY_EXISTS))
142 for (
unsigned attempt = 0; attempt < 64; ++attempt)
144 temporary = directory / (std::string(
".fray-file.") + std::to_string(
process) +
"." +
145 std::to_string(sequence.fetch_add(1)) +
".tmp");
146 file = ::open(temporary.c_str(), O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, 0600);
147 if (file >= 0 || errno != EEXIST)
152 log::engineError(
"Cannot create temporary application file: %s", std::strerror(errno));
153 return errors::FAILED_SAVE_STATE;
155 std::size_t offset = 0;
156 while (offset < value.size())
158 const ssize_t count = ::write(file, value.data() + offset, value.size() - offset);
159 if (count < 0 && errno == EINTR)
163 offset +=
static_cast<std::size_t
>(count);
165 const bool wrote = offset == value.size() && fsync(file) == 0;
166 const bool closed = ::close(file) == 0;
167 bool replaced =
false;
171 replaced = ::rename(temporary.c_str(), (directory / filename).c_str()) == 0;
173 replaced = ::link(temporary.c_str(), (directory / filename).c_str()) == 0 || errno == EEXIST;
176 if (!replaceExisting)
178 std::error_code cleanup;
179 std::filesystem::remove(temporary, cleanup);
182 log::engineError(
"Cannot remove temporary application file: %s", cleanup.message().c_str());
183 return errors::FAILED_SAVE_STATE;
189 std::error_code cleanup;
190 std::filesystem::remove(temporary, cleanup);
192 log::engineError(
"Cannot remove temporary application file: %s", cleanup.message().c_str());
193 return errors::FAILED_SAVE_STATE;