| 175 | } |
| 176 | |
| 177 | static void safe_write_file(const fs::path & path, const std::string & data) { |
| 178 | fs::path path_tmp = path.string() + ".tmp"; |
| 179 | |
| 180 | if (path.has_parent_path()) { |
| 181 | fs::create_directories(path.parent_path()); |
| 182 | } |
| 183 | |
| 184 | std::ofstream file(path_tmp); |
| 185 | file << data; |
| 186 | file.close(); |
| 187 | |
| 188 | std::error_code ec; |
| 189 | |
| 190 | if (!file.fail()) { |
| 191 | fs::rename(path_tmp, path, ec); |
| 192 | } |
| 193 | if (file.fail() || ec) { |
| 194 | fs::remove(path_tmp, ec); |
| 195 | throw std::runtime_error("failed to write file: " + path.string()); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | static nl::json api_get(const std::string & url, |
| 200 | const std::string & token) { |
no test coverage detected