diff --git a/edit/edit.h b/edit/edit.h index b406bd81c..04631c755 100644 --- a/edit/edit.h +++ b/edit/edit.h @@ -33,6 +33,7 @@ #define MC_EDIT_H #include "../src/global.h" /* PATH_SEP_STR */ +#include "../src/fileloc.h" /* Editor widget */ struct WEdit; @@ -77,18 +78,4 @@ const char *edit_get_file_name (const WEdit *edit); int edit_get_curs_col (const WEdit *edit); const char *edit_get_syntax_type (const WEdit *edit); -/* editor home directory */ -#define EDIT_DIR ".mc" PATH_SEP_STR "cedit" - -/* file names */ -#define EDIT_SYNTAX_FILE EDIT_DIR PATH_SEP_STR "Syntax" -#define EDIT_CLIP_FILE EDIT_DIR PATH_SEP_STR "cooledit.clip" -#define EDIT_MACRO_FILE EDIT_DIR PATH_SEP_STR "cooledit.macros" -#define EDIT_BLOCK_FILE EDIT_DIR PATH_SEP_STR "cooledit.block" -#define EDIT_TEMP_FILE EDIT_DIR PATH_SEP_STR "cooledit.temp" - -#define EDIT_GLOBAL_MENU "cedit.menu" -#define EDIT_LOCAL_MENU ".cedit.menu" -#define EDIT_HOME_MENU EDIT_DIR PATH_SEP_STR "menu" - #endif /* MC_EDIT_H */ diff --git a/edit/etags.h b/edit/etags.h index d1b0d05f1..155acbaff 100644 --- a/edit/etags.h +++ b/edit/etags.h @@ -9,7 +9,6 @@ #define SHORT_DEF_LEN 30 #define LONG_DEF_LEN 40 #define LINE_DEF_LEN 16 -#define TAGS_NAME "TAGS" typedef struct etags_hash_struct { size_t filename_len; diff --git a/src/charsets.c b/src/charsets.c index 4b8c3bc9b..f12d1753a 100644 --- a/src/charsets.c +++ b/src/charsets.c @@ -34,6 +34,7 @@ #include "strutil.h" /* utf-8 functions */ #include "main.h" #include "util.h" /* concat_dir_and_file() */ +#include "fileloc.h" int n_codepages = 0; diff --git a/src/charsets.h b/src/charsets.h index 96c2053ba..98186b8cb 100644 --- a/src/charsets.h +++ b/src/charsets.h @@ -10,7 +10,6 @@ #define UNKNCHAR '\001' -#define CHARSETS_INDEX "mc.charsets" extern int n_codepages; extern unsigned char conv_displ[256]; diff --git a/src/cmd.c b/src/cmd.c index 5b61194a6..0728622b1 100644 --- a/src/cmd.c +++ b/src/cmd.c @@ -81,6 +81,7 @@ #include "strutil.h" #include "dir.h" #include "cmddef.h" /* CK_InputHistoryShow */ +#include "fileloc.h" #ifndef MAP_FILE # define MAP_FILE 0 @@ -601,7 +602,7 @@ void ext_cmd (void) extdir = concat_dir_and_file (mc_home, MC_LIB_EXT); if (dir == 0){ - buffer = concat_dir_and_file (home_dir, MC_USER_EXT); + buffer = g_build_filename (home_dir, MC_USERCONF_DIR, MC_FILEBIND_FILE, NULL); check_for_default (extdir, buffer); do_edit (buffer); g_free (buffer); @@ -646,7 +647,7 @@ edit_mc_menu_cmd (void) break; case 1: - buffer = concat_dir_and_file (home_dir, MC_HOME_MENU); + buffer = g_build_filename (home_dir, MC_USERCONF_DIR, MC_USERMENU_FILE, NULL); check_for_default (menufile, buffer); break; @@ -673,7 +674,6 @@ void edit_fhl_cmd (void) { char *buffer = NULL; char *fhlfile = NULL; - char *user_mc_dir; int dir; @@ -686,9 +686,7 @@ void edit_fhl_cmd (void) fhlfile = concat_dir_and_file (mc_home, MC_FHL_INI_FILE); if (dir == 0){ - user_mc_dir = concat_dir_and_file (home_dir, MC_BASE); - buffer = concat_dir_and_file (user_mc_dir, MC_FHL_INI_FILE); - g_free(user_mc_dir); + buffer = g_build_filename (home_dir, MC_USERCONF_DIR, MC_FHL_INI_FILE, NULL); check_for_default (fhlfile, buffer); do_edit (buffer); g_free (buffer); @@ -1308,7 +1306,8 @@ void save_setup_cmd (void) { save_setup (); - message (D_NORMAL, _(" Setup "), _(" Setup saved to ~/%s"), PROFILE_NAME); + message (D_NORMAL, _(" Setup "), _(" Setup saved to ~/%s"), + MC_USERCONF_DIR PATH_SEP_STR MC_CONFIG_FILE); } static void diff --git a/src/ext.c b/src/ext.c index 1b914b16d..9c46ad9f6 100644 --- a/src/ext.c +++ b/src/ext.c @@ -449,7 +449,7 @@ regex_command (const char *filename, const char *action, int *move_dir) int mc_user_ext = 1; int home_error = 0; - extension_file = concat_dir_and_file (home_dir, MC_USER_EXT); + extension_file = g_build_filename (home_dir, MC_USERCONF_DIR, MC_FILEBIND_FILE, NULL); if (!exist_file (extension_file)) { g_free (extension_file); check_stock_mc_ext: @@ -489,11 +489,11 @@ regex_command (const char *filename, const char *action, int *move_dir) } if (home_error) { char *title = - g_strdup_printf (_(" ~/%s file error "), MC_USER_EXT); + g_strdup_printf (_(" ~/%s file error "), MC_USERCONF_DIR PATH_SEP_STR MC_FILEBIND_FILE); message (D_ERROR, title, _("The format of the ~/%s file has " "changed with version 3.0. You may either want to copy " "it from %smc.ext or use that file as an example of how " - "to write it."), MC_USER_EXT, mc_home); + "to write it."), MC_USERCONF_DIR PATH_SEP_STR MC_FILEBIND_FILE, mc_home); g_free (title); } } diff --git a/src/ext.h b/src/ext.h index 65ba6e0e3..70912c378 100644 --- a/src/ext.h +++ b/src/ext.h @@ -13,7 +13,4 @@ int regex_command (const char *filename, const char *action, int *move_dir); */ void flush_extension_file (void); -#define MC_USER_EXT ".mc/bindings" -#define MC_LIB_EXT "mc.ext" - #endif diff --git a/src/filehighlight/fhl.h b/src/filehighlight/fhl.h index 3794ac555..9fb16629a 100644 --- a/src/filehighlight/fhl.h +++ b/src/filehighlight/fhl.h @@ -5,8 +5,6 @@ #include "../../src/search/search.h" #include "../src/dir.h" -#define MC_FHL_INI_FILE "filehighlight.ini" - /*** typedefs(not structures) and defined constants **********************************************/ /*** enums ***************************************************************************************/ diff --git a/src/filehighlight/ini-file-read.c b/src/filehighlight/ini-file-read.c index 8d0a82486..167a0df4b 100644 --- a/src/filehighlight/ini-file-read.c +++ b/src/filehighlight/ini-file-read.c @@ -30,6 +30,7 @@ #include "../src/global.h" #include "../src/main.h" +#include "../src/fileloc.h" #include "../src/strescape.h" #include "../src/skin/skin.h" #include "fhl.h" @@ -206,7 +207,6 @@ gboolean mc_fhl_init_from_standart_files (mc_fhl_t * fhl) { gchar *name; - gchar *user_mc_dir; /* ${datadir}/mc/filehighlight.ini */ name = concat_dir_and_file (mc_home_alt, MC_FHL_INI_FILE); @@ -225,9 +225,8 @@ mc_fhl_init_from_standart_files (mc_fhl_t * fhl) g_free (name); /* ~/.mc/filehighlight.ini */ - user_mc_dir = concat_dir_and_file (home_dir, MC_BASE); - name = concat_dir_and_file (user_mc_dir, MC_FHL_INI_FILE); - g_free (user_mc_dir); + name = g_build_filename (home_dir, MC_USERCONF_DIR, MC_FHL_INI_FILE, NULL); + if (exist_file (name) && (!mc_fhl_read_ini_file (fhl, name))) { g_free (name); return FALSE; diff --git a/src/fileloc.h b/src/fileloc.h index 9ca023918..12f000769 100644 --- a/src/fileloc.h +++ b/src/fileloc.h @@ -14,7 +14,21 @@ #ifndef MC_FILELOC_H #define MC_FILELOC_H +#ifndef MC_USERCONF_DIR #define MC_USERCONF_DIR ".mc" +#endif + + +#define TAGS_NAME "TAGS" + +#define MC_GLOBAL_CONFIG_FILE "mc.lib" +#define MC_GLOBAL_MENU "mc.menu" +#define MC_LOCAL_MENU ".mc.menu" +#define MC_HINT "mc.hint" +#define GLOBAL_KEYMAP_FILE "mc.keymap" +#define CHARSETS_INDEX "mc.charsets" +#define MC_LIB_EXT "mc.ext" + #define MC_BASHRC_FILE "bashrc" #define MC_CONFIG_FILE "ini" @@ -23,5 +37,23 @@ #define MC_HISTORY_FILE "history" #define MC_HOTLIST_FILE "hotlist" #define MC_USERMENU_FILE "menu" +#define MC_TREESTORE_FILE "Tree" +#define MC_PANELS_FILE "panels.ini" +#define MC_FHL_INI_FILE "filehighlight.ini" +#define MC_SKINS_SUBDIR "skins" + +/* editor home directory */ +#define EDIT_DIR MC_USERCONF_DIR PATH_SEP_STR "cedit" + +/* file names */ +#define EDIT_SYNTAX_FILE EDIT_DIR PATH_SEP_STR "Syntax" +#define EDIT_CLIP_FILE EDIT_DIR PATH_SEP_STR "cooledit.clip" +#define EDIT_MACRO_FILE EDIT_DIR PATH_SEP_STR "cooledit.macros" +#define EDIT_BLOCK_FILE EDIT_DIR PATH_SEP_STR "cooledit.block" +#define EDIT_TEMP_FILE EDIT_DIR PATH_SEP_STR "cooledit.temp" + +#define EDIT_GLOBAL_MENU "cedit.menu" +#define EDIT_LOCAL_MENU ".cedit.menu" +#define EDIT_HOME_MENU EDIT_DIR PATH_SEP_STR "menu" #endif diff --git a/src/hotlist.c b/src/hotlist.c index 83428b0c8..c8c266d98 100644 --- a/src/hotlist.c +++ b/src/hotlist.c @@ -59,6 +59,8 @@ #include "glibcompat.h" /* g_strlcpy for glib < 2.0 */ #include "history.h" #include "strutil.h" +#include "util.h" +#include "fileloc.h" #define UX 5 #define UY 2 @@ -1450,7 +1452,7 @@ load_hotlist (void) } if (!hotlist_file_name) - hotlist_file_name = concat_dir_and_file (home_dir, HOTLIST_FILENAME); + hotlist_file_name = g_build_filename (home_dir, MC_USERCONF_DIR, MC_HOTLIST_FILE, NULL); hotlist = new_hotlist (); hotlist->type = HL_TYPE_GROUP; @@ -1477,7 +1479,7 @@ load_hotlist (void) } else { message (D_ERROR, _(" Hotlist Load "), _("MC was unable to write ~/%s file, your old hotlist entries were not deleted"), - HOTLIST_FILENAME); + MC_USERCONF_DIR PATH_SEP_STR MC_HOTLIST_FILE); } } else { hot_load_file (hotlist); @@ -1559,14 +1561,9 @@ int save_hotlist (void) struct stat stat_buf; if (!hotlist_state.readonly && hotlist_state.modified && hotlist_file_name) { - char *fbak = g_strconcat (hotlist_file_name, ".bak", (char *) NULL); + mc_util_make_backup_if_possible (hotlist_file_name, ".bak"); - rename (hotlist_file_name, fbak); if ((hotlist_file = fopen (hotlist_file_name, "w")) != 0) { - if (stat (fbak, &stat_buf) == 0) - chmod (hotlist_file_name, stat_buf.st_mode); - else - chmod (hotlist_file_name, S_IRUSR | S_IWUSR); hot_save_group (hotlist); fclose (hotlist_file); stat (hotlist_file_name, &stat_buf); @@ -1574,8 +1571,7 @@ int save_hotlist (void) saved = 1; hotlist_state.modified = 0; } else - rename (fbak, hotlist_file_name); - g_free (fbak); + mc_util_restore_from_backup_if_possible (hotlist_file_name, ".bak"); } return saved; diff --git a/src/keybind.h b/src/keybind.h index 540f49d15..7a3666108 100644 --- a/src/keybind.h +++ b/src/keybind.h @@ -4,8 +4,6 @@ #include "global.h" -#define GLOBAL_KEYMAP_FILE "mc.keymap" - typedef struct name_key_map_t { const char *name; int val; diff --git a/src/logging.c b/src/logging.c index bb5dc8d2b..c370eeb3c 100644 --- a/src/logging.c +++ b/src/logging.c @@ -36,6 +36,7 @@ #include "logging.h" #include "setup.h" #include "../src/mcconfig/mcconfig.h" +#include "fileloc.h" /*** file scope functions **********************************************/ @@ -64,7 +65,7 @@ mc_log(const char *fmt, ...) if (is_logging_enabled()) { va_start(args, fmt); - logfilename = g_strdup_printf("%s/.mc/log", home_dir); + logfilename = g_strdup_printf("%s/%s/log", home_dir, MC_USERCONF_DIR); if ((f = fopen(logfilename, "a")) != NULL) { (void)vfprintf(f, fmt, args); (void)fclose(f); diff --git a/src/main.c b/src/main.c index fdcf57e54..b44700e63 100644 --- a/src/main.c +++ b/src/main.c @@ -2051,7 +2051,7 @@ main (int argc, char *argv[]) /* create home directory */ /* do it after the screen library initialization to show the error message */ - mc_dir = concat_dir_and_file (home_dir, MC_BASE); + mc_dir = concat_dir_and_file (home_dir, MC_USERCONF_DIR); canonicalize_pathname (mc_dir); if ((stat (mc_dir, &s) != 0) && (errno == ENOENT) && mkdir (mc_dir, 0700) != 0) diff --git a/src/main.h b/src/main.h index 4dcb1fb4a..5ea3b164b 100644 --- a/src/main.h +++ b/src/main.h @@ -123,8 +123,6 @@ void init_menu (void); char *remove_encoding_from_path (const char *); -#define MC_BASE "/.mc/" - struct WPanel; void directory_history_add (struct WPanel *panel, const char *dir); diff --git a/src/mcconfig/common.c b/src/mcconfig/common.c index b35e54e4d..6e46d2986 100644 --- a/src/mcconfig/common.c +++ b/src/mcconfig/common.c @@ -22,10 +22,11 @@ #include #include #include +#include #include "global.h" -#include "../../vfs/vfs.h" /* mc_stat */ +#include "../../vfs/vfs.h" /* mc_stat */ #include "mcconfig.h" @@ -41,8 +42,47 @@ mc_config_t *mc_panels_config; /*** file scope variables **********************************************/ /*** file scope functions **********************************************/ +/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ +static gboolean +mc_config_new_or_override_file (mc_config_t * mc_config, const gchar * ini_path) +{ + gchar *data, *written_data; + gsize len, total_written; + gboolean ret; + int fd; + ssize_t cur_written; + + data = g_key_file_to_data (mc_config->handle, &len, NULL); + if (!exist_file (ini_path)) { + ret = g_file_set_contents (ini_path, data, len, NULL); + g_free (data); + return ret; + } + mc_util_make_backup_if_possible (ini_path, "~"); + + fd = mc_open (ini_path, O_WRONLY | O_TRUNC | O_SYNC, 0); + if (fd == -1) + return FALSE; + + for (written_data = data, total_written = len; + (cur_written = mc_write (fd, (const void *) written_data, total_written)) > 0; + written_data += cur_written, total_written -= cur_written); + mc_close (fd); + g_free (data); + + if (cur_written == -1) { + mc_util_restore_from_backup_if_possible (ini_path, "~"); + return FALSE; + } + + mc_util_unlink_backup_if_possible (ini_path, "~"); + return TRUE; +} + +/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ /*** public functions **************************************************/ +/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ mc_config_t * mc_config_init (const gchar * ini_path) @@ -53,24 +93,20 @@ mc_config_init (const gchar * ini_path) mc_config = g_try_malloc0 (sizeof (mc_config_t)); if (mc_config == NULL) - return NULL; + return NULL; mc_config->handle = g_key_file_new (); - if (mc_config->handle == NULL) - { - g_free (mc_config); - return NULL; + if (mc_config->handle == NULL) { + g_free (mc_config); + return NULL; } - if (!ini_path || !exist_file (ini_path)) - { - return mc_config; + if (!ini_path || !exist_file (ini_path)) { + return mc_config; } - if (!mc_stat (ini_path, &st) && st.st_size) - { - /* file present and not empty */ - g_key_file_load_from_file - (mc_config->handle, ini_path, G_KEY_FILE_KEEP_COMMENTS, NULL); + if (!mc_stat (ini_path, &st) && st.st_size) { + /* file present and not empty */ + g_key_file_load_from_file (mc_config->handle, ini_path, G_KEY_FILE_KEEP_COMMENTS, NULL); } mc_config->ini_path = g_strdup (ini_path); @@ -83,7 +119,7 @@ void mc_config_deinit (mc_config_t * mc_config) { if (!mc_config) - return; + return; g_free (mc_config->ini_path); g_key_file_free (mc_config->handle); @@ -94,11 +130,10 @@ mc_config_deinit (mc_config_t * mc_config) /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ gboolean -mc_config_has_param (mc_config_t * mc_config, const char *group, - const gchar * param) +mc_config_has_param (mc_config_t * mc_config, const char *group, const gchar * param) { if (!mc_config || !group || !param) - return FALSE; + return FALSE; return g_key_file_has_key (mc_config->handle, group, param, NULL); } @@ -109,7 +144,7 @@ gboolean mc_config_has_group (mc_config_t * mc_config, const char *group) { if (!mc_config || !group) - return FALSE; + return FALSE; return g_key_file_has_group (mc_config->handle, group); } @@ -118,11 +153,10 @@ mc_config_has_group (mc_config_t * mc_config, const char *group) gboolean -mc_config_del_param (mc_config_t * mc_config, const char *group, - const gchar * param) +mc_config_del_param (mc_config_t * mc_config, const char *group, const gchar * param) { if (!mc_config || !group || !param) - return FALSE; + return FALSE; #if GLIB_CHECK_VERSION (2, 15, 0) return g_key_file_remove_key (mc_config->handle, group, param, NULL); #else @@ -137,7 +171,7 @@ gboolean mc_config_del_group (mc_config_t * mc_config, const char *group) { if (!mc_config || !group) - return FALSE; + return FALSE; #if GLIB_CHECK_VERSION (2, 15, 0) return g_key_file_remove_group (mc_config->handle, group, NULL); @@ -157,32 +191,30 @@ mc_config_read_file (mc_config_t * mc_config, const gchar * ini_path) gchar **keys, **curr_key; gchar *value; - if (mc_config == NULL){ - return FALSE; + if (mc_config == NULL) { + return FALSE; } - tmp_config = mc_config_init(ini_path); + tmp_config = mc_config_init (ini_path); if (tmp_config == NULL) return FALSE; groups = mc_config_get_groups (tmp_config, NULL); - for (curr_grp = groups; *curr_grp != NULL; curr_grp++) - { - keys = mc_config_get_keys (tmp_config, *curr_grp ,NULL); - for (curr_key = keys; *curr_key != NULL; curr_key++) - { + for (curr_grp = groups; *curr_grp != NULL; curr_grp++) { + keys = mc_config_get_keys (tmp_config, *curr_grp, NULL); + for (curr_key = keys; *curr_key != NULL; curr_key++) { value = g_key_file_get_value (tmp_config->handle, *curr_grp, *curr_key, NULL); if (value == NULL) continue; g_key_file_set_value (mc_config->handle, *curr_grp, *curr_key, value); - g_free(value); + g_free (value); } - g_strfreev(keys); + g_strfreev (keys); } - g_strfreev(groups); - mc_config_deinit(tmp_config); + g_strfreev (groups); + mc_config_deinit (tmp_config); return TRUE; } @@ -191,22 +223,10 @@ mc_config_read_file (mc_config_t * mc_config, const gchar * ini_path) gboolean mc_config_save_file (mc_config_t * mc_config) { - gchar *data; - gsize len; - gboolean ret; - - if (mc_config == NULL || mc_config->ini_path == NULL){ - return FALSE; + if (mc_config == NULL || mc_config->ini_path == NULL) { + return FALSE; } - data = g_key_file_to_data (mc_config->handle,&len,NULL); - if (exist_file(mc_config->ini_path)) - { - mc_unlink (mc_config->ini_path); - } - - ret = g_file_set_contents(mc_config->ini_path,data,len,NULL); - g_free(data); - return ret; + return mc_config_new_or_override_file (mc_config, mc_config->ini_path); } /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ @@ -214,22 +234,12 @@ mc_config_save_file (mc_config_t * mc_config) gboolean mc_config_save_to_file (mc_config_t * mc_config, const gchar * ini_path) { - gchar *data; - gsize len; - gboolean ret; - if (mc_config == NULL){ - return FALSE; + if (mc_config == NULL) { + return FALSE; } - data = g_key_file_to_data (mc_config->handle,&len,NULL); - if (exist_file(ini_path)) - { - mc_unlink (ini_path); - } - ret = g_file_set_contents(ini_path,data,len,NULL); + return mc_config_new_or_override_file (mc_config, ini_path); - g_free(data); - return ret; } /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ diff --git a/src/setup.c b/src/setup.c index 0a253ddab..34fe20563 100644 --- a/src/setup.c +++ b/src/setup.c @@ -275,7 +275,7 @@ save_layout (void) char *profile; int i; - profile = concat_dir_and_file (home_dir, PROFILE_NAME); + profile = g_build_filename (home_dir, MC_USERCONF_DIR, MC_CONFIG_FILE, NULL); /* Save integer options */ for (i = 0; layout [i].opt_name; i++){ @@ -292,7 +292,7 @@ save_configure (void) char *profile; int i; - profile = concat_dir_and_file (home_dir, PROFILE_NAME); + profile = g_build_filename (home_dir, MC_USERCONF_DIR, MC_CONFIG_FILE, NULL); /* Save integer options */ for (i = 0; int_options[i].opt_name; i++) @@ -380,7 +380,7 @@ save_setup (void) mc_config_set_string(mc_main_config, "Misc" , "source_codepage", get_codepage_id( source_codepage )); #endif /* HAVE_CHARSET */ - tmp_profile = concat_dir_and_file (home_dir, PROFILE_NAME); + tmp_profile = g_build_filename (home_dir, MC_USERCONF_DIR, MC_CONFIG_FILE, NULL); mc_config_save_to_file (mc_main_config, tmp_profile); g_free (tmp_profile); saving_setup = 0; @@ -687,7 +687,7 @@ setup_init (void) if (profile_name) return profile_name; - profile = concat_dir_and_file (home_dir, PROFILE_NAME); + profile = g_build_filename (home_dir, MC_USERCONF_DIR, MC_CONFIG_FILE, NULL); if (!exist_file (profile)){ inifile = concat_dir_and_file (mc_home, "mc.ini"); if (exist_file (inifile)){ @@ -720,13 +720,13 @@ load_setup (void) /* mc.lib is common for all users, but has priority lower than ~/.mc/ini. FIXME: it's only used for keys and treestore now */ - global_profile_name = concat_dir_and_file (mc_home, "mc.lib"); + global_profile_name = concat_dir_and_file (mc_home, MC_GLOBAL_CONFIG_FILE); if (!exist_file(global_profile_name)) { g_free (global_profile_name); - global_profile_name = concat_dir_and_file (mc_home_alt, "mc.lib"); + global_profile_name = concat_dir_and_file (mc_home_alt, MC_GLOBAL_CONFIG_FILE); } - panels_profile_name = concat_dir_and_file (home_dir, PANELS_PROFILE_NAME); + panels_profile_name = g_build_filename (home_dir, MC_USERCONF_DIR, MC_PANELS_FILE, NULL); mc_main_config = mc_config_init(profile); diff --git a/src/setup.h b/src/setup.h index c65848acf..30301cb13 100644 --- a/src/setup.h +++ b/src/setup.h @@ -35,8 +35,4 @@ extern int verbose; extern int mouse_close_dialog; extern int reverse_files_only; -#define PROFILE_NAME ".mc/ini" -#define HOTLIST_FILENAME ".mc/hotlist" -#define PANELS_PROFILE_NAME ".mc/panels.ini" - #endif diff --git a/src/skin/ini-file.c b/src/skin/ini-file.c index 1affeedce..ce12e137b 100644 --- a/src/skin/ini-file.c +++ b/src/skin/ini-file.c @@ -30,6 +30,7 @@ #include "../src/global.h" #include "../src/main.h" +#include "../src/fileloc.h" #include "skin.h" #include "internal.h" @@ -49,7 +50,7 @@ mc_skin_ini_file_load_search_in_dir (mc_skin_t * mc_skin, const gchar * base_dir { char *file_name, *file_name2; - file_name = g_build_filename (base_dir, "skins", mc_skin->name, NULL); + file_name = g_build_filename (base_dir, MC_SKINS_SUBDIR, mc_skin->name, NULL); if (exist_file (file_name)) { mc_skin->config = mc_config_init (file_name); g_free (file_name); @@ -58,7 +59,7 @@ mc_skin_ini_file_load_search_in_dir (mc_skin_t * mc_skin, const gchar * base_dir g_free (file_name); file_name2 = g_strdup_printf ("%s.ini", mc_skin->name); - file_name = g_build_filename (base_dir, "skins", file_name2, NULL); + file_name = g_build_filename (base_dir, MC_SKINS_SUBDIR, file_name2, NULL); g_free (file_name2); if (exist_file (file_name)) { @@ -91,7 +92,7 @@ mc_skin_ini_file_load (mc_skin_t * mc_skin) g_free (file_name); /* ~/.mc/skins/ */ - user_home_dir = concat_dir_and_file (home_dir, MC_BASE); + user_home_dir = concat_dir_and_file (home_dir, MC_USERCONF_DIR); if (mc_skin_ini_file_load_search_in_dir (mc_skin, user_home_dir)) { g_free (user_home_dir); return TRUE; diff --git a/src/subshell.c b/src/subshell.c index 120bd99a1..1e33996f2 100644 --- a/src/subshell.c +++ b/src/subshell.c @@ -58,6 +58,7 @@ #include "../src/tty/key.h" /* XCTRL */ #include "subshell.h" #include "strutil.h" +#include "fileloc.h" #ifndef WEXITSTATUS # define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8) @@ -247,7 +248,7 @@ init_subshell_child (const char *pty_name) switch (subshell_type) { case BASH: - init_file = ".mc/bashrc"; + init_file = MC_USERCONF_DIR PATH_SEP_STR "bashrc"; if (access (init_file, R_OK) == -1) init_file = ".bashrc"; @@ -255,8 +256,8 @@ init_subshell_child (const char *pty_name) putenv ((char*)"HISTCONTROL=ignorespace"); /* Allow alternative readline settings for MC */ - if (access (".mc/inputrc", R_OK) == 0) - putenv ((char*)"INPUTRC=.mc/inputrc"); + if (access (MC_USERCONF_DIR PATH_SEP_STR "inputrc", R_OK) == 0) + putenv ((char*)"INPUTRC=" MC_USERCONF_DIR PATH_SEP_STR "/inputrc"); break; diff --git a/src/tree.c b/src/tree.c index a1a21050f..cf7dadfa2 100644 --- a/src/tree.c +++ b/src/tree.c @@ -60,6 +60,7 @@ #include "history.h" #include "strutil.h" #include "tree.h" +#include "fileloc.h" #define tlines(t) (t->is_panel ? t->widget.lines-2 - (show_mini_info ? 2 : 0) : t->widget.lines) @@ -154,13 +155,17 @@ static void load_tree (WTree *tree) static void save_tree (WTree *tree) { int error; + char *tree_name; (void) tree; error = tree_store_save (); + if (error){ - fprintf (stderr, _("Cannot open the %s file for writing:\n%s\n"), MC_TREE, + tree_name = g_build_filename (home_dir, MC_USERCONF_DIR, MC_TREESTORE_FILE, NULL); + fprintf (stderr, _("Cannot open the %s file for writing:\n%s\n"), tree_name, unix_error_string (error)); + g_free(tree_name); } } diff --git a/src/treestore.c b/src/treestore.c index c24985177..61126dc89 100644 --- a/src/treestore.c +++ b/src/treestore.c @@ -51,6 +51,7 @@ #include "treestore.h" #include "../src/mcconfig/mcconfig.h" #include "setup.h" +#include "fileloc.h" #define TREE_SIGNATURE "Midnight Commander TreeStore v 2.0" @@ -267,7 +268,7 @@ tree_store_load(void) char *name; int retval; - name = concat_dir_and_file(home_dir, MC_TREE); + name = g_build_filename (home_dir, MC_USERCONF_DIR, MC_TREESTORE_FILE, NULL); retval = tree_store_load_from(name); g_free(name); @@ -366,28 +367,21 @@ tree_store_save_to(char *name) int tree_store_save(void) { - char *tmp; char *name; int retval; - tmp = concat_dir_and_file(home_dir, MC_TREE_TMP); - retval = tree_store_save_to(tmp); + name = g_build_filename (home_dir, MC_USERCONF_DIR, MC_TREESTORE_FILE, NULL); + mc_util_make_backup_if_possible (name, ".tmp"); - if (retval) { - g_free(tmp); + if ((retval = tree_store_save_to(name)) != 0) { + mc_util_restore_from_backup_if_possible (name, ".tmp"); + g_free(name); return retval; } - name = concat_dir_and_file(home_dir, MC_TREE); - retval = rename(tmp, name); - - g_free(tmp); + mc_util_unlink_backup_if_possible (name, ".tmp"); g_free(name); - - if (retval) - return errno; - else - return 0; + return 0; } static tree_entry * diff --git a/src/treestore.h b/src/treestore.h index 93b4b1a5c..bc9a677bb 100644 --- a/src/treestore.h +++ b/src/treestore.h @@ -8,11 +8,6 @@ #ifndef MC_TREE_STORE_H #define MC_TREE_STORE_H -/* Default filenames for the tree */ - -#define MC_TREE ".mc/Tree" -#define MC_TREE_TMP ".mc/Tree.tmp" - typedef struct tree_entry { char *name; /* The full path of directory */ int sublevel; /* Number of parent directories (slashes) */ diff --git a/src/user.c b/src/user.c index d5f370232..c970df74f 100644 --- a/src/user.c +++ b/src/user.c @@ -758,8 +758,12 @@ user_menu_cmd (WEdit *edit_widget) menu = g_strdup (edit_widget ? EDIT_LOCAL_MENU : MC_LOCAL_MENU); if (!exist_file (menu) || !menu_file_own (menu)){ g_free (menu); - menu = concat_dir_and_file - (home_dir, edit_widget ? EDIT_HOME_MENU : MC_HOME_MENU); + if (edit_widget) + menu = concat_dir_and_file (home_dir, EDIT_HOME_MENU); + else + menu = g_build_filename (home_dir, MC_USERCONF_DIR, MC_USERMENU_FILE, NULL); + + if (!exist_file (menu)){ g_free (menu); menu = concat_dir_and_file diff --git a/src/user.h b/src/user.h index 8ec259d18..4866bce36 100644 --- a/src/user.h +++ b/src/user.h @@ -14,9 +14,4 @@ int check_format_view (const char *); int check_format_var (const char *, char **); int check_format_cd (const char *); -#define MC_GLOBAL_MENU "mc.menu" -#define MC_LOCAL_MENU ".mc.menu" -#define MC_HOME_MENU ".mc/menu" -#define MC_HINT "mc.hint" - #endif diff --git a/src/util.c b/src/util.c index 12b7ef7ed..66ffd894d 100644 --- a/src/util.c +++ b/src/util.c @@ -54,6 +54,7 @@ #include "fileopctx.h" #include "file.h" /* copy_file_file() */ #include "dir.h" +#include "fileloc.h" #ifdef HAVE_CHARSET #include "charsets.h" @@ -1139,6 +1140,30 @@ resolve_symlinks (const char *path) return buf; } +static gboolean +mc_util_write_backup_content(const char *from_file_name, const char *to_file_name) +{ + FILE *backup_fd; + char *contents; + gsize length; + + if (!g_file_get_contents (from_file_name, &contents, &length, NULL)) + return FALSE; + + backup_fd = fopen (to_file_name, "w"); + if (backup_fd == NULL) { + g_free(contents); + return FALSE; + } + + fwrite ( (const void *) contents, length, 1, backup_fd); + + fflush(backup_fd); + fclose(backup_fd); + g_free(contents); + return TRUE; +} + /* Finds out a relative path from first to second, i.e. goes as many .. * as needed up in first and then goes down using second */ char * @@ -1337,7 +1362,7 @@ load_file_position (const char *filename, long *line, long *column) *column = 0; /* open file with positions */ - fn = concat_dir_and_file (home_dir, MC_FILEPOS); + fn = g_build_filename (home_dir, MC_USERCONF_DIR, MC_FILEPOS_FILE, NULL); f = fopen (fn, "r"); g_free (fn); if (!f) @@ -1376,50 +1401,36 @@ load_file_position (const char *filename, long *line, long *column) void save_file_position (const char *filename, long line, long column) { - char *tmp, *fn; - FILE *f, *t; - char buf[MC_MAXPATHLEN + 20]; - int i = 1; - int len; + char *fn; + FILE *f; - len = strlen (filename); - tmp = concat_dir_and_file (home_dir, MC_FILEPOS_TMP); - fn = concat_dir_and_file (home_dir, MC_FILEPOS); + fn = g_build_filename (home_dir, MC_USERCONF_DIR, MC_FILEPOS_FILE, NULL); + if (fn == NULL) + return; - /* open temporary file */ - t = fopen (tmp, "w"); - if (!t) { - g_free (tmp); + if (! mc_util_make_backup_if_possible (fn, ".tmp")) + return; + + /* open file */ + f = fopen (fn, "a"); + if (f == NULL) { g_free (fn); return; } /* put the new record */ if (line != 1 || column != 0) { - fprintf (t, "%s %ld;%ld\n", filename, line, column); - } - - /* copy records from the old file */ - f = fopen (fn, "r"); - if (f) { - while (fgets (buf, sizeof (buf), f)) { - /* Skip entries for the current filename */ - if (strncmp (buf, filename, len) == 0 && buf[len] == ' ' - && !strchr (&buf[len + 1], ' ')) - continue; - - fprintf (t, "%s", buf); - if (++i > MC_FILEPOS_ENTRIES) - break; + if (fprintf (f, "%s %ld;%ld\n", filename, line, column) < 0) { + fclose (f); + mc_util_restore_from_backup_if_possible (fn, ".tmp"); } + } else fclose (f); - } - fclose (t); - rename (tmp, fn); - g_free (tmp); + mc_util_unlink_backup_if_possible (fn, ".tmp"); g_free (fn); + } extern const char * @@ -1468,3 +1479,65 @@ Q_ (const char *s) return (sep != NULL) ? sep + 1 : result; } + +gboolean +mc_util_make_backup_if_possible (const char *file_name, const char *backup_suffix) +{ + struct stat stat_buf; + char *backup_path; + gboolean ret; + + if (!exist_file (file_name)) + return FALSE; + + backup_path = g_strdup_printf("%s%s",file_name,backup_suffix); + + if (backup_path == NULL) + return FALSE; + + ret = mc_util_write_backup_content (file_name, backup_path); + + if (ret) { + /* Backup file will have same ownership with main file. */ + if (stat (file_name, &stat_buf) == 0) + chmod (backup_path, stat_buf.st_mode); + else + chmod (backup_path, S_IRUSR | S_IWUSR); + } + + g_free(backup_path); + + return ret; +} + +gboolean +mc_util_restore_from_backup_if_possible (const char *file_name, const char *backup_suffix) +{ + gboolean ret; + char *backup_path; + + backup_path = g_strdup_printf("%s%s",file_name,backup_suffix); + if (backup_path == NULL) + return FALSE; + + ret = mc_util_write_backup_content (backup_path, file_name); + g_free(backup_path); + + return ret; +} + +gboolean +mc_util_unlink_backup_if_possible (const char *file_name, const char *backup_suffix) +{ + char *backup_path; + + backup_path = g_strdup_printf("%s%s",file_name,backup_suffix); + if (backup_path == NULL) + return FALSE; + + if (exist_file (backup_path)) + mc_unlink (backup_path); + + g_free(backup_path); + return TRUE; +} diff --git a/src/util.h b/src/util.h index 69cbe18ec..46c3638b8 100644 --- a/src/util.h +++ b/src/util.h @@ -210,10 +210,6 @@ GList *list_append_unique (GList *list, char *text); /* Position saving and restoring */ -/* file where positions are stored */ -#define MC_FILEPOS ".mc/filepos" -/* temporary file */ -#define MC_FILEPOS_TMP ".mc/filepos.tmp" /* maximum entries in MC_FILEPOS */ #define MC_FILEPOS_ENTRIES 1024 /* Load position for the given filename */ @@ -286,6 +282,10 @@ static inline char * str_move(char * dest, const char * src) return memmove (dest, src, n); } +gboolean mc_util_make_backup_if_possible (const char *, const char *); +gboolean mc_util_restore_from_backup_if_possible (const char *, const char *); +gboolean mc_util_unlink_backup_if_possible (const char *, const char *); + #define MC_PTR_FREE(ptr) do { g_free(ptr); (ptr) = NULL; } while (0) #endif diff --git a/src/widget.c b/src/widget.c index c1ea0e56c..bbb9b47e9 100644 --- a/src/widget.c +++ b/src/widget.c @@ -56,8 +56,7 @@ #include "cmddef.h" /* CK_ cmd name const */ #include "keybind.h" /* global_key_map_t */ - -#define HISTORY_FILE_NAME ".mc/history" +#include "fileloc.h" const global_key_map_t *input_map; @@ -911,7 +910,7 @@ history_get (const char *input_name) if (!input_name || !*input_name) return NULL; - profile = concat_dir_and_file (home_dir, HISTORY_FILE_NAME); + profile = g_build_filename (home_dir, MC_USERCONF_DIR, MC_HISTORY_FILE, NULL); cfg = mc_config_init (profile); /* get number of keys */ @@ -953,7 +952,7 @@ history_put (const char *input_name, GList *h) if (!num_history_items_recorded) /* this is how to disable */ return; - profile = concat_dir_and_file (home_dir, HISTORY_FILE_NAME); + profile = g_build_filename (home_dir, MC_USERCONF_DIR, MC_HISTORY_FILE, NULL); if ((i = open (profile, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR)) != -1) close (i);