Files
mcde/programs/dtsystemsettings/backup.c
T
lotherkandClaude Sonnet 5.5 d53ea094e7 licence: CNCL for what Makronix wrote itself
LICENSE at the root; the 103 files new in ext/src/mcde (none was in the CDE
import) carry the CNCL line instead of the LGPL notice.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-14 11:48:02 +00:00

441 lines
14 KiB
C

/*
* backup.c -- dtsystemsettings: the backup of the whole computer
*
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
*
* A switch for the hourly backup; the backup disk, chosen among the
* partitions that are not the system's, with the password that encrypts
* the backups on it; when the last backup ran and how it ended; Back Up
* Now; how many backups are kept and the free space below which the
* oldest go; and Restore..., a path of a backup into a folder. The work
* is McdeSys's (lib/McdeSys/backup.c: BorgBackup). Everything here
* changes the system, so it needs root; as a user the pane shows the state
* and offers Unlock..., as Accounts does. Back Up Now runs dtbackup in a
* terminal, where it shows what it does.
*
* C99, POSIX, Xt and Motif only (ext/src/mcde/AGENTS.md).
*/
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <X11/Intrinsic.h>
#include <Xm/Xm.h>
#include <Xm/List.h>
#include <Xm/MessageB.h>
#include <Xm/RowColumn.h>
#include <Xm/Scale.h>
#include <Xm/SelectioB.h>
#include <Xm/Separator.h>
#include <Xm/TextF.h>
#include <Xm/ToggleB.h>
#include <Mcde/McdeSys.h>
#include "ui.h"
static Widget autoToggle, diskText, lastText, keepH, keepD, keepW, keepM;
static Widget minFree;
static int isRoot;
/* ---- the state ----------------------------------------------------------- */
static void
Read(void)
{
McdeSysBackup b;
char text[700], when[64];
(void) mcde_sys_backup_get(&b);
XmToggleButtonSetState(autoToggle, b.enabled, False);
XtSetSensitive(autoToggle, isRoot && b.configured);
if (b.configured)
snprintf(text, sizeof text, "Backup disk: %s, backups in %s",
b.disk, b.repo);
else
snprintf(text, sizeof text, "No backup disk chosen yet.");
SetText(diskText, text);
if (b.last > 0) {
time_t t = (time_t) b.last;
strftime(when, sizeof when, "%A, %e %B %Y, %H:%M",
localtime(&t));
snprintf(text, sizeof text, "Last backup: %s -- %s", when,
b.result[0] != '\0' ? b.result : "");
} else
snprintf(text, sizeof text, "No backup yet.%s", b.enabled ?
" The first runs within the hour." : "");
SetText(lastText, text);
XmScaleSetValue(keepH, b.keepHourly < 0 ? 0 : b.keepHourly);
XmScaleSetValue(keepD, b.keepDaily < 0 ? 0 : b.keepDaily);
XmScaleSetValue(keepW, b.keepWeekly < 0 ? 0 : b.keepWeekly);
XmToggleButtonSetState(keepM, b.keepMonthly < 0, False);
XmScaleSetValue(minFree, b.minFree);
}
static void
AutoChanged(Widget w, XtPointer data, XtPointer call)
{
(void) data;
if (mcde_sys_backup_enable(((XmToggleButtonCallbackStruct *)
call)->set) != MCDE_SYS_OK)
Message(w, mcde_sys_error(), 1);
Read();
}
static void
PolicyChanged(Widget w, XtPointer data, XtPointer call)
{
McdeSysBackup b;
int v;
(void) data;
(void) call;
(void) mcde_sys_backup_get(&b);
XmScaleGetValue(keepH, &v);
b.keepHourly = v;
XmScaleGetValue(keepD, &v);
b.keepDaily = v;
XmScaleGetValue(keepW, &v);
b.keepWeekly = v;
b.keepMonthly = XmToggleButtonGetState(keepM) ? -1 : 12;
XmScaleGetValue(minFree, &v);
b.minFree = v;
if (mcde_sys_backup_policy(&b) != MCDE_SYS_OK)
Message(w, mcde_sys_error(), 1);
}
/* ---- choosing the disk --------------------------------------------------- */
static McdeSysDisk choices[64];
static int nchoices;
static Widget chooseList, pass1, pass2;
static char p1[128], p2[128];
static size_t l1, l2;
/* the fields show a star for every character; the text is kept here */
static void
Secret(Widget w, XtPointer data, XtPointer call)
{
XmTextVerifyCallbackStruct *cb = (XmTextVerifyCallbackStruct *) call;
char *buf = data == (XtPointer) 1 ? p1 : p2;
size_t *len = data == (XtPointer) 1 ? &l1 : &l2;
size_t from = (size_t) cb->startPos, to = (size_t) cb->endPos;
size_t add = cb->text != NULL ? (size_t) cb->text->length : 0, i;
(void) w;
if (to > *len)
to = *len;
if (from > to || *len - (to - from) + add >= sizeof p1) {
cb->doit = False;
return;
}
memmove(buf + from + add, buf + to, *len - to);
if (add > 0)
memcpy(buf + from, cb->text->ptr, add);
*len = *len - (to - from) + add;
buf[*len] = '\0';
for (i = 0; i < add; i++)
cb->text->ptr[i] = '*';
}
static void
Forget(void)
{
memset(p1, 0, sizeof p1);
memset(p2, 0, sizeof p2);
l1 = l2 = 0;
}
static void
ChooseDone(Widget w, XtPointer data, XtPointer call)
{
XmAnyCallbackStruct *cb = (XmAnyCallbackStruct *) call;
int *pos = NULL, n = 0;
(void) data;
if (cb->reason == XmCR_OK) {
if (!XmListGetSelectedPos(chooseList, &pos, &n) || n < 1 ||
pos[0] < 1 || pos[0] > nchoices)
Message(w, "Choose the disk for the backups.", 1);
else if (strcmp(p1, p2) != 0)
Message(w, "The two passwords are not the same.", 1);
else if (mcde_sys_backup_setup(choices[pos[0] - 1].name, p1) !=
MCDE_SYS_OK)
Message(w, mcde_sys_error(), 1);
else if (mcde_sys_backup_enable(1) != MCDE_SYS_OK)
Message(w, mcde_sys_error(), 1);
else
Message(w, "The backup disk is ready. Keep the "
"password somewhere safe: without it the backups "
"cannot be read.", 0);
XtFree((char *) pos);
}
Forget();
XtDestroyWidget(XtParent(w));
Read();
}
static void
ChooseDisk(Widget w, XtPointer data, XtPointer call)
{
static McdeSysDisk all[64];
Widget d, box, f;
XmString s;
char line[300], size[32];
int n = mcde_sys_disks(all, 64), i;
Arg a[4];
(void) data;
(void) call;
nchoices = 0;
d = XmCreateTemplateDialog(w, "chooseBackupDisk", NULL, 0);
s = XmStringCreateLocalized("Backup Disk");
XtVaSetValues(d, XmNdialogTitle, s, NULL);
XmStringFree(s);
box = XtVaCreateManagedWidget("box", xmRowColumnWidgetClass, d, NULL);
(void) Text(box, "label", "The disk for the backups -- partitions "
"that do not hold the system:");
XtSetArg(a[0], XmNvisibleItemCount, 6);
XtSetArg(a[1], XmNselectionPolicy, XmBROWSE_SELECT);
XtSetArg(a[2], XmNwidth, 420);
chooseList = XmCreateScrolledList(box, "disks", a, 3);
for (i = 0; i < n && nchoices < 64; i++) {
McdeSysDisk *x = &all[i];
if (x->parent[0] == '\0' || x->system ||
strcmp(x->fstype, "swap") == 0 || x->fstype[0] == '\0' ||
strcmp(x->fstype, "cd9660") == 0)
continue;
choices[nchoices++] = *x;
if (x->bytes >= 1000000000ULL)
snprintf(size, sizeof size, "%.1f GB", x->bytes / 1e9);
else
snprintf(size, sizeof size, "%.0f MB", x->bytes / 1e6);
snprintf(line, sizeof line, "%s %s %s %s", x->name,
x->label[0] != '\0' ? x->label : "(no name)", x->fstype,
size);
s = XmStringCreateLocalized(line);
XmListAddItemUnselected(chooseList, s, 0);
XmStringFree(s);
}
XtManageChild(chooseList);
if (nchoices == 0)
(void) Text(box, "note", "There is no such disk: connect one, "
"and erase it in Disk Utility if need be.");
(void) Text(box, "label", "A password that encrypts the backups "
"(8 characters or more):");
Forget();
f = XtVaCreateManagedWidget("pass1", xmTextFieldWidgetClass, box,
XmNcolumns, 30, NULL);
XtAddCallback(f, XmNmodifyVerifyCallback, Secret, (XtPointer) 1);
(void) Text(box, "label", "The password again:");
f = XtVaCreateManagedWidget("pass2", xmTextFieldWidgetClass, box,
XmNcolumns, 30, NULL);
XtAddCallback(f, XmNmodifyVerifyCallback, Secret, (XtPointer) 2);
pass1 = pass2 = f;
s = XmStringCreateLocalized("Use This Disk");
XtVaSetValues(d, XmNokLabelString, s, NULL);
XmStringFree(s);
s = XmStringCreateLocalized("Cancel");
XtVaSetValues(d, XmNcancelLabelString, s, NULL);
XmStringFree(s);
XtAddCallback(d, XmNokCallback, ChooseDone, NULL);
XtAddCallback(d, XmNcancelCallback, ChooseDone, NULL);
XtManageChild(d);
}
/* ---- now, restore, unlock ------------------------------------------------ */
static void
BackupNow(Widget w, XtPointer data, XtPointer call)
{
char cmd[512];
(void) w;
(void) data;
(void) call;
/* in a terminal, where it shows what it does; as root already, or
* after su asks for the password */
if (isRoot)
Spawn("dtterm -title 'Backup' -geometry 90x24 -e sh -c "
"'dtbackup run; echo; echo Press Return.; read x'");
else {
AsRoot("Backup", "dtbackup run; echo; echo Press Return.; read x",
cmd, sizeof cmd);
Spawn(cmd);
}
}
static McdeSysBackupArchive archives[4096];
static int narchives;
static Widget archiveList, pathField;
static void
RestoreDone(Widget w, XtPointer data, XtPointer call)
{
XmAnyCallbackStruct *cb = (XmAnyCallbackStruct *) call;
int *pos = NULL, n = 0;
char *path, dest[600], text[900];
const char *home = getenv("HOME");
(void) data;
if (cb->reason == XmCR_OK) {
path = XmTextFieldGetString(pathField);
snprintf(dest, sizeof dest, "%s/Restored", home != NULL &&
strcmp(home, "/") != 0 ? home : "/root");
if (!XmListGetSelectedPos(archiveList, &pos, &n) || n < 1 ||
pos[0] < 1 || pos[0] > narchives || path == NULL ||
path[0] != '/')
Message(w, "Choose a backup and write the path to bring "
"back, beginning with /.", 1);
else if (mcde_sys_backup_restore(archives[pos[0] - 1].name,
path, dest) != MCDE_SYS_OK)
Message(w, mcde_sys_error(), 1);
else {
snprintf(text, sizeof text, "%s from %s is back in %s.",
path, archives[pos[0] - 1].time, dest);
Message(w, text, 0);
}
XtFree(path);
XtFree((char *) pos);
}
XtDestroyWidget(XtParent(w));
}
static void
Restore(Widget w, XtPointer data, XtPointer call)
{
Widget d, box;
XmString s;
char line[200];
Arg a[4];
int i;
(void) data;
(void) call;
narchives = mcde_sys_backup_archives(archives,
sizeof archives / sizeof archives[0]);
if (narchives < 0) {
Message(w, mcde_sys_error(), 1);
return;
}
d = XmCreateTemplateDialog(w, "restore", NULL, 0);
s = XmStringCreateLocalized("Restore from a Backup");
XtVaSetValues(d, XmNdialogTitle, s, NULL);
XmStringFree(s);
box = XtVaCreateManagedWidget("box", xmRowColumnWidgetClass, d, NULL);
(void) Text(box, "label", "The backups, the newest last:");
XtSetArg(a[0], XmNvisibleItemCount, 10);
XtSetArg(a[1], XmNselectionPolicy, XmBROWSE_SELECT);
XtSetArg(a[2], XmNwidth, 420);
archiveList = XmCreateScrolledList(box, "archives", a, 3);
for (i = 0; i < narchives; i++) {
snprintf(line, sizeof line, "%s %s", archives[i].time,
archives[i].name);
s = XmStringCreateLocalized(line);
XmListAddItemUnselected(archiveList, s, 0);
XmStringFree(s);
}
if (narchives > 0)
XmListSelectPos(archiveList, narchives, False);
XtManageChild(archiveList);
(void) Text(box, "label", "Bring back (a file or a folder, as it was "
"called):");
pathField = XtVaCreateManagedWidget("path", xmTextFieldWidgetClass, box,
XmNcolumns, 44, NULL);
(void) Text(box, "note", "It comes back into the folder Restored of "
"your home; nothing is overwritten.");
s = XmStringCreateLocalized("Restore");
XtVaSetValues(d, XmNokLabelString, s, NULL);
XmStringFree(s);
s = XmStringCreateLocalized("Cancel");
XtVaSetValues(d, XmNcancelLabelString, s, NULL);
XmStringFree(s);
XtAddCallback(d, XmNokCallback, RestoreDone, NULL);
XtAddCallback(d, XmNcancelCallback, RestoreDone, NULL);
XtManageChild(d);
}
static void
Unlock(Widget w, XtPointer data, XtPointer call)
{
char cmd[512];
(void) w;
(void) data;
(void) call;
AsRoot("Unlock Backup", "dtsystemsettings -pane backup", cmd,
sizeof cmd);
Spawn(cmd);
}
/* ---- the pane ------------------------------------------------------------ */
static Widget
Keep(Widget parent, const char *label, int max)
{
Widget w;
(void) Text(parent, "label", label);
w = XtVaCreateManagedWidget("keep", xmScaleWidgetClass, parent,
XmNorientation, XmHORIZONTAL, XmNminimum, 0, XmNmaximum, max,
XmNshowValue, True, XmNscaleWidth, 200, NULL);
XtAddCallback(w, XmNvalueChangedCallback, PolicyChanged, NULL);
XtSetSensitive(w, isRoot);
return w;
}
Widget
MakeBackup(Widget parent)
{
Widget box = XtVaCreateWidget("backup", xmRowColumnWidgetClass, parent,
XmNspacing, 4, NULL);
Widget row, grid;
isRoot = geteuid() == 0;
autoToggle = XtVaCreateManagedWidget("Back Up Automatically, every "
"hour", xmToggleButtonWidgetClass, box, NULL);
XtVaSetValues(autoToggle, XmNrenderTable, PaneFont(box, 12, True),
NULL);
XtAddCallback(autoToggle, XmNvalueChangedCallback, AutoChanged, NULL);
diskText = Text(box, "disk", "");
lastText = Text(box, "last", "");
row = XtVaCreateManagedWidget("buttons", xmRowColumnWidgetClass, box,
XmNorientation, XmHORIZONTAL, XmNmarginWidth, 0, NULL);
XtSetSensitive(Button(row, "Choose Disk...", ChooseDisk, NULL), isRoot);
(void) Button(row, "Back Up Now", BackupNow, NULL);
XtSetSensitive(Button(row, "Restore...", Restore, NULL), isRoot);
if (!isRoot)
(void) Button(row, "Unlock...", Unlock, NULL);
XtVaCreateManagedWidget("sep", xmSeparatorWidgetClass, box, NULL);
(void) Text(box, "label", "How many backups are kept:");
/* a row each, the label and its slider: packed in columns, a
* horizontal row column counts its rows in XmNnumColumns */
grid = XtVaCreateManagedWidget("keep", xmRowColumnWidgetClass, box,
XmNorientation, XmHORIZONTAL, XmNpacking, XmPACK_COLUMN,
XmNnumColumns, 3, XmNentryVerticalAlignment, XmALIGNMENT_CENTER,
NULL);
keepH = Keep(grid, "hourly", 48);
keepD = Keep(grid, "daily", 90);
keepW = Keep(grid, "weekly", 104);
keepM = XtVaCreateManagedWidget("every monthly one",
xmToggleButtonWidgetClass, box, NULL);
XtAddCallback(keepM, XmNvalueChangedCallback, PolicyChanged, NULL);
XtSetSensitive(keepM, isRoot);
(void) Text(box, "label", "When the disk has less free space than this "
"(percent), the oldest backups go:");
minFree = XtVaCreateManagedWidget("minFree", xmScaleWidgetClass, box,
XmNorientation, XmHORIZONTAL, XmNminimum, 1, XmNmaximum, 50,
XmNshowValue, True, XmNscaleWidth, 300, NULL);
XtAddCallback(minFree, XmNvalueChangedCallback, PolicyChanged, NULL);
XtSetSensitive(minFree, isRoot);
Read();
return box;
}