summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2021-06-25 09:48:48 +0200
committerThomas Haller <thaller@redhat.com>2021-07-01 11:20:59 +0200
commit080d7654476c0e7cc4c0a5baa801a2d22972ed1d (patch)
treed76ef6ad2347d976b5e83e2b0b7792a9df1b7442
parent2e720a1dc8b488969d80150487a733d462ccfacb (diff)
downloadNetworkManager-080d7654476c0e7cc4c0a5baa801a2d22972ed1d.tar.gz
glib-aux: add nm_utils_find_mkstemp_files()
-rw-r--r--src/libnm-glib-aux/nm-io-utils.c64
-rw-r--r--src/libnm-glib-aux/nm-io-utils.h2
2 files changed, 66 insertions, 0 deletions
diff --git a/src/libnm-glib-aux/nm-io-utils.c b/src/libnm-glib-aux/nm-io-utils.c
index 894c8726c0..87478a2dc2 100644
--- a/src/libnm-glib-aux/nm-io-utils.c
+++ b/src/libnm-glib-aux/nm-io-utils.c
@@ -564,3 +564,67 @@ nm_g_subprocess_terminate_in_background(GSubprocess *subprocess, int timeout_mse
NULL),
main_context);
}
+
+/*****************************************************************************/
+
+char **
+nm_utils_find_mkstemp_files(const char *dirname, const char *filename)
+{
+ static const char letters[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
+ DIR * dir;
+ struct dirent * entry;
+ GPtrArray * arr = NULL;
+ gsize l;
+
+ /* We write files with g_file_set_contents() and nm_utils_file_set_contents().
+ * These create temporary files using g_mkstemp_full(), with a random .XXXXXX suffix.
+ *
+ * If NetworkManager crashes while writing the file, then those temporary files are
+ * left over. We might want to find and delete such files.
+ *
+ * Beware: only delete such files if you are in full control about which files are
+ * supposed to be in the directory. For example, NetworkManager controls
+ * /var/lib/NetworkManager/timestamps files, and it thus takes the right to delete
+ * all files /var/lib/NetworkManager/timestamps.XXXXXX. That may not be appropriate
+ * in other cases! */
+
+ if (!dirname || !filename || !filename[0])
+ return NULL;
+
+ dir = opendir(dirname);
+ if (!dir)
+ return NULL;
+
+ l = strlen(filename);
+
+ while ((entry = readdir(dir))) {
+ const char *f = entry->d_name;
+ guint i;
+
+ if (strncmp(f, filename, l) != 0)
+ goto next;
+ if (f[l] != '.')
+ goto next;
+ for (i = 1; i <= 6; i++) {
+ /* @letters is also what g_mkstemp_full() does! */
+ if (!memchr(letters, f[l + i], G_N_ELEMENTS(letters)))
+ goto next;
+ }
+ if (f[l + 7] != '\0')
+ goto next;
+
+ if (!arr)
+ arr = g_ptr_array_new();
+
+ g_ptr_array_add(arr, g_strdup(f));
+next:;
+ }
+
+ closedir(dir);
+
+ if (!arr)
+ return NULL;
+
+ g_ptr_array_add(arr, NULL);
+ return (char **) g_ptr_array_free(arr, FALSE);
+}
diff --git a/src/libnm-glib-aux/nm-io-utils.h b/src/libnm-glib-aux/nm-io-utils.h
index 98e63ac01e..31ff6d0569 100644
--- a/src/libnm-glib-aux/nm-io-utils.h
+++ b/src/libnm-glib-aux/nm-io-utils.h
@@ -58,4 +58,6 @@ int nm_utils_file_stat(const char *filename, struct stat *out_st);
void nm_g_subprocess_terminate_in_background(GSubprocess *subprocess, int timeout_msec_before_kill);
+char **nm_utils_find_mkstemp_files(const char *dirname, const char *filename);
+
#endif /* __NM_IO_UTILS_H__ */