From 99c0d620d4bda9ecd23a02e9d5b98b4083b433a2 Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Thu, 23 Sep 2010 00:03:04 +0300 Subject: [PATCH] * mu-util.[ch]: use realpath to resolve dirs, some cosmetics --- src/mu-util.c | 31 +++++++++++++++++++++++++------ src/mu-util.h | 18 +++++++++--------- 2 files changed, 34 insertions(+), 15 deletions(-) diff --git a/src/mu-util.c b/src/mu-util.c index 214f477e..8ef41194 100644 --- a/src/mu-util.c +++ b/src/mu-util.c @@ -1,5 +1,5 @@ /* -** Copyright (C) 2010 Dirk-Jan C. Binnema +** Copyright (C) 2008-2010 Dirk-Jan C. Binnema ** ** This program is free software; you can redistribute it and/or modify ** it under the terms of the GNU General Public License as published by @@ -17,10 +17,20 @@ ** */ -#define _XOPEN_SOURCE +#define _XOPEN_SOURCE 500 #include /* for shell-style globbing */ - #include + +/* hopefully, the should get us a sane PATH_MAX */ +#include +/* not all systems provide PATH_MAX in limits.h */ +#ifndef PATH_MAX +#include +#ifndef PATH_MAX +#define PATH_MAX MAXPATHLEN +#endif /*!PATH_MAX*/ +#endif /*PATH_MAX*/ + #include #include /* for setlocale() */ @@ -40,16 +50,16 @@ mu_util_dir_expand (const char *path) { wordexp_t wexp; char *dir; + char resolved[PATH_MAX + 1]; int rv; g_return_val_if_fail (path, NULL); dir = NULL; - rv = wordexp (path, &wexp, 0); if (rv != 0) { g_debug ("%s: expansion failed for '%s' [%d]", - __FUNCTION__, path, rv); + __FUNCTION__, path, rv); return NULL; } else if (wexp.we_wordc != 1) { g_debug ("%s: expansion ambiguous for '%s'", @@ -64,8 +74,17 @@ mu_util_dir_expand (const char *path) #ifndef __APPLE__ wordfree (&wexp); #endif /*__APPLE__*/ + + /* now, resolve any symlinks, .. etc. */ + if (!realpath (dir, resolved)) { + g_debug ("%s: good not get realpath for '%s': %s", + __FUNCTION__, dir, strerror(errno)); + g_free (dir); + return NULL; + } else + g_free (dir); - return dir; + return g_strdup(resolved); } gboolean diff --git a/src/mu-util.h b/src/mu-util.h index 7fbf9ca4..6be883c4 100644 --- a/src/mu-util.h +++ b/src/mu-util.h @@ -24,7 +24,7 @@ G_BEGIN_DECLS -/** +/** * do system-specific initialization. should be called before anything * else. Initializes the locale and Gtype * @@ -32,7 +32,7 @@ G_BEGIN_DECLS */ gboolean mu_util_init_system (void); -/** +/** * get the expanded path; ie. perform shell expansion on the path * * @param path path to expand @@ -42,7 +42,7 @@ gboolean mu_util_init_system (void); */ char* mu_util_dir_expand (const char* path) G_GNUC_WARN_UNUSED_RESULT; -/** +/** * guess the maildir; first try $MAILDIR; if it is unset or * non-existant, try ~/Maildir if both fail, return NULL * @@ -52,7 +52,7 @@ char* mu_util_guess_maildir (void) G_GNUC_WARN_UNUSED_RESULT; -/** +/** * if path exists, check that's a read/writeable dir; otherwise try to * create it (with perms 0700) * @@ -64,7 +64,7 @@ char* mu_util_guess_maildir (void) G_GNUC_WARN_UNUSED_RESULT; gboolean mu_util_create_dir_maybe (const gchar *path) G_GNUC_WARN_UNUSED_RESULT; -/** +/** * check whether path is a directory, and optionally, if it's readable * and/or writeable * @@ -79,7 +79,7 @@ gboolean mu_util_check_dir (const gchar* path, gboolean readable, -/** +/** * create a writeable file and return its file descriptor (which * you'll need to close(2) when done with it.) * @@ -94,7 +94,7 @@ int mu_util_create_writeable_fd (const char* filename, const char* dir, gboolean overwrite); -/** +/** * convert a string array in to a string, with the elements separated * by ' ' * @@ -104,7 +104,7 @@ int mu_util_create_writeable_fd (const char* filename, const char* dir, */ gchar* mu_util_str_from_strv (const gchar **params) G_GNUC_WARN_UNUSED_RESULT; -/** +/** * * don't repeat these catch blocks everywhere... * @@ -133,7 +133,7 @@ gchar* mu_util_str_from_strv (const gchar **params) G_GNUC_WARN_UNUSED_RESULT; #define MU_XAPIAN_DIR_NAME "xapian" #define MU_XAPIAN_VERSION_KEY "db_version" -/** +/** * log something in the log file; note, we use G_LOG_LEVEL_INFO * for such messages */