MCPcopy Create free account
hub / github.com/bytedance/bolt / AsyncLocalWriteFile

Method AsyncLocalWriteFile

bolt/common/file/File.cpp:540–581  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

538}
539
540AsyncLocalWriteFile::AsyncLocalWriteFile(
541 std::string_view path,
542 bool shouldCreateParentDirectories,
543 bool shouldThrowOnFileAlreadyExists) {
544 auto dir = fs::path(path).parent_path();
545 if (shouldCreateParentDirectories && !fs::exists(dir)) {
546 BOLT_CHECK(
547 common::generateFileDirectory(dir.c_str()),
548 "Failed to generate file directory");
549 }
550 std::unique_ptr<char[]> buf(new char[path.size() + 1]);
551 buf[path.size()] = 0;
552 memcpy(buf.get(), path.data(), path.size());
553 {
554 if (shouldThrowOnFileAlreadyExists) {
555 FILE* exists = fopen(buf.get(), "rb");
556 BOLT_CHECK(
557 !exists,
558 "Failure in LocalWriteFile: path '{}' already exists.",
559 path);
560 }
561 }
562 FILE* file;
563 int ringDepth = 64;
564 ring_ = std::make_unique<io_uring>();
565 auto ret = io_uring_queue_init(ringDepth, ring_.get(), 0);
566 if (ret < 0) {
567 LOG(WARNING) << "Failed to init ring:" << -ret << ". Use sync write";
568 uringEnabled_ = false;
569 ring_.reset();
570 file = fopen(buf.get(), "ab");
571 } else {
572 LOG(INFO) << "io_uring sets up successfully for AsyncLocalWriteFile";
573 file = fopen(buf.get(), "wb");
574 }
575 BOLT_CHECK_NOT_NULL(
576 file,
577 "fopen failure in LocalWriteFile constructor, {} {}.",
578 path,
579 folly::errnoStr(errno));
580 file_ = file;
581}
582
583AsyncLocalWriteFile::~AsyncLocalWriteFile() {
584 try {

Callers

nothing calls this directly

Calls 6

generateFileDirectoryFunction · 0.85
memcpyFunction · 0.85
sizeMethod · 0.45
getMethod · 0.45
dataMethod · 0.45
resetMethod · 0.45

Tested by

no test coverage detected