/* * 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 #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "ui.h" static Widget autoToggle, diskText, lastText, keepH, keepD, keepW, keepM; static Widget freeText, oldestText; static Widget minFree; static int isRoot; /* ---- the state ----------------------------------------------------------- */ /* "Available: 73.8 GB of 74.2 GB" of the backup disk */ static void FreeSpace(const McdeSysBackup *b, char *buf, size_t len) { McdeSysDisk d[32]; struct statvfs vfs; int n = mcde_sys_disks(d, 32), i; const char *at = NULL; buf[0] = '\0'; if (!b->configured || b->disk[0] == '\0') return; for (i = 0; i < n; i++) if (strcmp(d[i].name, b->disk) == 0 && d[i].mountpoint[0] != '\0') at = d[i].mountpoint; if (at == NULL || statvfs(at, &vfs) != 0) return; { unsigned long long avail = (unsigned long long) vfs.f_bavail * vfs.f_frsize; unsigned long long total = (unsigned long long) vfs.f_blocks * vfs.f_frsize; char a[32], t[32]; if (avail >= 1000000000ULL) (void)snprintf(a, sizeof a, "%.1f GB", avail / 1e9); else (void)snprintf(a, sizeof a, "%.0f MB", avail / 1e6); if (total >= 1000000000ULL) (void)snprintf(t, sizeof t, "%.1f GB", total / 1e9); else (void)snprintf(t, sizeof t, "%.0f MB", total / 1e6); (void)snprintf(buf, len, "Available: %s of %s", a, t); } } static void Read(void) { McdeSysBackup b; char text[700], when[64]; (void) mcde_sys_backup_get(&b); XmToggleButtonSetState(autoToggle, b.enabled, False); { XmString sw = XmStringCreateLocalized(b.enabled ? "ON" : "OFF"); XtVaSetValues(autoToggle, XmNlabelString, sw, NULL); XmStringFree(sw); } 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); { char freeb[128]; FreeSpace(&b, freeb, sizeof freeb); SetText(freeText, freeb[0] != '\0' ? freeb : "Available: (no disk is set up)"); } { /* the oldest and the newest of the backups on the disk */ McdeSysBackupArchive arch[256]; int n = mcde_sys_backup_archives(arch, 256); if (n > 0) (void)snprintf(text, sizeof text, "Oldest backup: %s " "Latest backup: %s", arch[0].time, arch[n - 1].time); else (void)snprintf(text, sizeof text, "Oldest backup: -- " "Latest backup: %s", b.last > 0 ? "see above" : "--"); SetText(oldestText, text); } } 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; /* the reference's big switch: OFF / ON, told in the label */ autoToggle = XtVaCreateManagedWidget("OFF", xmToggleButtonWidgetClass, box, XmNindicatorOn, False, XmNshadowThickness, 2, XmNmarginWidth, 26, XmNmarginHeight, 10, XmNalignment, XmALIGNMENT_CENTER, NULL); XtVaSetValues(autoToggle, XmNrenderTable, PaneFont(box, 14, True), NULL); (void) Text(box, "autoLabel", "Back ups every hour when it is ON."); XtAddCallback(autoToggle, XmNvalueChangedCallback, AutoChanged, NULL); diskText = Text(box, "disk", ""); freeText = Text(box, "free", ""); lastText = Text(box, "last", ""); oldestText = Text(box, "oldest", ""); 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, "retention", "Kept: hourly backups for a day, daily " "ones for a month, weekly ones for a year, and every monthly " "state; when the disk is full, the oldest go first."); (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; }