mirror of
https://gitlab.freedesktop.org/NetworkManager/NetworkManager.git
synced 2026-05-05 18:08:05 +02:00
cli: use cleanup attribute in save_history_cmds()
This commit is contained in:
parent
f6b1b2401d
commit
67283b4932
1 changed files with 32 additions and 37 deletions
|
|
@ -6329,49 +6329,44 @@ load_history_cmds (const char *uuid)
|
||||||
static void
|
static void
|
||||||
save_history_cmds (const char *uuid)
|
save_history_cmds (const char *uuid)
|
||||||
{
|
{
|
||||||
HIST_ENTRY **hist = NULL;
|
gs_unref_keyfile GKeyFile *kf = NULL;
|
||||||
GKeyFile *kf;
|
gs_free_error GError *error = NULL;
|
||||||
char *filename;
|
gs_free char *filename = NULL;
|
||||||
size_t i;
|
gs_free char *data = NULL;
|
||||||
char *key;
|
HIST_ENTRY **hist;
|
||||||
char *data;
|
gsize len;
|
||||||
gsize len = 0;
|
gsize i;
|
||||||
GError *err = NULL;
|
|
||||||
|
|
||||||
hist = history_list ();
|
hist = history_list ();
|
||||||
if (hist) {
|
if (!hist)
|
||||||
filename = g_build_filename (g_get_home_dir (), NMCLI_EDITOR_HISTORY, NULL);
|
return;
|
||||||
kf = g_key_file_new ();
|
|
||||||
if (!g_key_file_load_from_file (kf, filename, G_KEY_FILE_KEEP_COMMENTS, &err)) {
|
|
||||||
if ( !g_error_matches (err, G_FILE_ERROR, G_FILE_ERROR_NOENT)
|
|
||||||
&& !g_error_matches (err, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_NOT_FOUND)) {
|
|
||||||
g_print ("Warning: %s parse error: %s\n", filename, err->message);
|
|
||||||
g_key_file_free (kf);
|
|
||||||
g_free (filename);
|
|
||||||
g_clear_error (&err);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
g_clear_error (&err);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Remove previous history group and save new history entries */
|
filename = g_build_filename (g_get_home_dir (), NMCLI_EDITOR_HISTORY, NULL);
|
||||||
g_key_file_remove_group (kf, uuid, NULL);
|
|
||||||
for (i = 0; hist[i]; i++)
|
|
||||||
{
|
|
||||||
key = g_strdup_printf ("%zd", i);
|
|
||||||
g_key_file_set_string (kf, uuid, key, hist[i]->line);
|
|
||||||
g_free (key);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Write history to file */
|
kf = g_key_file_new ();
|
||||||
data = g_key_file_to_data (kf, &len, NULL);
|
|
||||||
if (data) {
|
if (!g_key_file_load_from_file (kf, filename, G_KEY_FILE_KEEP_COMMENTS, &error)) {
|
||||||
g_file_set_contents (filename, data, len, NULL);
|
if ( !g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT)
|
||||||
g_free (data);
|
&& !g_error_matches (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_NOT_FOUND)) {
|
||||||
|
g_print ("Warning: %s parse error: %s\n", filename, error->message);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
g_key_file_free (kf);
|
g_clear_error (&error);
|
||||||
g_free (filename);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Remove previous history group and save new history entries */
|
||||||
|
g_key_file_remove_group (kf, uuid, NULL);
|
||||||
|
for (i = 0; hist[i]; i++) {
|
||||||
|
char key[100];
|
||||||
|
|
||||||
|
nm_sprintf_buf (key, "%zd", i);
|
||||||
|
g_key_file_set_string (kf, uuid, key, hist[i]->line);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Write history to file */
|
||||||
|
data = g_key_file_to_data (kf, &len, NULL);
|
||||||
|
if (data)
|
||||||
|
g_file_set_contents (filename, data, len, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*****************************************************************************/
|
/*****************************************************************************/
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue