diff --git a/env/fs_posix.cc b/env/fs_posix.cc index f3f7f152e..84bab192b 100644 --- a/env/fs_posix.cc +++ b/env/fs_posix.cc @@ -553,24 +553,35 @@ class PosixFileSystem : public FileSystem { IOStatus NewLogger(const std::string& fname, const IOOptions& /*opts*/, std::shared_ptr* result, IODebugContext* /*dbg*/) override { - FILE* f; + FILE* f = nullptr; + int fd; { IOSTATS_TIMER_GUARD(open_nanos); - f = fopen(fname.c_str(), - "w" + fd = open(fname.c_str(), + cloexec_flags(O_WRONLY | O_CREAT | O_TRUNC, nullptr), + GetDBFileMode(allow_non_owner_access_)); + if (fd != -1) { + f = fdopen(fd, + "w" #ifdef __GLIBC_PREREQ #if __GLIBC_PREREQ(2, 7) - "e" // glibc extension to enable O_CLOEXEC + "e" // glibc extension to enable O_CLOEXEC #endif #endif - ); + ); + } } - if (f == nullptr) { + if (fd == -1) { result->reset(); return status_to_io_status( - IOError("when fopen a file for new logger", fname, errno)); + IOError("when open a file for new logger", fname, errno)); + } + if (f == nullptr) { + close(fd); + result->reset(); + return status_to_io_status( + IOError("when fdopen a file for new logger", fname, errno)); } else { - int fd = fileno(f); #ifdef ROCKSDB_FALLOCATE_PRESENT fallocate(fd, FALLOC_FL_KEEP_SIZE, 0, 4 * 1024); #endif