[sheepdog] [PATCH 2/8] util: add a helper to purge directory
Liu Yuan
namei.unix at gmail.com
Sun Mar 10 15:19:23 CET 2013
From: Liu Yuan <tailai.ly at taobao.com>
This is preparation patch for raid series.
Signed-off-by: Liu Yuan <tailai.ly at taobao.com>
---
include/util.h | 1 +
lib/util.c | 79 ++++++++++++++++++++++++++++++--------------------------
2 files changed, 44 insertions(+), 36 deletions(-)
diff --git a/include/util.h b/include/util.h
index 3307d7e..eae4e9a 100644
--- a/include/util.h
+++ b/include/util.h
@@ -84,6 +84,7 @@ ssize_t xpread(int fd, void *buf, size_t count, off_t offset);
ssize_t xpwrite(int fd, const void *buf, size_t count, off_t offset);
void pstrcpy(char *buf, int buf_size, const char *str);
int rmdir_r(char *dir_path);
+int purge_directory(char *dir_path);
bool is_numeric(const char *p);
int install_sighandler(int signum, void (*handler)(int), bool once);
int install_crash_handler(void (*handler)(int));
diff --git a/lib/util.c b/lib/util.c
index 41396da..360e568 100644
--- a/lib/util.c
+++ b/lib/util.c
@@ -255,51 +255,58 @@ void notrace pstrcpy(char *buf, int buf_size, const char *str)
*q = '\0';
}
+#define RMDIR_PROPER \
+ int ret;\
+ struct stat s;\
+ DIR *dir;\
+ struct dirent *d;\
+ char path[PATH_MAX];\
+ dir = opendir(dir_path);\
+ if (!dir) {\
+ if (errno != ENOENT)\
+ sd_eprintf("failed to open %s: %m", dir_path);\
+ return -errno;\
+ }\
+ while ((d = readdir(dir))) {\
+ if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))\
+ continue;\
+ snprintf(path, sizeof(path), "%s/%s", dir_path, d->d_name);\
+ ret = stat(path, &s);\
+ if (ret) {\
+ sd_eprintf("failed to stat %s: %m", path);\
+ goto out;\
+ }\
+ if (S_ISDIR(s.st_mode))\
+ ret = rmdir_r(path);\
+ else\
+ ret = unlink(path);\
+ if (ret != 0) {\
+ sd_eprintf("failed to remove %s %s: %m",\
+ S_ISDIR(s.st_mode) ? "directory" : "file",\
+ path);\
+ goto out;\
+ }\
+ }
+
/* remove directory recursively */
int rmdir_r(char *dir_path)
{
- int ret;
- struct stat s;
- DIR *dir;
- struct dirent *d;
- char path[PATH_MAX];
-
- dir = opendir(dir_path);
- if (!dir) {
- if (errno != ENOENT)
- sd_eprintf("failed to open %s: %m", dir_path);
- return -errno;
- }
-
- while ((d = readdir(dir))) {
- if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
- continue;
-
- snprintf(path, sizeof(path), "%s/%s", dir_path, d->d_name);
- ret = stat(path, &s);
- if (ret) {
- sd_eprintf("failed to stat %s: %m", path);
- goto out;
- }
- if (S_ISDIR(s.st_mode))
- ret = rmdir_r(path);
- else
- ret = unlink(path);
-
- if (ret != 0) {
- sd_eprintf("failed to remove %s %s: %m",
- S_ISDIR(s.st_mode) ? "directory" : "file",
- path);
- goto out;
- }
- }
-
+ RMDIR_PROPER;
ret = rmdir(dir_path);
out:
closedir(dir);
return ret;
}
+/* Purge directory recursively */
+int purge_directory(char *dir_path)
+{
+ RMDIR_PROPER;
+out:
+ closedir(dir);
+ return ret;
+}
+
/* Trim zero sectors from the beginning and end of buffer */
void trim_zero_sectors(void *buf, uint64_t *offset, uint32_t *len)
{
--
1.7.9.5
More information about the sheepdog
mailing list