-some security updates for file read/write operations

This commit is contained in:
fix94.1 2013-07-07 01:50:26 +00:00
parent c77ede571f
commit 1efca149ed
4 changed files with 88 additions and 113 deletions

View File

@ -13,14 +13,11 @@ en exposed s_fsop fsop structure can be used by callback to update operation sta
#include <ogcsys.h> #include <ogcsys.h>
#include <ogc/lwp_watchdog.h> #include <ogc/lwp_watchdog.h>
#include <malloc.h> #include <malloc.h>
#include <dirent.h>
#include <unistd.h>
#include <sys/statvfs.h> #include <sys/statvfs.h>
#include "fileOps/fileOps.h" #include "fileOps/fileOps.h"
#include "gecko/gecko.hpp" #include "gecko/gecko.hpp"
#include "loader/utils.h" #include "loader/utils.h"
#include "memory/mem2.hpp"
#define SET(a, b) a = b; DCFlushRange(&a, sizeof(a)); #define SET(a, b) a = b; DCFlushRange(&a, sizeof(a));
#define STACKSIZE 8192 #define STACKSIZE 8192
@ -78,7 +75,7 @@ u64 fsop_GetFolderBytes(const char *source)
continue; continue;
snprintf(newSource, sizeof(newSource), "%s/%s", source, pent->d_name); snprintf(newSource, sizeof(newSource), "%s/%s", source, pent->d_name);
// If it is a folder... recurse... // If it is a folder... recurse...
if(fsop_DirExist(newSource)) if(fsop_FolderExist(newSource))
bytes += fsop_GetFolderBytes(newSource); bytes += fsop_GetFolderBytes(newSource);
else // It is a file ! else // It is a file !
{ {
@ -110,41 +107,6 @@ u32 fsop_GetFreeSpaceKb(const char *path) // Return free kb on the device passed
return ret ; return ret ;
} }
bool fsop_FileExist(const char *fn)
{
FILE * f;
f = fopen(fn, "rb");
if(f)
{
fclose(f);
return true;
}
return false;
}
bool fsop_DirExist(const char *path)
{
DIR *dir;
dir = opendir(path);
if(dir)
{
closedir(dir);
return true;
}
return false;
}
void fsop_MakeFolder(const char *path)
{
if(fsop_DirExist(path))
return;
gprintf("Folder path to create: %s\n", path);
mkdir(path, S_IREAD | S_IWRITE);
}
static void *thread_CopyFileReader() static void *thread_CopyFileReader()
{ {
u32 rb; u32 rb;
@ -169,7 +131,7 @@ static void *thread_CopyFileReader()
bool fsop_CopyFile(const char *source, const char *target, progress_callback_t spinner, void *spinner_data) bool fsop_CopyFile(const char *source, const char *target, progress_callback_t spinner, void *spinner_data)
{ {
gprintf("Creating file: %s\n", target); //gprintf("Creating file: %s\n", target);
int err = 0; int err = 0;
u32 size; u32 size;
@ -302,7 +264,7 @@ static bool doCopyFolder(const char *source, const char *target, progress_callba
snprintf(newTarget, sizeof(newTarget), "%s/%s", target, pent->d_name); snprintf(newTarget, sizeof(newTarget), "%s/%s", target, pent->d_name);
// If it is a folder... recurse... // If it is a folder... recurse...
if(fsop_DirExist(newSource)) if(fsop_FolderExist(newSource))
ret = doCopyFolder(newSource, newTarget, spinner, spinner_data); ret = doCopyFolder(newSource, newTarget, spinner, spinner_data);
else // It is a file ! else // It is a file !
ret = fsop_CopyFile(newSource, newTarget, spinner, spinner_data); ret = fsop_CopyFile(newSource, newTarget, spinner, spinner_data);
@ -322,11 +284,6 @@ bool fsop_CopyFolder(const char *source, const char *target, progress_callback_t
return doCopyFolder(source, target, spinner, spinner_data); return doCopyFolder(source, target, spinner, spinner_data);
} }
static inline void fsop_silentDelete(const char *source)
{
remove(source);
}
void fsop_deleteFolder(const char *source) void fsop_deleteFolder(const char *source)
{ {
DIR *pdir; DIR *pdir;
@ -342,7 +299,7 @@ void fsop_deleteFolder(const char *source)
continue; continue;
snprintf(newSource, sizeof(newSource), "%s/%s", source, pent->d_name); snprintf(newSource, sizeof(newSource), "%s/%s", source, pent->d_name);
// If it is a folder... recurse... // If it is a folder... recurse...
if(fsop_DirExist(newSource)) if(fsop_FolderExist(newSource))
{ {
closedir(pdir); closedir(pdir);
fsop_deleteFolder(newSource); fsop_deleteFolder(newSource);
@ -351,7 +308,7 @@ void fsop_deleteFolder(const char *source)
else // It is a file ! else // It is a file !
{ {
closedir(pdir); closedir(pdir);
fsop_silentDelete(newSource); fsop_deleteFile(newSource);
pdir = opendir(source); pdir = opendir(source);
} }
} }
@ -359,46 +316,3 @@ void fsop_deleteFolder(const char *source)
gprintf("Deleting directory: %s\n", source); gprintf("Deleting directory: %s\n", source);
unlink(source); unlink(source);
} }
void fsop_deleteFile(const char *source)
{
if(!fsop_FileExist(source))
return;
gprintf("Deleting file: %s\n", source);
fsop_silentDelete(source);
}
u8 *fsop_ReadFile(const char *path, u32 *size)
{
*(size) = 0;
if(!fsop_FileExist(path))
return NULL;
gprintf("Reading file: %s\n", path);
FILE *f = fopen(path, "rb");
fseek(f, 0, SEEK_END);
u32 filesize = ftell(f);
u8 *mem = (u8*)MEM2_alloc(filesize);
rewind(f);
fread(mem, filesize, 1, f);
fclose(f);
*(size) = filesize;
return mem;
}
bool fsop_WriteFile(const char *path, const void *mem, const u32 size)
{
if(mem == NULL || size == 0)
return false;
FILE *f = fopen(path, "wb");
if(f == NULL)
return false;
gprintf("Writing file: %s\n", path);
fwrite(mem, size, 1, f);
fclose(f);
return true;
}

View File

@ -6,21 +6,93 @@ extern "C"
#ifndef _FILEOPS #ifndef _FILEOPS
#define _FILEOPS #define _FILEOPS
#include <dirent.h>
#include <unistd.h>
#include "memory/mem2.hpp"
typedef void (*progress_callback_t)(int status,int total,void *user_data); typedef void (*progress_callback_t)(int status,int total,void *user_data);
bool fsop_GetFileSizeBytes(const char *path, size_t *filesize); bool fsop_GetFileSizeBytes(const char *path, size_t *filesize);
u64 fsop_GetFolderBytes(const char *source); u64 fsop_GetFolderBytes(const char *source);
u32 fsop_GetFolderKb(const char *source); u32 fsop_GetFolderKb(const char *source);
u32 fsop_GetFreeSpaceKb(const char *path); u32 fsop_GetFreeSpaceKb(const char *path);
bool fsop_FileExist(const char *fn);
bool fsop_DirExist(const char *path);
void fsop_MakeFolder(const char *path);
bool fsop_CopyFile(const char *source, const char *target, progress_callback_t spinner, void *spinner_data); bool fsop_CopyFile(const char *source, const char *target, progress_callback_t spinner, void *spinner_data);
bool fsop_CopyFolder(const char *source, const char *target, progress_callback_t spinner, void *spinner_data); bool fsop_CopyFolder(const char *source, const char *target, progress_callback_t spinner, void *spinner_data);
void fsop_deleteFile(const char *source);
void fsop_deleteFolder(const char *source); void fsop_deleteFolder(const char *source);
u8 *fsop_ReadFile(const char *path, u32 *size);
bool fsop_WriteFile(const char *path, const void *mem, const u32 size); static inline bool fsop_FileExist(const char *fn)
{
FILE * f;
f = fopen(fn, "rb");
if(f)
{
fclose(f);
return true;
}
return false;
}
static inline u8 *fsop_ReadFile(const char *path, u32 *size)
{
*(size) = 0;
if(!fsop_FileExist(path))
return NULL;
//gprintf("Reading file: %s\n", path);
FILE *f = fopen(path, "rb");
fseek(f, 0, SEEK_END);
u32 filesize = ftell(f);
u8 *mem = (u8*)MEM2_alloc(filesize);
if(mem != NULL)
{
rewind(f);
fread(mem, filesize, 1, f);
*(size) = filesize;
}
fclose(f);
return mem;
}
static inline bool fsop_WriteFile(const char *path, const void *mem, const u32 size)
{
if(mem == NULL || size == 0)
return false;
FILE *f = fopen(path, "wb");
if(f == NULL)
return false;
//gprintf("Writing file: %s\n", path);
fwrite(mem, size, 1, f);
fclose(f);
return true;
}
static inline void fsop_deleteFile(const char *source)
{
if(!fsop_FileExist(source))
return;
remove(source);
}
static inline bool fsop_FolderExist(const char *path)
{
DIR *dir;
dir = opendir(path);
if(dir)
{
closedir(dir);
return true;
}
return false;
}
static inline void fsop_MakeFolder(const char *path)
{
if(fsop_FolderExist(path))
return;
//gprintf("Folder path to create: %s\n", path);
mkdir(path, S_IREAD | S_IWRITE);
}
#endif #endif

View File

@ -318,18 +318,12 @@ s32 GCDump::DumpGame()
Asciify2(gc_hdr.title); Asciify2(gc_hdr.title);
snprintf(folder, sizeof(folder), basedir); snprintf(folder, sizeof(folder), basedir);
if(!fsop_DirExist(folder)) fsop_MakeFolder(folder);
{
gprintf("Creating directory: %s\n", folder);
fsop_MakeFolder(folder);
}
memset(folder, 0, sizeof(folder)); memset(folder, 0, sizeof(folder));
snprintf(folder, sizeof(folder), "%s/%s [%.06s]", basedir, gc_hdr.title, gc_hdr.id); snprintf(folder, sizeof(folder), "%s/%s [%.06s]", basedir, gc_hdr.title, gc_hdr.id);
if(!fsop_DirExist(folder)) if(!fsop_FolderExist(folder))
{
gprintf("Creating directory: %s\n", folder);
fsop_MakeFolder(folder); fsop_MakeFolder(folder);
}
else if(!Disc) else if(!Disc)
{ {
gprintf("Skipping game: %s (Already installed)(%d)\n", gc_hdr.title, Gamesize[MultiGameDump]); gprintf("Skipping game: %s (Already installed)(%d)\n", gc_hdr.title, Gamesize[MultiGameDump]);
@ -398,11 +392,7 @@ s32 GCDump::DumpGame()
{ {
memset(folder, 0, sizeof(folder)); memset(folder, 0, sizeof(folder));
snprintf(folder, sizeof(folder), "%s/%s [%.06s]/sys", basedir, gc_hdr.title, gc_hdr.id); snprintf(folder, sizeof(folder), "%s/%s [%.06s]/sys", basedir, gc_hdr.title, gc_hdr.id);
if(!fsop_DirExist(folder)) fsop_MakeFolder(folder);
{
gprintf("Creating directory: %s\n", folder);
fsop_MakeFolder(folder);
}
gprintf("Writing %s/boot.bin\n", folder); gprintf("Writing %s/boot.bin\n", folder);
snprintf(gamepath, sizeof(gamepath), "%s/boot.bin", folder); snprintf(gamepath, sizeof(gamepath), "%s/boot.bin", folder);

View File

@ -249,8 +249,7 @@ int CMenu::_GCcopyGame(void *obj)
m._setThrdMsg(L"", 0); m._setThrdMsg(L"", 0);
gprintf("Copying from:\n%s\nto:\n%s\n", source, target); gprintf("Copying from:\n%s\nto:\n%s\n", source, target);
LWP_MutexUnlock(m.m_mutex); LWP_MutexUnlock(m.m_mutex);
if (!fsop_DirExist(folder)) fsop_MakeFolder(folder);
fsop_MakeFolder(folder);
fsop_CopyFolder(source, target, _addDiscProgress, obj); fsop_CopyFolder(source, target, _addDiscProgress, obj);
LWP_MutexLock(m.m_mutex); LWP_MutexLock(m.m_mutex);
m._setThrdMsg(m._t("wbfsop14", L"Game copied, press Back to boot the game."), 1.f); m._setThrdMsg(m._t("wbfsop14", L"Game copied, press Back to boot the game."), 1.f);