Skip to content

Commit

Permalink
fs: Cleanup FS::ReadFile impl and fix memory leaks (data was never be…
Browse files Browse the repository at this point in the history
…ing freed after being read)
  • Loading branch information
Joel16 committed Apr 19, 2021
1 parent c6c7370 commit cd566bf
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 46 deletions.
3 changes: 2 additions & 1 deletion include/fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ namespace FS {
bool FileExists(const std::string &path);
bool DirExists(const std::string &path);
int CreateFile(const std::string &path);
int ReadFile(const std::string &path, unsigned char **buffer, SceOff *size);
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 GetDirList(const std::string &path, std::vector<SceIoDirent> &entries);
Expand Down
72 changes: 49 additions & 23 deletions source/applist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "utils.h"

namespace AppList {
constexpr char path[] = "ur0:/shell/db/app.db";
constexpr char path[] = "ur0:shell/db/app.db";

int Get(std::vector<AppInfoIcon> &entries, std::vector<AppInfoPage> &pages, std::vector<AppInfoFolder> &folders) {
entries.clear();
Expand Down Expand Up @@ -201,54 +201,80 @@ namespace AppList {
}

int Backup(void) {
// Create an original backup for first time use.
int ret = 0;
std::string backup_path;

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";

unsigned char *data = nullptr;
SceOff size = 0;

int ret = 0;
if (R_FAILED(ret = FS::ReadFile(path, &data, &size)))
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)))
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)))
if (R_FAILED(ret = FS::RemoveFile(backup_path))) {
delete[] data;
return ret;
}
}

if (R_FAILED(ret = FS::WriteFile(backup_path, data, size)))
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";
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";
restore_path = "ux0:data/VITAHomebrewSorter/backup/app.db";

int ret = 0;
if (R_FAILED(ret = FS::ReadFile(restore_path.c_str(), &data, &size)))
if (R_FAILED(ret = FS::GetFileSize(restore_path, &size)))
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)))
if (R_FAILED(ret = FS::RemoveFile(path))) {
delete[] data;
return ret;
}
}

if (R_FAILED(ret = FS::WriteFile(path, data, size)))
return ret;

if (R_FAILED(ret = FS::WriteFile(path, data, size))) {
delete[] data;
return ret;
}

delete[] data;
return 0;
}

Expand Down
24 changes: 17 additions & 7 deletions source/fs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,30 @@ namespace FS {
return 0;
}

int ReadFile(const std::string &path, unsigned char **buffer, SceOff *size) {
int GetFileSize(const std::string &path, SceOff *size) {
SceIoStat stat;
int ret = 0;

if (R_FAILED(ret = sceIoGetstat(path.c_str(), &stat))) {
Log::Error("sceIoOpen(%s) failed: 0x%lx\n", path.c_str(), ret);
return ret;
}

*size = stat.st_size;
return 0;
}

int ReadFile(const std::string &path, void *data, SceSize size) {
int ret = 0, bytes_read = 0;
SceUID file = 0;

if (R_FAILED(ret = file = sceIoOpen(path.c_str(), SCE_O_RDONLY, 0))) {
Log::Error("sceIoOpen(%s) failed: 0x%lx\n", path.c_str(), ret);
return ret;
}

*size = sceIoLseek(file, 0, SEEK_END);
*buffer = new unsigned char[*size];

if (R_FAILED(ret = sceIoPread(file, *buffer, *size, SCE_SEEK_SET))) {
Log::Error("sceIoPread(%s) failed: 0x%lx\n", path.c_str(), ret);
if (R_FAILED(ret = bytes_read = sceIoRead(file, data, size))) {
Log::Error("sceIoRead(%s) failed: 0x%lx\n", path.c_str(), ret);
return ret;
}

Expand All @@ -71,7 +81,7 @@ namespace FS {
return ret;
}

return 0;
return bytes_read;
}

int WriteFile(const std::string &path, const void *data, SceSize size) {
Expand Down
3 changes: 1 addition & 2 deletions source/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include "utils.h"

namespace Renderer {

static void End(bool clear, ImVec4 clear_color) {
glViewport(0, 0, static_cast<int>(ImGui::GetIO().DisplaySize.x), static_cast<int>(ImGui::GetIO().DisplaySize.y));

Expand Down Expand Up @@ -98,7 +97,7 @@ namespace GUI {
ImGui::PopStyleVar();
Renderer::End(true, ImVec4(0.05f, 0.07f, 0.13f, 1.00f));

if ((now.tick - start.tick) >= 9000000)
if ((now.tick - start.tick) >= 8000000)
done = true;
}
}
Expand Down
54 changes: 41 additions & 13 deletions source/loadouts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
#include "utils.h"

namespace Loadouts {
constexpr char path[] = "ur0:/shell/db/app.db";
constexpr char path[] = "ur0:shell/db/app.db";

int Backup(void) {
int ret = 0;
std::string filename = Keyboard::GetText("Enter loadout name");
if (filename.empty())
return -1;
Expand All @@ -14,38 +15,65 @@ namespace Loadouts {
unsigned char *data = nullptr;
SceOff size = 0;

int ret = 0;
if (R_FAILED(ret = FS::ReadFile(path, &data, &size)))
if (R_FAILED(ret = FS::GetFileSize(path, &size)))
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)))
if (R_FAILED(ret = FS::RemoveFile(loadout_path))) {
delete[] data;
return ret;
}
}

if (R_FAILED(ret = FS::WriteFile(loadout_path, data, size)))
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;

int ret = 0;
if (R_FAILED(ret = FS::ReadFile(loadout_path, &data, &size)))
if (R_FAILED(ret = FS::GetFileSize(loadout_path, &size)))
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)))
if (R_FAILED(ret = FS::RemoveFile(path))) {
delete[] data;
return ret;
}
}

if (R_FAILED(ret = FS::WriteFile(path, data, size)))
return ret;
if (R_FAILED(ret = FS::WriteFile(path, data, size))) {
delete[] data;
return ret;
}

delete[] data;
return 0;
}
}
}

0 comments on commit cd566bf

Please sign in to comment.