| 262 | } |
| 263 | |
| 264 | LocalWriteFile::LocalWriteFile( |
| 265 | std::string_view path, |
| 266 | bool shouldCreateParentDirectories, |
| 267 | bool shouldThrowOnFileAlreadyExists) { |
| 268 | auto dir = fs::path(path).parent_path(); |
| 269 | if (shouldCreateParentDirectories && !fs::exists(dir)) { |
| 270 | BOLT_CHECK( |
| 271 | common::generateFileDirectory(dir.c_str()), |
| 272 | "Failed to generate file directory"); |
| 273 | } |
| 274 | |
| 275 | std::unique_ptr<char[]> buf(new char[path.size() + 1]); |
| 276 | buf[path.size()] = 0; |
| 277 | memcpy(buf.get(), path.data(), path.size()); |
| 278 | { |
| 279 | if (shouldThrowOnFileAlreadyExists) { |
| 280 | FILE* exists = fopen(buf.get(), "rb"); |
| 281 | if (exists) { |
| 282 | fclose(exists); |
| 283 | BOLT_FAIL("Failure in LocalWriteFile: path '{}' already exists.", path); |
| 284 | } |
| 285 | } |
| 286 | } |
| 287 | auto* file = fopen(buf.get(), "ab"); |
| 288 | BOLT_CHECK_NOT_NULL( |
| 289 | file, |
| 290 | "fopen failure in LocalWriteFile constructor, {} {}.", |
| 291 | path, |
| 292 | folly::errnoStr(errno)); |
| 293 | file_ = file; |
| 294 | } |
| 295 | |
| 296 | LocalWriteFile::~LocalWriteFile() { |
| 297 | try { |
nothing calls this directly
no test coverage detected