+/* g->tmpdir can contain any files (but not subdirectories). Remove
+ * those and the directory itself. Note that errors in this function
+ * aren't really that important: if we end up not deleting temporary
+ * files it's only annoying.
+ */
+static void
+remove_tmpdir (guestfs_h *g)
+{
+ DIR *dir;
+ struct dirent *d;
+
+ if (!g->tmpdir)
+ return;
+
+ dir = opendir (g->tmpdir);
+ if (dir == NULL) {
+ perror (g->tmpdir);
+ return;
+ }
+
+ while ((d = readdir (dir)) != NULL) {
+ if (STRNEQ (d->d_name, ".") && STRNEQ (d->d_name, "..")) {
+ if (unlinkat (dirfd (dir), d->d_name, 0) == -1)
+ perror (d->d_name);
+ }
+ }
+
+ if (closedir (dir) == -1)
+ perror (g->tmpdir);
+
+ if (rmdir (g->tmpdir) == -1)
+ perror (g->tmpdir);
+
+ free (g->tmpdir);
+ g->tmpdir = NULL;
+}
+