Skip to content
Snippets Groups Projects
Commit 715a7723 authored by Eric Vidal's avatar Eric Vidal :speech_balloon:
Browse files

Cleanup directories before restoring a snapshot

parent 3db4a854
No related branches found
No related tags found
No related merge requests found
......@@ -104,8 +104,6 @@ This subcommand restores a previously created snapshot called *name*.
You can get a list of available snapshot invocating the [list](#list) subcommand.
The restoration process does not remove existing elements; instead, it overwrites any matching elements found in both the snapshot directory and the corresponding directory on the host
#### Options
- **-h**: prints this help.
......
......@@ -30,6 +30,7 @@
#include <66/resolve.h>
#include <66/sanitize.h>
#include <66/service.h>
#include <66/snapshot.h>
#include <66/ssexec.h>
#include <66/state.h>
#include <66/svc.h>
......
/*
* snapshot.h
*
* Copyright (c) 2018-2024 Eric Vidal <eric@obarun.org>
*
* All rights reserved.
*
* This file is part of Obarun. It is subject to the license terms in
* the LICENSE file found in the top-level directory of this
* distribution.
* This file may not be copied, modified, propagated, or distributed
* except according to the terms contained in the LICENSE file./
*/
#ifndef SS_SNAPSHOT_H
#define SS_SNAPSHOT_H
#include <66/config.h>
typedef struct snapshot_list_s snapshot_list_t ;
struct snapshot_list_s
{
char *name ;
} ;
extern snapshot_list_t snapshot_root_list[] ;
extern snapshot_list_t snapshot_user_list[] ;
#endif
......@@ -25,22 +25,15 @@
#include <skalibs/sgetopt.h>
#include <skalibs/djbunix.h>
#include <66/snapshot.h>
#include <66/ssexec.h>
#include <66/constants.h>
#include <66/utils.h>
typedef struct snapshot_list_s snapshot_list_t ;
struct snapshot_list_s
{
char *name ;
} ;
snapshot_list_t snapshot_root_list[] = {
{ .name = SS_SKEL_DIR },
{ .name = SS_SERVICE_SYSDIR },
{ .name = SS_SERVICE_SYSDIR_USER },
{ .name = SS_SERVICE_ADMDIR },
{ .name = SS_SERVICE_ADMDIR_USER },
{ .name = SS_SERVICE_ADMCONFDIR },
{ .name = SS_SCRIPT_SYSDIR },
{ .name = SS_SEED_SYSDIR },
......
......@@ -26,13 +26,52 @@
#include <skalibs/djbunix.h>
#include <66/ssexec.h>
#include <66/snapshot.h>
#include <66/constants.h>
static void snapshot_remove_directory(ssexec_t *info, char const *target)
{
size_t pos = 0 ;
snapshot_list_t *list = info->owner ? snapshot_user_list : snapshot_root_list ;
_alloc_stk_(stk, SS_MAX_PATH_LEN) ;
while(list[pos].name) {
if (!info->owner) {
auto_strings(stk.s, list[pos].name) ;
} else {
auto_strings(stk.s, target, list[pos].name) ;
}
log_trace("remove directory: ", stk.s) ;
if (!dir_rm_rf(stk.s))
log_warnusys("remove directory: ", stk.s) ;
pos++ ;
}
if (!info->owner) {
auto_strings(stk.s, SS_SYSTEM_DIR, SS_SYSTEM) ;
} else {
auto_strings(stk.s, target, SS_USER_DIR, SS_SYSTEM) ;
}
log_trace("remove directory: ", stk.s) ;
if (!dir_rm_rf(stk.s))
log_warnusys("remove directory: ", stk.s) ;
}
int ssexec_snapshot_restore(int argc, char const *const *argv, ssexec_t *info)
{
log_flow() ;
size_t pos = 0 ;
size_t pos = 0, dlen = 0 ;
char const *snapname = 0 ;
char const *exclude[1] = { 0 } ;
_alloc_stk_(snapdir, SS_MAX_PATH_LEN) ;
......@@ -74,44 +113,39 @@ int ssexec_snapshot_restore(int argc, char const *const *argv, ssexec_t *info)
if (!sastr_dir_get(&sa, snapdir.s, exclude, S_IFDIR))
log_dieusys(LOG_EXIT_SYS, "list snapshot directory: ", snapdir.s) ;
FOREACH_SASTR(&sa, pos) {
if (!info->owner) {
auto_strings(src.s, snapdir.s, "/", sa.s + pos) ;
auto_strings(dst.s, "/") ;
if (!info->owner) {
} else {
auto_strings(dst.s, "/", sa.s + pos) ;
int e = errno ;
struct passwd *st = getpwuid(info->owner) ;
errno = 0 ;
if (!st) {
if (!errno) errno = ESRCH ;
return 0 ;
}
errno = e ;
if (st->pw_dir == NULL)
log_warnusys(LOG_EXIT_ZERO, "get home directory") ;
} else {
auto_strings(dst.s, st->pw_dir, "/") ;
}
int e = errno ;
struct passwd *st = getpwuid(info->owner) ;
errno = 0 ;
if (!st) {
if (!errno) errno = ESRCH ;
return 0 ;
}
errno = e ;
if (st->pw_dir == NULL)
log_warnusys(LOG_EXIT_ZERO, "get home directory") ;
dlen = strlen(dst.s) ;
auto_strings(dst.s, st->pw_dir, "/", sa.s + pos) ;
}
snapshot_remove_directory(info, dst.s) ;
log_trace("copy: ", src.s , " to: ", dst.s) ;
if (!hiercopy(src.s, dst.s))
log_dieusys(LOG_EXIT_SYS, "copy: ", src.s," to: ", dst.s) ;
}
FOREACH_SASTR(&sa, pos) {
auto_strings(src.s, snapdir.s, "/", SS_SYSTEM, "/.version") ;
auto_strings(src.s, snapdir.s, "/", sa.s + pos) ;
/** remove .version file if it doesn't exist at snapshot dir.
* Typically version under 0.8.0.0 do not have this file.*/
if (access(src.s, F_OK) < 0) {
auto_strings(src.s, info->base.s, SS_SYSTEM, "/.version") ;
auto_strings(dst.s + dlen, sa.s + pos) ;
if (!dir_rm_rf(src.s))
log_warnusys("remove file: ", src.s) ;
log_trace("copy: ", src.s , " to: ", dst.s) ;
if (!hiercopy(src.s, dst.s))
log_dieusys(LOG_EXIT_SYS, "copy: ", src.s," to: ", dst.s) ;
}
log_info("Successfully restored snapshot: ", snapname) ;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment