diff options
author | costan <costan@google.com> | 2018-03-09 10:32:55 -0800 |
---|---|---|
committer | Victor Costan <pwnall@chromium.org> | 2018-03-09 10:38:04 -0800 |
commit | 623d014a54f8cf9b74ad6aaba9181ca1e65c43a1 (patch) | |
tree | 5f1a680c264df7917a6d700a89ea4533985c84fd | |
parent | 8c8024ea33d8efc8c415597fb7fa1745002961d6 (diff) | |
download | leveldb-623d014a54f8cf9b74ad6aaba9181ca1e65c43a1.tar.gz |
Expose Env::GetTempDirectory() for use in C test.
This removes the use of the non-portable headers <sys/types.h> and <unistd.h> in c_test.c.
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=188503102
-rw-r--r-- | db/c.cc | 13 | ||||
-rw-r--r-- | db/c_test.c | 19 | ||||
-rw-r--r-- | include/leveldb/c.h | 3 |
3 files changed, 19 insertions, 16 deletions
@@ -5,7 +5,6 @@ #include "leveldb/c.h" #include <stdlib.h> -#include <unistd.h> #include "leveldb/cache.h" #include "leveldb/comparator.h" #include "leveldb/db.h" @@ -584,6 +583,18 @@ void leveldb_env_destroy(leveldb_env_t* env) { delete env; } +char* leveldb_env_get_test_directory(leveldb_env_t* env) { + std::string result; + if (!env->rep->GetTestDirectory(&result).ok()) { + return NULL; + } + + char* buffer = static_cast<char*>(malloc(result.size() + 1)); + memcpy(buffer, result.data(), result.size()); + buffer[result.size()] = '\0'; + return buffer; +} + void leveldb_free(void* ptr) { free(ptr); } diff --git a/db/c_test.c b/db/c_test.c index 4a14151..7284de5 100644 --- a/db/c_test.c +++ b/db/c_test.c @@ -8,24 +8,14 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> -#include <sys/types.h> -#include <unistd.h> const char* phase = ""; -static char dbname[200]; static void StartPhase(const char* name) { fprintf(stderr, "=== Test %s\n", name); phase = name; } -static const char* GetTempDir(void) { - const char* ret = getenv("TEST_TMPDIR"); - if (ret == NULL || ret[0] == '\0') - ret = "/tmp"; - return ret; -} - #define CheckNoError(err) \ if ((err) != NULL) { \ fprintf(stderr, "%s:%d: %s: %s\n", __FILE__, __LINE__, phase, (err)); \ @@ -162,21 +152,19 @@ int main(int argc, char** argv) { leveldb_options_t* options; leveldb_readoptions_t* roptions; leveldb_writeoptions_t* woptions; + char* dbname; char* err = NULL; int run = -1; CheckCondition(leveldb_major_version() >= 1); CheckCondition(leveldb_minor_version() >= 1); - snprintf(dbname, sizeof(dbname), - "%s/leveldb_c_test-%d", - GetTempDir(), - ((int) geteuid())); - StartPhase("create_objects"); cmp = leveldb_comparator_create(NULL, CmpDestroy, CmpCompare, CmpName); env = leveldb_create_default_env(); cache = leveldb_cache_create_lru(100000); + dbname = leveldb_env_get_test_directory(env); + CheckCondition(dbname != NULL); options = leveldb_options_create(); leveldb_options_set_comparator(options, cmp); @@ -382,6 +370,7 @@ int main(int argc, char** argv) { leveldb_options_destroy(options); leveldb_readoptions_destroy(roptions); leveldb_writeoptions_destroy(woptions); + leveldb_free(dbname); leveldb_cache_destroy(cache); leveldb_comparator_destroy(cmp); leveldb_env_destroy(env); diff --git a/include/leveldb/c.h b/include/leveldb/c.h index dc6f255..1124153 100644 --- a/include/leveldb/c.h +++ b/include/leveldb/c.h @@ -245,6 +245,9 @@ LEVELDB_EXPORT void leveldb_cache_destroy(leveldb_cache_t* cache); LEVELDB_EXPORT leveldb_env_t* leveldb_create_default_env(); LEVELDB_EXPORT void leveldb_env_destroy(leveldb_env_t*); +/* If not NULL, the returned buffer must be released using leveldb_free(). */ +LEVELDB_EXPORT char* leveldb_env_get_test_directory(leveldb_env_t*); + /* Utility */ /* Calls free(ptr). |