[block-cache] fix potential file descriptor leak

Encapsulate file descriptor into an object, to ensure that an fd will be
closed properly while exception raised, e.g., the block_cache throws
exception during the block_manager's construction.
This commit is contained in:
Ming-Hung Tsai
2020-02-22 17:37:22 +08:00
parent c85ea5ef76
commit 955e11bc28
9 changed files with 42 additions and 32 deletions

View File

@@ -8,11 +8,20 @@
//----------------------------------------------------------------
namespace file_utils {
int open_file(std::string const &path, int flags);
struct file_descriptor {
// file_descriptor is movable but not copyable
file_descriptor(file_descriptor &&) = default;
file_descriptor& operator=(file_descriptor &&) = default;
file_descriptor(std::string const &path, int flags);
virtual ~file_descriptor();
int fd_;
};
bool file_exists(std::string const &path);
void check_file_exists(std::string const &file, bool must_be_regular_file = true);
int create_block_file(std::string const &path, off_t file_size);
int open_block_file(std::string const &path, off_t min_size, bool writeable, bool excl = true);
file_descriptor create_block_file(std::string const &path, off_t file_size);
file_descriptor open_block_file(std::string const &path, off_t min_size, bool writeable, bool excl = true);
uint64_t get_file_length(std::string const &file);
void zero_superblock(std::string const &path);
}