Commit 0e36db01 authored by Daniel Salzman's avatar Daniel Salzman

tests: unify mkdtemp usage

parent 84bb5b44
......@@ -89,6 +89,10 @@ static bool rm_dir_contents(int dir_fd)
bool test_rm_rf(const char *path)
{
if (!path) {
return false;
}
int fd = open(path, O_NOFOLLOW);
if (fd < 0) {
return false;
......
......@@ -112,10 +112,8 @@ static char *token_path = NULL;
*/
static void token_cleanup(void)
{
if (token_path) {
test_rm_rf(token_path);
free(token_path);
}
test_rm_rf(token_path);
free(token_path);
}
/*!
......
......@@ -14,14 +14,13 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <dirent.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <assert.h>
#include <tap/basic.h>
#include <tap/files.h>
#include "contrib/string.h"
#include "libknot/libknot.h"
......@@ -259,9 +258,8 @@ int main(int argc, char *argv[])
knot_mm_t pool;
mm_ctx_mempool(&pool, MM_DEFAULT_BLKSIZE);
/* Temporary DB identifier. */
char dbid_buf[] = "/tmp/namedb.XXXXXX";
char *dbid = mkdtemp(dbid_buf);
char *dbid = test_mkdtemp();
ok(dbid != NULL, "make temporary directory");
/* Random keys. */
unsigned nkeys = 10000;
......@@ -282,20 +280,8 @@ int main(int argc, char *argv[])
/* Cleanup. */
mp_delete(pool.ctx);
/* Cleanup temporary DB. */
DIR *dir = opendir(dbid);
struct dirent *dp;
while ((dp = readdir(dir)) != NULL) {
if (dp->d_name[0] == '.') {
continue;
}
char *file = sprintf_alloc("%s/%s", dbid, dp->d_name);
remove(file);
free(file);
}
closedir(dir);
remove(dbid);
test_rm_rf(dbid);
free(dbid);
return 0;
}
......@@ -15,11 +15,11 @@
*/
#include <assert.h>
#include <dirent.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <tap/basic.h>
#include <tap/files.h>
#include "contrib/string.h"
#include "libknot/libknot.h"
......@@ -41,9 +41,8 @@ int main(int argc, char *argv[])
return EXIT_SUCCESS;
}
// Temporary DB identifier.
char dbid_buf[] = "/tmp/timerdb.XXXXXX";
const char *dbid = mkdtemp(dbid_buf);
char *dbid = test_mkdtemp();
ok(dbid != NULL, "make temporary directory");
// Mockup zones.
knot_dname_t *zone_name;
......@@ -123,20 +122,8 @@ int main(int argc, char *argv[])
zone_free(&zone_1);
zone_free(&zone_2);
close_timers_db(db);
// Cleanup temporary DB.
DIR *dir = opendir(dbid);
struct dirent *dp;
while ((dp = readdir(dir)) != NULL) {
if (dp->d_name[0] == '.') {
continue;
}
char *file = sprintf_alloc("%s/%s", dbid, dp->d_name);
remove(file);
free(file);
}
closedir(dir);
remove(dbid);
test_rm_rf(dbid);
free(dbid);
return EXIT_SUCCESS;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment