+/*
+ * Remove given delta save directory (if any).
+ */
+static void
+rpc_uci_purge_dir(const char *path)
+{
+ DIR *d;
+ struct stat s;
+ struct dirent *e;
+ char file[PATH_MAX];
+
+ if (stat(path, &s) || !S_ISDIR(s.st_mode))
+ return;
+
+ if ((d = opendir(path)) != NULL)
+ {
+ while ((e = readdir(d)) != NULL)
+ {
+ snprintf(file, sizeof(file) - 1, "%s/%s", path, e->d_name);
+
+ if (stat(file, &s) || !S_ISREG(s.st_mode))
+ continue;
+
+ unlink(file);
+ }
+
+ closedir(d);
+
+ rmdir(path);
+ }
+}
+
+/*
+ * Session destroy callback to purge associated delta directory.
+ */
+static void
+rpc_uci_purge_savedir_cb(struct rpc_session *ses, void *priv)
+{
+ char path[PATH_MAX];
+
+ snprintf(path, sizeof(path) - 1, RPC_UCI_SAVEDIR_PREFIX "%s", ses->id);
+ rpc_uci_purge_dir(path);
+}
+
+/*
+ * Removes all delta directories which match the RPC_UCI_SAVEDIR_PREFIX.
+ * This is used to clean up garbage when starting rpcd.
+ */
+void rpc_uci_purge_savedirs(void)
+{
+ int i;
+ glob_t gl;
+
+ if (!glob(RPC_UCI_SAVEDIR_PREFIX "*", 0, NULL, &gl))
+ {
+ for (i = 0; i < gl.gl_pathc; i++)
+ rpc_uci_purge_dir(gl.gl_pathv[i]);
+
+ globfree(&gl);
+ }
+}
+