1 /*
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2005, 2013 Oracle and/or its affiliates.  All rights reserved.
5  *
6  * $Id$
7  */
8 
9 #include "bench.h"
10 
11 static int testdir_remove __P((char *));
12 
13 int
b_util_have_hash()14 b_util_have_hash()
15 {
16 #if defined(HAVE_HASH) ||\
17     DB_VERSION_MAJOR < 4 || DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR < 2
18 	return (0);
19 #else
20 	fprintf(stderr,
21     "library build did not include support for the Hash access method\n");
22 	return (1);
23 #endif
24 }
25 
26 int
b_util_have_heap()27 b_util_have_heap()
28 {
29 #if defined(HAVE_HEAP) ||\
30     DB_VERSION_MAJOR < 5 || DB_VERSION_MAJOR == 5 && DB_VERSION_MINOR < 2
31 	return (0);
32 #else
33 	fprintf(stderr,
34     "library build did not include support for the Heap access method\n");
35 	return (1);
36 #endif
37 }
38 
39 int
b_util_have_queue()40 b_util_have_queue()
41 {
42 #if defined(HAVE_QUEUE) ||\
43     DB_VERSION_MAJOR < 4 || DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR < 2
44 	return (0);
45 #else
46 	fprintf(stderr,
47     "library build did not include support for the Queue access method\n");
48 	return (1);
49 #endif
50 }
51 
52 /*
53  * b_util_dir_setup --
54  *	Create the test directory.
55  */
56 int
b_util_dir_setup()57 b_util_dir_setup()
58 {
59 	int ret;
60 
61 #if DB_VERSION_MAJOR > 4 || DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR > 3
62 	if ((ret = __os_mkdir(NULL, TESTDIR, 0755)) != 0) {
63 #else
64 	if ((ret = mkdir(TESTDIR, 0755)) != 0) {
65 #endif
66 		fprintf(stderr,
67 		    "%s: %s: %s\n", progname, TESTDIR, db_strerror(ret));
68 		return (1);
69 	}
70 	return (0);
71 }
72 
73 #if DB_VERSION_MAJOR > 4 || DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR > 4
74 #define	OS_EXISTS(a, b, c)	__os_exists(a, b, c)
75 #else
76 #define	OS_EXISTS(a, b, c)	__os_exists(b, c)
77 #endif
78 
79 /*
80  * b_util_dir_teardown
81  *	Clean up the test directory.
82  */
83 int
84 b_util_dir_teardown()
85 {
86 	int ret;
87 
88 	if (OS_EXISTS(NULL, TESTFILE, NULL) == 0 &&
89 	    (ret = b_util_unlink(TESTFILE)) != 0) {
90 		fprintf(stderr,
91 		    "%s: %s: %s\n", progname, TESTFILE, db_strerror(ret));
92 		return (1);
93 	}
94 	return (testdir_remove(TESTDIR) ? 1 : 0);
95 }
96 
97 /*
98  * testdir_remove --
99  *	Remove a directory and all its contents, the "dir" must contain no
100  *	subdirectories, because testdir_remove will not recursively delete
101  *	all subdirectories.
102  */
103 static int
104 testdir_remove(dir)
105 	char *dir;
106 {
107 	int cnt, i, isdir, ret;
108 	char buf[1024], **names;
109 
110 	ret = 0;
111 
112 	/* If the directory doesn't exist, we're done. */
113 	if (OS_EXISTS(NULL, dir, &isdir) != 0)
114 		return (0);
115 
116 	/* Get a list of the directory contents. */
117 #if DB_VERSION_MAJOR > 4 || DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR > 6
118 	if ((ret = __os_dirlist(NULL, dir, 0, &names, &cnt)) != 0)
119 		return (ret);
120 #else
121 	if ((ret = __os_dirlist(NULL, dir, &names, &cnt)) != 0)
122 		return (ret);
123 #endif
124 	/* Go through the file name list, remove each file in the list */
125 	for (i = 0; i < cnt; ++i) {
126 		(void)snprintf(buf, sizeof(buf),
127 		    "%s%c%s", dir, PATH_SEPARATOR[0], names[i]);
128 		if ((ret = OS_EXISTS(NULL, buf, &isdir)) != 0)
129 			goto file_err;
130 		if (!isdir && (ret = b_util_unlink(buf)) != 0) {
131 file_err:		fprintf(stderr, "%s: %s: %s\n",
132 			    progname, buf, db_strerror(ret));
133 			break;
134 		}
135 	}
136 
137 	__os_dirfree(NULL, names, cnt);
138 
139 	/*
140 	 * If we removed the contents of the directory, remove the directory
141 	 * itself.
142 	 */
143 	if (i == cnt && (ret = rmdir(dir)) != 0)
144 		fprintf(stderr,
145 		    "%s: %s: %s\n", progname, dir, db_strerror(errno));
146 	return (ret);
147 }
148 
149 void
150 b_util_abort()
151 {
152 #if DB_VERSION_MAJOR < 4 || DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR < 6
153 	abort();
154 #elif DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR == 6
155 	__os_abort();
156 #else
157 	__os_abort(NULL);
158 #endif
159 }
160 
161 int
162 b_util_unlink(path)
163 	char *path;
164 {
165 #if DB_VERSION_MAJOR < 4 || DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR < 7
166 	return (__os_unlink(NULL, path));
167 #else
168 	return (__os_unlink(NULL, path, 0));
169 #endif
170 }
171