Merge branch '1617_ownership_of_user_home_files'

* 1617_ownership_of_user_home_files:
  Reorganize work with files. Fixed permissions of files in mc home dir. All file names now accumulated into src/fileloc.h
  Ticket #1617: ownership of files ~/.mc
This commit is contained in:
Slava Zanko 2009-10-07 12:01:28 +03:00
commit 705acef312
28 changed files with 276 additions and 199 deletions

View File

@ -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 */

View File

@ -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;

View File

@ -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;

View File

@ -10,7 +10,6 @@
#define UNKNCHAR '\001'
#define CHARSETS_INDEX "mc.charsets"
extern int n_codepages;
extern unsigned char conv_displ[256];

View File

@ -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

View File

@ -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);
}
}

View File

@ -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

View File

@ -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 ***************************************************************************************/

View File

@ -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;

View File

@ -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

View File

@ -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;

View File

@ -4,8 +4,6 @@
#include "global.h"
#define GLOBAL_KEYMAP_FILE "mc.keymap"
typedef struct name_key_map_t {
const char *name;
int val;

View File

@ -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);

View File

@ -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)

View File

@ -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);

View File

@ -22,6 +22,7 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include "global.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)
@ -56,21 +96,17 @@ mc_config_init (const gchar * ini_path)
return NULL;
mc_config->handle = g_key_file_new ();
if (mc_config->handle == NULL)
{
if (mc_config->handle == NULL) {
g_free (mc_config);
return NULL;
}
if (!ini_path || !exist_file (ini_path))
{
if (!ini_path || !exist_file (ini_path)) {
return mc_config;
}
if (!mc_stat (ini_path, &st) && st.st_size)
{
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);
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);
@ -94,8 +130,7 @@ 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;
@ -118,8 +153,7 @@ 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;
@ -167,11 +201,9 @@ mc_config_read_file (mc_config_t * mc_config, const gchar * ini_path)
groups = mc_config_get_groups (tmp_config, NULL);
for (curr_grp = groups; *curr_grp != NULL; curr_grp++)
{
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_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;
@ -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;
}
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;
}
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;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

View File

@ -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);

View File

@ -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

View File

@ -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;

View File

@ -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;

View File

@ -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);
}
}

View File

@ -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,27 +367,20 @@ 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;
}

View File

@ -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) */

View File

@ -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

View File

@ -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

View File

@ -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;
}

View File

@ -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

View File

@ -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);