-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 <ogc/lwp_watchdog.h>
#include <malloc.h>
#include <dirent.h>
#include <unistd.h>
#include <sys/statvfs.h>
#include "fileOps/fileOps.h"
#include "gecko/gecko.hpp"
#include "loader/utils.h"
#include "memory/mem2.hpp"
#define SET(a, b) a = b; DCFlushRange(&a, sizeof(a));
#define STACKSIZE 8192
@ -78,7 +75,7 @@ u64 fsop_GetFolderBytes(const char *source)
continue;
snprintf(newSource, sizeof(newSource), "%s/%s", source, pent->d_name);
// If it is a folder... recurse...
if(fsop_DirExist(newSource))
if(fsop_FolderExist(newSource))
bytes += fsop_GetFolderBytes(newSource);
else // It is a file !
{
@ -110,41 +107,6 @@ u32 fsop_GetFreeSpaceKb(const char *path) // Return free kb on the device passed
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()
{
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)
{
gprintf("Creating file: %s\n", target);
//gprintf("Creating file: %s\n", target);
int err = 0;
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);
// If it is a folder... recurse...
if(fsop_DirExist(newSource))
if(fsop_FolderExist(newSource))
ret = doCopyFolder(newSource, newTarget, spinner, spinner_data);
else // It is a file !
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);
}
static inline void fsop_silentDelete(const char *source)
{
remove(source);
}
void fsop_deleteFolder(const char *source)
{
DIR *pdir;
@ -342,7 +299,7 @@ void fsop_deleteFolder(const char *source)
continue;
snprintf(newSource, sizeof(newSource), "%s/%s", source, pent->d_name);
// If it is a folder... recurse...
if(fsop_DirExist(newSource))
if(fsop_FolderExist(newSource))
{
closedir(pdir);
fsop_deleteFolder(newSource);
@ -351,7 +308,7 @@ void fsop_deleteFolder(const char *source)
else // It is a file !
{
closedir(pdir);
fsop_silentDelete(newSource);
fsop_deleteFile(newSource);
pdir = opendir(source);
}
}
@ -359,46 +316,3 @@ void fsop_deleteFolder(const char *source)
gprintf("Deleting directory: %s\n", 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
#define _FILEOPS
#include <dirent.h>
#include <unistd.h>
#include "memory/mem2.hpp"
typedef void (*progress_callback_t)(int status,int total,void *user_data);
bool fsop_GetFileSizeBytes(const char *path, size_t *filesize);
u64 fsop_GetFolderBytes(const char *source);
u32 fsop_GetFolderKb(const char *source);
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_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);
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

View File

@ -318,18 +318,12 @@ s32 GCDump::DumpGame()
Asciify2(gc_hdr.title);
snprintf(folder, sizeof(folder), basedir);
if(!fsop_DirExist(folder))
{
gprintf("Creating directory: %s\n", folder);
fsop_MakeFolder(folder);
}
memset(folder, 0, sizeof(folder));
snprintf(folder, sizeof(folder), "%s/%s [%.06s]", basedir, gc_hdr.title, gc_hdr.id);
if(!fsop_DirExist(folder))
{
gprintf("Creating directory: %s\n", folder);
if(!fsop_FolderExist(folder))
fsop_MakeFolder(folder);
}
else if(!Disc)
{
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));
snprintf(folder, sizeof(folder), "%s/%s [%.06s]/sys", basedir, gc_hdr.title, gc_hdr.id);
if(!fsop_DirExist(folder))
{
gprintf("Creating directory: %s\n", folder);
fsop_MakeFolder(folder);
}
gprintf("Writing %s/boot.bin\n", folder);
snprintf(gamepath, sizeof(gamepath), "%s/boot.bin", folder);

View File

@ -249,7 +249,6 @@ int CMenu::_GCcopyGame(void *obj)
m._setThrdMsg(L"", 0);
gprintf("Copying from:\n%s\nto:\n%s\n", source, target);
LWP_MutexUnlock(m.m_mutex);
if (!fsop_DirExist(folder))
fsop_MakeFolder(folder);
fsop_CopyFolder(source, target, _addDiscProgress, obj);
LWP_MutexLock(m.m_mutex);