Commit 1c09f3df authored by Carlos Garnacho's avatar Carlos Garnacho

libtracker-common: Make locale readonly

There's no reasonable situation why we should change this from
a library. Initialization (and shutdown) was also mostly useless
as it would just set the same settings that it just retrieved.
It's been turned into a sanity check for the relevant places.
parent 90bd9b57
......@@ -69,7 +69,7 @@ namespace Tracker {
[CCode (cheader_filename = "libtracker-common/tracker-locale.h")]
namespace Locale {
public void init ();
public void sanity_check ();
}
[Compact]
......
......@@ -26,146 +26,78 @@
#include "tracker-locale.h"
/* Current locales in use. They will be stored in heap and available throughout
* the whole program execution, so will be reported as still reachable by Valgrind.
*/
static gchar *current_locales[TRACKER_LOCALE_LAST];
static const gchar *locale_names[TRACKER_LOCALE_LAST] = {
"TRACKER_LOCALE_LANGUAGE",
"TRACKER_LOCALE_TIME",
"TRACKER_LOCALE_COLLATE",
"TRACKER_LOCALE_NUMERIC",
"TRACKER_LOCALE_MONETARY"
"LANG",
"LC_TIME",
"LC_COLLATE",
"LC_NUMERIC",
"LC_MONETARY"
};
/* Already initialized? */
static gboolean initialized;
static GRecMutex locales_mutex;
const gchar*
tracker_locale_get_name (guint i)
static const gchar *
tracker_locale_get_unlocked (TrackerLocaleID id)
{
g_return_val_if_fail (i < TRACKER_LOCALE_LAST, NULL);
return locale_names[i];
}
const gchar *env_locale = NULL;
void
tracker_locale_set (TrackerLocaleID id,
const gchar *value)
{
g_rec_mutex_lock (&locales_mutex);
if (current_locales[id]) {
g_debug ("Locale '%s' was changed from '%s' to '%s'",
locale_names[id],
current_locales[id],
value);
g_free (current_locales[id]);
} else {
g_debug ("Locale '%s' was set to '%s'",
locale_names[id],
value);
}
/* Store the new one */
current_locales[id] = g_strdup (value);
/* And also set the new one in the corresponding envvar */
switch (id) {
case TRACKER_LOCALE_LANGUAGE:
g_setenv ("LANG", value, TRUE);
env_locale = g_getenv ("LANG");
break;
case TRACKER_LOCALE_TIME:
setlocale (LC_TIME, value);
env_locale = setlocale (LC_TIME, NULL);
break;
case TRACKER_LOCALE_COLLATE:
setlocale (LC_COLLATE, value);
env_locale = setlocale (LC_COLLATE, NULL);
break;
case TRACKER_LOCALE_NUMERIC:
setlocale (LC_NUMERIC, value);
env_locale = setlocale (LC_NUMERIC, NULL);
break;
case TRACKER_LOCALE_MONETARY:
setlocale (LC_MONETARY, value);
env_locale = setlocale (LC_MONETARY, NULL);
break;
case TRACKER_LOCALE_LAST:
/* Make compiler happy */
g_warn_if_reached ();
default:
g_assert_not_reached ();
break;
}
g_rec_mutex_unlock (&locales_mutex);
return env_locale;
}
void
tracker_locale_init (void)
tracker_locale_sanity_check (void)
{
guint i;
for (i = 0; i < TRACKER_LOCALE_LAST; i++) {
if (!current_locales[i]) {
const gchar *env_locale = NULL;
switch (i) {
case TRACKER_LOCALE_LANGUAGE:
env_locale = g_getenv ("LANG");
break;
case TRACKER_LOCALE_TIME:
env_locale = setlocale (LC_TIME, NULL);
break;
case TRACKER_LOCALE_COLLATE:
env_locale = setlocale (LC_COLLATE, NULL);
break;
case TRACKER_LOCALE_NUMERIC:
env_locale = setlocale (LC_NUMERIC, NULL);
break;
case TRACKER_LOCALE_MONETARY:
env_locale = setlocale (LC_MONETARY, NULL);
break;
default:
g_assert_not_reached ();
break;
}
if (!env_locale) {
g_warning ("Locale '%d' is not set, defaulting to C locale", i);
tracker_locale_set (i, "C");
} else {
tracker_locale_set (i, env_locale);
}
}
}
g_rec_mutex_lock (&locales_mutex);
/* So we're initialized */
initialized = TRUE;
}
for (i = 0; i < TRACKER_LOCALE_LAST; i++) {
const gchar *env_locale = NULL;
void
tracker_locale_shutdown (void)
{
gint i;
env_locale = tracker_locale_get_unlocked (i);
for (i = 0; i < TRACKER_LOCALE_LAST; i++) {
g_free (current_locales[i]);
current_locales[i] = NULL;
if (!env_locale) {
g_warning ("Locale '%s' is not set, defaulting to C locale", locale_names[i]);
}
}
initialized = FALSE;
g_rec_mutex_unlock (&locales_mutex);
}
gchar *
tracker_locale_get (TrackerLocaleID id)
{
const gchar *env_locale = NULL;
gchar *locale;
g_return_val_if_fail (initialized, NULL);
g_rec_mutex_lock (&locales_mutex);
env_locale = tracker_locale_get_unlocked (id);
/* Always return a duplicated string, as the locale may change at any
* moment */
locale = g_strdup (current_locales[id]);
locale = g_strdup (env_locale);
g_rec_mutex_unlock (&locales_mutex);
......
......@@ -38,19 +38,13 @@ typedef enum {
TRACKER_LOCALE_LAST
} TrackerLocaleID;
void tracker_locale_init (void);
void tracker_locale_shutdown (void);
void tracker_locale_sanity_check (void);
/* Get the current locale of the given type.
* Note that it returns a newly-allocated string which should be g_free()-ed
*/
gchar *tracker_locale_get (TrackerLocaleID id);
const gchar* tracker_locale_get_name (guint i);
void tracker_locale_set (TrackerLocaleID id,
const gchar *value);
G_END_DECLS
#endif /* __LIBTRACKER_COMMON_LOCALE_H__ */
......@@ -3794,7 +3794,7 @@ tracker_data_manager_init (TrackerDBManagerFlags flags,
tracker_ontologies_init ();
if (!reloading) {
tracker_locale_init ();
tracker_locale_sanity_check ();
}
#ifndef DISABLE_JOURNAL
......@@ -3816,9 +3816,6 @@ tracker_data_manager_init (TrackerDBManagerFlags flags,
g_propagate_error (error, internal_error);
tracker_ontologies_shutdown ();
if (!reloading) {
tracker_locale_shutdown ();
}
tracker_data_update_shutdown ();
return FALSE;
......@@ -3863,9 +3860,6 @@ tracker_data_manager_init (TrackerDBManagerFlags flags,
tracker_db_manager_shutdown ();
tracker_ontologies_shutdown ();
if (!reloading) {
tracker_locale_shutdown ();
}
tracker_data_update_shutdown ();
return FALSE;
......@@ -3892,9 +3886,6 @@ tracker_data_manager_init (TrackerDBManagerFlags flags,
g_free (uri);
tracker_db_manager_shutdown ();
tracker_ontologies_shutdown ();
if (!reloading) {
tracker_locale_shutdown ();
}
tracker_data_update_shutdown ();
return FALSE;
}
......@@ -3919,9 +3910,6 @@ tracker_data_manager_init (TrackerDBManagerFlags flags,
tracker_db_manager_shutdown ();
tracker_ontologies_shutdown ();
if (!reloading) {
tracker_locale_shutdown ();
}
tracker_data_update_shutdown ();
return FALSE;
......@@ -3955,9 +3943,6 @@ tracker_data_manager_init (TrackerDBManagerFlags flags,
tracker_db_manager_shutdown ();
tracker_ontologies_shutdown ();
if (!reloading) {
tracker_locale_shutdown ();
}
tracker_data_update_shutdown ();
return FALSE;
......@@ -3998,9 +3983,6 @@ tracker_data_manager_init (TrackerDBManagerFlags flags,
#endif /* DISABLE_JOURNAL */
tracker_db_manager_shutdown ();
tracker_ontologies_shutdown ();
if (!reloading) {
tracker_locale_shutdown ();
}
tracker_data_update_shutdown ();
return FALSE;
......@@ -4020,9 +4002,6 @@ tracker_data_manager_init (TrackerDBManagerFlags flags,
#endif /* DISABLE_JOURNAL */
tracker_db_manager_shutdown ();
tracker_ontologies_shutdown ();
if (!reloading) {
tracker_locale_shutdown ();
}
tracker_data_update_shutdown ();
return FALSE;
......@@ -4047,9 +4026,6 @@ tracker_data_manager_init (TrackerDBManagerFlags flags,
ontology_writer = NULL;
tracker_db_manager_shutdown ();
tracker_ontologies_shutdown ();
if (!reloading) {
tracker_locale_shutdown ();
}
tracker_data_update_shutdown ();
return FALSE;
......@@ -4073,9 +4049,6 @@ tracker_data_manager_init (TrackerDBManagerFlags flags,
tracker_db_journal_free (ontology_writer, NULL);
ontology_writer = NULL;
#endif /* DISABLE_JOURNAL */
if (!reloading) {
tracker_locale_shutdown ();
}
tracker_data_update_shutdown ();
return FALSE;
......@@ -4104,9 +4077,6 @@ tracker_data_manager_init (TrackerDBManagerFlags flags,
tracker_db_manager_shutdown ();
tracker_ontologies_shutdown ();
if (!reloading) {
tracker_locale_shutdown ();
}
tracker_data_update_shutdown ();
return FALSE;
......@@ -4265,9 +4235,6 @@ tracker_data_manager_init (TrackerDBManagerFlags flags,
#endif /* DISABLE_JOURNAL */
tracker_db_manager_shutdown ();
tracker_ontologies_shutdown ();
if (!reloading) {
tracker_locale_shutdown ();
}
tracker_data_update_shutdown ();
return FALSE;
......@@ -4315,7 +4282,6 @@ tracker_data_manager_init (TrackerDBManagerFlags flags,
}
initialized = TRUE;
/* This also does tracker_locale_shutdown */
tracker_data_manager_shutdown ();
return tracker_data_manager_init (flags | TRACKER_DB_MANAGER_DO_NOT_CHECK_ONTOLOGY,
......@@ -4359,9 +4325,6 @@ tracker_data_manager_init (TrackerDBManagerFlags flags,
#endif /* DISABLE_JOURNAL */
tracker_db_manager_shutdown ();
tracker_ontologies_shutdown ();
if (!reloading) {
tracker_locale_shutdown ();
}
tracker_data_update_shutdown ();
return FALSE;
......@@ -4409,7 +4372,6 @@ tracker_data_manager_init (TrackerDBManagerFlags flags,
}
initialized = TRUE;
/* This also does tracker_locale_shutdown */
tracker_data_manager_shutdown ();
return tracker_data_manager_init (flags | TRACKER_DB_MANAGER_DO_NOT_CHECK_ONTOLOGY,
......@@ -4517,7 +4479,6 @@ tracker_data_manager_init (TrackerDBManagerFlags flags,
}
initialized = TRUE;
/* This also does tracker_locale_shutdown */
tracker_data_manager_shutdown ();
return tracker_data_manager_init (flags | TRACKER_DB_MANAGER_DO_NOT_CHECK_ONTOLOGY,
......@@ -4545,9 +4506,6 @@ tracker_data_manager_init (TrackerDBManagerFlags flags,
#endif /* DISABLE_JOURNAL */
tracker_db_manager_shutdown ();
tracker_ontologies_shutdown ();
if (!reloading) {
tracker_locale_shutdown ();
}
tracker_data_update_shutdown ();
return FALSE;
......@@ -4582,9 +4540,6 @@ tracker_data_manager_init (TrackerDBManagerFlags flags,
#endif /* DISABLE_JOURNAL */
tracker_db_manager_shutdown ();
tracker_ontologies_shutdown ();
if (!reloading) {
tracker_locale_shutdown ();
}
tracker_data_update_shutdown ();
return FALSE;
......@@ -4633,9 +4588,6 @@ tracker_data_manager_init (TrackerDBManagerFlags flags,
tracker_db_manager_shutdown ();
tracker_ontologies_shutdown ();
if (!reloading) {
tracker_locale_shutdown ();
}
tracker_data_update_shutdown ();
return FALSE;
......@@ -4653,9 +4605,6 @@ tracker_data_manager_init (TrackerDBManagerFlags flags,
tracker_db_manager_shutdown ();
tracker_ontologies_shutdown ();
if (!reloading) {
tracker_locale_shutdown ();
}
tracker_data_update_shutdown ();
return FALSE;
......@@ -4685,9 +4634,6 @@ tracker_data_manager_init (TrackerDBManagerFlags flags,
#endif /* DISABLE_JOURNAL */
tracker_db_manager_shutdown ();
tracker_ontologies_shutdown ();
if (!reloading) {
tracker_locale_shutdown ();
}
tracker_data_update_shutdown ();
return FALSE;
......@@ -4743,9 +4689,6 @@ tracker_data_manager_shutdown (void)
tracker_db_manager_shutdown ();
tracker_ontologies_shutdown ();
if (!reloading) {
tracker_locale_shutdown ();
}
#if HAVE_TRACKER_FTS
if (!tracker_fts_shutdown ()) {
......
......@@ -118,7 +118,7 @@ public class Tracker.Direct.Connection : Tracker.Sparql.Connection, AsyncInitabl
init_mutex.lock ();
try {
Locale.init ();
Locale.sanity_check ();
DBManagerFlags db_flags = DBManagerFlags.ENABLE_MUTEXES;
if ((flags & Sparql.ConnectionFlags.READONLY) != 0)
db_flags |= DBManagerFlags.READONLY;
......
......@@ -271,7 +271,7 @@ run_standalone (TrackerConfig *config)
}
output_format = enum_value->value;
tracker_locale_init ();
tracker_locale_sanity_check ();
/* This makes sure we don't steal all the system's resources */
initialize_priority_and_scheduling (tracker_config_get_sched_idle (config),
......@@ -285,7 +285,6 @@ run_standalone (TrackerConfig *config)
if (!object) {
g_object_unref (file);
g_free (uri);
tracker_locale_shutdown ();
return EXIT_FAILURE;
}
......@@ -295,8 +294,6 @@ run_standalone (TrackerConfig *config)
g_object_unref (file);
g_free (uri);
tracker_locale_shutdown ();
return EXIT_SUCCESS;
}
......@@ -398,7 +395,7 @@ main (int argc, char *argv[])
g_thread_self ());
#endif /* THREAD_ENABLE_TRACE */
tracker_locale_init ();
tracker_locale_sanity_check ();
controller = tracker_extract_controller_new (decorator);
tracker_miner_start (TRACKER_MINER (decorator));
......@@ -417,8 +414,6 @@ main (int argc, char *argv[])
tracker_miner_stop (TRACKER_MINER (decorator));
/* Shutdown subsystems */
tracker_locale_shutdown ();
g_object_unref (extract);
g_object_unref (decorator);
g_object_unref (controller);
......
......@@ -21,6 +21,7 @@
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <locale.h>
#include <glib.h>
#include <glib/gstdio.h>
......@@ -431,7 +432,8 @@ main (int argc, char **argv)
g_test_init (&argc, &argv, NULL);
tracker_locale_init ();
setlocale (LC_ALL, "");
ensure_file_exists (TEST_FILENAME);
ensure_file_exists (TEST_HIDDEN_FILENAME);
......@@ -470,7 +472,5 @@ main (int argc, char **argv)
remove_file (TEST_FILENAME);
remove_file (TEST_HIDDEN_FILENAME);
tracker_locale_shutdown ();
return result;
}
......@@ -24,6 +24,7 @@
#include <libtracker-common/tracker-file-utils.h>
#include <libtracker-common/tracker-utils.h>
#include <libtracker-common/tracker-locale.h>
#include <locale.h>
static void
test_seconds_to_string ()
......@@ -139,7 +140,7 @@ main (int argc, char **argv)
g_test_init (&argc, &argv, NULL);
tracker_locale_init ();
setlocale (LC_ALL, "");
g_test_add_func ("/libtracker-common/tracker-utils/seconds_to_string",
test_seconds_to_string);
......@@ -161,7 +162,5 @@ main (int argc, char **argv)
ret = g_test_run ();
tracker_locale_shutdown ();
return ret;
}
......@@ -23,6 +23,7 @@
#include <glib-object.h>
#include <libtracker-extract/tracker-encoding.h>
#include <libtracker-common/tracker-locale.h>
#include <locale.h>
static void
test_encoding_guessing ()
......@@ -65,12 +66,11 @@ main (int argc, char **argv)
{
g_test_init (&argc, &argv, NULL);
tracker_locale_init ();
setlocale (LC_ALL, "");
g_test_add_func ("/libtracker-extract/tracker-encoding/encoding_guessing",
test_encoding_guessing);
g_test_add_func ("/libtracker-extract/tracker-encoding/can_guess",
test_encoding_can_guess);
tracker_locale_shutdown ();
return g_test_run ();
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment