Ticket #1656: Incorrect processing of cases the lack of rights to save preferences.

In the case where insufficient rights to the contents of the directory ~/.mc/* to
save preferences I get "settings saved in ~ /.mc/ini", but really nothing is saved.

It would be correct to issue a message like "Not enough permissions to save the ~/.mc/ini"

Also, when autosave should be issued a message about the lack of rights.

This branch will check rights of config-files.

testCase:
 *) chmod 0400 ~/.mc
 *) run mc

Signed-off-by: Slava Zanko <slavazanko@gmail.com>
This commit is contained in:
Slava Zanko 2009-10-27 12:14:32 +02:00
parent 45d8208ff6
commit c9b4b0159d
11 changed files with 70 additions and 49 deletions

View File

@ -1305,7 +1305,8 @@ dirsizes_cmd (void)
void
save_setup_cmd (void)
{
save_setup ();
if (! save_setup ())
return;
message (D_NORMAL, _(" Setup "), _(" Setup saved to ~/%s"),
MC_USERCONF_DIR PATH_SEP_STR MC_CONFIG_FILE);
}

View File

@ -1487,8 +1487,10 @@ load_hotlist (void)
}
if (remove_old_list) {
GError *error = NULL;
clean_up_hotlist_groups ("Hotlist");
mc_config_save_file (mc_main_config);
if (! mc_config_save_file (mc_main_config, &error))
setup_save_config_show_error(mc_main_config->ini_path, &error);
}
stat (hotlist_file_name, &stat_buf);

View File

@ -548,7 +548,7 @@ void layout_cmd (void)
}
if (result == B_EXIT){
save_layout ();
mc_config_save_file (mc_main_config);
mc_config_save_file (mc_main_config, NULL);
}
destroy_dlg (layout_dlg);

View File

@ -334,7 +334,7 @@ learn_save (void)
* disk is much worse.
*/
if (profile_changed)
mc_config_save_file (mc_main_config);
mc_config_save_file (mc_main_config, NULL);
g_free (section);
}

View File

