This source file includes following definitions.
- mc_config_mkdir
- mc_config_init_one_config_path
- mc_config_init_config_paths
- mc_config_deinit_config_paths
- mc_config_get_data_path
- mc_config_get_cache_path
- mc_config_get_home_dir
- mc_config_get_path
- mc_config_get_full_path
- mc_config_get_full_vpath
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 #include <config.h>
27
28 #include <stdio.h>
29 #include <stdlib.h>
30
31 #include "lib/global.h"
32 #include "lib/fileloc.h"
33 #include "lib/vfs/vfs.h"
34 #include "lib/util.h"
35
36 #include "lib/mcconfig.h"
37
38
39
40
41
42
43
44
45
46
47
48 static gboolean xdg_vars_initialized = FALSE;
49 static char *mc_config_str = NULL;
50 static char *mc_cache_str = NULL;
51 static char *mc_data_str = NULL;
52
53 static gboolean config_dir_present = FALSE;
54
55 static const struct
56 {
57 char **basedir;
58 const char *filename;
59 } mc_config_files_reference[] =
60 {
61
62
63 { &mc_config_str, MC_CONFIG_FILE },
64 { &mc_config_str, MC_FHL_INI_FILE },
65 { &mc_config_str, MC_HOTLIST_FILE },
66 { &mc_config_str, GLOBAL_KEYMAP_FILE },
67 { &mc_config_str, MC_USERMENU_FILE },
68 { &mc_config_str, EDIT_HOME_MENU },
69 { &mc_config_str, MC_PANELS_FILE },
70
71
72 { &mc_config_str, MC_EXT_FILE },
73 { &mc_config_str, MC_EXT_OLD_FILE },
74
75
76 { &mc_data_str, MC_SKINS_DIR },
77 { &mc_data_str, VFS_SHELL_PREFIX },
78 { &mc_data_str, MC_ASHRC_FILE },
79 { &mc_data_str, MC_BASHRC_FILE },
80 { &mc_data_str, MC_INPUTRC_FILE },
81 { &mc_data_str, MC_ZSHRC_FILE },
82 { &mc_data_str, MC_EXTFS_DIR },
83 { &mc_data_str, MC_HISTORY_FILE },
84 { &mc_data_str, MC_FILEPOS_FILE },
85 { &mc_data_str, EDIT_SYNTAX_FILE },
86 { &mc_data_str, EDIT_HOME_CLIP_FILE },
87 { &mc_data_str, MC_MACRO_FILE },
88
89
90 { &mc_cache_str, "mc.log" },
91 { &mc_cache_str, MC_TREESTORE_FILE },
92 { &mc_cache_str, EDIT_HOME_TEMP_FILE },
93 { &mc_cache_str, EDIT_HOME_BLOCK_FILE },
94
95 { NULL, NULL }
96
97 };
98
99
100
101
102
103 static void
104 mc_config_mkdir (const char *directory_name, GError ** mcerror)
105 {
106 mc_return_if_error (mcerror);
107
108 if ((!g_file_test (directory_name, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) &&
109 (g_mkdir_with_parents (directory_name, 0700) != 0))
110 mc_propagate_error (mcerror, 0, _("Cannot create %s directory"), directory_name);
111 }
112
113
114
115 static char *
116 mc_config_init_one_config_path (const char *path_base, const char *subdir, GError ** mcerror)
117 {
118 char *full_path;
119
120 mc_return_val_if_error (mcerror, FALSE);
121
122 full_path = g_build_filename (path_base, subdir, (char *) NULL);
123
124 if (g_file_test (full_path, G_FILE_TEST_EXISTS))
125 {
126 if (g_file_test (full_path, G_FILE_TEST_IS_DIR))
127 config_dir_present = TRUE;
128 else
129 {
130 fprintf (stderr, "%s %s\n", _("FATAL: not a directory:"), full_path);
131 exit (EXIT_FAILURE);
132 }
133 }
134
135 mc_config_mkdir (full_path, mcerror);
136 if (mcerror != NULL && *mcerror != NULL)
137 MC_PTR_FREE (full_path);
138
139 return full_path;
140 }
141
142
143
144
145
146 void
147 mc_config_init_config_paths (GError ** mcerror)
148 {
149 const char *profile_root;
150 char *dir;
151
152 mc_return_if_error (mcerror);
153
154 if (xdg_vars_initialized)
155 return;
156
157 profile_root = mc_get_profile_root ();
158
159 if (strcmp (profile_root, mc_config_get_home_dir ()) != 0)
160 {
161
162
163
164
165
166
167
168 dir = g_build_filename (profile_root, ".config", (char *) NULL);
169 mc_config_str = mc_config_init_one_config_path (dir, MC_USERCONF_DIR, mcerror);
170 g_free (dir);
171
172 dir = g_build_filename (profile_root, ".cache", (char *) NULL);
173 mc_cache_str = mc_config_init_one_config_path (dir, MC_USERCONF_DIR, mcerror);
174 g_free (dir);
175
176 dir = g_build_filename (profile_root, ".local", "share", (char *) NULL);
177 mc_data_str = mc_config_init_one_config_path (dir, MC_USERCONF_DIR, mcerror);
178 g_free (dir);
179 }
180 else
181 {
182 mc_config_str =
183 mc_config_init_one_config_path (g_get_user_config_dir (), MC_USERCONF_DIR, mcerror);
184 mc_cache_str =
185 mc_config_init_one_config_path (g_get_user_cache_dir (), MC_USERCONF_DIR, mcerror);
186 mc_data_str =
187 mc_config_init_one_config_path (g_get_user_data_dir (), MC_USERCONF_DIR, mcerror);
188 }
189
190 xdg_vars_initialized = TRUE;
191 }
192
193
194
195 void
196 mc_config_deinit_config_paths (void)
197 {
198 if (!xdg_vars_initialized)
199 return;
200
201 g_free (mc_config_str);
202 g_free (mc_cache_str);
203 g_free (mc_data_str);
204
205 g_free (mc_global.share_data_dir);
206 g_free (mc_global.sysconfig_dir);
207
208 xdg_vars_initialized = FALSE;
209 }
210
211
212
213 const char *
214 mc_config_get_data_path (void)
215 {
216 if (!xdg_vars_initialized)
217 mc_config_init_config_paths (NULL);
218
219 return (const char *) mc_data_str;
220 }
221
222
223
224 const char *
225 mc_config_get_cache_path (void)
226 {
227 if (!xdg_vars_initialized)
228 mc_config_init_config_paths (NULL);
229
230 return (const char *) mc_cache_str;
231 }
232
233
234
235 const char *
236 mc_config_get_home_dir (void)
237 {
238 static const char *homedir = NULL;
239
240 if (homedir == NULL)
241 {
242
243
244
245
246 homedir = g_getenv ("HOME");
247 if (homedir == NULL || *homedir == '\0')
248 homedir = g_get_home_dir ();
249 }
250
251 return homedir;
252 }
253
254
255
256 const char *
257 mc_config_get_path (void)
258 {
259 if (!xdg_vars_initialized)
260 mc_config_init_config_paths (NULL);
261
262 return (const char *) mc_config_str;
263 }
264
265
266
267
268
269
270
271
272
273 char *
274 mc_config_get_full_path (const char *config_name)
275 {
276 size_t rule_index;
277
278 if (config_name == NULL)
279 return NULL;
280
281 if (!xdg_vars_initialized)
282 mc_config_init_config_paths (NULL);
283
284 for (rule_index = 0; mc_config_files_reference[rule_index].filename != NULL; rule_index++)
285 if (strcmp (config_name, mc_config_files_reference[rule_index].filename) == 0)
286 return g_build_filename (*mc_config_files_reference[rule_index].basedir,
287 mc_config_files_reference[rule_index].filename, (char *) NULL);
288
289 return NULL;
290 }
291
292
293
294
295
296
297
298
299
300 vfs_path_t *
301 mc_config_get_full_vpath (const char *config_name)
302 {
303 vfs_path_t *ret_vpath;
304 char *str_path;
305
306 str_path = mc_config_get_full_path (config_name);
307
308 ret_vpath = vfs_path_from_str (str_path);
309 g_free (str_path);
310
311 return ret_vpath;
312 }
313
314