2011-07-23 16:56:25 +02:00
|
|
|
/* -*- mode: c; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
|
2011-01-02 17:10:11 +01:00
|
|
|
**
|
2013-03-30 10:32:07 +01:00
|
|
|
** Copyright (C) 2010-2013 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
2010-11-14 12:55:04 +01:00
|
|
|
**
|
|
|
|
** 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 the
|
|
|
|
** Free Software Foundation; either version 3, or (at your option) any
|
|
|
|
** later version.
|
|
|
|
**
|
|
|
|
** This program is distributed in the hope that it will be useful,
|
|
|
|
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
** GNU General Public License for more details.
|
|
|
|
**
|
|
|
|
** You should have received a copy of the GNU General Public License
|
|
|
|
** along with this program; if not, write to the Free Software Foundation,
|
|
|
|
** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "mu-runtime.h"
|
|
|
|
|
|
|
|
#include <glib-object.h>
|
|
|
|
#include <locale.h> /* for setlocale() */
|
|
|
|
#include <stdio.h> /* for fileno() */
|
2010-11-27 15:06:55 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
2012-09-17 12:05:49 +02:00
|
|
|
|
2012-05-20 16:41:18 +02:00
|
|
|
#include "mu-msg.h"
|
2010-11-14 12:55:04 +01:00
|
|
|
#include "mu-log.h"
|
|
|
|
#include "mu-util.h"
|
2012-03-13 22:06:17 +01:00
|
|
|
|
2011-03-01 23:13:24 +01:00
|
|
|
#define MU_XAPIAN_DIRNAME "xapian"
|
|
|
|
#define MU_BOOKMARKS_FILENAME "bookmarks"
|
|
|
|
#define MU_CACHE_DIRNAME "cache"
|
|
|
|
#define MU_CONTACTS_FILENAME "contacts"
|
2011-03-19 09:07:32 +01:00
|
|
|
#define MU_LOG_DIRNAME "log"
|
|
|
|
|
2010-11-14 12:55:04 +01:00
|
|
|
|
|
|
|
struct _MuRuntimeData {
|
2011-03-19 09:07:32 +01:00
|
|
|
gchar *_str[MU_RUNTIME_PATH_NUM];
|
2011-07-23 16:56:25 +02:00
|
|
|
gchar *_name; /* e.g., 'mu', 'mug' */
|
2010-11-14 12:55:04 +01:00
|
|
|
};
|
2010-12-11 12:53:20 +01:00
|
|
|
typedef struct _MuRuntimeData MuRuntimeData;
|
2010-11-14 12:55:04 +01:00
|
|
|
|
|
|
|
/* static, global data for this singleton */
|
2010-12-11 12:53:20 +01:00
|
|
|
static gboolean _initialized = FALSE;
|
|
|
|
static MuRuntimeData *_data = NULL;
|
2010-11-14 12:55:04 +01:00
|
|
|
|
2011-07-23 16:56:25 +02:00
|
|
|
static void runtime_free (void);
|
|
|
|
static gboolean init_paths (const char* muhome, MuRuntimeData *data);
|
|
|
|
static const char* runtime_path (MuRuntimePath path);
|
|
|
|
|
2010-11-14 12:55:04 +01:00
|
|
|
|
2011-07-23 16:56:25 +02:00
|
|
|
static gboolean
|
2012-09-15 16:59:21 +02:00
|
|
|
init_log (const char *muhome, const char *name, MuLogOptions opts)
|
2011-07-23 16:56:25 +02:00
|
|
|
{
|
|
|
|
gboolean rv;
|
|
|
|
char *logpath;
|
2012-03-13 22:06:17 +01:00
|
|
|
|
2011-07-23 16:56:25 +02:00
|
|
|
logpath = g_strdup_printf ("%s%c%s%c%s.log",
|
|
|
|
muhome, G_DIR_SEPARATOR,
|
|
|
|
MU_LOG_DIRNAME, G_DIR_SEPARATOR,
|
|
|
|
name);
|
2012-09-15 16:59:21 +02:00
|
|
|
|
|
|
|
rv = mu_log_init (logpath, opts);
|
2011-07-23 16:56:25 +02:00
|
|
|
g_free (logpath);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-11-14 12:55:04 +01:00
|
|
|
gboolean
|
2011-07-23 16:56:25 +02:00
|
|
|
mu_runtime_init (const char* muhome_arg, const char *name)
|
2010-11-14 12:55:04 +01:00
|
|
|
{
|
|
|
|
gchar *muhome;
|
|
|
|
|
|
|
|
g_return_val_if_fail (!_initialized, FALSE);
|
2011-07-23 16:56:25 +02:00
|
|
|
g_return_val_if_fail (name, FALSE);
|
2012-03-13 22:06:17 +01:00
|
|
|
|
2012-03-19 19:47:38 +01:00
|
|
|
setlocale (LC_ALL, "");
|
2013-05-13 21:30:27 +02:00
|
|
|
|
|
|
|
#ifndef GLIB_VERSION_2_36
|
2012-03-19 19:47:38 +01:00
|
|
|
g_type_init ();
|
2013-05-13 21:30:27 +02:00
|
|
|
#endif /*GLIB_VERSION_2_36*/
|
2012-03-13 22:06:17 +01:00
|
|
|
|
2010-11-14 12:55:04 +01:00
|
|
|
if (muhome_arg)
|
|
|
|
muhome = g_strdup (muhome_arg);
|
|
|
|
else
|
|
|
|
muhome = mu_util_guess_mu_homedir ();
|
|
|
|
|
2011-08-09 06:54:23 +02:00
|
|
|
if (!mu_util_create_dir_maybe (muhome, 0700, TRUE)) {
|
|
|
|
g_printerr ("mu: invalid mu homedir specified;"
|
|
|
|
" use --muhome=<dir>\n");
|
2011-01-02 17:10:11 +01:00
|
|
|
runtime_free ();
|
|
|
|
return FALSE;
|
|
|
|
}
|
2012-03-13 22:06:17 +01:00
|
|
|
|
2010-11-14 12:55:04 +01:00
|
|
|
_data = g_new0 (MuRuntimeData, 1);
|
2012-09-17 12:05:49 +02:00
|
|
|
_data->_str[MU_RUNTIME_PATH_MUHOME] = muhome;
|
|
|
|
|
2011-03-01 23:13:24 +01:00
|
|
|
init_paths (muhome, _data);
|
2011-07-23 16:56:25 +02:00
|
|
|
_data->_name = g_strdup (name);
|
2011-05-09 01:58:33 +02:00
|
|
|
|
2012-09-15 16:59:21 +02:00
|
|
|
if (!init_log (muhome, name, MU_LOG_OPTIONS_BACKUP)) {
|
2011-07-23 16:56:25 +02:00
|
|
|
runtime_free ();
|
|
|
|
g_free (muhome);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2012-03-13 22:06:17 +01:00
|
|
|
|
2010-11-14 12:55:04 +01:00
|
|
|
return _initialized = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
runtime_free (void)
|
|
|
|
{
|
2011-03-01 23:13:24 +01:00
|
|
|
int i;
|
|
|
|
|
2012-09-17 12:05:49 +02:00
|
|
|
mu_log_uninit();
|
2012-03-13 22:06:17 +01:00
|
|
|
|
2012-09-17 12:05:49 +02:00
|
|
|
if (!_data)
|
|
|
|
return;
|
2010-11-14 12:55:04 +01:00
|
|
|
|
2012-09-17 12:05:49 +02:00
|
|
|
if (_data->_str)
|
|
|
|
for (i = 0; i != MU_RUNTIME_PATH_NUM; ++i)
|
|
|
|
g_free (_data->_str[i]);
|
2012-03-13 22:06:17 +01:00
|
|
|
|
2012-09-17 12:05:49 +02:00
|
|
|
g_free (_data->_name);
|
2012-03-13 22:06:17 +01:00
|
|
|
g_free (_data);
|
2010-11-14 12:55:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
mu_runtime_uninit (void)
|
|
|
|
{
|
2012-10-24 22:49:54 +02:00
|
|
|
if (!_initialized)
|
|
|
|
return;
|
2010-11-14 12:55:04 +01:00
|
|
|
|
2010-11-27 15:06:55 +01:00
|
|
|
runtime_free ();
|
|
|
|
|
2010-11-14 12:55:04 +01:00
|
|
|
_initialized = FALSE;
|
|
|
|
}
|
2012-03-13 22:06:17 +01:00
|
|
|
|
2010-11-14 12:55:04 +01:00
|
|
|
|
2011-03-19 09:07:32 +01:00
|
|
|
static gboolean
|
|
|
|
create_dirs_maybe (MuRuntimeData *data)
|
2010-11-14 12:55:04 +01:00
|
|
|
{
|
2011-03-19 09:07:32 +01:00
|
|
|
if (!mu_util_create_dir_maybe
|
2011-08-09 06:54:23 +02:00
|
|
|
(data->_str[MU_RUNTIME_PATH_CACHE], 0700, TRUE)) {
|
2011-03-19 09:07:32 +01:00
|
|
|
g_warning ("failed to create cache dir");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mu_util_create_dir_maybe
|
2011-08-09 06:54:23 +02:00
|
|
|
(data->_str[MU_RUNTIME_PATH_LOG], 0700, TRUE)) {
|
2011-03-19 09:07:32 +01:00
|
|
|
g_warning ("failed to create log dir");
|
|
|
|
return FALSE;
|
|
|
|
}
|
2012-03-13 22:06:17 +01:00
|
|
|
|
2011-03-19 09:07:32 +01:00
|
|
|
return TRUE;
|
2010-11-14 12:55:04 +01:00
|
|
|
}
|
|
|
|
|
2011-03-01 23:13:24 +01:00
|
|
|
|
2010-11-14 12:55:04 +01:00
|
|
|
|
2011-03-19 09:07:32 +01:00
|
|
|
static gboolean
|
|
|
|
init_paths (const char* muhome, MuRuntimeData *data)
|
2010-11-14 12:55:04 +01:00
|
|
|
{
|
2011-03-19 09:07:32 +01:00
|
|
|
data->_str [MU_RUNTIME_PATH_XAPIANDB] =
|
|
|
|
g_strdup_printf ("%s%c%s", muhome, G_DIR_SEPARATOR,
|
|
|
|
MU_XAPIAN_DIRNAME);
|
2012-03-13 22:06:17 +01:00
|
|
|
|
2011-03-19 09:07:32 +01:00
|
|
|
data->_str [MU_RUNTIME_PATH_BOOKMARKS] =
|
2011-04-24 15:03:40 +02:00
|
|
|
g_strdup_printf ("%s%c%s", muhome, G_DIR_SEPARATOR,
|
2011-03-19 09:07:32 +01:00
|
|
|
MU_BOOKMARKS_FILENAME);
|
|
|
|
|
|
|
|
data->_str [MU_RUNTIME_PATH_CACHE] =
|
|
|
|
g_strdup_printf ("%s%c%s", muhome, G_DIR_SEPARATOR,
|
|
|
|
MU_CACHE_DIRNAME);
|
2012-03-13 22:06:17 +01:00
|
|
|
|
2011-03-19 09:07:32 +01:00
|
|
|
data->_str [MU_RUNTIME_PATH_CONTACTS] =
|
|
|
|
g_strdup_printf ("%s%c%s", data->_str[MU_RUNTIME_PATH_CACHE],
|
|
|
|
G_DIR_SEPARATOR, MU_CONTACTS_FILENAME);
|
|
|
|
|
|
|
|
data->_str [MU_RUNTIME_PATH_LOG] =
|
2012-03-13 22:06:17 +01:00
|
|
|
g_strdup_printf ("%s%c%s", muhome,
|
2011-07-23 16:56:25 +02:00
|
|
|
G_DIR_SEPARATOR, MU_LOG_DIRNAME);
|
2012-03-13 22:06:17 +01:00
|
|
|
|
2011-03-19 09:07:32 +01:00
|
|
|
if (!create_dirs_maybe (data))
|
|
|
|
return FALSE;
|
2012-03-13 22:06:17 +01:00
|
|
|
|
2011-03-19 09:07:32 +01:00
|
|
|
return TRUE;
|
2011-03-01 23:13:24 +01:00
|
|
|
}
|
2010-11-14 12:55:04 +01:00
|
|
|
|
2011-07-23 16:56:25 +02:00
|
|
|
/* so we can called when _initialized is FALSE still */
|
|
|
|
static const char*
|
|
|
|
runtime_path (MuRuntimePath path)
|
2012-03-13 22:06:17 +01:00
|
|
|
{
|
2011-07-23 16:56:25 +02:00
|
|
|
return _data->_str[path];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-01 23:13:24 +01:00
|
|
|
|
|
|
|
const char*
|
2011-03-19 09:07:32 +01:00
|
|
|
mu_runtime_path (MuRuntimePath path)
|
2011-03-01 23:13:24 +01:00
|
|
|
{
|
|
|
|
g_return_val_if_fail (_initialized, NULL);
|
2011-03-19 09:07:32 +01:00
|
|
|
g_return_val_if_fail (path < MU_RUNTIME_PATH_NUM, NULL);
|
2012-03-13 22:06:17 +01:00
|
|
|
|
2011-07-23 16:56:25 +02:00
|
|
|
return runtime_path (path);
|
2010-11-14 12:55:04 +01:00
|
|
|
}
|