Skip to content
This repository was archived by the owner on Mar 22, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion include/libpmemobj++/pexceptions.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: BSD-3-Clause
/* Copyright 2016-2020, Intel Corporation */
/* Copyright 2016-2021, Intel Corporation */

/**
* @file
Expand Down Expand Up @@ -55,6 +55,24 @@ class pool_error : public std::runtime_error {
}
};

/**
* Custom pool error class.
*
* Thrown when there is an invalid argument passed to create/open pool.
*/
class pool_invalid_argument : public pool_error {
public:
using pool_error::pool_error;

pool_invalid_argument &
with_pmemobj_errormsg()
{
(*this) = pool_invalid_argument(what() + std::string(": ") +
detail::errormsg());
return *this;
}
};

/**
* Custom transaction error class.
*
Expand Down
34 changes: 22 additions & 12 deletions include/libpmemobj++/pool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#define LIBPMEMOBJ_CPP_POOL_HPP

#include <cstddef>
#include <errno.h>
#include <functional>
#include <mutex>
#include <string>
Expand Down Expand Up @@ -108,9 +109,7 @@ class pool_base {
#else
pmemobjpool *pop = pmemobj_open(path.c_str(), layout.c_str());
#endif
if (pop == nullptr)
throw pmem::pool_error("Failed opening pool")
.with_pmemobj_errormsg();
check_pool(pop, "opening");

pmemobj_set_user_data(pop, new detail::pool_data);

Expand Down Expand Up @@ -144,9 +143,7 @@ class pool_base {
pmemobjpool *pop = pmemobj_create(path.c_str(), layout.c_str(),
size, mode);
#endif
if (pop == nullptr)
throw pmem::pool_error("Failed creating pool")
.with_pmemobj_errormsg();
check_pool(pop, "creating");

pmemobj_set_user_data(pop, new detail::pool_data);

Expand Down Expand Up @@ -191,9 +188,7 @@ class pool_base {
open(const std::wstring &path, const std::wstring &layout)
{
pmemobjpool *pop = pmemobj_openW(path.c_str(), layout.c_str());
if (pop == nullptr)
throw pmem::pool_error("Failed opening pool")
.with_pmemobj_errormsg();
check_pool(pop, "opening");

pmemobj_set_user_data(pop, new detail::pool_data);

Expand Down Expand Up @@ -223,9 +218,7 @@ class pool_base {
{
pmemobjpool *pop = pmemobj_createW(path.c_str(), layout.c_str(),
size, mode);
if (pop == nullptr)
throw pmem::pool_error("Failed creating pool")
.with_pmemobj_errormsg();
check_pool(pop, "creating");

pmemobj_set_user_data(pop, new detail::pool_data);

Expand Down Expand Up @@ -434,6 +427,23 @@ class pool_base {
}

protected:
static void
check_pool(pmemobjpool *pop, std::string mode)
{
if (pop == nullptr) {
if (errno == EINVAL || errno == EFBIG ||
errno == ENOENT || errno == EEXIST) {
throw pmem::pool_invalid_argument(
"Failed " + mode + " pool")
.with_pmemobj_errormsg();
} else {
throw pmem::pool_error("Failed " + mode +
" pool")
.with_pmemobj_errormsg();
}
}
}

/* The pool opaque handle */
PMEMobjpool *pop;

Expand Down
5 changes: 5 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ add_test_generic(NAME pool CASE 2 TRACERS none)
add_test_generic(NAME pool CASE 3 TRACERS none)
add_test_generic(NAME pool CASE 4 TRACERS none)
add_test_generic(NAME pool CASE 5 TRACERS none)
add_test_generic(NAME pool CASE 6 TRACERS none)
add_test_generic(NAME pool CASE 7 TRACERS none)
if (NOT WIN32)
add_test_generic(NAME pool CASE 8 TRACERS none)
endif()

if(WIN32)
build_test(pool_win pool/pool_win.cpp)
Expand Down
38 changes: 35 additions & 3 deletions tests/pool/pool.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: BSD-3-Clause
/* Copyright 2015-2020, Intel Corporation */
/* Copyright 2015-2021, Intel Corporation */

