summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2021-06-25 09:54:17 +0200
committerThomas Haller <thaller@redhat.com>2021-07-01 11:20:59 +0200
commit3c0f1eb0fd15109fbac29b23d4240a93c0809333 (patch)
tree2832d2ea765c26f38ab53823223848f3ad487d33
parent080d7654476c0e7cc4c0a5baa801a2d22972ed1d (diff)
downloadNetworkManager-3c0f1eb0fd15109fbac29b23d4240a93c0809333.tar.gz
glib-aux: add nm_key_file_db_prune_tmp_files() helper
-rw-r--r--src/libnm-glib-aux/nm-keyfile-aux.c40
-rw-r--r--src/libnm-glib-aux/nm-keyfile-aux.h2
2 files changed, 42 insertions, 0 deletions
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
@@ -386,6 +386,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),
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);