@ -23,6 +23,9 @@
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h> /* strerror() */
#include <errno.h> /* extern int errno */
#include "global.h"
@ -45,7 +48,8 @@ mc_config_t *mc_panels_config;
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
static gboolean
mc_config_new_or_override_file (mc_config_t * mc_config, const gchar * ini_path)
mc_config_new_or_override_file (mc_config_t * mc_config, const gchar * ini_path,
GError **error)
{
gchar *data, *written_data;
gsize len, total_written;
@ -55,15 +59,17 @@ mc_config_new_or_override_file (mc_config_t * mc_config, const gchar * ini_path)
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);
ret = g_file_set_contents (ini_path, data, len, error);
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)
if (fd == -1) {
g_propagate_error (error, g_error_new (mc_main_error_quark() ,0, strerror(errno)));
return FALSE;
}
for (written_data = data, total_written = len;
(cur_written = mc_write (fd, (const void *) written_data, total_written)) > 0;
@ -73,6 +79,7 @@ mc_config_new_or_override_file (mc_config_t * mc_config, const gchar * ini_path)
if (cur_written == -1) {
mc_util_restore_from_backup_if_possible (ini_path, "~");
g_propagate_error (error, g_error_new (mc_main_error_quark() ,0, strerror(errno)));
return FALSE;
}
@ -221,24 +228,24 @@ mc_config_read_file (mc_config_t * mc_config, const gchar * ini_path)
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
gboolean
mc_config_save_file (mc_config_t * mc_config)
mc_config_save_file (mc_config_t * mc_config, GError **error)
{
if (mc_config == NULL || mc_config->ini_path == NULL) {
return FALSE;
}
return mc_config_new_or_override_file (mc_config, mc_config->ini_path);
return mc_config_new_or_override_file (mc_config, mc_config->ini_path, error);
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
gboolean
mc_config_save_to_file (mc_config_t * mc_config, const gchar * ini_path)
mc_config_save_to_file (mc_config_t * mc_config, const gchar * ini_path, GError **error)
{
if (mc_config == NULL) {
return FALSE;
}
return mc_config_new_or_override_file (mc_config, ini_path);
return mc_config_new_or_override_file (mc_config, ini_path, error);
}

View File

@ -9,8 +9,7 @@
/*** structures declarations (and typedefs of structures)***************/
typedef struct mc_config_struct
{
typedef struct mc_config_struct {
GKeyFile *handle;
gchar *ini_path;
} mc_config_t;
@ -36,9 +35,10 @@ gboolean mc_config_has_group (mc_config_t *, const char *);
gboolean mc_config_read_file (mc_config_t *, const gchar *);
gboolean mc_config_save_file (mc_config_t *);
gboolean mc_config_save_file (mc_config_t * config, GError **error);
gboolean mc_config_save_to_file (mc_config_t *, const gchar *);
gboolean mc_config_save_to_file (mc_config_t * config, const gchar * filename,
GError **error);
/* mcconfig/get.c: */
@ -46,54 +46,44 @@ gchar **mc_config_get_groups (mc_config_t *, gsize *);
gchar **mc_config_get_keys (mc_config_t *, const gchar *, gsize *);
gchar *mc_config_get_string (mc_config_t *, const gchar *, const gchar *,
const gchar *);
gchar *mc_config_get_string (mc_config_t *, const gchar *, const gchar *, const gchar *);
gchar *mc_config_get_string_raw (mc_config_t *, const gchar *, const gchar *,
const gchar *);
gchar *mc_config_get_string_raw (mc_config_t *, const gchar *, const gchar *, const gchar *);
gboolean mc_config_get_bool (mc_config_t *, const gchar *, const gchar *,
gboolean);
gboolean mc_config_get_bool (mc_config_t *, const gchar *, const gchar *, gboolean);
int mc_config_get_int (mc_config_t *, const gchar *, const gchar *, int);
gchar **mc_config_get_string_list (mc_config_t *, const gchar *,
const gchar *, gsize *);
gchar **mc_config_get_string_list (mc_config_t *, const gchar *, const gchar *, gsize *);
gboolean *mc_config_get_bool_list (mc_config_t *, const gchar *,
const gchar *, gsize *);
gboolean *mc_config_get_bool_list (mc_config_t *, const gchar *, const gchar *, gsize *);
int *mc_config_get_int_list (mc_config_t *, const gchar *,
const gchar *, gsize *);
int *mc_config_get_int_list (mc_config_t *, const gchar *, const gchar *, gsize *);
/* mcconfig/set.c: */
void
mc_config_direct_set_string (mc_config_t *, const gchar *,
const gchar *, const gchar *);
mc_config_direct_set_string (mc_config_t *, const gchar *, const gchar *, const gchar *);
void
mc_config_set_string (mc_config_t *, const gchar *,
const gchar *, const gchar *);
mc_config_set_string (mc_config_t *, const gchar *, const gchar *, const gchar *);
void
mc_config_set_bool (mc_config_t *, const gchar *, const gchar *, gboolean);
mc_config_set_bool (mc_config_t *, const gchar *, const gchar *, gboolean);
void mc_config_set_int (mc_config_t *, const gchar *, const gchar *, int);
void
mc_config_set_string_list (mc_config_t *, const gchar *,
const gchar *, const gchar * const[], gsize);
mc_config_set_string_list (mc_config_t *, const gchar *,
const gchar *, const gchar * const[], gsize);
void
mc_config_set_bool_list (mc_config_t *, const gchar *,
const gchar *, gboolean[], gsize);
mc_config_set_bool_list (mc_config_t *, const gchar *, const gchar *, gboolean[], gsize);
void
mc_config_set_int_list (mc_config_t *, const gchar *,
const gchar *, int[], gsize);
mc_config_set_int_list (mc_config_t *, const gchar *, const gchar *, int[], gsize);
/* mcconfig/dialog.c: */

View File

@ -244,7 +244,7 @@ void configure_box (void)
/* If they pressed the save button */
if (result == B_EXIT){
save_configure ();
mc_config_save_file (mc_main_config);
mc_config_save_file (mc_main_config, NULL);
}
destroy_dlg (conf_dlg);

View File

@ -366,7 +366,7 @@ void save_panelize (void)
current->label,
current->command);
}
mc_config_save_file (mc_main_config);
mc_config_save_file (mc_main_config, NULL);
}
void done_panelize (void)

