summaryrefslogtreecommitdiffstats
path: root/libglusterfs
diff options
context:
space:
mode:
Diffstat (limited to 'libglusterfs')
-rw-r--r--libglusterfs/src/common-utils.c72
-rw-r--r--libglusterfs/src/common-utils.h17
2 files changed, 89 insertions, 0 deletions
diff --git a/libglusterfs/src/common-utils.c b/libglusterfs/src/common-utils.c
index b1afa082844..3bd7c3d8fa2 100644
--- a/libglusterfs/src/common-utils.c
+++ b/libglusterfs/src/common-utils.c
@@ -3558,3 +3558,75 @@ err:
GF_FREE (absolute_path);
return ret;
}
+
+/* This is an utility function which will recursively delete
+ * a folder and its contents.
+ *
+ * @param delete_path folder to be deleted.
+ *
+ * @return 0 on success and -1 on failure.
+ */
+int
+recursive_rmdir (const char *delete_path)
+{
+ int ret = -1;
+ char path[PATH_MAX] = {0,};
+ struct stat st = {0,};
+ DIR *dir = NULL;
+ struct dirent *entry = NULL;
+ xlator_t *this = NULL;
+
+ this = THIS;
+ GF_ASSERT (this);
+ GF_VALIDATE_OR_GOTO (this->name, delete_path, out);
+
+ dir = opendir (delete_path);
+ if (!dir) {
+ gf_log (this->name, GF_LOG_DEBUG, "Failed to open directory %s."
+ " Reason : %s", delete_path, strerror (errno));
+ ret = 0;
+ goto out;
+ }
+
+ GF_FOR_EACH_ENTRY_IN_DIR (entry, dir);
+ while (entry) {
+ snprintf (path, PATH_MAX, "%s/%s", delete_path, entry->d_name);
+ ret = lstat (path, &st);
+ if (ret == -1) {
+ gf_log (this->name, GF_LOG_DEBUG, "Failed to stat "
+ "entry %s : %s", path, strerror (errno));
+ goto out;
+ }
+
+ if (S_ISDIR (st.st_mode))
+ ret = recursive_rmdir (path);
+ else
+ ret = unlink (path);
+
+ if (ret) {
+ gf_log (this->name, GF_LOG_DEBUG, " Failed to remove "
+ "%s. Reason : %s", path, strerror (errno));
+ }
+
+ gf_log (this->name, GF_LOG_DEBUG, "%s %s",
+ ret ? "Failed to remove":"Removed",
+ entry->d_name);
+
+ GF_FOR_EACH_ENTRY_IN_DIR (entry, dir);
+ }
+
+ ret = closedir (dir);
+ if (ret) {
+ gf_log (this->name, GF_LOG_DEBUG, "Failed to close dir %s. "
+ "Reason : %s", delete_path, strerror (errno));
+ }
+
+ ret = rmdir (delete_path);
+ if (ret) {
+ gf_log (this->name, GF_LOG_DEBUG, "Failed to rmdir: %s,err: %s",
+ delete_path, strerror (errno));
+ }
+
+out:
+ return ret;
+}
diff --git a/libglusterfs/src/common-utils.h b/libglusterfs/src/common-utils.h
index 23a932b71aa..2a46f768e75 100644
--- a/libglusterfs/src/common-utils.h
+++ b/libglusterfs/src/common-utils.h
@@ -28,6 +28,7 @@
#include <alloca.h>
#endif
#include <limits.h>
+#include <fnmatch.h>
void trap (void);
@@ -273,6 +274,19 @@ union gf_sock_union {
#define IOV_MIN(n) min(IOV_MAX,n)
+#define GF_FOR_EACH_ENTRY_IN_DIR(entry, dir) \
+ do {\
+ entry = NULL;\
+ if (dir) { \
+ entry = readdir (dir); \
+ while (entry && (!strcmp (entry->d_name, ".") || \
+ !fnmatch ("*.tmp", entry->d_name, 0) || \
+ !strcmp (entry->d_name, ".."))) { \
+ entry = readdir (dir); \
+ } \
+ } \
+ } while (0)
+
static inline void
iov_free (struct iovec *vector, int count)
{
@@ -660,4 +674,7 @@ fop_log_level (glusterfs_fop_t fop, int op_errno);
int32_t
gf_build_absolute_path (char *current_path, char *relative_path, char **path);
+int
+recursive_rmdir (const char *delete_path);
+
#endif /* _COMMON_UTILS_H */