/*
* obj_cpp_pool.c -- cpp pool implementation test
Expand All @@ -23,6 +23,27 @@ struct root {
nvobj::p<int> val;
};

/* emulate no more space in the memory */
void *
null_alloc_func(size_t s)
{
errno = ENOSPC;
return nullptr;
}

void
test_pool_exceptions()
{
/* try catching pool_invalid_argument as a pool_error */
try {
throw pmem::pool_invalid_argument("test");
} catch (pmem::pool_error &e) {
return;
}

UT_ASSERT(0);
}

/*
* pool_create -- (internal) test pool create
*/
Expand All @@ -35,9 +56,12 @@ pool_create(const char *path, const char *layout, size_t poolsize,
pop = nvobj::pool<root>::create(path, layout, poolsize, mode);
nvobj::persistent_ptr<root> root = pop.root();
UT_ASSERT(root != nullptr);
} catch (pmem::pool_error &e) {
} catch (pmem::pool_invalid_argument &e) {
UT_OUT("%s: pool::create: %s", path, e.what());
return;
} catch (pmem::pool_error &e) {
UT_OUT("%s: pool::create: (pool_error) %s", path, e.what());
return;
}

os_stat_t stbuf;
Expand Down Expand Up @@ -69,9 +93,12 @@ pool_open(const char *path, const char *layout)
nvobj::pool<root> pop;
try {
pop = nvobj::pool<root>::open(path, layout);
} catch (pmem::pool_error &e) {
} catch (pmem::pool_invalid_argument &e) {
UT_OUT("%s: pool::open: %s", path, e.what());
return;
} catch (pmem::pool_error &e) {
UT_OUT("%s: pool::open: (pool_error) %s", path, e.what());
return;
}

UT_OUT("%s: pool::open: Success", path);
Expand Down Expand Up @@ -142,6 +169,9 @@ test(int argc, char *argv[])
layout = argv[3];

switch (argv[1][0]) {
case 'n':
pmemobj_set_funcs(null_alloc_func, NULL, NULL, NULL);
/* no break */
case 'c':
poolsize = std::stoul(argv[4], nullptr, 0) *
MB; /* in megabytes */
Expand All @@ -165,6 +195,8 @@ test(int argc, char *argv[])
default:
UT_FATAL("unknown operation");
}

test_pool_exceptions();
}

int
Expand Down
11 changes: 11 additions & 0 deletions tests/pool/pool_6.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# SPDX-License-Identifier: BSD-3-Clause
# Copyright 2021, Intel Corporation

include(${SRC_DIR}/../helpers.cmake)

setup()

# test for creating pool, without enough memory available
execute(${TEST_EXECUTABLE} n ${DIR}/testfile "test" 20 0600)

finish()
1 change: 1 addition & 0 deletions tests/pool/pool_6_none.out.match
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$(nW)testfile: pool::create: (pool_error) Failed creating pool: $(*)
11 changes: 11 additions & 0 deletions tests/pool/pool_7.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# SPDX-License-Identifier: BSD-3-Clause
# Copyright 2021, Intel Corporation

include(${SRC_DIR}/../helpers.cmake)

setup()

# test for creating too small pool
execute(${TEST_EXECUTABLE} c ${DIR}/testfile "test" 1 0600)

finish()
1 change: 1 addition & 0 deletions tests/pool/pool_7_none.out.match
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$(nW)testfile: pool::create: Failed creating pool: $(*)
13 changes: 13 additions & 0 deletions tests/pool/pool_8.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# SPDX-License-Identifier: BSD-3-Clause
# Copyright 2021, Intel Corporation

include(${SRC_DIR}/../helpers.cmake)

setup()

# test for opening pool without access
execute(${TEST_EXECUTABLE} c ${DIR}/testfile "test" 20 0600)
execute_process(COMMAND chmod 000 ${DIR}/testfile)
execute(${TEST_EXECUTABLE} o ${DIR}/testfile "test")

finish()
2 changes: 2 additions & 0 deletions tests/pool/pool_8_none.out.match
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
$(nW)testfile: file size $(N) mode $(N)
$(nW)testfile: pool::open: (pool_error) Failed opening pool: $(*)