Skip to content

Commit

Permalink
[#138] Restore: Delete backup_label
Browse files Browse the repository at this point in the history
  • Loading branch information
jesperpedersen committed Jul 17, 2023
1 parent 44478e6 commit a743cfc
Show file tree
Hide file tree
Showing 3 changed files with 176 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/include/workflow.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ extern "C" {
#define PERMISSION_TYPE_RESTORE 1
#define PERMISSION_TYPE_ARCHIVE 2

#define CLEANUP_TYPE_RESTORE 0

typedef int (* setup)(int, char*, struct node*, struct node**);
typedef int (* execute)(int, char*, struct node*, struct node**);
typedef int (* teardown)(int, char*, struct node*, struct node**);
Expand Down Expand Up @@ -165,6 +167,14 @@ pgmoneta_workflow_create_recovery_info(void);
struct workflow*
pgmoneta_workflow_create_permissions(int type);

/**
* Create a workflow for cleanup
* @param type The type of operation
* @return The workflow
*/
struct workflow*
pgmoneta_workflow_create_cleanup(int type);

/**
* Create a workflow for encryption
* @param encrypt true for encrypt and false for decrypt
Expand Down
163 changes: 163 additions & 0 deletions src/libpgmoneta/wf_cleanup.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/*
* Copyright (C) 2023 Red Hat
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list
* of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may
* be used to endorse or promote products derived from this software without specific
* prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/* pgmoneta */
#include <pgmoneta.h>
#include <info.h>
#include <logging.h>
#include <utils.h>
#include <workflow.h>

/* system */
#include <stdlib.h>

static int cleanup_setup(int, char*, struct node*, struct node**);
static int cleanup_execute_restore(int, char*, struct node*, struct node**);
static int cleanup_teardown(int, char*, struct node*, struct node**);

struct workflow*
pgmoneta_workflow_create_cleanup(int type)
{
struct workflow* wf = NULL;

wf = (struct workflow*)malloc(sizeof(struct workflow));

wf->setup = &cleanup_setup;
switch (type)
{
case CLEANUP_TYPE_RESTORE:
wf->execute = &cleanup_execute_restore;
break;
default:
pgmoneta_log_error("Invalid cleanup type");
}
wf->teardown = &cleanup_teardown;
wf->next = NULL;

return wf;
}

static int
cleanup_setup(int server, char* identifier, struct node* i_nodes, struct node** o_nodes)
{
return 0;
}

static int
cleanup_execute_restore(int server, char* identifier, struct node* i_nodes, struct node** o_nodes)
{
char* d = NULL;
int number_of_backups = 0;
struct backup** backups = NULL;
char* id = NULL;
char* path = NULL;
struct configuration* config;

config = (struct configuration*)shmem;

if (!strcmp(identifier, "oldest"))
{
d = pgmoneta_get_server_backup(server);

if (pgmoneta_get_backups(d, &number_of_backups, &backups))
{
goto error;
}

for (int i = 0; id == NULL && i < number_of_backups; i++)
{
if (backups[i]->valid == VALID_TRUE)
{
id = backups[i]->label;
}
}
}
else if (!strcmp(identifier, "latest") || !strcmp(identifier, "newest"))
{
d = pgmoneta_get_server_backup(server);

if (pgmoneta_get_backups(d, &number_of_backups, &backups))
{
goto error;
}

for (int i = number_of_backups - 1; id == NULL && i >= 0; i--)
{
if (backups[i]->valid == VALID_TRUE)
{
id = backups[i]->label;
}
}
}
else
{
id = identifier;
}

path = pgmoneta_append(path, pgmoneta_get_node_string(i_nodes, "directory"));
if (!pgmoneta_ends_with(path, "/"))
{
path = pgmoneta_append(path, "/");
}
path = pgmoneta_append(path, config->servers[server].name);
path = pgmoneta_append(path, "-");
path = pgmoneta_append(path, id);
path = pgmoneta_append(path, "/backup_label");

pgmoneta_delete_file(path);

for (int i = 0; i < number_of_backups; i++)
{
free(backups[i]);
}
free(backups);

free(path);
free(d);

return 0;

error:

for (int i = 0; i < number_of_backups; i++)
{
free(backups[i]);
}
free(backups);

free(path);
free(d);

return 1;
}

static int
cleanup_teardown(int server, char* identifier, struct node* i_nodes, struct node** o_nodes)
{
return 0;
}
3 changes: 3 additions & 0 deletions src/libpgmoneta/workflow.c
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ wf_restore(void)
current->next = pgmoneta_workflow_create_permissions(PERMISSION_TYPE_RESTORE);
current = current->next;

current->next = pgmoneta_workflow_create_cleanup(CLEANUP_TYPE_RESTORE);
current = current->next;

return head;
}

Expand Down

0 comments on commit a743cfc

Please sign in to comment.