All checks were successful
Build / Build (push) Successful in 12s
- Backup deletion paths before update to sd:/backup/OmniNX/{version}
- Backup deletion paths before clean install to sd:/backup/OmniNX/pre-omninx
- Best-effort backup: continue install even if some copies fail
- Fix directory creation: ensure parent dirs exist before folder_copy
- Step 1 for update: Backup; Step 1-2 for clean: user data + pre-omninx backup
Co-authored-by: Cursor <cursoragent@cursor.com>
187 lines
5.4 KiB
C
187 lines
5.4 KiB
C
/*
|
|
* OmniNX Installer - Backup and Restore Operations
|
|
*/
|
|
|
|
#include "backup.h"
|
|
#include "fs.h"
|
|
#include <libs/fatfs/ff.h>
|
|
#include <string.h>
|
|
#include <stdarg.h>
|
|
#include <utils/sprintf.h>
|
|
|
|
#define BACKUP_PATH_MAX 270
|
|
|
|
// Check if file/directory exists
|
|
static bool path_exists(const char *path) {
|
|
FILINFO fno;
|
|
return (f_stat(path, &fno) == FR_OK);
|
|
}
|
|
|
|
// Create all parent directories for a path (mkdir -p style)
|
|
static void ensure_parent_dirs(const char *full_path) {
|
|
char buf[BACKUP_PATH_MAX];
|
|
int n = (int)strlen(full_path);
|
|
if (n >= BACKUP_PATH_MAX) return;
|
|
memcpy(buf, full_path, n + 1);
|
|
|
|
for (int i = 4; i < n; i++) {
|
|
if (buf[i] == '/') {
|
|
buf[i] = '\0';
|
|
f_mkdir(buf);
|
|
buf[i] = '/';
|
|
}
|
|
}
|
|
}
|
|
|
|
// Copy a single path (file or dir) to backup base, preserving SD structure
|
|
static int backup_single_path(const char *src_path, const char *backup_base) {
|
|
FILINFO fno;
|
|
if (f_stat(src_path, &fno) != FR_OK)
|
|
return FR_OK;
|
|
|
|
const char *relative = src_path;
|
|
if (strncmp(relative, "sd:/", 4) == 0)
|
|
relative += 4;
|
|
|
|
if (fno.fattrib & AM_DIR) {
|
|
const char *last_slash = strrchr(relative, '/');
|
|
char parent[BACKUP_PATH_MAX];
|
|
if (last_slash && last_slash > relative) {
|
|
s_printf(parent, "%s/%.*s", backup_base, (int)(last_slash - relative), relative);
|
|
/* Create full parent hierarchy (e.g. atmosphere, atmosphere/contents) */
|
|
ensure_parent_dirs(parent);
|
|
f_mkdir(parent); /* Ensure parent exists; FR_EXIST is fine */
|
|
return folder_copy(src_path, parent);
|
|
} else {
|
|
ensure_parent_dirs(backup_base);
|
|
return folder_copy(src_path, backup_base);
|
|
}
|
|
} else {
|
|
char dst_path[BACKUP_PATH_MAX];
|
|
s_printf(dst_path, "%s/%s", backup_base, relative);
|
|
ensure_parent_dirs(dst_path);
|
|
return file_copy(src_path, dst_path);
|
|
}
|
|
}
|
|
|
|
// Backup all paths from varargs lists (same structure as delete_path_lists_grouped)
|
|
int backup_deletion_lists(const char *backup_base, ...) {
|
|
va_list ap;
|
|
const char **list;
|
|
int res = FR_OK;
|
|
|
|
/* FatFS f_mkdir only creates the last component; parent must exist first */
|
|
ensure_parent_dirs(BACKUP_BASE_PATH);
|
|
res = f_mkdir(BACKUP_BASE_PATH);
|
|
if (res != FR_OK && res != FR_EXIST)
|
|
return res;
|
|
|
|
ensure_parent_dirs(backup_base);
|
|
res = f_mkdir(backup_base);
|
|
if (res != FR_OK && res != FR_EXIST)
|
|
return res;
|
|
|
|
va_start(ap, backup_base);
|
|
while ((list = va_arg(ap, const char **)) != NULL) {
|
|
for (int i = 0; list[i] != NULL; i++) {
|
|
if (path_exists(list[i])) {
|
|
int copy_res = backup_single_path(list[i], backup_base);
|
|
if (copy_res != FR_OK && res == FR_OK)
|
|
res = copy_res; // Remember first error but keep going
|
|
}
|
|
}
|
|
}
|
|
va_end(ap);
|
|
|
|
/* Best-effort: return FR_OK so install continues even if some copies failed */
|
|
return FR_OK;
|
|
}
|
|
|
|
// Backup user data before clean install
|
|
int backup_user_data(void) {
|
|
int res;
|
|
|
|
// Create temp backup directory
|
|
res = f_mkdir(TEMP_BACKUP_PATH);
|
|
if (res != FR_OK && res != FR_EXIST) {
|
|
return res;
|
|
}
|
|
|
|
// Backup DBI if it exists
|
|
if (path_exists("sd:/switch/DBI")) {
|
|
res = folder_copy("sd:/switch/DBI", TEMP_BACKUP_PATH);
|
|
if (res != FR_OK) {
|
|
return res;
|
|
}
|
|
}
|
|
|
|
// Backup Tinfoil if it exists
|
|
if (path_exists("sd:/switch/tinfoil")) {
|
|
res = folder_copy("sd:/switch/tinfoil", TEMP_BACKUP_PATH);
|
|
if (res != FR_OK) {
|
|
return res;
|
|
}
|
|
}
|
|
|
|
// Backup prod.keys if it exists
|
|
if (path_exists("sd:/switch/prod.keys")) {
|
|
res = file_copy("sd:/switch/prod.keys", "sd:/temp_backup/prod.keys");
|
|
if (res != FR_OK) {
|
|
return res;
|
|
}
|
|
}
|
|
|
|
return FR_OK;
|
|
}
|
|
|
|
// Restore user data after clean install
|
|
int restore_user_data(void) {
|
|
int res;
|
|
|
|
// Recreate switch directory (should already exist, but be safe)
|
|
res = f_mkdir("sd:/switch");
|
|
if (res != FR_OK && res != FR_EXIST) {
|
|
return res;
|
|
}
|
|
|
|
// Restore DBI if backup exists
|
|
if (path_exists("sd:/temp_backup/DBI")) {
|
|
res = folder_copy("sd:/temp_backup/DBI", "sd:/switch");
|
|
if (res == FR_OK) {
|
|
// Delete old DBI .nro files
|
|
f_unlink("sd:/switch/DBI/DBI_810_EN.nro");
|
|
f_unlink("sd:/switch/DBI/DBI_810_DE.nro");
|
|
f_unlink("sd:/switch/DBI/DBI_845_EN.nro");
|
|
f_unlink("sd:/switch/DBI/DBI_845_DE.nro");
|
|
f_unlink("sd:/switch/DBI/DBI.nro");
|
|
}
|
|
}
|
|
|
|
// Restore Tinfoil if backup exists
|
|
if (path_exists("sd:/temp_backup/tinfoil")) {
|
|
res = folder_copy("sd:/temp_backup/tinfoil", "sd:/switch");
|
|
if (res == FR_OK) {
|
|
// Delete old tinfoil.nro
|
|
f_unlink("sd:/switch/tinfoil/tinfoil.nro");
|
|
}
|
|
}
|
|
|
|
// Restore prod.keys if backup exists
|
|
if (path_exists("sd:/temp_backup/prod.keys")) {
|
|
res = file_copy("sd:/temp_backup/prod.keys", "sd:/switch/prod.keys");
|
|
if (res != FR_OK) {
|
|
return res;
|
|
}
|
|
}
|
|
|
|
return FR_OK;
|
|
}
|
|
|
|
// Clean up temporary backup directory
|
|
int cleanup_backup(void) {
|
|
if (path_exists(TEMP_BACKUP_PATH)) {
|
|
return folder_delete(TEMP_BACKUP_PATH);
|
|
}
|
|
return FR_OK;
|
|
}
|