* mu-util.[ch]: use realpath to resolve dirs, some cosmetics

This commit is contained in:
Dirk-Jan C. Binnema
2010-09-23 00:03:04 +03:00
parent 3375066c4f
commit 99c0d620d4
2 changed files with 34 additions and 15 deletions

View File

@ -1,5 +1,5 @@
/* /*
** Copyright (C) 2010 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl> ** Copyright (C) 2008-2010 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
** **
** This program is free software; you can redistribute it and/or modify ** 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 ** 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 <wordexp.h> /* for shell-style globbing */ #include <wordexp.h> /* for shell-style globbing */
#include <stdlib.h> #include <stdlib.h>
/* hopefully, the should get us a sane PATH_MAX */
#include <limits.h>
/* not all systems provide PATH_MAX in limits.h */
#ifndef PATH_MAX
#include <sys/param.h>
#ifndef PATH_MAX
#define PATH_MAX MAXPATHLEN
#endif /*!PATH_MAX*/
#endif /*PATH_MAX*/
#include <string.h> #include <string.h>
#include <locale.h> /* for setlocale() */ #include <locale.h> /* for setlocale() */
@ -40,16 +50,16 @@ mu_util_dir_expand (const char *path)
{ {
wordexp_t wexp; wordexp_t wexp;
char *dir; char *dir;
char resolved[PATH_MAX + 1];
int rv; int rv;
g_return_val_if_fail (path, NULL); g_return_val_if_fail (path, NULL);
dir = NULL; dir = NULL;
rv = wordexp (path, &wexp, 0); rv = wordexp (path, &wexp, 0);
if (rv != 0) { if (rv != 0) {
g_debug ("%s: expansion failed for '%s' [%d]", g_debug ("%s: expansion failed for '%s' [%d]",
__FUNCTION__, path, rv); __FUNCTION__, path, rv);
return NULL; return NULL;
} else if (wexp.we_wordc != 1) { } else if (wexp.we_wordc != 1) {
g_debug ("%s: expansion ambiguous for '%s'", g_debug ("%s: expansion ambiguous for '%s'",
@ -64,8 +74,17 @@ mu_util_dir_expand (const char *path)
#ifndef __APPLE__ #ifndef __APPLE__
wordfree (&wexp); wordfree (&wexp);
#endif /*__APPLE__*/ #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 gboolean

View File

@ -24,7 +24,7 @@
G_BEGIN_DECLS G_BEGIN_DECLS
/** /**
* do system-specific initialization. should be called before anything * do system-specific initialization. should be called before anything
* else. Initializes the locale and Gtype * else. Initializes the locale and Gtype
* *
@ -32,7 +32,7 @@ G_BEGIN_DECLS
*/ */
gboolean mu_util_init_system (void); gboolean mu_util_init_system (void);
/** /**
* get the expanded path; ie. perform shell expansion on the path * get the expanded path; ie. perform shell expansion on the path
* *
* @param path path to expand * @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; char* mu_util_dir_expand (const char* path) G_GNUC_WARN_UNUSED_RESULT;
/** /**
* guess the maildir; first try $MAILDIR; if it is unset or * guess the maildir; first try $MAILDIR; if it is unset or
* non-existant, try ~/Maildir if both fail, return NULL * 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 * if path exists, check that's a read/writeable dir; otherwise try to
* create it (with perms 0700) * 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; 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 * check whether path is a directory, and optionally, if it's readable
* and/or writeable * 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 * create a writeable file and return its file descriptor (which
* you'll need to close(2) when done with it.) * 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); gboolean overwrite);
/** /**
* convert a string array in to a string, with the elements separated * convert a string array in to a string, with the elements separated
* by ' ' * 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; gchar* mu_util_str_from_strv (const gchar **params) G_GNUC_WARN_UNUSED_RESULT;
/** /**
* *
* don't repeat these catch blocks everywhere... * 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_DIR_NAME "xapian"
#define MU_XAPIAN_VERSION_KEY "db_version" #define MU_XAPIAN_VERSION_KEY "db_version"
/** /**
* log something in the log file; note, we use G_LOG_LEVEL_INFO * log something in the log file; note, we use G_LOG_LEVEL_INFO
* for such messages * for such messages
*/ */