1
0
mirror of git://sourceware.org/git/lvm2.git synced 2024-12-21 13:34:40 +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 "toolcontext.h"
#include "lvm-string.h"
#include "lvm-file.h"
#include <sys/types.h>
#include <sys/stat.h>
@ -18,13 +19,7 @@
#include <string.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)
{
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;

View File

@ -14,6 +14,7 @@
#include <sys/stat.h>
#include <sys/file.h>
#include <fcntl.h>
#include <dirent.h>
/*
* 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;
}

View File

@ -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