~upstart-devel/upstart/trunk

« back to all changes in this revision

Viewing changes to init/xdg.c

* Merge of lp:~jamesodhunt/upstart/remove-basic-user-sessions.
  (Note: won't compile due to dependency of commit about to be applied).

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
#endif /* HAVE_CONFIG_H */
25
25
 
26
26
#include <stdlib.h>
 
27
#include <sys/stat.h>
 
28
#include <sys/types.h>
27
29
 
28
30
#include <nih/alloc.h>
29
31
#include <nih/logging.h>
33
35
#include "xdg.h"
34
36
 
35
37
/**
 
38
 * user_mode:
 
39
 *
 
40
 * If TRUE, upstart runs in user session mode.
 
41
 **/
 
42
int user_mode = FALSE;
 
43
 
 
44
/**
36
45
 * get_home_subdir:
37
 
 *
38
 
 * Construct path to directory in user's HOME dir.
 
46
 * @suffix: sub-directory name
 
47
 * @create: flag to create sub-directory
 
48
 * 
 
49
 * Construct path to @suffix directory in user's HOME dir. If @create
 
50
 * flag is TRUE, also attempt to create that directory. Errors upon
 
51
 * directory creation are ignored.
39
52
 * 
40
53
 * Returns: newly-allocated path, or NULL on error.
41
 
 */
42
 
 
 
54
 **/
43
55
char *
44
 
get_home_subdir (const char * suffix)
 
56
get_home_subdir (const char * suffix, int create)
45
57
{
46
58
        char *dir;
47
 
        nih_assert (suffix && suffix[0]);
 
59
        nih_assert (suffix != NULL);
 
60
        nih_assert (suffix[0]);
48
61
        
49
62
        dir = getenv ("HOME");
50
 
        if (dir && dir[0]) {
 
63
        if (dir && dir[0] == '/') {
51
64
                dir = nih_sprintf (NULL, "%s/%s", dir, suffix);
 
65
                if (! dir)
 
66
                        return NULL;
 
67
                if (create)
 
68
                        mkdir (dir, 0700);
52
69
                return dir;
53
70
        }
54
71
 
56
73
}
57
74
 
58
75
/**
 
76
 * xdg_get_cache_home:
 
77
 *
 
78
 * Determine an XDG compliant XDG_CACHE_HOME
 
79
 *
 
80
 * Returns: newly-allocated path, or NULL on error.
 
81
 **/
 
82
char *
 
83
xdg_get_cache_home (void)
 
84
{
 
85
        nih_local char  **env = NULL;
 
86
        char             *dir;
 
87
 
 
88
        dir = getenv ("XDG_CACHE_HOME");
 
89
        
 
90
        if (dir && dir[0] == '/') {
 
91
                mkdir (dir, 0700);
 
92
                dir = nih_strdup (NULL, dir);
 
93
                return dir;
 
94
        }
 
95
 
 
96
        /* Per XDG spec, we should only create dirs, if we are
 
97
         * attempting to write and the dir is not there. Here we
 
98
         * anticipate logging to happen really soon now, hence we
 
99
         * pre-create the cache dir. That does not protect us from
 
100
         * this directory disappering while upstart is running. =/
 
101
         * hence this dir should be created each time we try to write
 
102
         * log... */
 
103
        dir = get_home_subdir (".cache", TRUE);
 
104
 
 
105
        return dir;
 
106
}
 
107
 
 
108
/**
59
109
 * xdg_get_config_home:
60
110
 *
61
111
 * Determine an XDG compliant XDG_CONFIG_HOME
70
120
 
71
121
        dir = getenv ("XDG_CONFIG_HOME");
72
122
        
73
 
        if (dir && dir[0]) {
 
123
        if (dir && dir[0] == '/') {
 
124
                mkdir (dir, 0700);
74
125
                dir = nih_strdup (NULL, dir);
75
126
                return dir;
76
127
        }
77
128
 
78
 
        dir = get_home_subdir (".config");
 
129
        /* Per XDG spec, we should only create dirs, if we are
 
130
         * attempting to write to the dir. But we only read config
 
131
         * dir. But we rather create it, to place inotify watch on
 
132
         * it. */
 
133
        dir = get_home_subdir (".config", TRUE);
79
134
 
80
135
        return dir;
81
136
}
134
189
        if (path && path[0]) {
135
190
                if (! nih_strcat_sprintf (&path, NULL, "/%s", INIT_XDG_SUBDIR))
136
191
                        goto error;
 
192
                mkdir (path, 0700);
137
193
                if (! nih_str_array_add (&all_dirs, NULL, NULL, path))
138
194
                        goto error;
139
195
                nih_free (path);
141
197
        }
142
198
 
143
199
        /* Legacy User's: ~/.init */
144
 
        path = get_home_subdir (USERCONFDIR);
 
200
        path = get_home_subdir (USERCONFDIR, FALSE);
145
201
        if (! path)
146
202
                goto error;
147
203
 
158
214
                goto error;
159
215
 
160
216
        for (char **p = dirs; p && *p; p++) {
 
217
                if (*p[0] != '/')
 
218
                        continue;
161
219
                if (! nih_strcat_sprintf (p, NULL, "/%s", INIT_XDG_SUBDIR))
162
220
                        goto error;
163
221
                if (! nih_str_array_add (&all_dirs, NULL, NULL, *p))
185
243
        return NULL;
186
244
}
187
245
 
 
246
/**
 
247
 * get_user_log_dir:
 
248
 *
 
249
 * Constructs an XDG compliant path to a cache directory in the user's
 
250
 * home directory. It can be used to store logs.
 
251
 *
 
252
 * Returns: newly-allocated array of paths, or NULL or error.
 
253
 **/
 
254
char *
 
255
get_user_log_dir (void)
 
256
{
 
257
        nih_local char *path = NULL;
 
258
        char *dir = NULL;
 
259
        path = xdg_get_cache_home ();
 
260
        if (path && path[0] == '/') {
 
261
                dir = nih_sprintf (NULL, "%s/%s", path, INIT_XDG_SUBDIR);
 
262
                if (! dir)
 
263
                        return NULL;
 
264
                mkdir (dir, 0700);
 
265
                return dir;
 
266
        }
 
267
        return NULL;
 
268
}