mirror of
git://sourceware.org/git/lvm2.git
synced 2025-02-03 17:47:44 +03:00
o I decided that the archive_format shouldn't really be a format at
all since it only supports vg_write. It has been replaced with: int archive_vg(struct volume_group *vg, const char *dir, const char *desc, uint32_t retain_days, uint32_t min_archive); which is now called directly by tools/archive.c
This commit is contained in:
parent
c2ed40a74f
commit
138a27570b
@ -34,98 +34,18 @@
|
||||
* Backup files that have expired will be removed.
|
||||
*/
|
||||
|
||||
struct archive_c {
|
||||
uint32_t retain_days;
|
||||
uint32_t min_retains;
|
||||
|
||||
char *dir;
|
||||
char *desc;
|
||||
|
||||
/*
|
||||
* An ordered list of previous archives. Each list
|
||||
* entered against the vg name. Most recent first.
|
||||
*/
|
||||
struct hash_table *vg_archives;
|
||||
|
||||
/*
|
||||
* Scratch pool. Contents of vg_archives come from here.
|
||||
*/
|
||||
struct pool *mem;
|
||||
};
|
||||
|
||||
/*
|
||||
* A list of these is built up for each volume group. Ordered
|
||||
* A list of these is built up for our volume group. Ordered
|
||||
* with the least recent at the head.
|
||||
*/
|
||||
struct archive_file {
|
||||
struct list list;
|
||||
|
||||
char *path;
|
||||
char *vg;
|
||||
int index;
|
||||
};
|
||||
|
||||
/*
|
||||
* This format is write only.
|
||||
*/
|
||||
static void _unsupported(const char *cmd)
|
||||
{
|
||||
log_err("The archive format doesn't support '%s'", cmd);
|
||||
}
|
||||
|
||||
static struct list *_get_vgs(struct format_instance *fi)
|
||||
{
|
||||
_unsupported("get_vgs");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static struct list *_get_pvs(struct format_instance *fi)
|
||||
{
|
||||
_unsupported("get_pvs");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static struct physical_volume *_pv_read(struct format_instance *fi,
|
||||
const char *pv_name)
|
||||
{
|
||||
_unsupported("pv_read");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static int _pv_setup(struct format_instance *fi, struct physical_volume *pv,
|
||||
struct volume_group *vg)
|
||||
{
|
||||
_unsupported("pv_setup");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int _pv_write(struct format_instance *fi, struct physical_volume *pv)
|
||||
{
|
||||
_unsupported("pv_write");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int _vg_setup(struct format_instance *fi, struct volume_group *vg)
|
||||
{
|
||||
_unsupported("vg_setup");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static struct volume_group *_vg_read(struct format_instance *fi,
|
||||
const char *vg_name)
|
||||
{
|
||||
_unsupported("vg_read");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void _destroy(struct format_instance *fi)
|
||||
{
|
||||
struct archive_c *bc = (struct archive_c *) fi->private;
|
||||
if (bc->vg_archives)
|
||||
hash_destroy(bc->vg_archives);
|
||||
pool_destroy(bc->mem);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Extract vg name and version number from a filename.
|
||||
@ -183,50 +103,6 @@ static void _insert_file(struct list *head, struct archive_file *b)
|
||||
list_add_h(&bf->list, &b->list);
|
||||
}
|
||||
|
||||
static int _scan_vg(struct archive_c *bc, const char *file,
|
||||
const char *vg_name, int index)
|
||||
{
|
||||
struct archive_file *b;
|
||||
struct list *files;
|
||||
|
||||
/*
|
||||
* Do we need to create a new list of archive files for
|
||||
* this vg ?
|
||||
*/
|
||||
if (!(files = hash_lookup(bc->vg_archives, vg_name))) {
|
||||
if (!(files = pool_alloc(bc->mem, sizeof(*files)))) {
|
||||
stack;
|
||||
return 0;
|
||||
}
|
||||
|
||||
list_init(files);
|
||||
if (!hash_insert(bc->vg_archives, vg_name, files)) {
|
||||
log_err("Couldn't insert archive file "
|
||||
"into hash table.");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Create a new archive file.
|
||||
*/
|
||||
if (!(b = pool_alloc(bc->mem, sizeof(*b)))) {
|
||||
log_err("Couldn't create new archive file.");
|
||||
return 0;
|
||||
}
|
||||
|
||||
b->index = index;
|
||||
b->path = (char *)file;
|
||||
b->vg = (char *)vg_name;
|
||||
|
||||
/*
|
||||
* Insert it to the correct part of the list.
|
||||
*/
|
||||
_insert_file(files, b);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static char *_join(struct pool *mem, const char *dir, const char *name)
|
||||
{
|
||||
if (!pool_begin_object(mem, 32) ||
|
||||
@ -241,71 +117,90 @@ static char *_join(struct pool *mem, const char *dir, const char *name)
|
||||
return pool_end_object(mem);
|
||||
}
|
||||
|
||||
static int _scan_dir(struct archive_c *bc)
|
||||
/*
|
||||
* Returns a list of archive_files.
|
||||
*/
|
||||
static struct list *_scan_archive(struct pool *mem,
|
||||
const char *vg, const char *dir)
|
||||
{
|
||||
int r = 0, i, count, index;
|
||||
int i, count, index;
|
||||
char vg_name[64], *path;
|
||||
struct dirent **dirent;
|
||||
struct archive_file *af;
|
||||
struct list *results;
|
||||
|
||||
if ((count = scandir(bc->dir, &dirent, NULL, alphasort)) < 0) {
|
||||
if (!(results = pool_alloc(mem, sizeof(*results)))) {
|
||||
stack;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
list_init(results);
|
||||
|
||||
if ((count = scandir(dir, &dirent, NULL, alphasort)) < 0) {
|
||||
log_err("Couldn't scan archive directory.");
|
||||
return 0;
|
||||
}
|
||||
|
||||
for (i = 0; i < count; i++) {
|
||||
if ((dirent[i]->d_name[0] == '.') ||
|
||||
!_split_vg(dirent[i]->d_name, vg_name,
|
||||
/* ignore dot files */
|
||||
if (dirent[i]->d_name[0] == '.')
|
||||
continue;
|
||||
|
||||
/* check the name is the correct format */
|
||||
if (!_split_vg(dirent[i]->d_name, vg_name,
|
||||
sizeof(vg_name), &index))
|
||||
continue;
|
||||
|
||||
if (!(path = _join(bc->mem, bc->dir, dirent[i]->d_name))) {
|
||||
/* is it the vg we're interested in ? */
|
||||
if (strcmp(vg, vg_name))
|
||||
continue;
|
||||
|
||||
if (!(path = _join(mem, dir, dirent[i]->d_name))) {
|
||||
stack;
|
||||
goto out;
|
||||
}
|
||||
|
||||
_scan_vg(bc, path, vg_name, index);
|
||||
/*
|
||||
* Create a new archive_file.
|
||||
*/
|
||||
if (!(af = pool_alloc(mem, sizeof(*af)))) {
|
||||
log_err("Couldn't create new archive file.");
|
||||
results = NULL;
|
||||
goto out;
|
||||
}
|
||||
|
||||
af->index = index;
|
||||
af->path = path;
|
||||
|
||||
/*
|
||||
* Insert it to the correct part of the list.
|
||||
*/
|
||||
_insert_file(results, af);
|
||||
}
|
||||
r = 1;
|
||||
|
||||
out:
|
||||
for (i = 0; i < count; i++)
|
||||
free(dirent[i]);
|
||||
free(dirent);
|
||||
|
||||
return r;
|
||||
return results;
|
||||
}
|
||||
|
||||
static int _scan_archives(struct archive_c *bc)
|
||||
int archive_vg(struct volume_group *vg,
|
||||
const char *dir, const char *desc,
|
||||
uint32_t retain_days, uint32_t min_archive)
|
||||
{
|
||||
pool_empty(bc->mem);
|
||||
|
||||
if (bc->vg_archives)
|
||||
hash_destroy(bc->vg_archives);
|
||||
|
||||
if (!(bc->vg_archives = hash_create(128))) {
|
||||
log_err("Couldn't create hash table for scanning archives.");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!_scan_dir(bc)) {
|
||||
stack;
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int _vg_write(struct format_instance *fi, struct volume_group *vg)
|
||||
{
|
||||
struct archive_c *bc = (struct archive_c *) fi->private;
|
||||
|
||||
int r = 0, i, fd;
|
||||
int i, fd;
|
||||
unsigned int index = 0;
|
||||
struct archive_file *last;
|
||||
FILE *fp = NULL;
|
||||
char temp_file[PATH_MAX], archive_name[PATH_MAX];
|
||||
struct list *archives;
|
||||
|
||||
if (!create_temp_name(bc->dir, temp_file, sizeof(temp_file), &fd)) {
|
||||
/*
|
||||
* Write the vg out to a temporary file.
|
||||
*/
|
||||
if (!create_temp_name(dir, temp_file, sizeof(temp_file), &fd)) {
|
||||
log_err("Couldn't create temporary archive name.");
|
||||
return 0;
|
||||
}
|
||||
@ -316,7 +211,7 @@ static int _vg_write(struct format_instance *fi, struct volume_group *vg)
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!text_vg_export(fp, vg, bc->desc)) {
|
||||
if (!text_vg_export(fp, vg, desc)) {
|
||||
stack;
|
||||
fclose(fp);
|
||||
return 0;
|
||||
@ -327,106 +222,32 @@ static int _vg_write(struct format_instance *fi, struct volume_group *vg)
|
||||
/*
|
||||
* Now we want to rename this file to <vg>_index.vg.
|
||||
*/
|
||||
if (!_scan_archives(bc)) {
|
||||
log_err("Couldn't scan the archive directory (%s).", bc->dir);
|
||||
goto out;
|
||||
if (!(archives = _scan_archive(vg->cmd->mem, vg->name, dir))) {
|
||||
log_err("Couldn't scan the archive directory (%s).", dir);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if ((last = (struct archive_file *) hash_lookup(bc->vg_archives,
|
||||
vg->name))) {
|
||||
/* move to the last in the list */
|
||||
last = list_item(last->list.p, struct archive_file);
|
||||
if (list_empty(archives))
|
||||
index = 0;
|
||||
|
||||
else {
|
||||
last = list_item(archives->p, struct archive_file);
|
||||
index = last->index + 1;
|
||||
}
|
||||
|
||||
for (i = 0; i < 10; i++) {
|
||||
if (lvm_snprintf(archive_name, sizeof(archive_name),
|
||||
"%s/%s_%05d.vg",
|
||||
bc->dir, vg->name, index) < 0) {
|
||||
dir, vg->name, index) < 0) {
|
||||
log_err("archive file name too long.");
|
||||
goto out;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (lvm_rename(temp_file, archive_name)) {
|
||||
r = 1;
|
||||
if (lvm_rename(temp_file, archive_name))
|
||||
break;
|
||||
}
|
||||
|
||||
index++;
|
||||
}
|
||||
|
||||
out:
|
||||
return r;
|
||||
}
|
||||
|
||||
void archive_expire(struct format_instance *fi)
|
||||
{
|
||||
/* FIXME: finish */
|
||||
}
|
||||
|
||||
static struct format_handler _archive_handler = {
|
||||
get_vgs: _get_vgs,
|
||||
get_pvs: _get_pvs,
|
||||
pv_read: _pv_read,
|
||||
pv_setup: _pv_setup,
|
||||
pv_write: _pv_write,
|
||||
vg_setup: _vg_setup,
|
||||
vg_read: _vg_read,
|
||||
vg_write: _vg_write,
|
||||
destroy: _destroy
|
||||
};
|
||||
|
||||
/*
|
||||
* FIXME: format_instances shouldn't be allocated from the pool.
|
||||
*/
|
||||
struct format_instance *archive_format_create(struct cmd_context *cmd,
|
||||
const char *dir,
|
||||
uint32_t retain_days,
|
||||
uint32_t min_retains,
|
||||
const char *desc)
|
||||
{
|
||||
struct format_instance *fi;
|
||||
struct archive_c *bc = NULL;
|
||||
struct pool *mem = cmd->mem;
|
||||
|
||||
if (!(bc = pool_zalloc(mem, sizeof(*bc)))) {
|
||||
stack;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!(bc->mem = pool_create(1024))) {
|
||||
stack;
|
||||
goto bad;
|
||||
}
|
||||
|
||||
if (!(bc->dir = pool_strdup(mem, dir))) {
|
||||
stack;
|
||||
goto bad;
|
||||
}
|
||||
|
||||
if (!(bc->desc = pool_strdup(mem, desc))) {
|
||||
stack;
|
||||
goto bad;
|
||||
}
|
||||
|
||||
bc->retain_days = retain_days;
|
||||
bc->min_retains = min_retains;
|
||||
|
||||
if (!(fi = pool_alloc(mem, sizeof(*fi)))) {
|
||||
stack;
|
||||
goto bad;
|
||||
}
|
||||
|
||||
fi->cmd = cmd;
|
||||
fi->ops = &_archive_handler;
|
||||
fi->private = bc;
|
||||
|
||||
return fi;
|
||||
|
||||
bad:
|
||||
if (bc->mem)
|
||||
pool_destroy(bc->mem);
|
||||
|
||||
pool_free(mem, bc);
|
||||
return NULL;
|
||||
return 1;
|
||||
}
|
||||
|
@ -12,19 +12,17 @@
|
||||
#include "uuid-map.h"
|
||||
|
||||
/*
|
||||
* The archive format is used to maintain a set of metadata
|
||||
* backup files in an archive directory. 'retain_days' is the
|
||||
* minimum number of days that an archive file must be held for.
|
||||
* 'min_archives' is the minimum number of archives required to
|
||||
* be kept for each volume group.
|
||||
* Archives a vg config. 'retain_days' is the minimum number of
|
||||
* days that an archive file must be held for. 'min_archives' is
|
||||
* the minimum number of archives required to be kept for each
|
||||
* volume group.
|
||||
*/
|
||||
struct format_instance *archive_format_create(struct cmd_context *cmd,
|
||||
const char *dir,
|
||||
uint32_t retain_days,
|
||||
uint32_t min_archives,
|
||||
const char *desc);
|
||||
int archive_vg(struct volume_group *vg,
|
||||
const char *dir,
|
||||
const char *desc,
|
||||
uint32_t retain_days,
|
||||
uint32_t min_archive);
|
||||
|
||||
void backup_expire(struct format_instance *fi);
|
||||
|
||||
/*
|
||||
* The text format can read and write a volume_group to a file.
|
||||
|
@ -81,8 +81,6 @@ static char *_build_desc(struct pool *mem, const char *line, int before)
|
||||
|
||||
static int __archive(struct volume_group *vg)
|
||||
{
|
||||
int r;
|
||||
struct format_instance *archiver;
|
||||
char *desc;
|
||||
|
||||
if (!(desc = _build_desc(vg->cmd->mem, vg->cmd->cmd_line, 1))) {
|
||||
@ -90,20 +88,9 @@ static int __archive(struct volume_group *vg)
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!(archiver = archive_format_create(vg->cmd,
|
||||
_archive_params.dir,
|
||||
_archive_params.keep_days,
|
||||
_archive_params.keep_number,
|
||||
desc))) {
|
||||
log_error("Couldn't create archiver object.");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!(r = archiver->ops->vg_write(archiver, vg)))
|
||||
stack;
|
||||
|
||||
archiver->ops->destroy(archiver);
|
||||
return r;
|
||||
return archive_vg(vg, _archive_params.dir, desc,
|
||||
_archive_params.keep_days,
|
||||
_archive_params.keep_number);
|
||||
}
|
||||
|
||||
int archive(struct volume_group *vg)
|
||||
|
Loading…
x
Reference in New Issue
Block a user