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