2006-08-26 Gonzalo Paniagua Javier <gonzalo@ximian.com>
authorGonzalo Paniagua Javier <gonzalo.mono@gmail.com>
Sat, 26 Aug 2006 23:32:25 +0000 (23:32 -0000)
committerGonzalo Paniagua Javier <gonzalo.mono@gmail.com>
Sat, 26 Aug 2006 23:32:25 +0000 (23:32 -0000)
* test/file.c:
* src/glib.h:
* src/gfile.c:  implemented g_file_test. Changed the default mask to
.XXXXXX. Add tests.

svn path=/trunk/mono/; revision=64419

eglib/ChangeLog
eglib/src/gfile.c
eglib/src/glib.h
eglib/test/file.c

index a040801844161abe3239a2e1b5d34711bc3414e5..1681ab44e587e9de18e9619b263a57fab6b9082d 100644 (file)
@@ -1,3 +1,10 @@
+2006-08-26 Gonzalo Paniagua Javier <gonzalo@ximian.com>
+
+       * test/file.c:
+       * src/glib.h:
+       * src/gfile.c:  implemented g_file_test. Changed the default mask to
+       .XXXXXX. Add tests.
+
 2006-08-26  Raja R Harinath  <rharinath@novell.com>
 
        * src/sort.frag.h (digit): Declare here based on externally
index 5d6a6f3fba44cbc5c120421892ef96e7974c838a..72bf600dba0d55bd91134aed6d38e5405be3f196 100644 (file)
@@ -156,7 +156,7 @@ g_file_get_contents (const gchar *filename, gchar **contents, gsize *length, GEr
 gint
 g_file_open_tmp (const gchar *tmpl, gchar **name_used, GError **error)
 {
-       const static gchar *default_tmpl = "file-g-XXXXXX";
+       const static gchar *default_tmpl = ".XXXXXX";
        gchar *t;
        gint fd;
        gint len;
@@ -200,3 +200,44 @@ g_file_open_tmp (const gchar *tmpl, gchar **name_used, GError **error)
        return fd;
 }
 
+gboolean
+g_file_test (const gchar *filename, GFileTest test)
+{
+       struct stat st;
+       gboolean have_stat;
+
+       if (filename == NULL || test == 0)
+               return FALSE;
+
+       have_stat = FALSE;
+       if ((test & G_FILE_TEST_EXISTS) != 0) {
+               if (access (filename, F_OK) == 0)
+                       return TRUE;
+       }
+
+       if ((test & G_FILE_TEST_IS_EXECUTABLE) != 0) {
+               if (access (filename, X_OK) == 0)
+                       return TRUE;
+       }
+
+       if ((test & G_FILE_TEST_IS_SYMLINK) != 0) {
+               have_stat = (lstat (filename, &st) == 0);
+               if (have_stat && S_ISLNK (st.st_mode))
+                       return TRUE;
+       }
+
+       if ((test & G_FILE_TEST_IS_REGULAR) != 0) {
+               if (!have_stat)
+                       have_stat = (stat (filename, &st) == 0);
+               if (have_stat && S_ISREG (st.st_mode))
+                       return TRUE;
+       }
+       if ((test & G_FILE_TEST_IS_DIR) != 0) {
+               if (!have_stat)
+                       have_stat = (stat (filename, &st) == 0);
+               if (have_stat && S_ISDIR (st.st_mode))
+                       return TRUE;
+       }
+       return FALSE;
+}
+
index 2fc6ba41219dadab44a6f2a7e5518b473ab08df0..d321f95d717dcbd808f6b49f7447175ff6ef39d3 100644 (file)
@@ -513,8 +513,20 @@ typedef enum {
        G_FILE_ERROR_FAILED
 } GFileError;
 
+typedef enum {
+       G_FILE_TEST_IS_REGULAR = 1 << 0,
+       G_FILE_TEST_IS_SYMLINK = 1 << 1,
+       G_FILE_TEST_IS_DIR = 1 << 2,
+       G_FILE_TEST_IS_EXECUTABLE = 1 << 3,
+       G_FILE_TEST_EXISTS = 1 << 4
+} GFileTest;
+
+
 gboolean   g_file_get_contents (const gchar *filename, gchar **contents, gsize *length, GError **error);
 GFileError g_file_error_from_errno (gint err_no);
 gint       g_file_open_tmp (const gchar *tmpl, gchar **name_used, GError **error);
+gboolean   g_file_test (const gchar *filename, GFileTest test);
+
+
 #endif
 
index a7973ea55ac1d165815e8b9b69945144b638fcae..bbf15f454816f0ac18d9145628022bf1dd0f3d26 100644 (file)
@@ -89,9 +89,113 @@ test_open_tmp ()
        return OK;
 }
 
