1
0
mirror of git://sourceware.org/git/lvm2.git synced 2025-01-18 10:04:20 +03:00

Move is_empty_dir to lvm-file

This commit is contained in:
Alasdair Kergon 2002-03-11 22:23:24 +00:00
parent bb13858120
commit 251502f9a1
3 changed files with 38 additions and 55 deletions

View File

@ -8,6 +8,7 @@
#include "log.h" #include "log.h"
#include "toolcontext.h" #include "toolcontext.h"
#include "lvm-string.h" #include "lvm-string.h"
#include "lvm-file.h"
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
@ -18,13 +19,7 @@
#include <string.h> #include <string.h>
#include <libdevmapper.h> #include <libdevmapper.h>
#include <dirent.h>
/*
* 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) static int _mk_dir(struct volume_group *vg)
{ {
char vg_path[PATH_MAX]; char vg_path[PATH_MAX];
@ -36,51 +31,16 @@ static int _mk_dir(struct volume_group *vg)
return 0; return 0;
} }
if (dir_exists(vg_path))
return 1;
log_very_verbose("Creating directory %s", vg_path); log_very_verbose("Creating directory %s", vg_path);
mkdir(vg_path, 0555); if (mkdir(vg_path, 0555)) {
log_sys_error("mkdir", vg_path);
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)
return 1;
if (count < 0) {
log_err("Couldn't scan directory '%s'.", dir);
return 0; return 0;
} }
/* return 1;
* 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;
} }
static int _rm_dir(struct volume_group *vg) 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); log_very_verbose("Removing directory %s", vg_path);
if (_is_empty_dir(vg_path)) if (is_empty_dir(vg_path))
rmdir(vg_path); rmdir(vg_path);
return 1; return 1;

View File

@ -14,6 +14,7 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/file.h> #include <sys/file.h>
#include <fcntl.h> #include <fcntl.h>
#include <dirent.h>
/* /*
* Creates a temporary filename, and opens a descriptor to the * Creates a temporary filename, and opens a descriptor to the
@ -21,8 +22,7 @@
* rename the file after successfully writing it. Grab * rename the file after successfully writing it. Grab
* NFS-supported exclusive fcntl discretionary lock. * NFS-supported exclusive fcntl discretionary lock.
*/ */
int create_temp_name(const char *dir, char *buffer, size_t len, int create_temp_name(const char *dir, char *buffer, size_t len, int *fd)
int *fd)
{ {
int i, num; int i, num;
pid_t pid; pid_t pid;
@ -99,7 +99,6 @@ int lvm_rename(const char *old, const char *new)
return 1; return 1;
} }
int path_exists(const char *path) int path_exists(const char *path)
{ {
struct stat info; struct stat info;
@ -129,7 +128,6 @@ int dir_exists(const char *path)
return 1; return 1;
} }
/* FIXME: Make this create directories recursively */ /* FIXME: Make this create directories recursively */
int create_dir(const char *dir) int create_dir(const char *dir)
{ {
@ -153,3 +151,23 @@ int create_dir(const char *dir)
return 0; 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;
}

View File

@ -22,6 +22,11 @@ int lvm_rename(const char *old, const char *new);
int path_exists(const char *path); int path_exists(const char *path);
int dir_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 * Create directory (but not recursively) if necessary
* Return 1 if directory exists on return, else 0 * Return 1 if directory exists on return, else 0