View File

@ -50,6 +50,7 @@
#include "file.h" /* safe_delete */
#include "keybind.h" /* lookup_action */
#include "fileloc.h"
#include "wtools.h"
#ifdef USE_VFS
#include "../vfs/gc.h"
@ -283,7 +284,7 @@ save_layout (void)
for (i = 0; layout [i].opt_name; i++){
mc_config_set_int(mc_main_config, "Layout", layout [i].opt_name, *layout [i].opt_addr);
}
mc_config_save_to_file (mc_main_config, profile);
mc_config_save_to_file (mc_main_config, profile, NULL);
g_free (profile);
}
@ -292,6 +293,7 @@ void
save_configure (void)
{
char *profile;
GError *error = NULL;
int i;
profile = g_build_filename (home_dir, MC_USERCONF_DIR, MC_CONFIG_FILE, NULL);
@ -304,7 +306,9 @@ save_configure (void)
for (i = 0; str_options[i].opt_name != NULL; i++)
mc_config_set_string(mc_main_config, CONFIG_APP_SECTION, str_options[i].opt_name, *str_options[i].opt_addr);
mc_config_save_to_file (mc_main_config, profile);
if (! mc_config_save_to_file (mc_main_config, profile, &error))
setup_save_config_show_error(profile, &error);
g_free (profile);
}
@ -348,13 +352,14 @@ save_panel_types (void)
mc_config_del_group (mc_panels_config, "Temporal:New Left Panel");
mc_config_del_group (mc_panels_config, "Temporal:New Right Panel");
mc_config_save_file (mc_panels_config);
mc_config_save_file (mc_panels_config, NULL);
}
void
gboolean
save_setup (void)
{
char *tmp_profile;
gboolean ret;
saving_setup = 1;
@ -383,9 +388,11 @@ save_setup (void)
get_codepage_id( source_codepage ));
#endif /* HAVE_CHARSET */
tmp_profile = g_build_filename (home_dir, MC_USERCONF_DIR, MC_CONFIG_FILE, NULL);
mc_config_save_to_file (mc_main_config, tmp_profile);
ret = mc_config_save_to_file (mc_main_config, tmp_profile, NULL);
g_free (tmp_profile);
saving_setup = 0;
return ret;
}
void
@ -504,7 +511,7 @@ setup__move_panels_config_into_separate_file(const char*profile)
curr_grp++;
}
mc_config_save_to_file (tmp_cfg, panels_profile_name);
mc_config_save_to_file (tmp_cfg, panels_profile_name, NULL);
mc_config_deinit(tmp_cfg);
tmp_cfg = mc_config_init(profile);
@ -526,7 +533,7 @@ setup__move_panels_config_into_separate_file(const char*profile)
curr_grp++;
}
g_strfreev(groups);
mc_config_save_file (tmp_cfg);
mc_config_save_file (tmp_cfg, NULL);
mc_config_deinit(tmp_cfg);
}
@ -993,3 +1000,16 @@ load_keymap_defs (void)
mc_config_deinit (mc_global_keymap);
}
}
void
setup_save_config_show_error(const char *filename, GError **error)
{
if (error == NULL || *error == NULL)
return;
message (D_ERROR, MSG_ERROR, _("Cannot save file %s:\n%s"),
filename, (*error)->message);
g_error_free(*error);
*error = NULL;
}

View File

@ -12,7 +12,7 @@ char *setup_init (void);
void save_layout (void);
void save_configure (void);
void load_setup (void);
void save_setup (void);
gboolean save_setup (void);
void done_setup (void);
void load_key_defs (void);
char *load_anon_passwd (void);
@ -36,5 +36,6 @@ extern int mouse_close_dialog;
extern int reverse_files_only;
extern int setup_copymove_persistent_attr;
void setup_save_config_show_error(const char *filename, GError **error);
#endif

View File

@ -989,7 +989,7 @@ history_put (const char *input_name, GList *h)
}
}
mc_config_save_file (cfg);
mc_config_save_file (cfg, NULL);
mc_config_deinit(cfg);
g_free (profile);
}