avoid using file stat when possible. it's slow.

This commit is contained in:
Daryl Borth 2018-08-28 15:02:35 -06:00
parent b738b86176
commit 16181ef6b9
6 changed files with 1515 additions and 1560 deletions

View File

@ -23,6 +23,7 @@
#include <di/di.h> #include <di/di.h>
#include <ogc/dvd.h> #include <ogc/dvd.h>
#include <iso9660.h> #include <iso9660.h>
#include <fat.h>
#include "vbagx.h" #include "vbagx.h"
#include "vbasupport.h" #include "vbasupport.h"
@ -35,7 +36,7 @@
#define THREAD_SLEEP 100 #define THREAD_SLEEP 100
unsigned char *savebuffer; unsigned char *savebuffer = NULL;
static mutex_t bufferLock = LWP_MUTEX_NULL; static mutex_t bufferLock = LWP_MUTEX_NULL;
FILE * file; // file pointer - the only one we should ever use! FILE * file; // file pointer - the only one we should ever use!
bool unmountRequired[7] = { false, false, false, false, false, false, false }; bool unmountRequired[7] = { false, false, false, false, false, false, false };
@ -473,22 +474,6 @@ static char *GetExt(char *file)
return ext; return ext;
} }
bool GetFileSize(int i)
{
if(browserList[i].length > 0)
return true;
struct stat filestat;
char path[MAXPATHLEN+1];
snprintf(path, MAXPATHLEN, "%s%s", browser.dir, browserList[i].filename);
if(stat(path, &filestat) < 0)
return false;
browserList[i].length = filestat.st_size;
return true;
}
void FindAndSelectLastLoadedFile () void FindAndSelectLastLoadedFile ()
{ {
int indexFound = -1; int indexFound = -1;
@ -674,7 +659,6 @@ ParseDirectory(bool waitParse, bool filter)
AddBrowserEntry(); AddBrowserEntry();
sprintf(browserList[0].filename, ".."); sprintf(browserList[0].filename, "..");
sprintf(browserList[0].displayname, "Up One Level"); sprintf(browserList[0].displayname, "Up One Level");
browserList[0].length = 0;
browserList[0].isdir = 1; // flag this as a dir browserList[0].isdir = 1; // flag this as a dir
browserList[0].icon = ICON_FOLDER; browserList[0].icon = ICON_FOLDER;
browser.numEntries++; browser.numEntries++;
@ -776,7 +760,7 @@ LoadSzFile(char * filepath, unsigned char * rbuffer)
* LoadFile * LoadFile
***************************************************************************/ ***************************************************************************/
size_t size_t
LoadFile (char * rbuffer, char *filepath, size_t length, bool silent) LoadFile (char * rbuffer, char *filepath, size_t length, size_t buffersize, bool silent)
{ {
char zipbuffer[2048]; char zipbuffer[2048];
size_t size = 0, offset = 0, readsize = 0; size_t size = 0, offset = 0, readsize = 0;
@ -828,7 +812,7 @@ LoadFile (char * rbuffer, char *filepath, size_t length, bool silent)
if (IsZipFile (zipbuffer)) if (IsZipFile (zipbuffer))
{ {
size = UnZipBuffer ((unsigned char *)rbuffer); // unzip size = UnZipBuffer ((unsigned char *)rbuffer, buffersize); // unzip
} }
else else
{ {
@ -836,14 +820,12 @@ LoadFile (char * rbuffer, char *filepath, size_t length, bool silent)
size = ftello(file); size = ftello(file);
fseeko(file,0,SEEK_SET); fseeko(file,0,SEEK_SET);
if(size > buffersize) {
size = 0;
}
else {
while(!feof(file)) while(!feof(file))
{ {
// If the size requested is *less* than the filesize, only read that much - we don't want to overrun the buffer
int toread = 4096;
if (length > 0 && offset+toread > length) {
toread = length - offset;
}
ShowProgress ("Loading...", offset, size); ShowProgress ("Loading...", offset, size);
readsize = fread (rbuffer + offset, 1, 4096, file); // read in next chunk readsize = fread (rbuffer + offset, 1, 4096, file); // read in next chunk
@ -851,14 +833,12 @@ LoadFile (char * rbuffer, char *filepath, size_t length, bool silent)
break; // reading finished (or failed) break; // reading finished (or failed)
offset += readsize; offset += readsize;
if (length > 0 && offset >= length) {
break;
}
} }
size = offset; size = offset;
CancelAction(); CancelAction();
} }
} }
}
retry = 0; retry = 0;
fclose (file); fclose (file);
} }
@ -871,19 +851,7 @@ LoadFile (char * rbuffer, char *filepath, size_t length, bool silent)
size_t LoadFile(char * filepath, bool silent) size_t LoadFile(char * filepath, bool silent)
{ {
struct stat filestat; return LoadFile((char *)savebuffer, filepath, 0, SAVEBUFFERSIZE, silent);
if(stat(filepath, &filestat) != 0) {
return 0;
}
int size = filestat.st_size;
if(size >= SAVEBUFFERSIZE) {
return 0;
}
return LoadFile((char *)savebuffer, filepath, 0, silent);
} }
/**************************************************************************** /****************************************************************************
@ -952,7 +920,6 @@ SaveFile (char * buffer, char *filepath, size_t datasize, bool silent)
// go back to checking if devices were inserted/removed // go back to checking if devices were inserted/removed
ResumeDeviceThread(); ResumeDeviceThread();
if(!silent) if(!silent)
CancelAction(); CancelAction();
return written; return written;

View File

@ -11,11 +11,8 @@
#ifndef _FILEOP_H_ #ifndef _FILEOP_H_
#define _FILEOP_H_ #define _FILEOP_H_
#include <gccore.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <ogcsys.h>
#include <fat.h>
#include <unistd.h> #include <unistd.h>
#define SAVEBUFFERSIZE (1024 * 1024 * 2) // leave room for IPS/UPS files and larger images #define SAVEBUFFERSIZE (1024 * 1024 * 2) // leave room for IPS/UPS files and larger images
@ -31,13 +28,12 @@ char * StripDevice(char * path);
bool ChangeInterface(int device, bool silent); bool ChangeInterface(int device, bool silent);
bool ChangeInterface(char * filepath, bool silent); bool ChangeInterface(char * filepath, bool silent);
void CreateAppPath(char * origpath); void CreateAppPath(char * origpath);
bool GetFileSize(int i);
void FindAndSelectLastLoadedFile(); void FindAndSelectLastLoadedFile();
int ParseDirectory(bool waitParse = false, bool filter = true); int ParseDirectory(bool waitParse = false, bool filter = true);
bool CreateDirectory(char * path); bool CreateDirectory(char * path);
void AllocSaveBuffer(); void AllocSaveBuffer();
void FreeSaveBuffer(); void FreeSaveBuffer();
size_t LoadFile(char * rbuffer, char *filepath, size_t length, bool silent); size_t LoadFile(char * rbuffer, char *filepath, size_t length, size_t buffersize, bool silent);
size_t LoadFile(char * filepath, bool silent); size_t LoadFile(char * filepath, bool silent);
size_t LoadSzFile(char * filepath, unsigned char * rbuffer); size_t LoadSzFile(char * filepath, unsigned char * rbuffer);
size_t SaveFile(char * buffer, char *filepath, size_t datasize, bool silent); size_t SaveFile(char * buffer, char *filepath, size_t datasize, bool silent);

View File

@ -97,7 +97,7 @@ IsZipFile (char *buffer)
******************************************************************************/ ******************************************************************************/
size_t size_t
UnZipBuffer (unsigned char *outbuffer) UnZipBuffer (unsigned char *outbuffer, size_t buffersize)
{ {
PKZIPHEADER pkzip; PKZIPHEADER pkzip;
size_t zipoffset = 0; size_t zipoffset = 0;
@ -122,6 +122,10 @@ UnZipBuffer (unsigned char *outbuffer)
pkzip.uncompressedSize = FLIP32 (pkzip.uncompressedSize); pkzip.uncompressedSize = FLIP32 (pkzip.uncompressedSize);
if(pkzip.uncompressedSize > buffersize) {
return 0;
}
ShowProgress ("Loading...", 0, pkzip.uncompressedSize); ShowProgress ("Loading...", 0, pkzip.uncompressedSize);
/*** Prepare the zip stream ***/ /*** Prepare the zip stream ***/
@ -211,7 +215,7 @@ GetFirstZipFilename ()
return NULL; return NULL;
// read start of ZIP // read start of ZIP
if(LoadFile (tempbuffer, filepath, ZIPCHUNK, NOTSILENT) < 35) if(LoadFile (tempbuffer, filepath, ZIPCHUNK, ZIPCHUNK, NOTSILENT) < 35)
return NULL; return NULL;
tempbuffer[28] = 0; // truncate - filename length is 2 bytes long (bytes 26-27) tempbuffer[28] = 0; // truncate - filename length is 2 bytes long (bytes 26-27)
@ -428,7 +432,6 @@ int SzParse(char * filepath)
sprintf(browserList[0].filename, ".."); sprintf(browserList[0].filename, "..");
sprintf(browserList[0].displayname, "Up One Level"); sprintf(browserList[0].displayname, "Up One Level");
browserList[0].isdir = 1; browserList[0].isdir = 1;
browserList[0].length = filelen;
browserList[0].icon = ICON_FOLDER; browserList[0].icon = ICON_FOLDER;
// get contents and parse them into file list structure // get contents and parse them into file list structure

View File

@ -12,7 +12,7 @@
int IsZipFile (char *buffer); int IsZipFile (char *buffer);
char * GetFirstZipFilename(); char * GetFirstZipFilename();
size_t UnZipBuffer (unsigned char *outbuffer); size_t UnZipBuffer (unsigned char *outbuffer, size_t buffersize);
int SzParse(char * filepath); int SzParse(char * filepath);
size_t SzExtractFile(int i, unsigned char *buffer); size_t SzExtractFile(int i, unsigned char *buffer);
void SzClose(); void SzClose();

View File

@ -420,7 +420,7 @@ bool SaveBatteryOrState(char * filepath, int action, bool silent)
const char* generic_goomba_error = "Cannot save SRAM in Goomba format (did not load correctly.)"; const char* generic_goomba_error = "Cannot save SRAM in Goomba format (did not load correctly.)";
// check for goomba sram format // check for goomba sram format
char* old_sram = (char*)malloc(GOOMBA_COLOR_SRAM_SIZE); char* old_sram = (char*)malloc(GOOMBA_COLOR_SRAM_SIZE);
size_t br = LoadFile(old_sram, filepath, GOOMBA_COLOR_SRAM_SIZE, true); size_t br = LoadFile(old_sram, filepath, GOOMBA_COLOR_SRAM_SIZE, GOOMBA_COLOR_SRAM_SIZE, true);
if (br >= GOOMBA_COLOR_SRAM_SIZE && goomba_is_sram(old_sram)) { if (br >= GOOMBA_COLOR_SRAM_SIZE && goomba_is_sram(old_sram)) {
void* cleaned = goomba_cleanup(old_sram); void* cleaned = goomba_cleanup(old_sram);
if (cleaned == NULL) { if (cleaned == NULL) {
@ -1005,12 +1005,12 @@ void LoadPNGBorder(const char* fallback)
char error[1024]; error[1023] = 0; char error[1024]; error[1023] = 0;
int r; int r;
bool borderLoaded = LoadFile((char*)png_tmp_buf, borderPath, 1024*1024, SILENT); bool borderLoaded = LoadFile((char*)png_tmp_buf, borderPath, 0, 1024*1024, SILENT);
if (!borderLoaded) { if (!borderLoaded) {
// Try default border.png // Try default border.png
free(borderPath); free(borderPath);
borderPath = AllocAndGetPNGBorderPath(fallback); borderPath = AllocAndGetPNGBorderPath(fallback);
borderLoaded = LoadFile((char*)png_tmp_buf, borderPath, 1024*1024, SILENT); borderLoaded = LoadFile((char*)png_tmp_buf, borderPath, 0, 1024*1024, SILENT);
} }
if (!borderLoaded) goto cleanup; if (!borderLoaded) goto cleanup;
@ -1060,12 +1060,7 @@ bool LoadGBROM()
{ {
gbEmulatorType = GCSettings.GBHardware; gbEmulatorType = GCSettings.GBHardware;
if (browserList[browser.selIndex].length > 1024*1024*8) gbRom = (u8 *)malloc(1024*1024*8);
{
InfoPrompt("ROM size is too large (> 8 MB)");
return false;
}
gbRom = (u8 *)malloc(1024*1024*8); // 32 MB is too much for sure
if (!gbRom) if (!gbRom)
{ {
InfoPrompt("Unable to allocate 8 MB of memory"); InfoPrompt("Unable to allocate 8 MB of memory");
@ -1082,7 +1077,7 @@ bool LoadGBROM()
if(!MakeFilePath(filepath, FILE_ROM)) if(!MakeFilePath(filepath, FILE_ROM))
return false; return false;
gbRomSize = LoadFile ((char *)gbRom, filepath, browserList[browser.selIndex].length, NOTSILENT); gbRomSize = LoadFile ((char *)gbRom, filepath, 0, (1024*1024*8), NOTSILENT);
} }
else else
{ {
@ -1175,12 +1170,6 @@ bool LoadVBAROM()
return false; return false;
} }
if(!GetFileSize(browser.selIndex))
{
ErrorPrompt("Error loading game!");
return false;
}
srcWidth = 0; srcWidth = 0;
srcHeight = 0; srcHeight = 0;

View File

@ -165,7 +165,7 @@ int VMCPULoadROM()
if(!MakeFilePath(filepath, FILE_ROM)) if(!MakeFilePath(filepath, FILE_ROM))
return 0; return 0;
GBAROMSize = LoadFile ((char *)rom, filepath, browserList[browser.selIndex].length, NOTSILENT); GBAROMSize = LoadFile ((char *)rom, filepath, 0, (1024*1024*32), NOTSILENT);
} }
else else
{ {