applist: Make db edits to a copy of app database and not the actual

This commit is contained in:
Joel16 2021-04-28 00:01:42 -04:00
parent c9c7de7c89
commit 62bfe6cf6d
4 changed files with 62 additions and 115 deletions

View File

@ -10,10 +10,8 @@ namespace FS {
bool FileExists(const std::string &path);
bool DirExists(const std::string &path);
int CreateFile(const std::string &path);
int GetFileSize(const std::string &path, SceOff *size);
int ReadFile(const std::string &path, void *data, SceSize size);
int WriteFile(const std::string &path, const void *data, SceSize size);
int RemoveFile(const std::string &path);
int CopyFile(const std::string &src_path, const std::string &dest_path);
int GetDirList(const std::string &path, std::vector<SceIoDirent> &entries);
}

View File

@ -10,6 +10,7 @@
namespace AppList {
constexpr char path[] = "ur0:shell/db/app.db";
constexpr char path_edit[] = "ur0:shell/db/app.vhb.db";
int Get(AppEntries *entries) {
entries->icons.clear();
@ -91,11 +92,17 @@ namespace AppList {
int Save(std::vector<AppInfoIcon> &entries) {
sqlite3 *db;
int ret = sqlite3_open_v2(path, &db, SQLITE_OPEN_READWRITE, nullptr);
if (ret)
int ret = 0;
if (R_FAILED(ret = FS::CopyFile(path, path_edit)))
return ret;
ret = sqlite3_open_v2(path_edit, &db, SQLITE_OPEN_READWRITE, nullptr);
if (ret) {
FS::RemoveFile(path_edit);
return ret;
}
// Hacky workaround to avoid SQL's unique constraints. Please look away!
for (int i = 0, counter = 10; i < entries.size(); i++, counter++) {
std::string title = entries[i].title;
@ -122,7 +129,7 @@ namespace AppList {
if (ret != SQLITE_OK) {
Log::Error("sqlite3_exec1 error %s\n", query.c_str());
sqlite3_close(db);
AppList::Restore(); // Restore from backup incase sort fails
FS::RemoveFile(path_edit);
return ret;
}
}
@ -152,12 +159,16 @@ namespace AppList {
if (ret != SQLITE_OK) {
Log::Error("sqlite3_exec2 error %s\n", query.c_str());
sqlite3_close(db);
AppList::Restore(); // Restore from backup incase sort fails
FS::RemoveFile(path_edit);
return ret;
}
}
sqlite3_close(db);
if (R_FAILED(ret = FS::CopyFile(path_edit, path)))
return ret;
return 0;
}
@ -216,78 +227,30 @@ namespace AppList {
int Backup(void) {
int ret = 0;
std::string backup_path;
unsigned char *data = nullptr;
SceOff size = 0;
if (!FS::FileExists("ux0:data/VITAHomebrewSorter/backup/app.db.bkp"))
backup_path = "ux0:data/VITAHomebrewSorter/backup/app.db.bkp";
else
backup_path = "ux0:data/VITAHomebrewSorter/backup/app.db";
if (R_FAILED(ret = FS::GetFileSize(path, &size)))
if (R_FAILED(ret = FS::CopyFile(path, backup_path)))
return ret;
data = new unsigned char[size];
if (!data)
return -1;
if (R_FAILED(ret = FS::ReadFile(path, data, size))) {
delete[] data;
return ret;
}
if (FS::FileExists(backup_path)) {
if (R_FAILED(ret = FS::RemoveFile(backup_path))) {
delete[] data;
return ret;
}
}
if (R_FAILED(ret = FS::WriteFile(backup_path, data, size))) {
delete[] data;
return ret;
}
delete[] data;
return 0;
}
int Restore(void) {
int ret = 0;
std::string restore_path;
unsigned char *data = nullptr;
SceOff size = 0;
if (!FS::FileExists("ux0:data/VITAHomebrewSorter/backup/app.db"))
restore_path = "ux0:data/VITAHomebrewSorter/backup/app.db.bkp";
else
restore_path = "ux0:data/VITAHomebrewSorter/backup/app.db";
if (R_FAILED(ret = FS::GetFileSize(restore_path, &size)))
if (R_FAILED(ret = FS::CopyFile(restore_path, path)))
return ret;
data = new unsigned char[size];
if (!data)
return -1;
if (R_FAILED(ret = FS::ReadFile(restore_path, data, size))) {
delete[] data;
return ret;
}
if (FS::FileExists(path)) {
if (R_FAILED(ret = FS::RemoveFile(path))) {
delete[] data;
return ret;
}
}
if (R_FAILED(ret = FS::WriteFile(path, data, size))) {
delete[] data;
return ret;
}
delete[] data;
return 0;
}

View File

@ -49,7 +49,7 @@ namespace FS {
return 0;
}
int GetFileSize(const std::string &path, SceOff *size) {
static int GetFileSize(const std::string &path, SceOff *size) {
SceIoStat stat;
int ret = 0;
@ -62,7 +62,7 @@ namespace FS {
return 0;
}
int ReadFile(const std::string &path, void *data, SceSize size) {
static int ReadFile(const std::string &path, void *data, SceSize size) {
int ret = 0, bytes_read = 0;
SceUID file = 0;
@ -84,7 +84,7 @@ namespace FS {
return bytes_read;
}
int WriteFile(const std::string &path, const void *data, SceSize size) {
static int WriteFile(const std::string &path, const void *data, SceSize size) {
int ret = 0, bytes_written = 0;
SceUID file = 0;
@ -118,6 +118,39 @@ namespace FS {
return 0;
}
int CopyFile(const std::string &src_path, const std::string &dest_path) {
int ret = 0;
unsigned char *data = nullptr;
SceOff size = 0;
if (R_FAILED(ret = FS::GetFileSize(src_path, &size)))
return ret;
data = new unsigned char[size];
if (!data)
return -1;
if (R_FAILED(ret = FS::ReadFile(src_path, data, size))) {
delete[] data;
return ret;
}
if (FS::FileExists(dest_path)) {
if (R_FAILED(ret = FS::RemoveFile(dest_path))) {
delete[] data;
return ret;
}
}
if (R_FAILED(ret = FS::WriteFile(dest_path, data, size))) {
delete[] data;
return ret;
}
delete[] data;
return 0;
}
static std::string GetFileExt(const std::string &filename) {
std::string ext = std::filesystem::path(filename).extension();
std::transform(ext.begin(), ext.end(), ext.begin(), ::toupper);

View File

@ -8,72 +8,25 @@ namespace Loadouts {
int Backup(void) {
int ret = 0;
std::string filename = Keyboard::GetText("Enter loadout name");
if (filename.empty())
return -1;
std::string loadout_path = "ux0:data/VITAHomebrewSorter/loadouts/" + filename + ".db";
unsigned char *data = nullptr;
SceOff size = 0;
if (R_FAILED(ret = FS::GetFileSize(path, &size)))
if (R_FAILED(ret = FS::CopyFile(path, loadout_path)))
return ret;
data = new unsigned char[size];
if (!data)
return -1;
if (R_FAILED(ret = FS::ReadFile(path, data, size))) {
delete[] data;
return ret;
}
if (FS::FileExists(loadout_path)) {
if (R_FAILED(ret = FS::RemoveFile(loadout_path))) {
delete[] data;
return ret;
}
}
if (R_FAILED(ret = FS::WriteFile(loadout_path, data, size))) {
delete[] data;
return ret;
}
delete[] data;
return 0;
}
int Restore(const char *name) {
int ret = 0;
std::string loadout_path = "ux0:data/VITAHomebrewSorter/loadouts/" + std::string(name);
unsigned char *data = nullptr;
SceOff size = 0;
if (R_FAILED(ret = FS::GetFileSize(loadout_path, &size)))
if (R_FAILED(ret = FS::CopyFile(loadout_path, path)))
return ret;
data = new unsigned char[size];
if (!data)
return -1;
if (R_FAILED(ret = FS::ReadFile(loadout_path, data, size))) {
delete[] data;
return ret;
}
if (FS::FileExists(path)) {
if (R_FAILED(ret = FS::RemoveFile(path))) {
delete[] data;
return ret;
}
}
if (R_FAILED(ret = FS::WriteFile(path, data, size))) {
delete[] data;
return ret;
}
delete[] data;
return 0;
}
}