From 4b942e95194b858902137839fddf9654d7eb48e8 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 25 Jun 2021 09:54:17 +0200 Subject: [PATCH] glib-aux: add nm_key_file_db_prune_tmp_files() helper (cherry picked from commit 3c0f1eb0fd15109fbac29b23d4240a93c0809333) --- src/libnm-glib-aux/nm-keyfile-aux.c | 40 +++++++++++++++++++++++++++++ src/libnm-glib-aux/nm-keyfile-aux.h | 2 ++ 2 files changed, 42 insertions(+) diff --git a/src/libnm-glib-aux/nm-keyfile-aux.c b/src/libnm-glib-aux/nm-keyfile-aux.c index 5d2c0028f7..9cda1cf714 100644 --- a/src/libnm-glib-aux/nm-keyfile-aux.c +++ b/src/libnm-glib-aux/nm-keyfile-aux.c @@ -385,6 +385,46 @@ nm_key_file_db_to_file(NMKeyFileDB *self, gboolean force) /*****************************************************************************/ +void +nm_key_file_db_prune_tmp_files(NMKeyFileDB *self) +{ + gs_free char * n_file = NULL; + gs_free char * n_dir = NULL; + gs_strfreev char **tmpfiles = NULL; + gsize i; + + n_file = g_path_get_basename(self->filename); + n_dir = g_path_get_dirname(self->filename); + + tmpfiles = nm_utils_find_mkstemp_files(n_dir, n_file); + if (!tmpfiles) + return; + + for (i = 0; tmpfiles[i]; i++) { + const char * tmpfile = tmpfiles[i]; + gs_free char *full_file = NULL; + int r; + + full_file = g_strdup_printf("%s/%s", n_dir, tmpfile); + + r = unlink(full_file); + if (r != 0) { + int errsv = errno; + + if (errsv != ENOENT) { + _LOGD("prune left over temp file %s failed: %s", + full_file, + nm_strerror_native(errsv)); + } + continue; + } + + _LOGD("prune left over temp file %s", full_file); + } +} + +/*****************************************************************************/ + void nm_key_file_db_prune(NMKeyFileDB *self, gboolean (*predicate)(const char *key, gpointer user_data), diff --git a/src/libnm-glib-aux/nm-keyfile-aux.h b/src/libnm-glib-aux/nm-keyfile-aux.h index c4d4e65111..e756c57a84 100644 --- a/src/libnm-glib-aux/nm-keyfile-aux.h +++ b/src/libnm-glib-aux/nm-keyfile-aux.h @@ -50,6 +50,8 @@ void nm_key_file_db_set_string_list(NMKeyFileDB * self, void nm_key_file_db_to_file(NMKeyFileDB *self, gboolean force); +void nm_key_file_db_prune_tmp_files(NMKeyFileDB *self); + void nm_key_file_db_prune(NMKeyFileDB *self, gboolean (*predicate)(const char *key, gpointer user_data), gpointer user_data);