[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

@ -136,11 +136,12 @@ namespace persistent_data {
private:
uint64_t choose_cache_size(block_address nr_blocks) const;
int open_or_create_block_file(std::string const &path, off_t file_size,
mode m, bool excl);
file_utils::file_descriptor open_or_create_block_file(std::string const &path,
off_t file_size,
mode m, bool excl);
void check(block_address b) const;
int fd_;
file_utils::file_descriptor fd_;
mutable block_cache bc_;
unsigned superblock_ref_count_;
};

View File

@ -154,7 +154,7 @@ namespace persistent_data {
}
template <uint32_t BlockSize>
int
file_utils::file_descriptor
block_manager<BlockSize>::open_or_create_block_file(std::string const &path, off_t file_size, mode m, bool excl)
{
switch (m) {