From b76bb6713ba12a88fbccdaaf063d916ecd3af0b2 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Sun, 14 Aug 2011 14:09:58 -0400 Subject: Add g_mkdtemp in the spirit of g_mkstemp At the same time, also add g_mkdtemp_full and g_dir_make_tmp variants. The patch also unifies the unique-name-generating code for all variants of mkstemp and mkdtemp and adds tests for the new functions. Based on patches by Paolo Bonzini, http://bugzilla.gnome.org/show_bug.cgi?id=118563 --- tests/file-test.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) (limited to 'tests') diff --git a/tests/file-test.c b/tests/file-test.c index adab1f20f..62f712c11 100644 --- a/tests/file-test.c +++ b/tests/file-test.c @@ -43,6 +43,8 @@ #include #endif +#include /* For open() */ + #ifdef G_OS_WIN32 #include /* For read(), write() etc */ #endif @@ -95,6 +97,55 @@ test_mkstemp (void) remove (template); } +static void +test_mkdtemp (void) +{ + char template[32], *retval; + int fd; + int i; + + strcpy (template, "foodir"); + retval = g_mkdtemp (template); + if (retval != NULL) + { + g_warning ("g_mkdtemp works even if template doesn't contain XXXXXX"); + g_rmdir (retval); + } + + strcpy (template, "foodir"); + retval = g_mkdtemp (template); + if (retval != NULL) + { + g_warning ("g_mkdtemp works even if template contains less than six X"); + g_rmdir (retval); + } + + strcpy (template, "fooXXXXXX"); + retval = g_mkdtemp (template); + g_assert (retval != NULL && "g_mkdtemp didn't work for template fooXXXXXX"); + g_assert (retval == template && "g_mkdtemp allocated the resulting string?"); + g_assert (!g_file_test (template, G_FILE_TEST_IS_REGULAR)); + g_assert (g_file_test (template, G_FILE_TEST_IS_DIR)); + + strcat (template, "/abc"); + fd = g_open (template, O_WRONLY | O_CREAT, 0600); + g_assert (fd != -1 && "couldn't open file in temporary directory"); + close (fd); + g_assert (g_file_test (template, G_FILE_TEST_IS_REGULAR)); + i = g_unlink (template); + g_assert (i != -1 && "couldn't unlink file in temporary directory"); + + template[9] = '\0'; + i = g_rmdir (template); + g_assert (i != -1 && "couldn't remove temporary directory"); + + strcpy (template, "fooXXXXXX.dir"); + retval = g_mkdtemp (template); + g_assert (retval != NULL && "g_mkdtemp didn't work for template fooXXXXXX.dir"); + g_assert (g_file_test (template, G_FILE_TEST_IS_DIR)); + g_rmdir (template); +} + static void test_readlink (void) { @@ -173,6 +224,7 @@ int main (int argc, char *argv[]) { test_mkstemp (); + test_mkdtemp (); test_readlink (); test_get_contents (); -- cgit v1.2.3