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