Skip to content
This repository was archived by the owner on Mar 22, 2023. It is now read-only.

Commit d0f80c0

Browse files
committed
add pool_invalid_argument exception
1 parent f22c61e commit d0f80c0

10 files changed

Lines changed: 120 additions & 16 deletions

File tree

include/libpmemobj++/pexceptions.hpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// SPDX-License-Identifier: BSD-3-Clause
2-
/* Copyright 2016-2020, Intel Corporation */
2+
/* Copyright 2016-2021, Intel Corporation */
33

44
/**
55
* @file
@@ -55,6 +55,24 @@ class pool_error : public std::runtime_error {
5555
}
5656
};
5757

58+
/**
59+
* Custom pool error class.
60+
*
61+
* Thrown when there is an invalid argument passed to create/open pool.
62+
*/
63+
class pool_invalid_argument : public pool_error {
64+
public:
65+
using pool_error::pool_error;
66+
67+
pool_invalid_argument &
68+
with_pmemobj_errormsg()
69+
{
70+
(*this) = pool_invalid_argument(what() + std::string(": ") +
71+
detail::errormsg());
72+
return *this;
73+
}
74+
};
75+
5876
/**
5977
* Custom transaction error class.
6078
*

include/libpmemobj++/pool.hpp

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#define LIBPMEMOBJ_CPP_POOL_HPP
1111

1212
#include <cstddef>
13+
#include <errno.h>
1314
#include <functional>
1415
#include <mutex>
1516
#include <string>
@@ -108,9 +109,7 @@ class pool_base {
108109
#else
109110
pmemobjpool *pop = pmemobj_open(path.c_str(), layout.c_str());
110111
#endif
111-
if (pop == nullptr)
112-
throw pmem::pool_error("Failed opening pool")
113-
.with_pmemobj_errormsg();
112+
check_pool(pop, "opening");
114113

115114
pmemobj_set_user_data(pop, new detail::pool_data);
116115

@@ -144,9 +143,7 @@ class pool_base {
144143
pmemobjpool *pop = pmemobj_create(path.c_str(), layout.c_str(),
145144
size, mode);
146145
#endif
147-
if (pop == nullptr)
148-
throw pmem::pool_error("Failed creating pool")
149-
.with_pmemobj_errormsg();
146+
check_pool(pop, "creating");
150147

151148
pmemobj_set_user_data(pop, new detail::pool_data);
152149

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

198193
pmemobj_set_user_data(pop, new detail::pool_data);
199194

@@ -223,9 +218,7 @@ class pool_base {
223218
{
224219
pmemobjpool *pop = pmemobj_createW(path.c_str(), layout.c_str(),
225220
size, mode);
226-
if (pop == nullptr)
227-
throw pmem::pool_error("Failed creating pool")
228-
.with_pmemobj_errormsg();
221+
check_pool(pop, "creating");
229222

230223
pmemobj_set_user_data(pop, new detail::pool_data);
231224

@@ -434,6 +427,23 @@ class pool_base {
434427
}
435428

436429
protected:
430+
static void
431+
check_pool(pmemobjpool *pop, std::string mode)
432+
{
433+
if (pop == nullptr) {
434+
if (errno == EINVAL || errno == EFBIG ||
435+
errno == ENOENT || errno == EEXIST) {
436+
throw pmem::pool_invalid_argument(
437+
"Failed " + mode + " pool")
438+
.with_pmemobj_errormsg();
439+
} else {
440+
throw pmem::pool_error("Failed " + mode +
441+
" pool")
442+
.with_pmemobj_errormsg();
443+
}
444+
}
445+
}
446+
437447
/* The pool opaque handle */
438448
PMEMobjpool *pop;
439449

tests/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,11 @@ add_test_generic(NAME pool CASE 2 TRACERS none)
161161
add_test_generic(NAME pool CASE 3 TRACERS none)
162162
add_test_generic(NAME pool CASE 4 TRACERS none)
163163
add_test_generic(NAME pool CASE 5 TRACERS none)
164+
add_test_generic(NAME pool CASE 6 TRACERS none)
165+
add_test_generic(NAME pool CASE 7 TRACERS none)
166+
if (NOT WIN32)
167+
add_test_generic(NAME pool CASE 8 TRACERS none)
168+
endif()
164169

165170
if(WIN32)
166171
build_test(pool_win pool/pool_win.cpp)

