~ubuntu-branches/ubuntu/trusty/lightdm/trusty-proposed

« back to all changes in this revision

Viewing changes to common/configuration.c

  • Committer: Package Import Robot
  • Author(s): Robert Ancell
  • Date: 2014-03-25 14:45:54 UTC
  • Revision ID: package-import@ubuntu.com-20140325144554-3r1xxa7xms68mk5x
Tags: 1.9.13-0ubuntu1
* New upstream release:
  - Handle not getting an X connection when attempting to get X layouts.
    (LP: #1235915)
  - Read config data from both XDG_DATA_DIRS and XDG_CONFIG_DIRS.

Show diffs side-by-side

added added

removed removed

Lines of Context:
9
9
 * license.
10
10
 */
11
11
 
 
12
#include <string.h>
 
13
 
12
14
#include "configuration.h"
13
15
 
14
16
struct ConfigurationPrivate
70
72
    return TRUE;
71
73
}
72
74
 
 
75
static gchar *
 
76
path_make_absolute (gchar *path)
 
77
{
 
78
    gchar *cwd, *abs_path;
 
79
 
 
80
    if (!path)
 
81
        return NULL;
 
82
 
 
83
    if (g_path_is_absolute (path))
 
84
        return path;
 
85
 
 
86
    cwd = g_get_current_dir ();
 
87
    abs_path = g_build_filename (cwd, path, NULL);
 
88
    g_free (path);
 
89
 
 
90
    return abs_path;
 
91
}
 
92
 
 
93
static int
 
94
compare_strings (gconstpointer a, gconstpointer b)
 
95
{
 
96
    return strcmp (a, b);
 
97
}
 
98
 
 
99
static void
 
100
load_config_directory (const gchar *path, GList **messages)
 
101
{
 
102
    GDir *dir;
 
103
    GList *files = NULL, *link;
 
104
    GError *error = NULL;
 
105
 
 
106
    /* Find configuration files */
 
107
    dir = g_dir_open (path, 0, &error);
 
108
    if (error && !g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT))
 
109
        g_printerr ("Failed to open configuration directory %s: %s\n", path, error->message);
 
110
    g_clear_error (&error);
 
111
    if (dir)
 
112
    {
 
113
        const gchar *name;
 
114
        while ((name = g_dir_read_name (dir)))
 
115
            files = g_list_append (files, g_strdup (name));
 
116
        g_dir_close (dir);
 
117
    }
 
118
 
 
119
    /* Sort alphabetically and load onto existing configuration */
 
120
    files = g_list_sort (files, compare_strings);
 
121
    for (link = files; link; link = link->next)
 
122
    {
 
123
        gchar *filename = link->data;
 
124
        gchar *conf_path;
 
125
 
 
126
        conf_path = g_build_filename (path, filename, NULL);
 
127
        if (g_str_has_suffix (filename, ".conf"))
 
128
        {
 
129
            if (messages)
 
130
                *messages = g_list_append (*messages, g_strdup_printf ("Loading configuration from %s", conf_path));
 
131
            config_load_from_file (config_get_instance (), conf_path, &error);
 
132
            if (error && !g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT))
 
133
                g_printerr ("Failed to load configuration from %s: %s\n", filename, error->message);
 
134
            g_clear_error (&error);
 
135
        }
 
136
        else
 
137
            g_debug ("Ignoring configuration file %s, it does not have .conf suffix", conf_path);
 
138
        g_free (conf_path);
 
139
    }
 
140
    g_list_free_full (files, g_free);
 
141
}
 
142
 
 
143
static void
 
144
load_config_directories (const gchar * const *dirs, GList **messages)
 
145
{
 
146
    gint i;
 
147
 
 
148
    /* Load in reverse order, because XDG_* fields are preference-ordered and the directories in front should override directories in back. */
 
149
    for (i = g_strv_length ((gchar **)dirs) - 1; i >= 0; i--)
 
150
    {
 
151
        gchar *full_dir = g_build_filename (dirs[i], "lightdm", "lightdm.conf.d", NULL);
 
152
        if (messages)
 
153
            *messages = g_list_append (*messages, g_strdup_printf ("Loading configuration dirs from %s", full_dir));
 
154
        load_config_directory (full_dir, messages);
 
155
        g_free (full_dir);
 
156
    }
 
157
}
 
158
 
 
159
gboolean
 
160
config_load_from_standard_locations (Configuration *config, const gchar *config_path, GList **messages)
 
161
{
 
162
    gchar *config_dir, *config_d_dir = NULL;
 
163
    gboolean explicit_config = FALSE;
 
164
    gboolean success = TRUE;
 
165
    GError *error = NULL;
 
166
 
 
167
    load_config_directories (g_get_system_data_dirs (), messages);
 
168
    load_config_directories (g_get_system_config_dirs (), messages);
 
169
 
 
170
    if (config_path)
 
171
    {
 
172
        config_dir = g_path_get_basename (config_path);
 
173
        config_dir = path_make_absolute (config_dir);
 
174
        explicit_config = TRUE;
 
175
    }
 
176
    else
 
177
    {
 
178
        config_dir = g_strdup (CONFIG_DIR);
 
179
        config_d_dir = g_build_filename (config_dir, "lightdm.conf.d", NULL);
 
180
        config_path = g_build_filename (config_dir, "lightdm.conf", NULL);
 
181
    }
 
182
    config_set_string (config, "LightDM", "config-directory", config_dir);
 
183
    g_free (config_dir);
 
184
 
 
185
    if (config_d_dir)
 
186
        load_config_directory (config_d_dir, messages);
 
187
    g_free (config_d_dir);
 
188
 
 
189
    if (messages)
 
190
        *messages = g_list_append (*messages, g_strdup_printf ("Loading configuration from %s", config_path));
 
191
    if (!config_load_from_file (config, config_path, &error))
 
192
    {
 
193
        gboolean is_empty;
 
194
 
 
195
        is_empty = error && g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
 
196
 
 
197
        if (explicit_config || !is_empty)
 
198
        {
 
199
            if (error)
 
200
                g_printerr ("Failed to load configuration from %s: %s\n", config_path, error->message);
 
201
            success = FALSE;
 
202
        }
 
203
    }
 
204
    g_clear_error (&error);
 
205
 
 
206
    return success;
 
207
}
 
208
 
73
209
gchar **
74
210
config_get_groups (Configuration *config)
75
211
{