+RESULT
+test_file ()
+{
+       gboolean res;
+       const gchar *tmp;
+       gchar *path, *sympath;
+
+       res = g_file_test (NULL, 0);
+       if (res)
+               return FAILED ("Should return FALSE HERE");
+
+       res = g_file_test ("file.c", 0);
+       if (res)
+               return FAILED ("Should return FALSE HERE");
+
+       tmp = g_get_tmp_dir ();
+       res = g_file_test (tmp, G_FILE_TEST_EXISTS);
+       if (!res)
+               return FAILED ("tmp does not exist.");
+       res = g_file_test (tmp, G_FILE_TEST_IS_REGULAR);
+       if (res)
+               return FAILED ("tmp is regular");
+
+       res = g_file_test (tmp, G_FILE_TEST_IS_DIR);
+       if (!res)
+               return FAILED ("tmp is not a directory");
+       res = g_file_test (tmp, G_FILE_TEST_IS_EXECUTABLE);
+       if (!res)
+               return FAILED ("tmp is not a executable");
+
+       res = g_file_test (tmp, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_SYMLINK);
+       if (!res)
+               return FAILED ("2 tmp does not exist.");
+       res = g_file_test (tmp, G_FILE_TEST_IS_REGULAR | G_FILE_TEST_IS_SYMLINK);
+       if (res)
+               return FAILED ("2 tmp is regular");
+
+       res = g_file_test (tmp, G_FILE_TEST_IS_DIR | G_FILE_TEST_IS_SYMLINK);
+       if (!res)
+               return FAILED ("2 tmp is not a directory");
+       res = g_file_test (tmp, G_FILE_TEST_IS_EXECUTABLE | G_FILE_TEST_IS_SYMLINK);
+       if (!res)
+               return FAILED ("2 tmp is not a executable");
+
+       close (g_file_open_tmp (NULL, &path, NULL)); /* create an empty file */
+       res = g_file_test (path, G_FILE_TEST_EXISTS);
+       if (!res)
+               return FAILED ("3 %s should exist", path);
+       res = g_file_test (path, G_FILE_TEST_IS_REGULAR);
+       /* This is strange. Empty file is reported as not existing! */
+       if (!res)
+               return FAILED ("3 %s IS_REGULAR", path);
+       res = g_file_test (path, G_FILE_TEST_IS_DIR);
+       if (res)
+               return FAILED ("3 %s should not be a directory", path);
+       res = g_file_test (path, G_FILE_TEST_IS_EXECUTABLE);
+       if (res)
+               return FAILED ("3 %s should not be executable", path);
+       res = g_file_test (path, G_FILE_TEST_IS_SYMLINK);
+       if (res)
+               return FAILED ("3 %s should not be a symlink", path);
+
+       sympath = g_strconcat (path, "-link", NULL);
+       symlink (path, sympath);
+       res = g_file_test (sympath, G_FILE_TEST_EXISTS);
+       if (!res)
+               return FAILED ("4 %s should not exist", sympath);
+       res = g_file_test (sympath, G_FILE_TEST_IS_REGULAR);
+       if (!res)
+               return FAILED ("4 %s should not be a regular file", sympath);
+       res = g_file_test (sympath, G_FILE_TEST_IS_DIR);
+       if (res)
+               return FAILED ("4 %s should not be a directory", sympath);
+       res = g_file_test (sympath, G_FILE_TEST_IS_EXECUTABLE);
+       if (res)
+               return FAILED ("4 %s should not be executable", sympath);
+       res = g_file_test (sympath, G_FILE_TEST_IS_SYMLINK);
+       if (!res)
+               return FAILED ("4 %s should be a symlink", sympath);
+
+       unlink (path);
+
+       res = g_file_test (sympath, G_FILE_TEST_EXISTS);
+       if (res)
+               return FAILED ("5 %s should exist", sympath);
+       res = g_file_test (sympath, G_FILE_TEST_IS_REGULAR);
+       if (res)
+               return FAILED ("5 %s should be a regular file", sympath);
+       res = g_file_test (sympath, G_FILE_TEST_IS_DIR);
+       if (res)
+               return FAILED ("5 %s should not be a directory", sympath);
+       res = g_file_test (sympath, G_FILE_TEST_IS_EXECUTABLE);
+       if (res)
+               return FAILED ("5 %s should not be executable", sympath);
+       res = g_file_test (sympath, G_FILE_TEST_IS_SYMLINK);
+       if (!res)
+               return FAILED ("5 %s should be a symlink", sympath);
+       unlink (sympath);
+       g_free (path);
+       g_free (sympath);
+       return OK;
+}
+
 static Test file_tests [] = {
-       {"g_file_test_contents", test_file_get_contents},
+       {"g_file_get_contents", test_file_get_contents},
        {"g_file_open_tmp", test_open_tmp},
+       {"g_file_test", test_file},
        {NULL, NULL}
 };