| 510 | } |
| 511 | |
| 512 | Status WritePBToPath(Env* env, const std::string& path, |
| 513 | const MessageLite& msg, |
| 514 | SyncMode sync) { |
| 515 | const string tmp_template = path + kTmpInfix + kTmpTemplateSuffix; |
| 516 | string tmp_path; |
| 517 | |
| 518 | unique_ptr<WritableFile> file; |
| 519 | WritableFileOptions opts; |
| 520 | opts.is_sensitive = false; |
| 521 | RETURN_NOT_OK(env->NewTempWritableFile(opts, tmp_template, &tmp_path, &file)); |
| 522 | auto tmp_deleter = MakeScopedCleanup([&]() { |
| 523 | WARN_NOT_OK(env->DeleteFile(tmp_path), "Could not delete file " + tmp_path); |
| 524 | }); |
| 525 | |
| 526 | WritableFileOutputStream output(file.get()); |
| 527 | bool res = msg.SerializeToZeroCopyStream(&output); |
| 528 | if (!res || !output.Flush()) { |
| 529 | return Status::IOError("Unable to serialize PB to file"); |
| 530 | } |
| 531 | |
| 532 | if (sync == pb_util::SYNC) { |
| 533 | RETURN_NOT_OK_PREPEND(file->Sync(), "Failed to Sync() " + tmp_path); |
| 534 | } |
| 535 | RETURN_NOT_OK_PREPEND(file->Close(), "Failed to Close() " + tmp_path); |
| 536 | RETURN_NOT_OK_PREPEND(env->RenameFile(tmp_path, path), "Failed to rename tmp file to " + path); |
| 537 | tmp_deleter.cancel(); |
| 538 | if (sync == pb_util::SYNC) { |
| 539 | RETURN_NOT_OK_PREPEND(env->SyncDir(DirName(path)), "Failed to SyncDir() parent of " + path); |
| 540 | } |
| 541 | return Status::OK(); |
| 542 | } |
| 543 | |
| 544 | Status ReadPBFromPath(Env* env, const std::string& path, MessageLite* msg) { |
| 545 | unique_ptr<SequentialFile> rfile; |
nothing calls this directly
no test coverage detected