diff --git a/lib/activate/fs.c b/lib/activate/fs.c index 3edfb48a2..f23d78499 100644 --- a/lib/activate/fs.c +++ b/lib/activate/fs.c @@ -8,6 +8,7 @@ #include "log.h" #include "toolcontext.h" #include "lvm-string.h" +#include "lvm-file.h" #include #include @@ -18,13 +19,7 @@ #include #include -#include -/* - * Lazy programmer: I'm just going to always try - * and create/remove the vg directory, and not say - * anything if it fails. - */ static int _mk_dir(struct volume_group *vg) { char vg_path[PATH_MAX]; @@ -36,51 +31,16 @@ static int _mk_dir(struct volume_group *vg) return 0; } - log_very_verbose("Creating directory %s", vg_path); - mkdir(vg_path, 0555); - - return 1; -} - -static int _is_empty_dir(const char *dir) -{ - int i, count, r = 1; - struct dirent **dirent; - const char *name; - - count = scandir(dir, &dirent, NULL, alphasort); - if (!count) + if (dir_exists(vg_path)) return 1; - if (count < 0) { - log_err("Couldn't scan directory '%s'.", dir); + log_very_verbose("Creating directory %s", vg_path); + if (mkdir(vg_path, 0555)) { + log_sys_error("mkdir", vg_path); return 0; } - /* - * Scan the devices. - */ - for (i = 0; i < count; i++) { - name = dirent[i]->d_name; - - /* - * Ignore dot files. - */ - if (!strcmp(name, ".") || !strcmp(name, "..")) - continue; - - r = 0; - break; - } - - /* - * Free the directory entries. - */ - for (i = 0; i < count; i++) - free(dirent[i]); - free(dirent); - - return r; + return 1; } static int _rm_dir(struct volume_group *vg) @@ -96,7 +56,7 @@ static int _rm_dir(struct volume_group *vg) log_very_verbose("Removing directory %s", vg_path); - if (_is_empty_dir(vg_path)) + if (is_empty_dir(vg_path)) rmdir(vg_path); return 1; diff --git a/lib/misc/lvm-file.c b/lib/misc/lvm-file.c index f0ae9aad8..cbe446985 100644 --- a/lib/misc/lvm-file.c +++ b/lib/misc/lvm-file.c @@ -14,6 +14,7 @@ #include #include #include +#include /* * Creates a temporary filename, and opens a descriptor to the @@ -21,17 +22,16 @@ * rename the file after successfully writing it. Grab * NFS-supported exclusive fcntl discretionary lock. */ -int create_temp_name(const char *dir, char *buffer, size_t len, - int *fd) +int create_temp_name(const char *dir, char *buffer, size_t len, int *fd) { int i, num; pid_t pid; char hostname[255]; struct flock lock = { - l_type: F_WRLCK, - l_whence: 0, - l_start: 0, - l_len: 0 + l_type:F_WRLCK, + l_whence:0, + l_start:0, + l_len:0 }; num = rand(); @@ -99,7 +99,6 @@ int lvm_rename(const char *old, const char *new) return 1; } - int path_exists(const char *path) { struct stat info; @@ -129,7 +128,6 @@ int dir_exists(const char *path) return 1; } - /* FIXME: Make this create directories recursively */ int create_dir(const char *dir) { @@ -153,3 +151,23 @@ int create_dir(const char *dir) return 0; } +int is_empty_dir(const char *dir) +{ + struct dirent *dirent; + DIR *d; + + if (!(d = opendir(dir))) { + log_sys_error("opendir", dir); + return 0; + } + + while ((dirent = readdir(d))) + if (strcmp(dirent->d_name, ".") && strcmp(dirent->d_name, "..")) + break; + + if (closedir(d)) { + log_sys_error("closedir", dir); + } + + return dirent ? 0 : 1; +} diff --git a/lib/misc/lvm-file.h b/lib/misc/lvm-file.h index 3c3e9072c..c6915e032 100644 --- a/lib/misc/lvm-file.h +++ b/lib/misc/lvm-file.h @@ -22,6 +22,11 @@ int lvm_rename(const char *old, const char *new); int path_exists(const char *path); int dir_exists(const char *path); +/* + * Return 1 if dir is empty + */ +int is_empty_dir(const char *dir); + /* * Create directory (but not recursively) if necessary * Return 1 if directory exists on return, else 0