tests/pool/pool.cpp

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// SPDX-License-Identifier: BSD-3-Clause
2-
/* Copyright 2015-2020, Intel Corporation */
2+
/* Copyright 2015-2021, Intel Corporation */
33

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

26+
/* emulate no more space in the memory */
27+
void *
28+
null_alloc_func(size_t s)
29+
{
30+
errno = ENOSPC;
31+
return nullptr;
32+
}
33+
34+
void
35+
test_pool_exceptions()
36+
{
37+
/* try catching pool_invalid_argument as a pool_error */
38+
try {
39+
throw pmem::pool_invalid_argument("test");
40+
} catch (pmem::pool_error &e) {
41+
return;
42+
}
43+
44+
UT_ASSERT(0);
45+
}
46+
2647
/*
2748
* pool_create -- (internal) test pool create
2849
*/
@@ -35,9 +56,12 @@ pool_create(const char *path, const char *layout, size_t poolsize,
3556
pop = nvobj::pool<root>::create(path, layout, poolsize, mode);
3657
nvobj::persistent_ptr<root> root = pop.root();
3758
UT_ASSERT(root != nullptr);
38-
} catch (pmem::pool_error &e) {
59+
} catch (pmem::pool_invalid_argument &e) {
3960
UT_OUT("%s: pool::create: %s", path, e.what());
4061
return;
62+
} catch (pmem::pool_error &e) {
63+
UT_OUT("%s: pool::create: (pool_error) %s", path, e.what());
64+
return;
4165
}
4266

4367
os_stat_t stbuf;
@@ -69,9 +93,12 @@ pool_open(const char *path, const char *layout)
6993
nvobj::pool<root> pop;
7094
try {
7195
pop = nvobj::pool<root>::open(path, layout);
72-
} catch (pmem::pool_error &e) {
96+
} catch (pmem::pool_invalid_argument &e) {
7397
UT_OUT("%s: pool::open: %s", path, e.what());
7498
return;
99+
} catch (pmem::pool_error &e) {
100+
UT_OUT("%s: pool::open: (pool_error) %s", path, e.what());
101+
return;
75102
}
76103

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

144171
switch (argv[1][0]) {
172+
case 'n':
173+
pmemobj_set_funcs(null_alloc_func, NULL, NULL, NULL);
174+
/* no break */
145175
case 'c':
146176
poolsize = std::stoul(argv[4], nullptr, 0) *
147177
MB; /* in megabytes */
@@ -165,6 +195,8 @@ test(int argc, char *argv[])
165195
default:
166196
UT_FATAL("unknown operation");
167197
}
198+
199+
test_pool_exceptions();
168200
}
169201

170202
int

tests/pool/pool_6.cmake

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# SPDX-License-Identifier: BSD-3-Clause
2+
# Copyright 2021, Intel Corporation
3+
4+
include(${SRC_DIR}/../helpers.cmake)
5+
6+
setup()
7+
8+
# test for creating pool, without enough memory available
9+
execute(${TEST_EXECUTABLE} n ${DIR}/testfile "test" 20 0600)
10+
11+
finish()

tests/pool/pool_6_none.out.match

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
$(nW)testfile: pool::create: (pool_error) Failed creating pool: $(*)

tests/pool/pool_7.cmake

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# SPDX-License-Identifier: BSD-3-Clause
2+
# Copyright 2021, Intel Corporation
3+
4+
include(${SRC_DIR}/../helpers.cmake)
5+
6+
setup()
7+
8+
# test for creating too small pool
9+
execute(${TEST_EXECUTABLE} c ${DIR}/testfile "test" 1 0600)
10+
11+
finish()

tests/pool/pool_7_none.out.match

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
$(nW)testfile: pool::create: Failed creating pool: $(*)

tests/pool/pool_8.cmake

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# SPDX-License-Identifier: BSD-3-Clause
2+
# Copyright 2021, Intel Corporation
3+
4+
include(${SRC_DIR}/../helpers.cmake)
5+
6+
setup()
7+
8+
# test for opening pool without access
9+
execute(${TEST_EXECUTABLE} c ${DIR}/testfile "test" 20 0600)
10+
execute_process(COMMAND chmod 000 ${DIR}/testfile)
11+
execute(${TEST_EXECUTABLE} o ${DIR}/testfile "test")
12+
13+
finish()

tests/pool/pool_8_none.out.match

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
$(nW)testfile: file size $(N) mode $(N)
2+
$(nW)testfile: pool::open: (pool_error) Failed opening pool: $(*)

0 commit comments

Comments
 (0)