1
1

Ticket #1617: ownership of files ~/.mc

Test case:

$ sudo bash
files in the directory ~/.mc overwritten and hence change of ownership at the root.
Later, when starting from a user, MC can not save history (the file ~/.mc/history),
because this file is not overwritten.

Fix issue: Now files will overwrite if exists (rather than remove and create new file).
 * Added backup of saved files.  If someone wrong in 'write config' stage, backup file still present.
 * Identation of code.

Signed-off-by: Slava Zanko <slavazanko@gmail.com>
Этот коммит содержится в:
Slava Zanko 2009-10-05 13:15:30 +03:00
родитель 669ac422f5
Коммит a931ff73cc

Просмотреть файл

@ -22,10 +22,11 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include "global.h"
#include "../../vfs/vfs.h" /* mc_stat */
#include "../../vfs/vfs.h" /* mc_stat */
#include "mcconfig.h"
@ -41,8 +42,94 @@ mc_config_t *mc_panels_config;
/*** file scope variables **********************************************/
/*** file scope functions **********************************************/
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
static void
mc_config_make_backup_if_possible (const gchar * ini_path)
{
char *backup_path, *contents;
gsize length;
if (!g_file_get_contents (ini_path, &contents, &length, NULL))
return;
/* If length of main file is 0, then do nothing */
if (length == 0) {
g_free (contents);
return;
}
backup_path = g_strdup_printf ("%s~", ini_path);
if (backup_path == NULL) {
g_free (contents);
return;
}
/* if backup file not exists, then do backup */
if (!exist_file (backup_path))
g_file_set_contents (backup_path, contents, length, NULL);
g_free (contents);
g_free (backup_path);
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
static void
mc_config_remove_backup_if_possible (const gchar * ini_path)
{
char *backup_path;
backup_path = g_strdup_printf ("%s~", ini_path);
if (backup_path == NULL)
return;
if (exist_file (backup_path))
mc_unlink (backup_path);
g_free (backup_path);
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
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_config_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)
return FALSE;
mc_config_remove_backup_if_possible (ini_path);
return TRUE;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/*** public functions **************************************************/
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
mc_config_t *
mc_config_init (const gchar * ini_path)
@ -53,24 +140,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 +166,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 +177,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 +191,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 +200,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 +218,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 +238,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 +270,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 +281,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;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */